RLT Robotics 3D Robot -‐‑ Part 2 V0.3 – 2015 Teacher Information Workshop Robotics Part 2 This workshop is used to familiarize teachers and students with robotics at school. The workshop takes between several hours and two full days, depending on which parts are being used. The full workshop includes building a robot and performing the Rescue Mission In this second part we will add the battery holder and the line sensor to the robot. This allows the robot to operate autonomously and also to handle the RoboCupJunior Rescue Mission. In the workshop we are using instruction cards like this one. The first cards are for the teachers or coaches and explain the idea of the workshop and how to prepare for it. In addition we are using the Arduino software, which is freely available on the Web. The robot is printed using a 3D printer and uses the Arduino hardware and some sensors. This equipment is available from various sources and through RoboCupJunior Netherlands. Building the robot is part of this workshop, but it could also be split into two different workshops. Schools also may choose to have the robots built in advance and just give the workshop to teach how to make a program. This course material can also be found on the RoboCupJunior website (www.robocupjunior.nl) under the NLT Robotics download section. This section is only available on the Dutch version of the website. The material is in English however. RLT is the English translation of the Dutch NLT module and stands for RoboDidactics Learning Track. The remaining instruction cards are meant for the students. In the front of each card is a short description of the assignment. The backside gives step-‐by-‐step instructions. There are not too many details to keep the amount of required reading to a minimum. Most of the time will be spent in studying the program code and using copy/paste of example code to make a new program. During the workshop one or more tutors should be available to assist the students. Because making and testing the program takes time, several students can share the same robot. This second workshop assumes the use of a line sensor and the battery holder. A simpler version of the robot may also be built without the sensors and is described in Part 1.
1
RLT Robotics 3D Robot -‐‑ Part 2 V0.3 – 2015 1. The RLT 3D Robot Project Introduction and backgrounds
The RoboDidactics Learning Track (RLT) 3D robot project is a continued effort to introduce students of the age group from 9 to 19 years old to the concept of Robotics. It originated with the Dutch RoboCupJunior (RCJ) organization and has a number of different tracks, varying in complexity and depth of understanding. It is based on one of the most popular RCJ competitions, the Rescue Challenge. This challenge is a simplified version of the current Rescue Line competition and is played on a small green field with a black line and a yellow area that represents a swamp. The robot has to follow the line until it reaches the swamp and there it has to find a dangerous container (a soft-‐drink can) and move it out of the swamp to save the environment from a possible explosion. There are several tracks that teach how to design, build and program a robot to participate in this challenge. There is a version for the Lego MindStorms robots, programmed in EV3-‐G for the NXT and the EV3 robots. There is another version that uses Eclipse and Java to program the robot, which is a more advanced version. This 3D version is the most extensive and teaches how to design and build a 3D printed robot and then shows how to program the robot using the Arduino framework in the popular C language. Using the module, students are made aware of the process of 3D Design, 3D printing and programming in C and in using Arduino, a flexible platform for hardware development. We are using simple instruction cards, each with a single phase of the process on which a short description of a part of the project and detailed instructions on the back of each card. It is accompanied by software examples where the students can select parts and build their own programs out of code snippets. By reusing existing software students learn to read and interpret code and are actively involved in the creation of a simple but flexible robot system. The project is based on and earlier project, called MiniSkyBot 2.0, designed by Juan Gonzalez-‐Gomez in 2012 and is made available under the Creative Commons Attribution, ShareAlike 3.0. (www.iearobotics.com) 2
RLT Robotics 3D Robot -‐‑ Part 2 V0.3 – 2015 It uses the popular OpenSCAD 3D Design software, distributed under the GNU GPL software license V2. (www.openscad.org) The applications are developed with the Arduino software, developed and distributed by Arduino under the Creative Commons Attribution, ShareAlike 3.0. (www.arduino.cc) This educational material is made available by RoboCupJunior Netherlands and is also part of the Dutch Certified Robotics Learning Track (NLT Robotica), which is used to teach science and technology subjects to students in the Dutch educational system. The material is released under the Creative Commons Attribution, ShareAlike 3.0 Netherlands license. Apart from the 3D printed robot it uses a FreaDuino board, created by ElecFreaks (www.elecfreaks.com). This Arduino-‐Uno compatible board has rows of connectors for the servomotors and sensors without the need of an additional shield. The robot uses two RC servomotors, modified for continuous rotation, a Sharp IR distance sensor (GP2Y0A21YK or equivalent) and a Pololu QTR-‐3A sensor. (www.pololu.com) In this module education material is available to perform the following steps in a project: Step
Description
Description
1
Printing the Battery holder Programming a wall follower Programming a line follower Programming the Rescue Mission Further use of the FreaDuino board
Printing the battery holder and assembling it. Testing the robot with the batteries
2 3 4 5
Estimated duration 8 hours
# of Worksheets 3
Status
4 hours
2
Available
Programming the first line follower
4 hours
2
Available
Running a competition on the Rescue Field Learning how to use Arduino for other purposes
4 hours
2
In progress
Planned
3
Available
RLT Robotics 3D Robot -‐‑ Part 2 V0.3 – 2015
4
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1. The autonomous Arduino 3D robot Setup of the Arduino 3D robot
In the second part of the robotics workshop we will first print the battery holder and assemble it. Once that has been done, we can use the robot autonomously and use it without a cable. Using a set of full batteries the robot will last for about 30 minutes at full use. So it is very important to always switch off the robot when not in use. For programming and testing it can best be used with the USB cable to save battery power. In addition to the motors and the distance sensor we will add the line sensor and the battery holder. This is a more difficult part to assemble, requiring some precision insertion of pre-‐made components and cables that need to fit accurately. Using the Arduino software we then make several programs to have the robot follow a wall and later on follow a line. In Part 1 the frame and wheels were printed and the distance sensor, the motors and the FreaDuino board were mounted. We tested the motors and made the robot react to the distance sensor. But all of this was done using the USB cable, powering the robot. We now will add the battery holder so the robot can be unplugged and perform tasks on its own. We will also add the line sensor and include a feature to measure the battery power, so we can warn if we are running out of power. We then develop some programs to follow a wall and to follow a line and show how to perform the Rescue Mission. 1
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1
2
3
4
5
6
3D Printing Print and ready the frame for further assembly. While you are printing you may work on testing the motors and the sensors. So make sure you start with printing the frame.
Part 1
Print the wheels and mount them. While you are printing, test and mount the motors, the processor board and the distance sensor. Test the robot and calibrate the motors. You will need the frame before you can complete this step Print the battery holder and mount the battery cable. While you are printing, prepare the line sensor cable and test the sensor. Then mount the line sensor and connect it to the processor board.
Part 1
Assembling the robot In Part 1 the board was mounted and the motors and distance sensor were tested. We partly disassemble the board and connect the line sensor and the battery cable.
Part 1 1 hour
Test and mount the distance sensor and show the value in the built-‐in serial monitor (optional)
Part 1
Test and mount the line sensor and test it with a ready made program (optional)
0.5 hours
Programming Write a program to make the robot follow a wall. Then write a program that reacts to the line sensor and make a line follower. Write a program to make the robot follow a line using the line sensor on the rescue field. Learn how to calibrate the line sensor.
2 hours
Competition with the robots Write a program to do the RoboCupJunior rescue mission. Compete with your robot against classmates.
2 hours
Explore other possibilities of the Arduino 3D robot
1 hour
7 8
9
10
2
2 hours
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 2. Building the battery holder Printing the battery holder Like we did in Part 1, we will start printing the battery holder. Use a layer height of 0.25 mm and printing will take about one hour. During that time we will test the line sensor, like we did with the distance sensor. So first skip to the instructions on how to prepare and test the line sensor. Come back here to complete the battery holder when printing is done.
Preparing the battery holder When the battery holder has been printed, we need to put in the switch, the nuts and connection strips that make the batteries into a power pack. Once the power pack has been assembled, replacing the batteries is not so easy, so we always need to make sure that we conserve energy and switch off the batteries when not in use. Let us first look at the way that the batteries are connected in the following diagram. The four AAA batteries are connected in series so they deliver a total voltage of 4x1.5v = 6v. You can also use rechargeable batteries, which deliver 1.2v each for a total voltage of 4.8v. The switch on the right connects the first two batteries. The connector on the lower left side is the plus (+) and the one on the top left is the minus or ground (-‐). Between battery 3 and 4 on the right is a connection strip as well as between batteries 2 and 3 on the left side. There are six holes for the nuts, which act as contact points. Two metal strips are used to make the connections between batteries 2 and 3 on one side and 3 and 4 on the other side. The connection between batteries 1 and 2 is formed by a switch, which can be operated on the left side of the battery holder. Batteries 1 and 4 have a connector on the left, where a small bolt is used to attach the end of the power cable. The front of the battery holder has a space and mounting holes for the line sensor. Two small M2 bolts and nuts are used for this. The flat cable connects to the five pins on the backside underneath the sensor.
3
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1
You are now going to assemble the battery holder. The holder also serves as the basis for the line sensor. You will need: the printed battery holder, a small screwdriver, the line sensor cable and the power cable, the line sensor and a switch. To mount the components you need two M2 bolts and nuts, 6 M3 nuts, 2 M3x10 bolts and 2 M3x12 bolts and two metal contact strips. Assembly is not easy. You may have to make the holes for the bolts a bit wider. Let us go through the process step by step. First you will have to put in the six nuts. They are a tight fit. The best way to put the nuts in is to first take a longer M3 bolt and fit it through the hole. Then put the nut at the other side and use the bolt to pull the nut in its cavity. You may also use a screwdriver to pull in the nut and secure it. Put in the two short M3x10 bolts in the connection spaces, where we will later on fit in the plus and minus wires.
2
3
4
Put in the line sensor and fix it with the two M2 bolts and nuts.
5
Put in the switch. The back of the switch has two patches of solder, a small one of two holes and a larger one with three holes. Put the small patch nearest to the side of the battery holder. This is where the plus pole of the first battery will be connected.
6
Connect the line sensor cable. You may have to disconnect it later on. Make sure that the red wire is on the right side, like you see in the picture. Make sure you have this checked by your teacher. Connecting it the wrong way will destroy the sensor, so be careful.
7
8
9
10
11
Now connect the power cable by putting the stripped end under the bolt and then screw it in to tighten the wire. The black minus goes on the left side, the red plus wire goes on the right side. This cable should come out in front. Now put in the two connection strips. One goes next to the switch and connects the two nuts. There is a small opening in the plastic in front of the nuts. Put the connection strip in there. Do the same with the strip on the other side, which connects the two middle nuts. Check the diagram in the beginning of this instruction card so you understand how the battery connections work and where the switch fits in the diagram. The last step is to insert the four batteries. If you understand the diagram correctly, you will know how to put in the batteries. Make sure they all connect well to the switch contact points, the cable connectors and the contact strips.
Use a voltmeter to first check if the contact points carry power. Use the switch to see if you can switch the power on and off. Also check if the voltage is 6 volts. Then check the voltage at the connector, so you are sure that the FreaDuino board will receive power through the bus connector. Now that the battery holder is complete, you will need to mount it on the frame.
4
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 3. Preparing and testing the Line Sensor Preparing the Line Sensor The first thing to be done now is to connect the cable to the sensor and then connect it to the Arduino board. We are using the row of pins on the analog side of the board like with the distance sensor. The Line Sensor actually is an array of three sensors and so it will take up three analog input ports (ports A0, A1 and A2). An analog input pin receives a signal that varies between 0v and 5v. There a special circuit, called an Analog-‐to-‐Digital converter (AD) takes the voltage and converts it into a value ranging from 0 to 1023, or a resolution of 10 bits. The value returned is then used to calculate the voltage that was given as input. A white surface returns a low value (0 to 30) and a black surface returns a high value (700 – 900). Our Line Sensor Array will return three values that represent the position of the front of the robot above the line. If all three sensors see white, there is no line in sight. If all three see black, the robot is precisely above the line. If one or more sensors see black, this can be used to find out if the line is on the left or on the right side of the robot. The first square pin on the left is the ground pin, followed by the plus pin. The remaining pins are for sensor 1, 2 and 3. If you use the camera on your mobile phone, you can check if the three sensors are emitting infrared light, which is not visible by the human eye. 1
a. Take the line sensor and connect the cable to the sensor. b. Make sure the red wire is on the right side, where on the PC board you see GND (ground). This is the pin with the square patch. The other patches are round. c. Make sure the USB cable is disconnected from the Arduino board before you plug in the sensor to the analog connectors. The gray line sensor cable has two connectors at the other side. One with three wires and another one with two wires. The one with the three wires has the red wire at the ground, corresponding to the black ground pin. The middle wire is the plus, connected to the red pin. The third pin is the Sensor 0 pin, which goes on the yellow pin labeled A0.
2
3
a. Connect the header with three wires to the last analog port on the left side of the board, which is labeled A0. Make sure the red wire is on the black pin. c. Then connect the second header to the two yellow pins labeled A1 and A2. Make sure that the last three wires of the cable are connected to the three yellow pins of the analog inputs. d. Have the connection of the line sensor cable checked by your teacher before you continue. Connecting the sensor the wrong way will destroy it, so be careful and double-‐check.
5
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 Testing the Line Sensor We are using the program 3DTestLineSensor that uses the Serial Monitor like we did with the Distance Sensor. This program continually reads the three input ports and puts the value in a variable called sensorValueX, one for each sensor, where X is the sensor number. It displays these values in the Serial Monitor screen.
How the sensor works The Pololu QTR-‐3A sensor has three very small Infrared Reflection sensors. These sensors emit infrared light, that you can see when you use the camera on your mobile phone. A sensor that returns a voltage between 0 and 5v sees the light that reflects back from the surface underneath the robot. The more light comes back, the lower the value that is returned. The maximum value is 1023. When the sensor returns a high value of around 700 it sees the line. We are now going to use a small program to show the measured values in the Serial Monitor output screen. From these values you can calculate the position of the line underneath the robot. The lighter the surface, the lower the value is. So when the robot sees a black line, the sensor returns a high value. Please notice that since the sensor uses infrared light, it can hardly detect a difference between green and black. The difference between black and white is much larger and therefore the sensor is good at detecting a black line on a light background. This type of sensor has difficulties making a difference between green and yellow or white, so it cannot detect the yellow line.
1
a. Find the program _3DTestLineSensor and open it. b. Plug in the USB cable and upload the program to the Arduino. c. Select the icon in the right upper corner with the small magnifying glass. d. This will start the Serial Monitor program that allows you to communicate with the Arduino, while it is running e. Take a look at the code of this program and see if you can get an idea what is going on here.
2
3
The sensor values are inverted
4
a. The sensor values are shown in the Serial Monitor. b. The lighter the surface is, the smaller the value. c. When you hold the sensors over a dark surface, the values will get higher. d. Find out the highest value and the lowest value that your sensor detects. Notice that the three sensors return different values. e. Take several measurements at various places above the black line. Make a note of the values you see for black and white for each of the three sensors. f. Write down these values for later use in your own program. Note that a dark color returns a higher number than a light color. A light color reflects more light, so you would expect it to return a higher value, but the electronics invert the value. You are now ready to mount the battery holder on the robot, so you can have your robot drive around without a cable.
6
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 4. Mounting the battery holder Mounting the battery holder When you have prepared the battery holder you are now ready to mount it onto the frame. The first thing you need to do is to put two nuts in the bottom nut holders. There is no need to fix all four corners. One in the front and another one in the back at the opposite side is enough. You then need to feed the cables through the frame and put them on the connectors. The line sensor plugs into the first three analog inputs. The power cable goes on the first analog input. Be very careful when connecting the cables. Always connect cables without the USB being connected. Also make sure the batteries are turned off. When the switch is moved to the front position, the batteries are off. Have everything checked first, before you turn on the power. Making a wrong connection with the line sensors or the power cable can destroy the electronics. So be careful and have everything double-‐checked.
7
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1
At the bottom put a nut in one side in front and another one in the back on the other side. Two bolts are enough to keep the battery holder in place.
2
a. Unscrew the two bolts in the back of the FreadDuino but just a little bit. b. Unscrew the bolt in the front of the processor board, so you can lift up the board at the front. c. Feed the two cables, coming from the battery holder through the big hole.
3
5
6
Make sure both cables come out in front of the robot, just next to the front spacer. If you get in trouble with the length of the cable you may detach the line sensor cable. Just make sure that the red wire stays on the right side.
a. The gray line sensor cable has two connectors at the top. One with three wires and another one with two wires. b. Connect the header with three wires to the analog port on the left side of the board, which is labeled A0. Make sure the red wire is on the black pin. c. Then connect the second header to the two yellow pins labeled A1 and A2. d. Have the connection of the line sensor cable checked by your teacher before you continue. Connecting the sensor the wrong way will destroy it, so be careful and double-‐check. a. Make sure that the battery is switched off. Moving the switch forward turns it off. Double-‐check it first before continuing. b. Now connect the power cable to the first analog connector on the right side, labeled A5. Make sure that the black and red and white/yellow pins match the pins on the connector. c. Have this checked. Connecting the power cable the wrong way will destroy the processor board. a. Now put in the two bolts in the bottom holes and fix the battery holder to the bottom of the frame.
b. Have it all checked for a last time. If all is well and your teacher gives you the final go, switch on the power by putting the switch in the back position. The red LED should come on. If it does, you are ready to get started. If it does not, switch off the power immediately. 7 If the red LED does not come on Switch off the battery. There is something wrong and ask your teacher to help you. The batteries may not have been mounted correctly, the cable may not be attached firmly or the connection strips may not touch the battery. Use a voltmeter to check every part of the circuitry.
8
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 5. Testing the battery power Checking the voltage When you are running the robot on battery power it will consume energy. After a while the batteries will get empty and you will have to replace them. So always switch off your battery when not using the robot. But it would be nice if we could see how much power is left. Fortunately the FreaDuino board has a facility to measure the internal power so we can have our program check what capacity is left in the battery. With a small test program we can measure the power level and show it in the Serial Monitor window. But when we are running on battery power, we are not connected to the PC and when connected to the PC, the battery is not used. So, how do we solve that?
Well we are going to use the red LED and make it blink fast when the battery is full and slow when the battery gets empty. This way you can easily see the status of the battery.
You are now going to test this. And remember that you can use this feature in your own program later on. The best way is to copy the showVoltage and getVoltage functions to your own program and call it in the setup function.
9
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1
a. Find the program _3DTestBattery and upload it to the robot. b. Read the code and see that in the Loop, the batteryVoltage is read from the internal analog port. The value read is in millivolts, so a value of 500 indicates 5.0 volts. c. It turns on the LED on digital port 13 and leaves it on for 550 milliseconds (ms) minus the value of the voltage read. So if the voltage is over 5.5v there is no delay. If the voltage is 3.3v there is a delay of 220 ms, which makes the LED blink 5 times per second. So the slower it blinks, the lower the battery level is.
2
a. Change the switch in the back of the FreaDuino board that is labeled 3V3 and 5v into the 3V3 position. b. Push the reset button next to the red LEDs c. Because the power supply is now 3.3 volts, the LED should start blinking about 5 times per second. d. Switch it back to 5v and it blinks much faster.
3
4
5
a. Now take off the USB cable. b. Switch on the battery. c. If the batteries are full the robot is now powered at 6 volts. This will make the LED stay on and not blink at all.
a. If you want to use this in your own program make sure that: b. You include ledPin, set pinMode to OUTPUT c. Include getVoltage and showVoltage in your own program. d. Call showVoltage in you Setup routine for at least 1 second.
So you are now ready to make your first program to make the robot run on its own without a connection to the PC.
10
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 6. Making an autonomous program Following the wall The first program we will test while running on batteries is a so-‐called wall-‐follower. We are using the distance sensor to detect if the robot sees the wall. If it does see the wall, it will turn away from it. If not, it will turn towards the wall. The distance sensor needs to be set at a certain distance so it will be able to detect the wall easily. When set at a distance that is too short, it will bump into the wall. This is because the sensor is mounted in front and can only detect the wall when it is at an angle to the wall. So the robot is continually moving away from the wall and then towards the wall again. A problem occurs when it reaches an outer corner. It loses the wall since it looks ahead about 20 cm and then wants to go towards the wall. So it bumps into the wall before it can reach the corner. When in an inner corner, things go better because it actually detects the opposite wall earlier and then makes a nice turn.
Using a PID controller
Notice that the robot makes relatively sharp turns to get back to the wall. We could make them less sharp but then it loses the wall. What we need the robot to do is to learn what the best way is to follow the wall. A so-‐called PID controller does this for us. What the PID controller does is taking the actual difference between the wall and the desired distance and makes a correction that is proportional to the difference. So Proportional stands for the P in PID. We can also Integrate the error, which means that the longer things keep going wrong, the larger the correction will be. The Derivative is the third part of the PID controller and is used to check if the error is getting smaller. We will mostly be using a PD controller, where there is no Integration involved, since with wall followers that is generally not the case.
11
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1
a. Find the program _3DTestWallFollower and upload it to the robot. b. Look at the code in the main Loop. c. It will first read the value of the distance sensor and compares it with the constant WALL_DIST. This is the distance that the robot needs to stay from the wall. But because the sensor is mounted at the front of the robot, the distance is measured diagonally. If it lost the wall it will go right and if it sees the wall it will go left. Notice that it makes larger turns to get back to the wall than turning away from the wall.
2
a. Your robot may not follow the wall properly and then you have to adjust the distance in WALL_DIST, b. If your robot bumps into the wall continually you may have to change the angle under which it corrects itself.
3
4
5
6
Experiment
7
a. You may also have the robot drive the other way. b. Try to find out what you need to change to have it follow the wall in the other direction.
a. Sometimes the corrections, the robot makes are rather large and it starts zigzagging. The robot can be made to move much smoother when we take the actual distance into account and make it react Proportionally. b. To do that, you first measure the difference between the wall and the required distance. The larger this difference is, the larger the correction that we will make. c. Check the version of the program that is called _3DTestWallFollowerPid and load it. Please note that this program also has the voltage check included. You may do the same later on in your own programs.
Experiment with this program and try to understand how the wall follower works and how you can make it work more smoothly. Also note the state of your batteries. While using the robot you will develop a feel of how it works and when things go well or do not go well. When you have mastered the art of Wall Following you are ready to get into the world of Line Followers and start using the Line Sensor array underneath the battery compartment.
12
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 7. Following a line Calibrating the line sensor
Before we can let the robot follow the black line, it needs to know what values each of the sensor will detect when noticing the black line and its surroundings. Of course we can put the robot over the black line with each of its sensors as we did when we tested the line sensor, but that is a bit of extra work. We would like the robot to find this out for itself. In addition we would like to eliminate the possible differences between the three sensors. So we will develop a fully automated calibration routine that scans the line and its surroundings before we start the competition. Because we do not want this routine to be executed every time, we also need some way to tell the robot to start the calibration. We will use the distance sensor to do that and hold our hand close to the robot to calibrate. We may also want to include the battery test at the beginning so we know if there is enough capacity left to follow the line.
13
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1
a. Find the program 3DCalibrateLineSensor and open it in Arduino. b. Look at the main Loop. There you see first that we check if there is an object in front of the distance sensor. If so, it will call Calibrate(). c. If there is no object in front, it will call readSensors(), which reads the sensors a few times and then calculates the average value for each sensor. We do this go get rid of differneces between the readings and get a stable value.
2
a. The calibration routine assumes that the robot is placed at the beginning of the black line with the three sensors above the green field, just next to the line. b. It turns on the LED and then executes a sweeping movement for four times. The drive instructions make the robot turn in place so that it first sweeps the sensors over the line and then back again. This way all three sensors read the green AND the black values. c. What the readSweep() function does is collect the highest and lowest value for each of the three sensors. That information is stored in the sensorsMin and sensorsMax arrays. d. This information is then printed on the screen using the serial monitor. You need to write down these values, so we can put them in the programs that we are using later on. e. When the calibration is finished, the LED is turned off again. So you can check the LED to see if the robot is calibrating or not.
3
a. The calibration values can of course be collected and stored automatically but then you need to include the entire calibration function in your own program later on. b. For now we will write down the calibration values and copy them into our own program for simplicity.
4
5
Now upload the program to the robot and place it at the beginning of the line with the sensors on the green part of the field, parallel to the line. Make sure that it is placed in such a way that all three sensors will see the black line when it starts moving. Because you need to watch the output in the Serial Monitor you need to bring your computer to the field of have a long cable.
Write down the results so you can include them in the LineFollower program that we will be using in the next lesson. Beware of large differences that are much different than the other values and may probably be wrong.
14
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 8. Following a line Programming the line follower Now that we have calibrated the line sensor we are ready to follow a line on the field. In order to do this we need to enter the calibration data into the program. Of course this could be done automatically, but the whole idea of this workshop is to show you how things work and provide you with a lot of examples that you can then use to build your own program. So let us now move on to following a line. The procedure is as follows: 700 350 26 23 350 700 23 700 26 0 1000 2000 0 1000 2000 0 1000 2000 Suppose that the sensors are over the line as shown in the picture. The value of each sensor is then used in the following formula: 𝑆1 + 𝑆2 ∗ 1000 + 𝑆3 ∗ 2000 𝑃𝑜𝑠𝑖𝑡𝑖𝑜𝑛 = 𝑆1 + 𝑆2 + 𝑆3 The LineFollower subtracts 1000 from this value and the resulting value is an indication of how far the line is to the left or to the right. If the value is negative the line is to the left, if the value is positive it will be to the right. The higher the number, the farther the line is from the center. This information is then used to steer the robot back to the center of the line. So in this example the value will be 752023 / 749 = 1004 Error = 4 In the left case the position is 1750023 / 1073 = 1630 Error = 630 In the right case the position is 402700 / 1076 = 374 Error = -‐626 But what happens when the robot does not see the line anymore? This depends on what the actual situation is. If the robot is following the line and lost it for some reason, the program remembers on what side it saw the line last and will then turn into that direction. But if it is checking for the end of the line, it will stop when it loses the line. So let us put this idea into practice and make our first line following program.
15
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1
a. Find the program 3DTestLineFollower and open it. b. Look at the code in the LineFollower. c. Notice that readLine() returns the calculated line position. Then 1000 is subtracted from that and is used to decide iwhat direction the robot needs to correct itself. readLine() calls readSensors to get the values for each of the sensors. It then calculates the position value and returns it to the caller.
2
3
4
5
If the sensor has lost the line it takes the last position it has seen and returns that as the position.
Also notice that in the beginning of the program we have a number of definitions for fields that we need. The sensorsMin[3] and sensorsMax[3] arrays have the calibrated values that we collected during the calibration earlier.
Upload your program to the robot and test the line follower. You may notice that the robot makes zig-‐zagging movements like we saw before with the Wall Follower.
16
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 9. Programming the rescue mission Following the line
How the LineFollower works We have seen that the three line sensors are used to calculate the position of the line under the sensor. If the robot is above the line it will return a value of 0. If it is to the left it will return a negative value, if it is to the right a positive value. The value is an indication of how far off the line is and we can use this information to control the steering of the robot. What we do not know however is how sharp the turn in the black line is. If the line is almost straight we can drive faster and have to give less corrections than when the line is very curved. So it would be good if the robot knew what kind of turns is will get. But with our little robot that is not really possible. So what we do instead is to give the LineFollower information about how fast it should drive and how large the corrections in the road ahead should be. We also will tell it how long this part of the road will last.
Using the LineFollower The LineFollower that we will use now has three parameters: Speed – Tells the LineFollower how fast it should drive. Angle – Tells the LineFollower how sharp the turns are in this part of the road. Time – Tells the LineFollower how many seconds it should keep running before returning to the main program. So you will be calling the LineSensor several times, each time with different parameters and a time parameter that tells the program when to change to a new instance of the LineSensor. The first part of the black line is relatively straight, so we can go full speed until we reach the yellow road. Unfortunately the Line Sensor is emitting infrared light, which is not capable of distinguishing between the green and yellow colors on the field. So we cannot detect the yellow line and therefore cannot use the shortcut there. So we continue with a larger angle until we reach the part with the sharp corners and there we increase the angle and possibly decrease the speed. Then we need to detect the end of the line, so we know if we have reached the swamp. There we need to do something else to find the container, which we will handle in the last lesson.
17
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 1
2
3
4
a. Find the program 3DLineFollower and load it in Arduino. b. Check the main Loop and the LineFollower to see what is going on here. c. Notice that we have included different calls to the LineFollower. These are different settings for the LineFollower. Find out what this does and determine which settings are needed for each part of the field. a. When we give a Time parameter of 0 to the robot, this does not mean that it should stop, but it is used to tell the LineFollower to check for the end of the line and stop then. b. When it detects the end of the line, the robot needs to stop the motors too. c. We need this last statement to detect the end of the line, so we can start looking for the container. But we will handle that in the last lesson. d. There is a complication here. The robot tends to lose the line at the last sharp corner and because of the 0 time parameter does not return to the line. e. What you need to do here is to count from the last part of the line and then have only the last part of the line followed with a zero time parameter. f. The best thing is to test each part of the field separately. Now find out the proper settings for each of the parts of the field. First take the beginning where you can drive fast and have a low angle. In this part there are some larger turns that need a different setting.
5
These turns are very sharp and need a high angle.
6
We have already discussed this last part. The robot should stop at the yellow swamp. Here we need to start looking for the container and we will see in the last lesson how to do that.
7
Test each part of your program separately. Count how long the robot takes to travel each part and make sure that it switches over to the next part. Take some extra safety time to make sure it will handle each part reliably.
18
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 10. Finding and evacuating the container Finding the container You have correctly followed the line and have reached the swamp. Now you need to find the container and push it out of the swamp. What is the procedure to do this ? 1 2 3 4 5 6
7
19
RLT Robotics 3D Robot -‐‑ Part 2 V0.2 – 2015 Evacuating the victim We now have the robot ready with the two motors and the distance sensor. So we are ready for our first program. Find the program 3DFleeBehavior. This program uses the distance sensor and when you hold your hand in front of it, the robot moves backward. This is called FleeBehavior. You will experiment a bit with this program. If you understand what it does, you will then load the program 3DCuriousBehavior. This is actually the same program as 3DFleeBehavior and you will modify it to make the robot follow your hand, which we call CuriousBehavior. Once you have experimented with these programs you are ready to move to the second part, where you will learn to program the robot and we will show you how to mount the battery holder and line follower sensor. This will allow the robot to operate without a cable and move autonomously. 1 2 3 4 5 6
7
20