Writing the code for a command in C++
Close claw command in RobotBuilder
This is the definition of the Close claw command in RobotBuilder. Notice that it requires the Claw subsystem. This is explained in the next step.
Generated Closeclaw class generated in Closeclaw.cpp
RobotBuilder will generate the class files (header and implementation) for the Closeclaw command. The command represents the behavior of the claw, that is the operation over time. To operate this very simple claw mechanism the motor needs to operate for 1 second in the close direction. The Claw subsystem has methods to start the motor running in the right direction and to stop it. The commands responsibility is to run the motor for the correct time. The lines of code that are shown in the boxes are added to add this behavior.
- Set the one second timeout for this command. When the command is scheduled, a timer will be started so the one second operation can easily be tested.
- Start the claw motor moving in the closing direction by calling the Close method that was added to the Claw subsystem.
- This command is finished when the timer runs out which happens after one second has passed. This is the timer set in step 1.
- The End() method is called when the command is finished and is a place to clean up. In this case, the motor is stopped since the time has run out.
- The Interrupted() method is called is this command is interrupted if another command that also requires the Claw subsystem is scheduled before this finishes. For example, if the Closeclaw command was scheduled and running, then the Openclaw command was scheduled it would interrupt the Openclaw command, call its Interrupted() method, and the motor would stop.