The last sketch, MovementsWithSimpleFunctions, was kind of long and clunky. And, the four functions it uses to drive the robot are almost the same. The TestManeuverFunction sketch takes advantage of those function's similarities and streamlines the code.
TestManeuverFunction has a single function for motion named maneuver that accepts three parameters: speedLeft, speedRight, and msTime:
void maneuver(int speedLeft, int speedRight, int msTime)
The rules for speedLeft and speedRight, listed below, are easy to remember. Best of all, with this maneuver function you don’t have to think about clockwise and counterclockwise rotation anymore.
- positive values for moving the robot forward
- negative values for moving the robot backward
- 200 for full speed forward
- –200 for full speed backward
- 0 for stop
- 100 to –100 range for linear speed control
The rules for msTime are:
- Positive values for the number of ms to execute the maneuver
- –1 to disable the servo signal
Here is what calls to this function will look like for the familiar forward-backward-left-right-stop sequence:
maneuver(200, 200, 2000); // Forward 2 seconds maneuver(-200, 200, 600); // Left 0.6 seconds maneuver(200, -200, 600); // Right 0.6 seconds maneuver(-200, -200, 2000); // Backward 2 seconds maneuver(0, 0, -1); // Disable servos
Example Sketch - TestManeuverFunction
- Enter, save, and upload TestManeuverFunction.
- Verify that it completes the forward, left, right, backward, stop sequence.
// Robotics with the BOE Shield - TestManeuverFunction // Move forward, left, right, then backward with maneuver function. #include <Servo.h> // Include servo library Servo servoLeft; // Declare left and right servos Servo servoRight; void setup() // Built-in initialization block { tone(4, 3000, 1000); // Play tone for 1 second delay(1000); // Delay to finish tone servoLeft.attach(13); // Attach left signal to pin 13 servoRight.attach(12); // Attach right signal to pin 12 maneuver(200, 200, 2000); // Forward 2 seconds maneuver(-200, 200, 600); // Left 0.6 seconds maneuver(200, -200, 600); // Right 0.6 seconds maneuver(-200, -200, 2000); // Backward 2 seconds maneuver(0, 0, -1); // Disable servos } void loop() // Main loop auto-repeats { // Empty, nothing needs repeating } void maneuver(int speedLeft, int speedRight, int msTime) { // speedLeft, speedRight ranges: Backward Linear Stop Linear Forward // -200 -100......0......100 200 servoLeft.writeMicroseconds(1500 + speedLeft); // Set Left servo speed servoRight.writeMicroseconds(1500 - speedRight); // Set right servo speed if(msTime==-1) // if msTime = -1 { servoLeft.detach(); // Stop servo signals servoRight.detach(); } delay(msTime); // Delay for msTime }
Your Turn – Customize Speed and Duration Control
With the maneuver function, 0 is stop, 100 to –100 is the speed control range, and 200 and –200 are overkill to keep the servos running as fast as they possibly can.
The TestManeuverFunction sketch makes it easy to define custom maneuvers quickly. Just pass new parameters for each wheel rotation and maneuver duration to each call of the maneuver function. For example, let’s make the left wheel move at half speed while the right wheel moves at full speed to draw an arc for 3 seconds. Here is what that function call would look like:
maneuver(50, 100, 3000);
Here is another example that keeps the left wheel still and moves the right wheel forward for a left pivot turn:
maneuver(0, 200, 1200);
- Try adding both of the example maneuver calls to your sketch.
- Try adding the other three wheel-pivot turns to the sequence: forward-right, backward-right, and backward-left.