<< return to Pixycam.com

User Tools

Site Tools


wiki:v2:lego_chase_demo

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
wiki:v2:lego_chase_demo [2018/05/28 01:40]
pixycam
wiki:v2:lego_chase_demo [2019/01/30 23:28] (current)
pixycam
Line 12: Line 12:
 The robot we're going to build can be found in Laurens Valk's book [[http://​robotsquare.com/​books/​ev3discovery|LEGO Mindstorms EV3 Discovery Book]]. The robot is called the Explor3r and it uses parts that are available in the retail Mindstorms EV3 kit. The robot we're going to build can be found in Laurens Valk's book [[http://​robotsquare.com/​books/​ev3discovery|LEGO Mindstorms EV3 Discovery Book]]. The robot is called the Explor3r and it uses parts that are available in the retail Mindstorms EV3 kit.
  
-  - Assemble Explor3r through step 11. [[http://​robotsquare.com/​2015/​10/​06/​explor3r-building-instructions/​|Click here for instructions on how to build Explor3r]]."​ That is, stop short of mounting the IR Sensor (step 12). Instead of the IR Sensor, we'll be mounting ​Pixy. If you are using an NXT brick, your instructions will be very similar+  - Assemble Explor3r through step 11. [[http://​robotsquare.com/​2015/​10/​06/​explor3r-building-instructions/​|Click here for instructions on how to build Explor3r]]."​ That is, stop short of mounting the IR Sensor (step 12). Instead of the IR Sensor, we'll be mounting ​Pixy2
-  - Locate these LEGO parts and your LEGO-mounted ​Pixy. (There are instructions on how to mount Pixy to LEGO [[wiki:​v2:​Mounting_Pixy_with_LEGO.|here]])\\ +  - Locate these LEGO parts and your LEGO-mounted ​Pixy2. (There are instructions on how to mount Pixy2 to LEGO [[wiki:​v2:​Mounting_Pixy_with_LEGO.|here]])\\ 
-{{wiki:img:0ce334b7f975bd1bc2a8ff02364c66bab884f45e.jpg?​400}}\\+{{wiki:v1:img_5313_result.jpg?​400}}\\
    
   - \\   - \\
-{{wiki:img:4700af4039676ed890a5b07d73f88c66aa2adeb5.jpg?​400}}\\+{{wiki:v1:img_5314_result.jpg?​400}}\\
    
   - \\   - \\
-{{wiki:img:c57f4f15003d90419bd269594dd44be85e151068.jpg?​400}}\\+{{wiki:v1:img_5316_result.jpg?​400}}\\
    
   - \\   - \\
-{{wiki:img:3d1acda20f7e4fdad7ff81c7af3c6b56085068d4.jpg?​400}}\\+{{wiki:v1:img_5318_result.jpg?​400}}\\
    
   - \\   - \\
-{{wiki:img:1a172eb992784b7e8b99ca2a72b48e86669d908d.jpg?​400}}\\+{{wiki:v1:img_5319_result.jpg?​400}}\\
    
-  - Attach ​Pixy as shown.\\ +  - Attach ​Pixy2 as shown.\\ 
-{{wiki:img:1d6bdb767ebf4c690f2ab91c35d30b6819ad0572.jpg?​400}}\\+{{wiki:v1:img_5320_result.jpg?​400}}\\
    
-  - Connect the Pixy cable. Plug one end of the cable into Pixy and the other end into port 1 of your brick.\\ +  - Connect the Pixy2 cable. Plug one end of the cable into Pixy2 and the other end into port 1 of your brick.\\ 
-{{wiki:img:7a4ceae022477335cae33edf506435c88303588f.jpg?400}}+{{wiki:v1:img_5322_result.jpg?400}}
  
  
Line 38: Line 38:
  
   - The motors connect to motor ports A and B on your brick. Don't "​cross"​ the cables. That is, if you are facing the motor ports (the back of the robot), the left motor connects to port B, the right motor connects to port A.   - The motors connect to motor ports A and B on your brick. Don't "​cross"​ the cables. That is, if you are facing the motor ports (the back of the robot), the left motor connects to port B, the right motor connects to port A.
-  - Pixy connects to sensor port 1.\\+  - Pixy2 connects to sensor port 1.\\
    
  
  
-===== Teach Pixy an object =====+===== Teach Pixy2 an object =====
  
-Go ahead and [[wiki:​v2:​Teach_Pixy_an_object_2|teach ​Pixy an object]]. You might try using the button press method. Using the button press method doesn'​t require that you hook up a USB cable to Pixy and run PixyMon, so it's much more convenient!+Go ahead and [[wiki:​v2:​Teach_Pixy_an_object_2|teach ​Pixy2 an object]]. You might try using the button press method. Using the button press method doesn'​t require that you hook up a USB cable to Pixy2 and run PixyMon, so it's much more convenient!
  
  
-===== Load Chase example ​=====+===== Load Chase Example ​=====
  
   - Turn on your LEGO brick if you haven'​t done so, and hook up a USB cable between your computer and your LEGO brick.   - Turn on your LEGO brick if you haven'​t done so, and hook up a USB cable between your computer and your LEGO brick.
   - From the LEGO Mindstorms EV3 Software, go to the **File** menu and select **Open Project**.\\   - From the LEGO Mindstorms EV3 Software, go to the **File** menu and select **Open Project**.\\
-{{wiki:​img:​3e3b64f2ff052ac58280fa2315eff9d803714e6e.png?400}}\\+{{wiki:​img:​3e3b64f2ff052ac58280fa2315eff9d803714e6e.png}}\\
    
-  - Browse to the location where you unzipped the "LEGO blocks and examples"​ file (which you can download [[https://​pixycam.com/​downloads-pixy2/​|on this page]]), and then to the **examples** directory and then either to the **EV3** or **NXT** directory depending on your brick version. Select ​"chase.ev3" and click on **Open**. +  - Browse to the location where you unzipped the "LEGO blocks and examples"​ file (which you can download [[https://​pixycam.com/​downloads-pixy2/​|on this page]]), and then to the **examples** directory and select ​"ccc\_chase.ev3" and click on **Open**. ​ You should see a program that looks like this (below).\\ 
-  - Click on the "​track"​ tab. You should see a program that looks like this (below).\\ +{{wiki:v1:image_735.png}}\\
-{{wiki:img:043148247c8d379fb867582e6230deb13bba5371.png?400}}\\+
    
-  - Run the program! Click on the play icon in the lower right corner ​of your EV3 software windowIf you don't see the play icon, make sure your LEGO brick is powered on, finished booting, and connected to you computer via a USB cable.\\ +  - To run the chase program
-{{wiki:​img:​aa5d26848e8509a94a41c8bc66ed36125ee5af36.png?400}}\\ + 
- . +    - Make sure you have a fair amount ​of space for your robot to move around in. 
-  If you are running PixyMonmake sure you are running ​the "​Default ​program" ​by clicking on the home icon.\\ +    - Make sure Pixy2 can reliably detect/​track ​your object(Look at Pixy2'​s LEDDoes it glow when the object is present?) 
-{{wiki:img:0cb61d054b661f140bf4037bae3a8f67a95494cd.png?400}}\\ +    - Tilt Pixy2'​s lens down such that it is looking slightly down at the ground, like in the final assembled picture above
-  +    Place the object that you taught Pixy2 on the ground in front of the robotbut such that Pixy2 can still see it. (A ball is perfect for this.) 
-  - Move the object ​that you taught Pixy in front its camera lensThe track program simply adjusts ​the angle of the robot so that it always faces your object. ​It doesn'​t chase your object, ​it just "​faces"​ your object, but it's quick!\\+    - Now run the program by clicking on the play icon in the lower right corner of your EV3 software window.\\ 
 +{{wiki:img:aa5d26848e8509a94a41c8bc66ed36125ee5af36.png}}\\ 
 + 
 +You should notice that the robot chases ​the object. ​Or more specifically,​ it will maintain a certain distance from the object. If the object is farther away, the robot will approach (chase) the object. ​If the object ​is too closethe robot will back up until the correct distance to the object ​is reestablished.  ​
    
  
 +===== Troubleshooting =====
  
-===== What's going on in the track program? =====+If your robot doesn'​t chase after your object, here are the things to check first. ​  
 + 
 +  - Check to make sure that Pixy2 can track your object reliably. ​ Bring up PixyMon to verify. ​ If it doesn'​t track the object reliably, [[wiki:​v2:​some_tips_on_generating_color_signatures_2|this page]] has some helpful pointers. 
 +  - If your robot turns away from your object, make sure that the motor cables aren't crossed. ​ That is, if you are facing the motor ports (the back of the robot), the left motor connects to port B, the right motor connects to port A. 
 +  - If your robot gets too close to your object when it's chasing it, tilt Pixy2 such that it is looking more level. ​ Alternatively,​ if your robot isn't getting close enough to your object while it's chasing it, tilt Pixy2 more down. 
 +  - Sometimes it's easier to debug things with the robot "up on blocks"​ such that it can't move, but its wheels are free to rotate. ​   
 + 
 + 
 +===== What's going on in the chase program? =====
  
 The track program uses **X centroid** output of the [[wiki:​v2:​Pixy_LEGO_Block|Pixy block]]\\ The track program uses **X centroid** output of the [[wiki:​v2:​Pixy_LEGO_Block|Pixy block]]\\
-{{wiki:img:9396b60542bb7b47d8badfd066bcbb250754fc9d.png?400}}\\+{{wiki:v1:image_737.png}}\\
 to adjust the heading of the robot by using a [[wiki:​v2:​PID_LEGO_Block|PID controller]].\\ to adjust the heading of the robot by using a [[wiki:​v2:​PID_LEGO_Block|PID controller]].\\
-{{wiki:img:b081d3680dbafee48b02da203d65785b8c937eec.png?400}}\\ +{{wiki:v1:image_738.png}}\\ 
-That is, the **X centroid** output of the Pixy block ranges between 0 and 255, depending on where the detected object is in Pixy's image. The "​center pixel" is therefore 128. The PID controller tries to move the motors such that the detected object is always centered in the image. It does this by comparing the **X centroid** value to 128 (the PID controller'​s **Set Point** input).+That is, the **X centroid** output of the Pixy2 block ranges between 0 and 255, depending on where the detected object is in Pixy2's image. The "​center pixel" is therefore 128. The PID controller tries to move the motors such that the detected object is always centered in the image. It does this by comparing the **X centroid** value to 128 (the PID controller'​s **Set Point** input).
  
-The **Control Output** of the PID controller is fed directly into the left wheel controller (the **Power Left** input of the Move Tank controller block). The **Power Right** input is fed the inverted output of the PID controller by subtracting the PID controller'​s output from 0. Why are the wheels controlled in opposite directions? We are interested in controlling only the rotation of the robot, and rotating the robot is achieved by powering the motors in opposite directions at the same power/​speed. It is in this way that the robot "​tracks"​ the object and always faces it. The program is surprisingly simple!+The **Control Output** of the PID controller is fed directly into the left wheel controller (the **Power Left** input of the Move Tank controller block). The **Power Right** input is fed the inverted output of the PID controller by subtracting the PID controller'​s output from 0. Why are the wheels controlled in opposite directions? We are interested in controlling only the rotation of the robot, and rotating the robot is achieved by powering the motors in opposite directions at the same power/​speed. It is in this way that the robot "​tracks"​ the object and always faces it. 
  
-There is some additional logic in the program to make sure that if there are no detected objects, the robot doesn'​t move. This is done by comparing the **Signature Output** of the Pixy block with 0. If the **Signature Output** is zero, Pixy has not detected an object. If the **Signature Output** is non-zero, ​Pixy has detected an object. The Switch block selects between these two possibilitiesand feeds the PID controller output into the motors when an object is detected, and feeds 0 into the motors when no objects are detected.+There is some additional logic in the program to make sure that if there are no detected objects, the robot doesn'​t move. This is done by comparing the **Signature Output** of the Pixy2 block with 0. If the **Signature Output** is zero, Pixy2 has not detected an object. If the **Signature Output** is non-zero, ​Pixy2 has detected an object. The Switch block selects between these two possibilities and feeds the PID controller output into the motors when an object is detected, and feeds 0 into the motors when no objects are detected.
  
  
Line 91: Line 102:
 The proportional gain "does most of the work" as they say, so tuning should begin by adjusting this value. Start with a small value of 0.1 and run the program. Notice how the robot is now very slow and sluggish. That is, when trying to track the object (which you're moving around) the robot seems to be trying, but it's heart doesn'​t seem to be into it. This is telling you that your proportional gain is too small. The proportional gain "does most of the work" as they say, so tuning should begin by adjusting this value. Start with a small value of 0.1 and run the program. Notice how the robot is now very slow and sluggish. That is, when trying to track the object (which you're moving around) the robot seems to be trying, but it's heart doesn'​t seem to be into it. This is telling you that your proportional gain is too small.
  
-Try settnig ​the proportional gain to 1.and run the program. You might notice that the robot does a shimmy after it faces your object. In other words you move the object quickly, and the robot responds by turning to face the object, and instead of stopping when it faces your object, it keeps on going. It overshoots. In fact, it may overshoot several times before coming to a rest, finally facing your object like it was programmed to do. This is oscillation,​ and it's a very common problem.+Try setting ​the proportional gain to 1.and run the program. You might notice that the robot does a shimmy after it faces your object. In other words you move the object quickly, and the robot responds by turning to face the object, and instead of stopping when it faces your object, it keeps on going. It overshoots. In fact, it may overshoot several times before coming to a rest, finally facing your object like it was programmed to do. This is oscillation,​ and it's a very common problem.
  
 To reduce the oscillation,​ you need to increase the derivative gain. Increasing the derivative gain is like adding friction to your system, which may sound like something you should avoid. However, increasing the derivative gain does not adversely affect the efficiency of your system as friction does. Start by setting the derivative gain to the same value as the proportional gain. And continue to increase the derivative gain until the oscillation is under control. To reduce the oscillation,​ you need to increase the derivative gain. Increasing the derivative gain is like adding friction to your system, which may sound like something you should avoid. However, increasing the derivative gain does not adversely affect the efficiency of your system as friction does. Start by setting the derivative gain to the same value as the proportional gain. And continue to increase the derivative gain until the oscillation is under control.
Line 99: Line 110:
 If you're successful in removing the oscillation,​ you might try your luck by increasing the proportional gain again (not as high as before) and seeing if you can still bring the oscillation under control with derivative gain. And if you're unsuccessful in removing the oscillation,​ you can always reduce the proportional gain and try different derivative gains as before. If you're successful in removing the oscillation,​ you might try your luck by increasing the proportional gain again (not as high as before) and seeing if you can still bring the oscillation under control with derivative gain. And if you're unsuccessful in removing the oscillation,​ you can always reduce the proportional gain and try different derivative gains as before.
  
-Our goal is to have our robot that tracks the object quicklybut without oscillation. This is usually the goal, but you may want a robot that oscillates or is sluggish. It's all up to you!+Our goal is to have our robot that tracks the object quickly but without oscillation. This is usually the goal, but you may want a robot that oscillates or is sluggish. It's all up to you!
  
-What about the integral gain? Integral gain is useful when the system has trouble reaching the goal. Say if our robot tended to get close to facing our object and then stopped, we could try adding a little integral gain. The integral gain would then make sure that the robot would face the object, eventually. The integral gain is zero in this example because ​the the LEGO motors have their own built-in control loops that make sure that we always reach our goal.+What about the integral gain? Integral gain is useful when the system has trouble reaching the goal. Say if our robot tended to get close to facing our object and then stopped, we could try adding a little integral gain. The integral gain would then make sure that the robot would face the object, eventually. The integral gain is zero in this example because the LEGO motors have their own built-in control loops that make sure that we always reach our goal.
  
  
 ===== Chasing the object ===== ===== Chasing the object =====
  
-Click on the "​chase"​ tab. You should see a program that looks like this (below).\\ 
-{{wiki:​img:​4c2db0b428995f2e1d1c0139a4d990e814f89032.png?​400}} 
- 
-Notice that it's very similar to the track program. In fact, the track program is mostly intact in the top half of the chase program, but there'​s an additional PID controller in the bottom half of the program. This PID controller adds the "​chase"​ element. 
- 
-To run the chase program: 
- 
-  - Make sure you have a fair amount of space for your robot to move around in. 
-  - Make sure Pixy can reliably detect/​track your object. (Look at Pixy's LED. Does it glow when the object is present?) 
-  - Tilt Pixy's lens down such that it is looking slightly down at the ground, like in the final assembled picture above. 
-  - Place the object that you taught Pixy on the ground in front of the robot, but such that Pixy can still see it. (A ball is perfect for this.) 
-  - Now run the program by clicking on the play icon in the lower right corner of your EV3 software window. 
- 
-You should notice that the robot chases the object. Or more specifically,​ it will maintain a certain distance from the object. If the object is farther away, the robot will approach (chase) the object. If the object is too close, the robot will back up until the correct distance to the object is reestablished. The program is more complicated than the track program, but not by much! 
  
  
 ===== What's going on in the chase program? ===== ===== What's going on in the chase program? =====
  
-If you read and understood the section on how the track program works, much of the chase program should make sense. The main difference is the extra PID controller. The new controller takes the **Y Centroid** output of the Pixy block and compares it to 150. Why 150? The **Y Centroid** output varies between 0 and 199 depending on the position of the detected object in the image. If the object is higher in the image, the **Y Centroid** value is lower, and if the object is lower in the image, the **Y Centroid** value is higher. It'​s ​backwards ​in a sense, but many cameras follow this same scheme.+If you read and understood the section on how the track program works, much of the chase program should make sense. The main difference is the extra PID controller. The new controller takes the **Y Centroid** output of the Pixy2 block and compares it to 150. Why 150? The **Y Centroid** output varies between 0 and 199 depending on the position of the detected object in the image. If the object is higher in the image, the **Y Centroid** value is lower, and if the object is lower in the image, the **Y Centroid** value is higher. It'​s ​backward ​in a sense, but many cameras follow this same scheme.
  
-Now consider that Pixy is angled slightly such that it is looking downward. This allows your robot to use a simple distance-measuring technique. Assuming the object of interest is resting on the groundif the object is higher in the image, it is farther away from Pixy. If the object is lower in the image, it is closer to Pixy. In fact, you can estimate the actual distance by performing a simple calculation:​ distance = constant * 1/(1 + Y Centroid). You just need to figure out the value of the constant by doing some simple calibration with a known distance. Anyway, by choosing the value of 150, we are choosing the lower part of the image, or when the object is relatively close. You can change this value if you like. In particular, if you lower the value (to 100, for example), the robot will stop when it is farther away. If you raise the value (no greater than 199 though!), the robot will stop when it is closer.+Now consider that Pixy2 is angled slightly such that it is looking downward. This allows your robot to use a simple distance-measuring technique. Assuming the object of interest is resting on the ground if the object is higher in the image, it is farther away from Pixy2. If the object is lower in the image, it is closer to Pixy2. In fact, you can estimate the actual distance by performing a simple calculation:​ distance = constant * 1/(1 + Y Centroid). You just need to figure out the value of the constant by doing some simple calibration with a known distance. Anyway, by choosing the value of 150, we are choosing the lower part of the image, or when the object is relatively close. You can change this value if you like. In particular, if you lower the value (to 100, for example), the robot will stop when it is farther away. If you raise the value (no greater than 199 though!), the robot will stop when it is closer.
  
 How is the output of the new PID controller used to control the motors? The track program was already controlling the motors such that it controlled the rotation of the robot. The new PID controller must somehow control the translation (forward/​backward) motion of the robot while not interfering with the rotation motion. It does this by using a simple set of equations. How is the output of the new PID controller used to control the motors? The track program was already controlling the motors such that it controlled the rotation of the robot. The new PID controller must somehow control the translation (forward/​backward) motion of the robot while not interfering with the rotation motion. It does this by using a simple set of equations.
Line 134: Line 131:
 translation = left_motor + right_motor translation = left_motor + right_motor
 </​code>​ </​code>​
-If you've ever driven a tank, these equations probably make sense. But we're interested in the "​inverted"​ equations. We want expressions for the left_wheel ​and the right_wheel ​in terms of rotation and translation. That is, we already know how we want our robot to rotate and translate -- these are the outputs of our PID controllers. So rotation and translation are our **knowns**. Our **unknowns** are left_wheel ​and right_wheel. Using some algebra, we get the inverted equations.+If you've ever driven a tank, these equations probably make sense. But we're interested in the "​inverted"​ equations. We want expressions for the left\_wheel ​and the right\_wheel ​in terms of rotation and translation. That is, we already know how we want our robot to rotate and translate -- these are the outputs of our PID controllers. So rotation and translation are our **knowns**. Our **unknowns** are left\_wheel ​and right\_wheel. Using some algebra, we get the inverted equations.
  
 <​code>​ <​code>​
Line 145: Line 142:
 ===== Some notes on the PID block ===== ===== Some notes on the PID block =====
  
-By putting the PID controller in a block, we make things simpler. It's now very easy to make programs with PID control loops. In fact, you can use the PID block for other programs that don't use Pixy. Use the PID block to control your robot'​s distance to the wall for a wall-following robot. That is, use the IR sensor (in proximity mode) as an input to the PID controller. And feed the output of the PID controller into the steering control of your robot.+By putting the PID controller in a block, we make things simpler. It's now very easy to make programs with PID control loops. In fact, you can use the PID block for other programs that don't use Pixy2. Use the PID block to control your robot'​s distance to the wall for a wall-following robot. That is, use the IR sensor (in proximity mode) as an input to the PID controller. And feed the output of the PID controller into the steering control of your robot.
  
-But by making the PID controller a block, we hide the implementation details. These details are simple thoughand can be summarized with a few simple expressions.+But by making the PID controller a block, we hide the implementation details. These details are simple though and can be summarized with a few simple expressions.
  
 <​code>​ <​code>​
Line 161: Line 158:
 ===== Where to go from here ===== ===== Where to go from here =====
  
-You've created a robot that can chase things. Using the same concepts you can create a robot that goes over to an object and picks it up. Or you can create a robot that launches projectiles at things. Or you can create a robot that goes over to its charger and plugs itself in. Or you can create the worlds first (as far as we know) soccer-playing LEGO robot. The programs to create these robots are more complicatedbut armed with these concepts, much of the added complexity is in the interleaving of these concepts.+You've created a robot that can chase things. Using the same concepts you can create a robot that goes over to an object and picks it up. Or you can create a robot that launches projectiles at things. Or you can create a robot that goes over to its charger and plugs itself in. Or you can create the worlds first (as far as we know) soccer-playing LEGO robot. The programs to create these robots are more complicated but armed with these concepts, much of the added complexity is in the interleaving of these concepts.
  
-So think about what you want your robot to do, then do it! (And by all means tell us about it! <​support@charmedlabs.com>)+So think about what you want your robot to do, then do it! (And by all meanstell us about it! <​support@pixycam.com>)
  
  
wiki/v2/lego_chase_demo.1527471637.txt.gz · Last modified: 2018/05/28 01:40 by pixycam