ROBOTC Software EV3 Robot Workshop Lawrence Technological University Course Overview • 2015 Robofest competition RoboGolf • SPbot introduction • Using the SPbot to solve the RoboGolf challenge 2 2016 Robofest competition • • Video overview – My Youtube Channel – This channel is not complete, but will have videos about using RobotC. Key tasks – Find the edge of the table – Follow the edge of the table – Find a putting green – Find the golf ball – Aim for the hole – Mathematics to locate the center hole – Rotate the robot to putt – Putt the golf ball 3 2016 Robofest competition • Please note that putting the golf balls is beyond the scope of this workshop 4 LEGO EV3 robot used – SPbot Right Motor: C Touch Sensor EV3 Computer Sonar Sensor Color Sensor 1 Left Motor: B Color Sensor 2 Remember the connections! • Left Motor connects to B • Right Motor connects to C – If your motors are upside down forward will be backwards in your program • • • • Color sensor 1 connects to port no. 1 Color sensor 2 connects to port no. 2 Touch sensor connects to port no. 3 Sonar sensor connects to port no. 4 Please note that the retail version of EV3 uses an infrared sensor, not a sonar sensor. 6 ROBOTC Versions Used • ROBOTC Version 4.52 • Build Date Dec 7, 2015 • PowerPoint and all example programs are available at robofest.net under Tech Resources 7 Setting Up The ROBOTC Environment • Opening the source codes files for the workshop will assist in setting up the ROBOTC environment • Once the source files are loaded the EV3 motors and sensors should be assigned Setting Up The ROBOTC Environment • The first step in using ROBOTC is connecting to your EV3 robot – Robot -> LEGO Brick -> Communication Link Setup – Select your EV3 – Hit the Close button Setting Up The ROBOTC Environment • The first time you use an EV3 robot with ROBOTC, you need to download the ROBOTC kernel – Robot -> Download EV3 Linux Kernel -> Standard Kernel Brick Overview Setting Up The ROBOTC Environment Under Robot Menu Compiler Target • Physical Robot Platform Type • LEGO Mindstroms EV3 • Uncheck Natural Language Motors and Sensor Setup • Reviewed on the next slide Firmware Download 12 Motors and Sensors Setup • Select Custom Configuration 13 Motors and Sensors Setup • Set left and right motors **Check these boxes if your motors are upside down or if your robot moves the wrong direction.** 14 Motors and Sensors Setup • Set up sensors 15 Code generation • Once the motors and sensors at set up, ROBOTC will generate code to configure them • We will use this code in all programs we write in this course 16 Task 0 Find the edge of the table 17 Task 0: Example Solution Set up a Threshold of 20 Turn on motors forward. Wait until the edge of the table is detected. Stop the robot. Program: findTableEdge.c 18 YouTube: https://youtu.be/Nq7mPQIY4pE Reading sensors values • One method of monitor the sensor values is to use the ROBOTC debugger window – Download program to your robot • This opens the Debugger and Debugger windows Debugger Windows • ROBOTC offers many debugging options Reading sensors values • We can write a program to display the sensor values on the EV3 LCD screen as well Program: sensorValues.c 22 Task 1 Follow the edge of the table 23 Left Edge • Use the zig-zag method to follow the edge of the table • Edge following is also referred to as line following • We need to determine when the robot is on or off the table Right Edge Follow The Edge Of The Table Table 24 Follow The Edge Of The Table • Get color sensor values to determine when the robot is on or off the table and putting green. We will use the color sensor in Reflective Light Intensity mode. • We can use the sensorValues.c program to assist or use the Sensor debugging window. • Color Sensor 1 Color Sensor 2 – Off table = ______ (10) – On table = ______ (60) On green = ______ (20) On table = ______ (60) Color Sensor Readings 25 Follow The Edge Of The Table • Light sensor settings example – Off table = 10 – On table = 60 – Median threshold = (10+60)/2 = 35 • Two cases – Light sensor reading > 35. On table. – Light sensor reading < 35. Off table. 26 Simple Line Following Algorithm Set the threshold value. Loop forever – the robot will not stop. Based on color senor reading, determine which direction to travel to line follow. Program: LineFollowZZ.c 27 YouTube: https://youtu.be/8ZhOBW_ofk4 How to improve our line following algorithm • Zig-zag method can cause a bumpy response • To improve the response, you can use a 3-level line follower (concept shown below) Off Table On Table Off Table On Table 29 Task 2 Find a putting green 30 Find A Putting Green • One method of finding a putting green requires two color sensors – Sensor 1 used to follow the edge of the table – Sensor 2 used to locate the putting green • General idea – Follow the edge of the table until the second color sensor detects a putting green 31 Find A Putting Green • Let’s modify the previous program to stop when the robot reaches the putting green Currently the program will line follow until we stop the robot. Let’s change the outer loop to stop when the green is reached. 32 Find A Putting Green Here we modify the while loop conditional statement to use the second color sensor to detect when the putting green is reached. Once putting green is reached, we exit while loop and stop the robot. Program: LineFollowZZStop.c 33 YouTube: https://youtu.be/8ChSq_KQk5Q Task 3 Find the golf ball 35 Find The Golf Ball • General idea – Let’s assume we located the putting green and we know where the golf ball is on the green relative to the edge of the green – How can we begin to position our robot to putt? 36 Find The Golf Ball • Example • Follow the edge of the putting green a distance “m” • This will position the robot in line with the golf ball m Robot m = 11 cm for the Junior Division. What about the Senior Division? 37 Find The Golf Ball • How to find “m” given n/m – From the diagram of the putting green we have π + π = 22 cm – Let’s assume that n/m = X (X is known) – Now we can solve for m π =π → π =π∗π π π + π = π ∗ π + π = π π + 1 = 22 cm 22 cm π= π+1 38 Find The Golf Ball • One solution – Follow the edge of the putting green until we reach the position of the ball • Approach – Let’s modify LineFollowZZStop.c to stop at the location of the ball • Tools needed – Line following – Measure distance traveled 39 Measure Distances • Determine how far the robot travels moving forward for 2 seconds Compute distance traveled by measuring the number of rotations of the wheel Distance 40 Measure Distances • Use the wheel geometry PI = 3.14 Radius How can use this information? 41 Measure Distances • For each rotation of the wheel, the robot will travel (Wheel Diameter) x (PI) • Distance = (Wheel Diameter) x (PI) x (# Rotations) • Distance = (5.5 cm) x (PI) x (# Rotations) • Distance = (17.28 cm) x (# Rotations) 42 Measure Distances Here we reset the a motor encoder. The encoder outputs the rotation of the motor in degrees so we convert the output to rotations. Code added to wait until the touch sensor is pressed to keep the information visible on the robot screen. Program: measureDistance.c 43 Aligning The Robot With The Golf Ball • Proposed method: – Compute the distance to travel along the edge of the putting green – Compute the number of rotations required to travel that distance – Find the edge of the putting green – Reset motor rotation sensor – Follow the edge of the putting green – Stop the robot when the desired number of rotations is reached 44 Aligning The Robot With The Golf Ball • Example – Putting green dimensions • m = 11 cm, n = 11 cm – Number of rotations • Distance = (Wheel Diameter) x (PI) x (# Rotations) • Solve for (# Rotations) (# Rotations) = (# Rotations) = Distance (Wheel Diameter) x (PI) 11 cm (5.5 cm) x (PI) = 0.64 rotations 45 Aligning The Robot With The Golf Ball Here we define some variables. Loop until the desired distance is traveled. Compute the distance traveled. Program: lineFollowDistance.c 46 Task 4 Aim for the hole 47 Aim for the hole • We will review a few methods to aim for the hole • Method 1: Search for the flag pole – Scan using the sonar senor • Method 2: Compute the location of the hole – Mathematically determine the location of the hole • Step 1 – Determine the angle we must rotate to aim the robot towards the golf hole • Step 2 – Rotate the robot the determined amount • Method 3: Determine the location using trial and error – Here we find the hole by rotating the robot different amounts in an attempt to find the correct orientation Method 1: Scan For Hole • Here we are going to have the robot spin until it “sees” the center hole flag with the sonar sensor This empty loop with allow the robot to spin until an object is detected by the ultrasonic sensor. Program: spinSearch.c 49 Method 2: Mathematical Approach • Using this approach we can calculate how far to rotate the robot to face the center hole • We complete this in two steps – Step 1 • Determine the angle we must rotate to aim the robot towards the golf hole – Step 2 • Rotate the robot the determined amount 50 Determine The Rotation Angle • We can use geometry to determine the location of the hole r s t 51 Determine The Rotation Angle • We can use trigonometry to determine the location of the hole and aim the robot π siπ π = π‘ π πππ π = π‘ π tan π = π r s π π = atan π θ t Determine The Rotation Angle • Use an advanced math block to compute the necessary rotation angle 40 – Assume the following • r = 40 cm • s = 30 cm 36.87° 30 53.13° 50 Program: trigMath.c 53 Rotate The Robot To Putt • We need to rotate the robot θ degrees to aim the robot at the golf hole Starting Position Robot Rotated Position 90° - θ Rotate The Robot To Putt Robot • We will use the spin feature to turn the robot θ degrees • When the robot spins, the wheel path is a circle centered between the wheels • The diameter is the track width of the robot Rotate The Robot To Putt • For an example, let’s spin the robot 90 deg – Robot track width = 16.2 cm – The circumference of the robot’s path • C = PI * D = 3.14 * 16.2 cm = 50.87 cm – The circumference of the robot’s wheel • C = PI * D = 3.14 * 5.5 cm = 17.27 cm • 90 degrees is ¼ of the circle. The robot travels – D = ¼ x 50.87 cm = 12.72 cm • How rotations to travel 12.72 cm? – # Rot = Distance / (Wheel Circumference) – # Rot = 12.72 cm / 17.27 cm = 0.74 56 Rotate The Robot To Putt • Spinning robot example – Robot width = 16.2 cm – Wheel Diameter = 5.5 cm • Circumference = 17.27cm • Number of rotations π·ππ π‘ππππ ππππ£ππππ # π ππ£πππ’π‘ππππ = πβπππ πΆππππ’ππππππππ 12.72 ππ # π ππ£πππ’π‘ππππ = = 0.74 πππ‘ππ‘ππππ 17.27 ππ 57 Rotate The Robot To Putt • We can use one block to spin the robot Set motor targets to the degrees they need to turn, or #Rotations * 360 Speed of 20 for leftMotor and a speed of -20 for rightMotor so they spin in opposite directions. Program: spin90.c 58 YouTube: https://youtu.be/icAw-SnjIes Whew that’s A LOT of math!!! Task 5 Putt the golf ball 61 Putt The Golf Ball • Again, this task is beyond the scope of this course • However, your robot should be in position to putt the ball in the center hole • Remember, you can only hit the golf ball once and only with the wooden block putter 62 Functions • Solving the Robofest Game challenge will typically require a fairly large EV3 program • Very large programs can be difficult to understand, navigate and use • To alleviate this issue, ROBOTC allows the use of functions group and reuse sections of your program Functions • For example, let’s assume you have a section code that completes the following: – Move forward until the edge of the table is found with color sensor 1, then stop – After stopping, rotate the robot 90 degrees • Here is an example… Functions • Let’s create a function called findEdgeAndTurn Now can call the function from our main task program Program: findEdgeAndTurn.c Putting It All Together • In this course we learned how to – Find the edge of the table – Follow the edge of the table – Find a putting green – Find the golf ball – Aim for the hole – Putt the golf ball 66 Little Robots, Big Missions Questions? Email me at Bjb01234@gmail.com chung@LTU.edu 67