SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
User avatar
By TrollHammer
#172982
Hi, I'm confused. :lol:

I was looking at someone's code for a Kiwi drive robot (three omni wheels), and he's doing something I don't quite understand, and would like clarified.

He uses the same label for two two different functions, it seems, or I'm just not understanding some aspect of C?

Here's his sketch (the simple form):
Code: Select all
/* Kiwi drive macros and functions
 * By Jonathan M. Guberman, 2011
 * http://upnotnorth.net
 * 
 * Use this code however you like, but don't blame me if it doesn't work!
 */

#include <Servo.h> 

#define SPEED(x) (90 + (x))
#define DRIVE(x,y,z)   servo1.write(90 + (x)); servo2.write(90 + (y));servo3.write(90 + (z))
 
#define MAXSPEED 20
 
Servo servo1; 
Servo servo2;
Servo servo3;
 
void setup() 
{ 
 
} 
 
 
void loop() 
{

} 

void setDrive(int angle, int maxspeed){
  float x = cos(3.14159 * (float) angle / 180);
  float y = sin(3.14159 * (float) angle / 180);
  
  DRIVE((int)(x*maxspeed),(int)((-0.5*x + 0.866*y)*maxspeed),(int)((-0.5*x-0.866*y)*maxspeed));
}

void setDrive(int angle){
  setDrive(angle, MAXSPEED);
}

void stopDrive(){
  DRIVE(0,0,0);
}

void stopDrive(int time){
  stopDrive();
  delay(time);
}

How is the complier understanding the difference between a call to stopDrive() and stopDrive(value) or setDrive() and setDrive(value)? What is actually going on here, and what are the limitations?
By lyndon
#172983
Arduino is programmed in C++. The C++ language allows a principle called overloading, whereby the same function can have different parameter signatures and the compiler will correctly dispatch to the right function.
By Valen
#173003
The parameter types of the values or variables given to the function has to match. If no parameter is given then it will execute the stopDrive(). If you want to start the function while giving it a whole numbered value to work on, it will choose stopDrive(int time). Trying to execute stopDrive(1.0) should make the compiler complain about non matching types.
User avatar
By TrollHammer
#173162
Thank you for clarifying that for me, I guess I'll need to start looking at c++ references!

I so seem to remember someone saying it's 'somewhere between C and C++" or something like that, or that it's not a fully featured ANSI language. If I use a C++ reference (like Sam's), what parts can I ignore, what parts don't work, and is there anything added/different?

I wish I had understood this sooner, it would have taken a number of months off of my last major project (only using the C rule-set)