|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Functions
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 |
|
#2
|
|||
|
|||
|
Re: Functions
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).
|
|
#3
|
||||
|
||||
|
Well then how do you do so?
|
|
#4
|
|||
|
|||
|
Re: Functions
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. |
|
#5
|
||||
|
||||
|
Re: Functions
Code:
class myRobot : SimpleRobot {
RobotDrive drivetrain;
void MoveFwd2Sec() {
drivetrain.ArcadeDrive(1, 0);
Wait(2);
drivetrain.ArcadeDrive(0,0);
}
public:
void Autonomous() {
MoveFwd2Sec();
}
};
|
|
#6
|
||||
|
||||
|
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 |
|
#7
|
||||
|
||||
|
Re: Functions
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
|
|
#8
|
|||
|
|||
|
Re: Functions
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)
Code:
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...
}
}
Last edited by DjScribbles : 02-14-2012 at 11:02 AM. Reason: fixed a copy paste error |
|
#9
|
|||
|
|||
|
Re: Functions
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.
|
|
#10
|
||||
|
||||
|
If I wanted to put said functions in there own header file and pass a Jaguar reference into them how would I do so
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|