Go to Post The "line" is the point at which we've raised the floor so high that the advanced teams are hitting the ceiling. The ceiling is pretty darned high (and the GDC keeps raising it), so I don't think we're in danger of that any time soon. - EricVanWyk [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 25-02-2005, 20:24
amateurrobotguy's Avatar
amateurrobotguy amateurrobotguy is offline
Lead Programmer/Senior Engineer
no team
 
Join Date: Feb 2005
Rookie Year: 2000
Location: ****
Posts: 136
amateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these parts
Math.h and Functions and Variables

Let me know if this function is right:

This is the 'pointer'
MyFunction()

/************************************************** *****************************
* FUNCTION NAME:
* PURPOSE:
* CALLED FROM: this file
* ARGUMENTS: none
* RETURNS: void
************************************************** *****************************/
void MyFunction(void)
{

}

Also, in the User routines.h, I added MyFunction(void) to the very end. Is that needed?


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?

Do i need to put #include<Math.h>in the top?

Man, I wish we could get back to the ye olde days of BASIC. No predefinition required, etc.
__________________
I quit FRC over 2 years ago (more if you're reading this past 2010).

Last edited by amateurrobotguy : 25-02-2005 at 20:44.
  #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...
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of Functions in Autonomous VideoMan053 Programming 11 17-02-2005 16:27
Updated: Serial Port Driver Code Kevin Watson Programming 4 05-02-2005 18:39
Overloaded functions jgannon Programming 5 31-12-2004 11:04
Do you write functions for your code? Max Lobovsky Programming 26 11-03-2004 07:04
math.h library mightywombat Programming 24 17-01-2004 13:10


All times are GMT -5. The time now is 13:11.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi