The function that sets the rate for the Periodic loop has been depreciated in Update 3. Periodic functions are now synchronized to run when the robot receives new data from the Driver Station, similar to the old "slow loop" on the IFI platform.
The Continuous functions will run as fast as they can, and they're not interrupted by the Periodic functions.
This is the sequence of events for IterativeRobot, in pseudocode:
Code:
call RobotInit()
while (1)
{
if current mode has not been initialized:
call Init() for current mode (eg TeleopInit)
if there has been new data from DS:
call Periodic() for current mode
call Continuous() for current mode
}
Any code that depends on new information from the Driver Station really should be in a Periodic function.
Code that sets up joysticks, speed controllers, sensors, etc should be in your robot class constructor or in
RobotInit(). The variables for these objects should be declared in the robot class, outside of any functions. Anything else that needs to continuously run should be in a continuous function.
Regarding the example with the Ultrasonic class, this part would be for initialization:
Code:
Ultrasonic ultra(ULTRASONIC_PING, ULTRASONIC_ECHO);
ultra.SetAutomaticMode(true);
You'd need to declare the Ultrasonic object somewhere in your class as a member variable, and initialize it in
RobotInit() or the constructor.
Code:
class MyRobot : public IterativeRobot
{
Ultrasonic *m_ultra;
// other declarations...
void RobotInit()
{
m_ultra = new Ultrasonic(pingchannel, echochannel);
m_ultra.SetAutomaticMode(true);
}
void AutonomousPeriodic()
{
// Do something with m_ultra->GetRangeInches();
}
}
The
Ultrasonic::GetRangeInches() function is non-blocking and will return 0 if no measurements have completed. Also, when you use Automatic Mode with the Ultrasonic class the pinging will be done automatically in the background. You won't have to do anything except ask it for range information wherever you need it.