Running commands during the autonomous period
Once commands are defined they can run in either the teleop or autonomous part of the program. In fact, the power of the command based programming approach is that you can reuse the same commands in either place. If the robot has a command that can shoot Frisbees during autonomous with camera aiming and accurate shooting, there is no reason not to use it to help the drivers during the teleop period of the game.
Creating a command to use for Autonomous
Our robot must do the following tasks during the autonomous period: pick up a soda can off the floor then drive a set distance from a table and deliver the can there. The process consists of:
- Prepare to grab (move elevator, wrist, and gripper into position)
- Grab the soda can
- Drive to a distance from the table indicated by an ultrasonic rangefinder
- Place the soda
- Back off to a distance from the rangefinder
- Re-stow the gripper
To do these tasks there are 6 command groups that are executed sequentially as shown in this example.
Setting that command to run as the autonomous behavior
To get the SodaDelivery command to run as the Autonomous program,
- Instantiate it in the RobotInit() method. RobotInit() is called only once when the robot starts so it is a good time to create the command instance.
- Start it during the AutonomousInit() method. AutonomousInit() is called once at the start of the autonomous period so we schedule the command there.
- Be sure the scheduler is called repeatedly during the AutonomousPeriodic() method. AutonomousPeriodic() is called (nominally) every 20ms so that is a good time to run the scheduler which makes a pass through all the currently scheduled commands.