CS 153: Lab 9

Title image Fall 2018

Lab Exercise 9:

The lab today will be primarily building a simple rover and getting it to move with some basic instructions. The primary CS concepts this week will be continuing to work with classes. In the second rover lab we'll make use of inheritance as we add sensors to the rover.


Tasks

    Board

    For this project you will need the following items.

    1. One rover board/body (provided)
    2. Four twist-ties (provided)
    3. A couple of nuts and bolts (provided)
    4. Two velcro pads (provided)
    5. Two DC motors (the yellow ones).
    6. Two diodes
    7. Two transitors
    8. Two resistors
    9. One 4-battery pack

    Leave the AC sensor on your breadboard.

  1. Use two twist ties per motor to anchor the motors in place as shown below. Once in place, feed the wires up through the big holes in the board.

  2. Mount your board onto the rover using either a velcro pad or two nuts and bolts.
  3. Use one or two velcro pads to attach the 4-battery pack. It's your choice as to where you want to put the battery pack. One velcro pad is strong enough to hold it underneath.
  4. Wire up both motors using a transitor, a diode, and a resistor for each motor.

    Directions for wiring a DC motor are provided on the Adafruit DC motor pages. You will need to wire up both motors. The control signals should come from PWM pins on the Metro board (e.g. pin 5 and pin 9). Note that you will probably need to wire your right motor with the control wires in reverse order so the two motors spin the same direction when a voltge is applied.

  5. Once you have the motors hooked up, use the following code to test them. When downloading a program to the board, hold the rover off the ground. Once you have downloaded your program, disconnect the rover and reset it to run the program. I generally disconnect the USB cable from the computer, as it is easier and quicker than disconnecting the board, even though I then have to hold the cable while the robot moves.
    /* 
      Your name and header here 
    */
    #include <Arduino.h>
    
    int main() {
      init();
    
      pinMode(5, OUTPUT);  // uses pins 5 and 9 to control the motors
      pinMode(9, OUTPUT);
      
      analogWrite(5, 180 ); // turn on the motors about 2/3 strength (180/255)
      analogWrite(9, 180 );
      delay(1000);          // delay for 1s
      analogWrite(5, 0 );   // turn off the motors
      analogWrite(9, 0 );
    
      return(0);
    }
    						

    Double-check that your motors are spinning in the same direction and that it will move the cart forward. If not, switch around the motor leads until you have the right configuration.

  6. Code

  7. Design a class DualMotor to control the two motors. The class should have at least one constructor and then functions to make the bot go forward by a certain distance, turn left by a given angle, or turn right by a given angle. The constructor should take as arguments the pins being used by the left and right motors. The member function prototypes are given below.

    • DualMotor( int leftAnalogPin, int rightAnalogPin ) - constructor that stores the pin information into fields.
    • void forward( float distance ) - move the robot forward by a certain distance (for now, turn on the motors and delay for a certain amount of time, similar to the code above). You can choose the units for distance. Be sure to specify them in the function comment.
    • void left( float angle ) - turn the robot left by a certain angle (for now, turn on one of the wheels, then delay for a certain amount of time). You can choose the units for angle. Be sure to specify them in the function comment.
    • void right( float angle ) - turn the robot right by a certain angle (for now, turn on one of the wheels, then delay for a certain amount of time). You can choose the units for angle. Be sure to specify them in the function comment.

  8. Write some code that makes use of your DualMotor class to make the robot do a series of turns and forward motion. You may want to put a small delay in between each step. Inlcude a short video of this working in your handin materials.

When you are done with the lab exercises, you may start on the rest of the project.