Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ECGR2252 Mini Project 2. Build a robot, Install Arduino software on your laptop and create a program
to follow a line. In class presentation /demo
The Goal of this project is to build a Robot by using the SparkFun Inventors kit for RedBot and to
build a small program that will follow the black line to make a 90 degree angle and stop (first
drawing), 2nd drawing make two 90 degree angles (maximum time is 30 sec per drawing two drawing
total).
Each Team to build the robot (View the YouTube DEMO of the assembly)
Each student to complete Arduino Software Installation and experiment 1 by themselves, should
show the software and demo experiment 1 to the instructor in class to receive credit.
Tips for programing try to review experiments 2, 3 and six. Complete a report and demo/present the
experiments.
1. Each team will receive a Kit from the Instructor. Each student to install Watch the Video from [4]. In
Class follow the Assembly guide from https://learn.sparkfun.com/tutorials/redbot-assembly-guide
2. At the SparkFun RedBot website you could find a guide that will go through nine different
experiments. These experiments ranging from learning how to drive your RedBot to using an
accelerometer to trigger your RedBot to move[1]. Learning the first four experiments will help you
with your final project design and implementation. From reference 3 you learn about the RedBot
Mainboard which is a robotic method that works with the Arduino IDE.
3.Complete these experiments from [1] Pages Introduction RedBot Library Quick Reference
Experiment 1: Software Install and Basic Test Experiment 2: Drive Forward Experiment 3:
Turning Experiment 4: Push to Start & Making Sounds Experimen5 and experiment 6)
Reference:
1. https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-redbot
2. https://learn.sparkfun.com/tutorials/redbot-assembly-guide
3. https://www.sparkfun.com/products/12097
4. http://www.bing.com/videos/search?q=sparkfun+redbot+assembly&&view=detail&mid=3E2B0
773F48E73933C1B3E2B0773F48E73933C1B&rvsmid=270D33953FD11C975849270D33953FD11
C975849&fsscr=0&FORM=VDMCNR
Details instructions for Robot Assembly all info below received from the link below
https://learn.sparkfun.com/tutorials/redbot-assembly-guide-rev-02
Introduction
The RedBot is robotic development platform capable of teaching basic robotics and sensor
integration! Based on the SparkFun RedBoard and programmable in the Arduino environment, the
RedBot has all the I/O you need to make a small robot in no time at all.
Materials Included
When you purchase the RedBot Kit, you should have the following materials:
1x RedBot Mainboard
1x Magician Chassis
1x Accelerometer RedBot Sensor
3x Line Follower RedBot Sensor
10x F/F 6" Jumper Wire
4x 4-40 1-" Flat Head Screw
3x 4-40 1-" Aluminum Hex Standoff
4x 4-40 3/8" Metal Standoff
7x 4-40 1" Metal Standoff
3x 4-40 " Phillips Screw (10 Pack)
4x 4-40 Hex Nut
4x Sticky Tab
Along with everything listed above, you will also need 4x AA batteries to power your RedBot.
Experiment 1: Software Install and Basic Test
Install Arduino IDE
In order to get your RedBot up and running, youll first need to download the newest version of the
Arduino software from www.arduino.cc. This software, known as the Arduino IDE (Integrated
Development Environment), will allow you to program the board to do exactly what you want. Its like
a word processor for writing programs. Please visit our Installing Arduino IDE tutorial for step-by-step
directions on installing the Arduino IDE.
Connect your RedBot to your computer
Use a USB miniB cable to connect the RedBot to one of your computers USB inputs. Make sure you
have the four AA batteries in the battery holder.
Install FTDI drivers
Depending on your computers operating system, you will need to follow specific instructions. Please
go to How to Install FTDI Drivers for specific instructions on how to install the FTDI drivers onto your
RedBot.
Install the RedBot library
A library in Arduino is a set of files containing pre-written code that simplifies instructions and
commands to perform certain tasks. We have written a specific library for the RedBot. Make sure
you install the RedBot library. You will need it for all the example code. Click the link below to
download it.
RedBot Library
Copy/Move the RedBot folder to the libraries folder into your Arduino Documents folder. If you need
a refresher on how to install an Arduino library, please see our library tutorial.
Example Code
Included in the library are a set of examples for the 9 RedBot Experiments. Click on File >
Examples > RedBot > RedBot_Experiments, you should see a list of the 9 Examples we have
created.
If you need to find the example code separately, click the link below
You can also find the RedBot Kit Experiments example code on the RedBot Github page. To
download all the code, click the Download ZIP button on the right-hand side of the page.
Open the downloaded ZIP file and copy the RedBot_Experiments folder to your Arduino
sketchbook / documents folder.
Open the Arduino IDE and select board:
Open the Arduino IDE software on your computer. This step is to set your IDE to identify your
RedBot. You will want to select the board, Arduino Uno. To do this, go to Tools > Board > Arduino
Uno.
Select your Serial Port
Window users: Select the serial port for the RedBot from the Tools > Serial Port menu. This is
likely to be COM3 or higher (COM1 and COM2 are usually reserved for other internal devices). To
check, disconnect your RedBot and re-open the menu; the entry that disappears is the one for the
RedBot. Reconnect the board, and select that serial port.
Mac users: Select the serial device of the RedBot from the Tools > Serial Port menu. On the Mac,
this should be something with /dev/tty.usbmodem or /dev/tty.usbserial in it. To find out, you can
disconnect your RedBot and re-open the menu; the entry that disappears should be the RedBot.
Reconnect the board and select that serial port.
Note: If the Serial Port is not showing up, go back and re-install the FTDI drivers for your
machine or try re-starting Arduino.
Code To Note
The RedBot has an LED connected to pin 13. Control of pin 13 can be accessed through the
digitalWrite([pin], [HIGH\LOW]) command.
digitalWrite(13, HIGH) puts 5V on pin 13. In this case, this turns the LED on.
digitalWrite(13, LOW) puts 0V on pin 13, and turns the LED off.
The Arduino microcontroller runs at 16 MHz. This means it performs 16 Million operations per
second. The digitalWrite() command takes less than 4 uS to execute. In order for us to see the LED
turn on, we need to pause or delay the program flow. We do this with the command
delay([time_ms]).
In our example, delay(500); 500 indicates the number of milliseconds the program is delayed for.
Going Further
Experiment around with the delay() function to change the rate of the blink. What is the fastest blink
that you can still see? 10 ms? 20 ms?
Can you change the blink pattern to resemble a heart-beat? (Hint: You might need to add a second
blink sequence to your loop.)
Troubleshooting
My code wont upload!
Make sure your USB cable is plugged into both the robot and the computer youre using to
write code.
Make sure the Power switch is switched to ON.
Double check that you have the right serial port selected under the Tools menu. The
easiest way to check is to see which item disappears from the menu when you unplug the
USB cable, and select that one when you plug the board back in.
If you have an Xbee module plugged in, make sure the Serial Select switch at the top edge
of the board is switched to XBEE SW SERIAL.
Check that you have the right board selected under the Tools menu. The RedBot is Arduino
Uno-compatible, so select Arduino Uno from the list.
This sketch does not turn on the motors it just blinks. So, the motors should not be
spinning
https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-redbot/experiment-1-
software-install-and-basic-test
Experiment 2 from
https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-redbot/experiment-2-drive-
forward-
Copy Code
RedBotMotors motors;
RedBotMotors motors;
This line creates an object called motors that allows us to control the right and left motors and drive
the robot. See more details below in the Code to Note section.
Before uploading this example code, make sure that the RedBot is in a safe position. The program
will start immediately after uploading, and it might drive off your desk, knock over your drink, or
stomp through your lunch. We suggest standing the RedBot upright on the flat edge of the rear end
so that the wheels are off the table.
Note: When uploading, the power must be on, the motors must be connected, and the board must
be powered by a battery. Also, the motor switch should be set to RUN.
Code To Note
Adding the #include <RedBot.h> to the top of your code gives us access to a number of classes,
functions, objects, and methods that make controlling the RedBot much easier. The RedBot library
has custom routines for creating objects such as:
Our focus here is going to be only on the RedBotMotors class. The RedBotMotors class has a
number of methods for controlling the motors. In programming, methods are behaviors or questions
you ask of an object.
Copy Code
RedBotMotors motors; // Instantiate the motor control object.
Copy Code
motors.drive(255); // drives forward at full power. motors.drive(-255); // drives reverse at f
ull power.
motors.drive(255); // drives forward at full power. motors.drive(-255); // drives reverse at full power.
Sometimes running the motors at full power causes the wheels to spin-out. If you notice traction
issues, you may want to try playing around with slower speeds.
Stopping
motors.stop() turns off power to both motors and coasts to a stop.
Copy Code
motors.stop(); // sets the motor speeds to 0 and coasts to a stop.
Sometimes, you might want a more precise stop. the RedBotMotors class also has a brake()
method that forces the motors to come to a more abrupt stop.
Try replacing the motors.stop() with motors.brake() in your example code. Measure the distance
your robot travels. How much farther does it travel when it coasts compared to braking?
Copy Code
motors.brake(); // Stops both motors and applies "brakes" by shorting out the motors
motors.brake(); // Stops both motors and applies "brakes" by shorting out the motors
When might you use stop() and when might you want to use brake()?
Going Further
Now that you have control of driving the robot, see if you can get your robot to drive forward for 1
second, stop, and reverse for 1 second. Repeat your test a few times. Does your robot always return
to where it started?
How far from your starting point does it return to? What factors might cause it not to come back to
its starting point?
How might this be useful for robotics applications?
Experiment / Activity
Adjust the [motorPower] for your robot so that it drives about 2 - 3 feet in 2 seconds.
Approximate this. Write down the motorPower here: ______
Run a minimum of 5 trials with your robot and measure how far your robot travels in 2
seconds.
Calculate the average speed of your robot (in inches per second) using the equation:
avgSpeed = distance / time.
Copy Code
void driveDistance(int distance) { // this function takes a distance input parameter and the a
vgSpeed of your // RedBot to compute a delayTime for driving forward. int avgSpeed = 16; // av
erage speed in inches per second. long driveTime; driveTime = (long) 1000 * distance / avgSpee
d; motors.drive(200); // make sure you're using the same motorPower as your tests. delay(drive
Time); motors.brake(); }
void driveDistance(int distance) { // this function takes a distance input parameter and the avgSpeed
of your // RedBot to compute a delayTime for driving forward. int avgSpeed = 16; // average speed in
inches per second. long driveTime; driveTime = (long) 1000 * distance / avgSpeed;
motors.drive(200); // make sure you're using the same motorPower as your tests. delay(driveTime);
motors.brake(); }
Now, replace your own drive code with a function call driveDistance(12); Upload and test. Your
RedBot should have driven 12 inches. How far did it go? If it drove too far, then adjust the variable
avgSpeed until your RedBot is within an inch of 12 inches. Change the driveDistance(12); to
driveDistance(24); and test this again. How far did your RedBot go?
This indicates that the RedBot library is not properly included into your sketch. This is usually a
result of one of two things:
#include <RedBot.h> this line must be at the top of your code, and RedBot.h must be
spelled and capitalized exactly as it is shown here.
The RedBot library is not in the Arduino libraries folder. Go back to Experiment 1 and make
sure that you have properly installed the RedBot library.
Check the MOTOR switch on the board and make sure that its switched over to RUN.
Do you have external power connected? The RedBots motors need more power than a USB
plug can supply, so they wont run unless a power supply is connected to the barrel jack on
the board.
Make sure that the motors are correctly connected; it may be that you have connected one
wire from each board to each power header, or that the wires are plugged into the wrong
locations. Match the wire colors to the labels on the board.
This is a pretty common thing for the RedBot, and for all robots with independent drive
wheels. There are lots of reasons for it, but its something that simply must be dealt with.
o First check to make sure that there is nothing rubbing on either the wheels or on
the magnetic motor encoder. Move the wheels out so that they are not rubbing, but
still tightly seated into the motor. Any added friction on one side will cause one side
to drive faster than the other (resulting it the RedBot pulling to one side.)
o Second Driving straight requires equal power on both wheels. This requires equal
amounts of traction on both wheels. Running the motors up at full power sometimes
causes the wheels to spin out and lose traction. Try reducing the motorPower to
something lower than 255.
o Third use an encoder to ensure that both wheels turn the same distance each time.
Whats an encoder? Shucks well cover encoders later on in Experiment 8.
redbot/experiment-3-turning
Experiment 3: Turning
In this experiment, we will look at how to fine-tune the control of the RedBot by controlling the right
motor and the left motor separately. We will introduce a few new methods of the RedBotMotor class
that will allow us to control the motors separately.
In this experiment, we will break-down the commands for the RedBot to drive forward, turn 90
degrees, drive forward again, and then stop.
Make sure RedBot is in safe location, or standing on the flat back edge of the chassis. This code
requires only the most basic setup: the motors must be connected, and the board must be receiving
power from the battery pack.
Lets load this experiment onto the RedBot. Go to File > Examples > RedBot_Experiments >
Exp3_Turning or copy and paste the example code below:
Code To Note
Clockwise or Counter-Clockwise
In the last example, to drive the RedBot forward, we used a single command
motors.drive([speed]); Positive speeds cause the RedBot to go forward by spinning the right side
clockwise and the left side counter-clockwise.
To control the individual motors, the RedBotMotors class has two methods that are used in this
example.
Copy Code
motors.rightMotor([motorPower]); // controls the right motor motors.leftMotor([motorPower]); /
/ controls the left motor
Copy Code
// motors.leftMotor(-200); // Left motor CCW at 200 // motors.rightMotor(80); // Right motor C
W at 80 // delay(2000); // motors.leftMotor(-80); // Left motor CCW at 80 // motors.rightMotor
(200); // Right motor CW at 200 // delay(2000);
Going Further
Box step?
Now that you have fine-tuned a 90 degree turn, repeat this four times to see if you can get your
RedBot to trace out a box. If you have whiteboard sheets, you can tape a dry-erase marker to the
back of the RedBot to trace out your path.
Dance party
Adjust the figure-8 pattern until your RedBot is tracing out figure-8s on the floor. Now, plan out your
own dance routine for your robot. See if you can choreograph it to music!
Experiment / Activity
Take the motorPower that you used above and the turningTime to calculate the
turningSpeed of your RedBot. We define turningSpeed = angle / time. In this case, it
should be 90 degrees divided by the turningTime you used above. Write down your
turningSpeed.
turningSpeed = _______
Writing your own custom sub-routine \ function.
Similar to what we did in the DriveForward activity, we will write a sub-routine called turnAngle().
This function will use the the average turningSpeed you calculated above to make motion planning
easier.
Copy and paste this block of code to the end of your program after the void loop(){}
Copy Code
void turnAngle(int angle) { int turningSpeed = 180; // degreees / second long turningTime; tur
ningTime = (long) 1000 * angle / turningSpeed; motors.rightMotor(-100); // Turn CCW at motorPo
wer of 100 motors.leftMotor(-100); // Turn CCW at motorPower of 100 delay(turningTime); // tur
ning Time motors.brake(); // brake() motors }
void turnAngle(int angle) { int turningSpeed = 180; // degreees / second long turningTime;
turningTime = (long) 1000 * angle / turningSpeed; motors.rightMotor(-100); // Turn CCW at
motorPower of 100 motors.leftMotor(-100); // Turn CCW at motorPower of 100 delay(turningTime); //
turning Time motors.brake(); // brake() motors }
Now, replace your turning code with a function call turnAngle(90); Upload and test. Your RedBot
should still be turning 90 degrees. If its turning too far or not far enough, then adjust the variable
turningSpeed until your RedBot is turning a good 90 degrees. Use this with the driveDistance()
function from before to trace out a triangle, a square, and a pentagon.
Troubleshooting
My wheels dont turn but I hear a high-pitched noise.
The motors may not have enough torque at such a low speed. Try driving the motors at a higher
speed, or reducing the weight on the RedBot.
Everytime I plug the USB cable in, the RedBot acts funny for a few seconds! Whats happening?
The RedBot will try to run its code every time it is reset. When you plug something into a computer
the computer has to identify what it is. During this period, the computer resets the RedBot controller
board multiple times. No harm will come to your RedBot. If it distracts you, switch the motor switch
from RUN to STOP and the motors will be disabled.
Experiment 6
Set the robot flat on your desk. What are the nominal values of the three sensors?
Set the robot down on a piece of white paper. What are the values of the sensors now?
Making some marks on your paper with different materials: electrical tape, paint, markers,
pencils, etc.
With the robot still sitting on your desk, turn out the lights or throw a blanket over it. What
happens to the values of the sensor readings? What do you think is causing this?
Slowly roll the robot towards the edge of a table. Watch the value as the sensor moves off
the edge of the table. What is the sensor value when it rolls off the table?
We can now use these values to program our robot to do things like stay on a table or follow a dark
line.
Code to Note
To setup communication between the RedBot and your computer, we need to use the Serial object
in Arduino. In the setup(), you will see a command: Serial.begin(9600); This initializes the Serial
object and the determines the data rate (speed) that data will be transmitted & received betwen the
RedBot and the computer. 9600 refers to a baud rate or the number of bis per second. 9600 is a good
moderate data rate for most applications. However, other speeds including: 300, 1200, 2400, 4800,
9600, 14400, 19200, 28800, 38400, 57600, and 115200 are supported in Arduino.
Copy Code
Serial.begin(9600);
Serial.begin(9600);
To send data from the RedBot to your computer, we have two commands: Serial.print(""); and
Serial.println(""); The first command, Serial.print(""); simply prints the text in or data (in
the form of a number or variable) to the Serial bus this shows up in the Serial monitor. The second
command, Serial.println(""); does the same, but adds a carriage-return/line-feed (CRLF)
character, moving the cursor to the next line.
Copy Code
Serial.print("IR Sensor Readings: "); Serial.println(); // Sends a CRLF character -- moves cur
sor to next line.
Serial.print("IR Sensor Readings: "); Serial.println(); // Sends a CRLF character -- moves cursor to
next line.
\t TAB character
RedBot Library
Library support for the line sensor is included in the main RedBot library. The line following sensors
will be attached to pins A3, A6, and A7. We will create three objects for each of the three sensors
with the following lines of code:
Copy Code
RedBotSensor IRSensor1 = RedBotSensor(A3); // initialize a sensor object on A3 RedBotSensor IR
Sensor2 = RedBotSensor(A6); // initialize a sensor object on A6 RedBotSensor IRSensor3 = RedBo
tSensor(A7); // initialize a sensor object on A7
RedBotSensor IRSensor1 = RedBotSensor(A3); // initialize a sensor object on A3 RedBotSensor
IRSensor2 = RedBotSensor(A6); // initialize a sensor object on A6 RedBotSensor IRSensor3 =
RedBotSensor(A7); // initialize a sensor object on A7
The RedBotSensor class has only one method of interest to us. It is xxxxxx.read(); This method
returns a value from 0 to 1023 based on the input voltage of the sensor. Remember that the xxxxxx
represents the name of the object In this case, IRSensor1, IRSensor2, or IRSensor3.
Copy Code
Serial.print(IRSensor1.read()); // prints out the sensor value of IRSensor1 to the Serial Moni
tor
Serial.print(IRSensor1.read()); // prints out the sensor value of IRSensor1 to the Serial Monitor
Going Further
Modify the code to make the RedBot drive around and stop before it crosses a black line.
Use the xxxxxx.read() method to check the values and then write some code that will stop the robot
if the level of one of the sensors changes more than a certain amount.
You will need to use an if() statement. Go back to experiment 5 Bumpers to remind yourself how
to make comparisons and conditional statements within an if() statement.
Lets take a look at a few basic scenarios.
The center moves off the line and the left sensor is now on the line. Adjust the power to turn
/ veer left until the center is back on the line.
The center moves off the line and the right sensor is now on the line. Adjust the power to
turn / veer right until the center is back on the line.
The RedBot approaches a left turn, the center AND the left are BOTH on the line. Turn 90
degrees to the left.
The RedBot approaches a right turn, the center AND the right are BOTH on the line. Turn 90
degrees to the right.
Play around with these basic ideas to see if you can program your RedBot to:
A) Follow a straight line.
B) Follow a straight line and turn Left or Right.
C) Follow a maze traced out with electrical tape.
Troubleshooting
Nothing is printing in the serial monitor!
Check to make sure the baud rate is set properly in the serial monitor. Theres a drop-down
menu in the lower right corner, and by default, this sketch wants the baud rate at 9600.
I dont see any change in my sensor values, no matter what I do!
Here is a quick example of using the line following sensors to follow a dark line on a white
background. The algorithm is fairly straight forward. If the center sensor is on the line, the RedBot
drives straight (left & right motors are at the same speed). If the right sensor is on the line, we adjust
by turning the RedBot to the right (driving the left a little faster and the right a little slower). Similarly,
if the left sensor is on the line, the RedBot adjusts by turning to the left (driving the right a little faster
and the left a little slower).
This example really only works when the RedBot is moving slowly, and it doesnt always handle
sharp turns well. What can you do to make this example work better?