Total Time: 2.5 hours
This is the first in a series of two labs that will introduce you to building and programming your own robot. In this series, you will learn the basics of DC motors, motor control, sensor integration and algorithm development. In this first lab, we will learn about DC motors and tackle the assembly and motor control. By the end you will have your robot driving a trajectory you designed.
(1 minute read)
The voltages and currents we are using are not capable of being harmful, but there are infinite ways to fry your circuit.
Always follow these tips as you complete this and future labs:
Ask if you have trouble finding anything.
We are using the L293D motor shield. It has support for 4 DC motors (or 2 steppers) and 2 servos. We can also power the motors and Ardiuno through the shield. We are using the AFMotor library because, while we technically don’t need it, libraries make our code easier to write and read. We don’t have to worry about the exact pins and values we are writing to or creating our own PWM signals.
We are going to start this lab by understanding how we can control our motors using the AF_Motor library you installed. Then we will test the motors and make a function that will be even better at controlling the motors.
#include <AFMotor.h>
AF_DCMotor rightMotor(1); // Motor connected to M1
AF_DCMotor leftMotor(2); // Motor connected to M2
void setup() {
rightMotor.setSpeed(255);
leftMotor.setSpeed(255);
}
void setup() {
rightMotor.setSpeed(255);
leftMotor.setSpeed(255);
rightMotor.run(FORWARD);
leftMotor.run(FORWARD);
}
Now, alter your code to drive the motors forward for 3 seconds, stop for 1 seconds, drive backward (remember how to change direction) for 3 seconds and repeat (Hint: write code in loop())
We are now going to assemble our chassis. Make sure you have all the following pieces as shown in the image below.
Now attach components to the laser cut chassis in the following order. Don't do it out of order. you can use the gridlines on the cutting matts in between desks to determine fastener length.
Now we need to plug in the motors and battery bank to the Ardiuno Shield.
Congrats! You have built a robot.
We are now going to control our robot
void driveStraight(int millis) {
// TODO: Fill in the body of this function
}
void turnRight() {
// TODO: Fill in the body of this function
}
void turnLeft() {
// TODO: Fill in the body of this functions
}
In the loop() of your code, use your turning and driving functions to drive in a square with side length 50cm clockwise. Do it again going counterclockwise. How well does it return to its starting location? What might cause this? What potential sensors could we use to fix this?
You may have noticed that it was difficult to get your robot to drive straight and turn a desired amount consistently. This is because the robot is open-loop. It has no idea where it is or where it is going. We are going to close the loop next lab by adding sensors so the robot can detect where it is and where it wants to go.
Now that you have a robot that can drive as you command it, try driving in more interesting shapes. Here are some ideas to get you started:
This will be the video that shows up on your portfolio, so make it drive well.