You are previewing Make an Arduino-Controlled Robot.

Make an Arduino-Controlled Robot

Cover of Make an Arduino-Controlled Robot by Michael Margolis Published by O'Reilly Media, Inc.
  1. Make an Arduino-Controlled Robot
  2. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  3. A Note Regarding Supplemental Files
  4. Preface
    1. Who This Book Is For
    2. How This Book Is Organized
    3. What Was Left Out
    4. Code Style (About the Code)
    5. Arduino Hardware and Software
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari® Books Online
    9. How to Contact Us
    10. Acknowledgments
  5. 1. Introduction to Robot Building
    1. Why Build a Robot?
    2. How Robots Move
    3. Tools
  6. 2. Building the Electronics
    1. Hardware Required
    2. Construction Techniques
      1. Soldering
      2. Building the Motor Controller
      3. Soldering the Reflectance Sensors
      4. Making a Line Sensor Mount
      5. Next Steps
  7. 3. Building the Two-Wheeled Mobile Platform
    1. Hardware Required
    2. Mechanical Assembly
      1. Lay Out the Chassis Parts
      2. Motor Assembly
      3. Assemble the Chassis Components
      4. Attaching the Control Electronics
    3. Mounting the IR sensors
      1. Mounting the IR Sensors for Edge Detection
      2. Mounting the IR Sensors for Line Following
    4. Next Steps
  8. 4. Building the Four-Wheeled Mobile Platform
    1. Hardware Required
    2. Mechanical Assembly
      1. Lay Out the Chassis Parts
      2. Motor Assembly
      3. Assemble the Chassis Components
      4. Solder the Power and Motor Connections
      5. Connecting the Battery Pack and Power Switch
      6. Building the Optional Trickle Charger
      7. Assemble the Chassis
      8. Mounting Arduino and Connecting Wires to the Shield
    3. Mounting the IR sensors
      1. Mounting the IR Sensors for Edge Detection
      2. Mounting the IR Sensors for Line Following
    4. Next Steps
  9. 5. Tutorial: Getting Started with Arduino
    1. Hardware Required
    2. Arduino Software
    3. Arduino Hardware
    4. Installing the Integrated Development Environment (IDE)
      1. Installing Arduino on Windows
      2. Installing Arduino on OS X
      3. Installing Arduino on Linux
      4. Driver Installation
    5. Connecting the Arduino Board
    6. Using the IDE
    7. Uploading and Running the Blink Sketch
    8. Using Tabs
    9. Installing Third-Party Libraries
  10. 6. Testing the Robot's Basic Functions
    1. Hardware Required
    2. Software Prerequisites
    3. Sketches Used in This Chapter
    4. Load and Run helloRobot.ino
    5. About the Sketch
    6. Troubleshooting
    7. Making the Sketch Easy to Enhance
  11. 7. Controlling Speed and Direction
    1. Hardware Required
    2. Sketches Used in This Chapter
    3. Types of Motors
    4. Motor Controllers
    5. Controlling Motor Speed
      1. How Motor Speed Is Controlled
      2. Code for Motor Control
      3. Calibrating Rotation and Tracking
    6. Software Architecture for Robot Mobility
    7. Functions to Encapsulate Robot Movements
      1. Core Movement Code
      2. Additional Core Functions
      3. Functions to Rotate the Robot
      4. Higher-Level Movement Functions
  12. 8. Tutorial: Introduction to Sensors
    1. Hardware Discussed
    2. Software
    3. Infrared Reflectance Sensors
    4. Sonar Distance Sensors
    5. Maxbotix EZ1 Sonar Distance Sensor
    6. Sharp IR Distance Sensor
    7. Proximity Sensor
    8. Sound Sensor
    9. Arduino Cookbook
  13. 9. Modifying the Robot to React to Edges and Lines
    1. Hardware Required
    2. Sketches Used in This Chapter
    3. The Look Code
    4. Edge Detection
    5. Line Following
    6. Seeing Sketch Data
  14. 10. Autonomous Movement
    1. Hardware Required
    2. Sketches Used in This Chapter
    3. Mounting a Ping Distance Sensor
      1. Making a Mount for the Ping Sensor
      2. Mounting the Ping Sensor in a Fixed Position
      3. Mounting the Ping Sensor on a Servo
    4. Letting the Robot Wander
    5. Adding Scanning
  15. 11. Remote Control
    1. Hardware Required
    2. Sketches Used in This Chapter
    3. Design of the Remote Control Code
    4. Controlling the Robot with a TV Type IR Remote
      1. Installing the IR Decoder Chip
      2. The IR Remote Software
  16. A. Enhancing Your Robot
    1. Planning
      1. Think Before You Code
      2. Avoid Feature Bloat
      3. Don't Reinvent the Wheel
      4. Structure to Reflect Functionality
      5. Use Clear Names for Functions and Variables
    2. Implementing a Complex Project
      1. Test Often
      2. Simplify
      3. If It Is Awkward, Start Over
      4. Don't Confuse Activity with Progress
      5. Experiment
      6. Be Tenacious
      7. Have Fun
  17. B. Using Other Hardware with Your Robot
    1. Alternative Motor Controllers
      1. Ardumoto
      2. Continuous Rotation Servos
  18. C. Debugging Your Robot
    1. Identify the Symptoms and Localize the problem
      1. Seeing What the Robot Is Doing
  19. D. Power Sources
    1. Monitoring Battery Voltage
    2. Trickle Charging
  20. E. Programming Constructs
    1. Digital I/O
    2. Analog I/O
    3. Math functions
    4. Other Functions and Constructs
  21. F. Arduino Pin and Timer Usage
    1. Handling Resource Conflicts
      1. Modifying a Library to Change Timer Allocation
      2. Writing Code That Avoids the Use of a Timer
    2. Pin and Timer Tables
  22. About the Author
  23. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  24. Copyright
O'Reilly logo

Chapter 4. Building the Four-Wheeled Mobile Platform

This chapter provides advice on the construction of the 4WD (4 Wheel Drive) chassis shown in Figure 4-1. Construction is straightforward—you can follow the detailed steps or improvise if you want to customize your robot. The chapter also shows how you attach and connect sensors used in the projects covered in later chapters.

If you prefer to build a four wheeled robot of your own design, you should read the sections on attaching control electronics and sensors if you want to use the code for the projects in the chapters to come. Information in this chapter my also provide some ideas to help with the design of your own robot.

The 4WD robot chassis

Figure 4-1. The 4WD robot chassis

You will need a Phillips screwdriver, long-nose pliers, wire cutters, wire strippers, a soldering iron, and solder. If you don't have these on hand, you can find more information in Chapter 1, Introduction to Robot Building.

Hardware Required

See http://shop.oreilly.com/product/0636920028024.do for a detailed parts list.

  • Tools listed in Tools

  • The assembled electronics (see Chapter 2, Building the Electronics

  • 4WD Mobile Platform (four wheeled robot kit made by DFRobot)

  • Four 0.1uF ceramic capacitors

  • Two lengths of 3 conductor ribbon cable, two 3 way 0.1" headers for edge sensors

  • Optional: charging circuit resistors and diode, see detailed parts list

Mechanical Assembly

Mechanical assembly of the 4WD chassis is straightforward and the only tools needed are a Phillips screwdriver and pliers. Following the steps in order will ensure that you use the correct hardware in each assembly. You will need a soldering iron, wire cutters, and wire strippers to wire up the motor and power leads.

Lay Out the Chassis Parts

Figure 4-2 shows all of the parts contained in the 4WD chassis package. Figure 4-3 shows the contents of the bag containing the mounting hardware. The three black brackets to the left of the figure are not needed for any of the projects in this book. Locate the two bolts with the flat heads and put them aside for mounting the battery case. The remaining short bolts in this pack are identical.

4WD chassis parts

Figure 4-2. 4WD chassis parts

4WD hardware pack contents

Figure 4-3. 4WD hardware pack contents

Motor Assembly

Use four long bolts to attach two motors to each of the side plates. The motor shaft goes through the large hole and there is a small locating stud on the motor that fits into the smaller hole. The lock washer (the one with a raised edge) goes between the nut and flat washer. Ensure the motor is flat against the plate and tighten the nuts firmly but take care not to use too much force or you will stress the plastic motor housing. Figure 4-4 and Figure 4-5 shows the assembly.

Motor assembly

Figure 4-4. Motor assembly

Assemble the Chassis Components

The battery pack is bolted to the bottom base plate with two countersunk (flat headed) Phillips bolts as shown in Figure 4-6 and Figure 4-7.

Battery holder assembly

Figure 4-6. Battery holder assembly

Battery holder assembly

Figure 4-7. Battery holder assembly

The DC power jack is bolted to the rear plate using the large (M8) lock washer and nut as shown in Figure 4-8. The switch is mounted using two nuts and a lock washer (the locating washer is not used). Put one nut on the switch, leaving about enough thread for the nut to be attached to the other side. Place the lock washer on the thread and push this through the opening in the rear plate and secure with the second M6 nut. Orient the switch so the toggle moves from side to side, as shown in the figure. Figure 4-9 and Figure 4-10 show two views of the assembly.

Switch and power jack assembly

Figure 4-8. Switch and power jack assembly

Solder the Power and Motor Connections

Connecting the Battery Pack and Power Switch

The battery can be wired as shown in Figure 4-14, but you cannot charge the battery in this configuration. The power switch will disconnect the battery when the robot is not in use. The DC jack is not used in this configuration (other than as a junction point for the black ground wires). The switch is off when the toggle is closer to the DC jack as shown (the toggle is a lever, when the exposed end is up as seen in the figure, the contact at the bottom is connected and the contact wired to the shield is open). Figure 4-15 shows the completed circuit.

Basic switch wiring (no trickle charger)

Figure 4-14. Basic switch wiring (no trickle charger)

Red wires soldered to switch

Figure 4-15. Red wires soldered to switch

Building the Optional Trickle Charger

You can build a simple trickle charger into the robot if you will be using rechargeable NiMH batteries. See Trickle Charging for information about using the charger.

The circuit is wired as shown in Figure 4-16 and Figure 4-17. The battery is connected to both the robot and charger when it is switched on, enabling the Arduino to monitor and display the battery voltage. The connection via the resistor to pin 13 is required to tell the Arduino that a charger is connected so it can monitor the voltage instead of driving the robot.

Wiring for trickle charging with Arduino voltage monitoring

Figure 4-16. Wiring for trickle charging with Arduino voltage monitoring

Wiring for trickle charging with Arduino voltage monitoring

Figure 4-17. Wiring for trickle charging with Arduino voltage monitoring

Assemble the Chassis

Attach the front and rear plates to the sides using eight of the M3x6 bolts (Figure 4-18). The sides are symmetrical so it doesn't matter which end goes to the front or back.

Chassis assembly

Figure 4-18. Chassis assembly

Attach the bottom plate using four M3x6 bolts (Figure 4-19).

Bottom plate assembly

Figure 4-19. Bottom plate assembly

Mounting Arduino and Connecting Wires to the Shield

The easiest way to mount the Arduino board is with a strip of Velcro. A 2.5" x 1.5" strip is supplied with the Rovera 4W (Arduino-Controlled 4 Wheel Robotics Platform) kit. To prevent the Arduino pins from accidentally shorting to the chassis, apply insulating tape to the underside of the Arduino board. Gaffer tape works well but you can use (non-conductive) duct tape or heavy duty electrical tape. Attach the 'hairy' side of the Velcro to the taped Arduino board; the hook side is fastened as shown in Figure 4-20. Figure 4-21 shows some other views of this.

Velcro pad in position on the top plate

Figure 4-20. Velcro pad in position on the top plate

The Velcro will hold the boards in position when the robot is moving about, but use one hand to steady the Arduino when you unplug the shield and take care not to use too much downward pressure that could push the Arduino pins through the tape when plugging in the shield.

Inset shows Velcro attached to the Arduino board.

Figure 4-21.  Inset shows Velcro attached to the Arduino board.

If you prefer a more rigid mount, you can use three 3/8" or 1/4 inch (5mm) spacers with three 1/2 inch 2-56 bolts and nuts. Figure 4-22 and Figure 4-23 show the location of the mounting hardware.

Figure 4-24 shows the motor wires and battery wires inserted through the cutouts in the top plate ready for the connections shown in Figure 4-25 .

Figure 4-26 shows how the motor and battery wires attach to the connectors on the motor shield.

Motor and battery connections

Figure 4-26. Motor and battery connections

Attach the sensor plate with two M3 bolts as shown in Figure 4-27; the top plate is attached using four M3 bolts as seen in Figure 4-28.

Sensor plate assembly

Figure 4-27. Sensor plate assembly

Top plate assembly

Figure 4-28. Top plate assembly

The upper deck is bolted to four 50mm standoffs that are attached as shown in Figure 4-29.

Attach the upper deck

Figure 4-29. Attach the upper deck

Figure 4-30 shows the fully-assembled chassis (a side view is visible in Figure 4-31). You can see the front and rear views in Figure 4-32 and Figure 4-33.

Mounting the IR sensors

This section covers mounting of the infrared (IR) reflectance sensors for use in edge detecting or line following. Infrared Reflectance Sensors explains how these sensors work and Chapter 9, Modifying the Robot to React to Edges and Lines describes how to use IR sensors. This section explains how to mount these sensors onto the 4WD platform and connect them to Arduino. The first projects in this book should have the sensors mounted as shown in the section on edge detection. When you are ready to implement the line following application in Chapter 9, refer back to the section on positioning the sensors for line following. The stripboard mount described in Making a Line Sensor Mount simplifies the attachment and wiring of the sensors for line detection and this can also be used for edge detection, but bear in mind that the robot will perform the edge detection task best with the sensors further apart. If the sensors are close together, the robot can have difficulty determining the best angle to turn when an edge is encountered.

Mounting the IR Sensors for Edge Detection

Edge detection requires two QTR-1A sensors mounted on the front of the robot. These should be spaced as widely apart as possible. The ideal location is with each sensor positioned in front of a wheel so an edge can be detected before a wheel would otherwise fall off a 'cliff'. However, if your priority is simplicity of construction rather than accuracy of edge detection, you can use the same mount described in the next section covering line detection. But bear in mind that the robot will perform the edge detection task best with the sensors further apart. If the sensors are close together, the robot can have difficulty determining the best angle to turn when an edge is encountered.

Mount each sensor using a 2-56 bolt and nut (M2 bolt and nut can also be used). The component side faces the ground and the header pins face upwards. The sensors can be angled as shown in Figure 4-34 and Figure 4-35.

Reflectance sensor location for edge detection

Figure 4-34. Reflectance sensor location for edge detection

Reflectance sensor location for edge detection

Figure 4-35. Reflectance sensor location for edge detection

Mounting the IR Sensors for Line Following

Three QTR-1a sensors are required for line following. Making a Line Sensor Mount describes how to build a stripboard mount for line sensing. However, you can also mount and attach each sensors as described in this section if you want to experiment with how varying the spacing of the sensors affects line following. Like the edge sensors, they can be attached using 2-56 or M2 hardware. The component side faces down and the header pins face upwards. They are mounted in the front, equally spaced with approximately 1/2 inch between the center and the left and right bolts (see Figure 4-36). Figure 4-37 and Figure 4-38 show the sensors attached to the chassis.

Reflectance sensor location for line following

Figure 4-36. Reflectance sensor location for line following

Next Steps

Chapter 5, Tutorial: Getting Started with Arduino explains how to set up and use the development environment that will be used to upload code to the robot. If you are already an Arduino expert, you can skip to Chapter 6, Testing the Robot's Basic Functions, but first, see Installing Third-Party Libraries for advice on the libraries used with the code for this book and the steps needed to configure the RobotMotor library for the 4WD robot.

If you have the libraries installed and want run a simple test to verify that the motors are working correctly, you can run the following sketch:

Example 4-1. Initial motor test for 4WD

/*******************************************
* MotorTest4wd.ino
* Initial motor test for 4WD 
* robot rotates clockwise 
*  (Left motors driven forward, right backward)

* Michael Margolis 24 July 2012
********************************************/
const int LED_PIN = 13;
const int speed = 60; // percent of maximum speed

#include <AFMotor.h>  // adafruit motor shield library (modified my mm)
AF_DCMotor Motor_Left_Front(4, MOTOR34_1KHZ);   // Motor 4 
AF_DCMotor Motor_Right_Front(3, MOTOR34_1KHZ);  // Motor 3
AF_DCMotor Motor_Left_Rear(1, MOTOR12_1KHZ);    // Motor 1 
AF_DCMotor Motor_Right_Rear(2, MOTOR12_1KHZ);   // Motor 2

int pwm;

void setup()
{
  Serial.begin(9600);
  blinkNumber(8); // open port while flashing. Needed for Leonardo only  
   
  // scale percent into pwm range (0-255) 
  pwm= map(speed, 0,100, 0,255);  
  Motor_Left_Front.setSpeed(pwm);
  Motor_Right_Front.setSpeed(pwm);
  Motor_Left_Rear.setSpeed(pwm);
  Motor_Right_Rear.setSpeed(pwm);
}

// run over and over
void loop()
{  
  Serial.println("rotate cw");
  Motor_Left_Front.run(FORWARD);
  Motor_Left_Rear.run(FORWARD);
  
  Motor_Right_Front.run(BACKWARD);
  Motor_Right_Rear.run(BACKWARD);

  delay(5000); // run for 5 seconds
  Serial.println("stopped");
  Motor_Left_Front.run(RELEASE);  // stop the motors
  Motor_Right_Front.run(RELEASE);
  Motor_Left_Rear.run(RELEASE);  // stop the motors
  Motor_Right_Rear.run(RELEASE);

  delay(5000); // stop for 5 seconds
}

// function to indicate numbers by flashing the built-in LED
void blinkNumber( byte number) {
   pinMode(LED_PIN, OUTPUT); // enable the LED pin for output
   while(number--) {
     digitalWrite(LED_PIN, HIGH); delay(100);
     digitalWrite(LED_PIN, LOW);  delay(400);
   }
}
 

This sketch runs the motors in opposite directions to cause the robot to rotate clockwise for 5 seconds, then stops for 5 seconds. This will repeat until the power is switched off.

Note

This test sketch does not use the RobotMotor library—if this test functions correctly but the test in Chapter 6, Testing the Robot's Basic Functions does not work, the most likely cause is the configuration of the motor library—make sure you copy the 4wd version of the library code as described in Installing Third-Party Libraries.

The best content for your career. Discover unlimited learning on demand for around $1/day.