Log in

View Full Version : Functions


inkspell4
11-02-2012, 10:37
In C++ is there a way to incorporate your own functions into the code for use in autonomous?
For example creating a void for moving backwards that would then be used in autonomous.

Thanks in Advance

Supernovapsy
11-02-2012, 18:28
Yes, definitely. You can create a function that tells a robot to go straight or turn or whatever, and call them in the autonomous function of the main program. The robot will automatically do what the function says when it is in autonomous mode (just don't stand in front of it when that happens).

inkspell4
11-02-2012, 19:12
Well then how do you do so?

Supernovapsy
12-02-2012, 00:29
It's really up to you what to do depending on your robot's capabilities.

For example, you can make a function called Straight(), which sets the motors all going forward, and make another called Right(), which sets the motors turning in opposite directions. You then call them one after the other separated by a timer in the autonomous part of your program. This also depends on what kind of project you are using, whether it is SimpleRobot, IterativeRobot, or CommandBasedRobot.

WizenedEE
12-02-2012, 00:39
class myRobot : SimpleRobot {
RobotDrive drivetrain;

void MoveFwd2Sec() {
drivetrain.ArcadeDrive(1, 0);
Wait(2);
drivetrain.ArcadeDrive(0,0);
}
public:
void Autonomous() {
MoveFwd2Sec();
}
};

inkspell4
12-02-2012, 07:51
So it has to go before the public portion of the class

Can anyone else also post an example they have used on their robot

inkspell4
13-02-2012, 09:08
So it has to go before the public portion of the class

Can anyone else also post an example they have used on their robot

Let me rephrase this where must a/the function be declared and initialized if it was being added into the simple robot example code again please post and examples you might have

DjScribbles
14-02-2012, 10:56
Just to try to add some clarity, you don't ever initialize a function, you can declare and define a function. It's typically good practice (probably a matter of opinion, but isn't neccessary) to declare your function before you define it; alternatively, you can simply define the function without declaring it at all (in which case the definition is the declaration)



class SomeRobot
{
void NewFunction(void); //declartion of new function prior to definition
... some other stuff...
void NewFunction(void) //definition of new function
{
...some action to run during autonomous...
}

void OtherFunction(void) //Definition of OtherFunction(which is also the declaration)
{
... some stuff...
}
public:
Autonomous()
{
NewFunction();
OtherFunction();
LastFunction();
}

void LastFunction(void) //you can also declare it later, inside a class it doesn't need to declared before it's used
{
... some stuff...
}
}

theprgramerdude
14-02-2012, 22:45
You just have to declare what the function is before you call it. If you wish, you could just declare the function prototype in the class definition, then write the actual code down at the bottom of the file for organization.

inkspell4
16-02-2012, 21:05
If I wanted to put said functions in there own header file and pass a Jaguar reference into them how would I do so