View Single Post
  #2   Spotlight this post!  
Unread 26-02-2005, 03:19
ace123's Avatar
ace123 ace123 is offline
Registered User
AKA: Patrick Horn
FRC #0008 (Paly Robotics - http://robotics.paly.net/)
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2004
Location: Palo Alto, CA
Posts: 50
ace123 has a spectacular aura aboutace123 has a spectacular aura about
Send a message via AIM to ace123
Re: Math.h and Functions and Variables

I think that it's a little late to learn C...

But you have the basics down. Here are the answers to your questions:
Quote:
Also, in the User routines.h, I added MyFunction(void) to the very end. Is that needed?
This is called a declaration of your function and it is proper practice to put it in the header file. If you don't do this then other files like user_routines_fast can't use it.
Quote:
Next, do my variables always need to be predefined with int x, double x, etc.
Are variables always open to the whole .c file or just the function?
Yes they do, unless you are using something called macros. Macros aren't needed and for all of the stuff you ever willl do, you have to declare them like normal variables.
Variables and functions always require the type. Generally, you use int, unless you run out of space in a file.
Basically, variables MUST be declared right after the brackets. if there are any statements that are not variables then it will give syntax errors.
Code:
int function(void) {
int var1=2;
if (var1==2) var1=3;
int var2=var1+3; // <-- Syntax error points here, because this cannot be after statements.
The corrected code is:
Code:
int function(void) {
int var1=2;
int var2;
if (var1==2) var1=3;
var2=var1+3; // <-- Syntax error points here, because this cannot be after statements.
Also, variables only stay around from where you declare them (at the beginning of any bracketed area) to the end of the brackets. Anything declared outside functions are "global" and can be accessed anywhere in the file. To use them outside the file, put in your header the variable declaration with "extern" before it:
Code:
// user_routines.h
extern int motor1_speed;
extern int motor2_speed;

// user_routines.c
...
int motor1_speed=0;
int motor2_speed=0;
...
void function(void) {
  // motor1_speed can be accessed anywhere...
  motor1_speed=127;
  motor2_speed=127;
}
void function2(void) {
  if (motor1_speed>7||motor1_speed<-7) {
    int pwmval=motor1_speed+127; // Legal after any brackets.
    pwm01=pwmval;
  }
  if (motor2_speed>7||motor2_speed<-7) {
    int pwmval=motor2_speed+127; // Legal after any brackets.
    pwm02=pwmval;
  }
}
Quote:
Do i need to put #include<Math.h>in the top?
Only if you want to use stuff like sin() and cos() and pow(). I have never had to do this stuff in First code. Beware: sin() takes angles from 0 to 255 I think. Read about how to use it before using these.

Quote:
Man, I wish we could get back to the ye olde days of BASIC. No predefinition required, etc.
The C code supports basic/assembly:
Just look at ifi_startup.c! It's all written in "inline assembly". Have fun.

You technically don't need predefintion. The compiler figures it out usually.
I prefer C a lot more. It's a lot more portable. I cam compile the code for the PC for example and use the RoboEmu to simulate a robot. This is not the type of thing I want to even attempt with PBASIC.
__________________
-Patrick Horn, Paly Robotics

Check out the space simulator called Vega Strike, modelled after the space simulator games Elite and Wing Commander. It's Open Source too!
If you have ever played Wing Commander, or especially Privateer, and had a feeling of nostalga derived from the you will enjoy these two Vega Strike mods: Privateer Gemini Gold and Privateer Remake!
I'm working on adding multiplayer support this year...