LEGO Chase Demo

Version 13 (Edward Getz, 10/07/2015 10:12 pm)

1 1 Edward Getz
h1. LEGO Chase Demo
2 1 Edward Getz
3 7 Edward Getz
This page describes how to make a LEGO robot that chases things, like a ball.  It's the same robot and program that's used in the "Pixy LEGO video":https://www.youtube.com/watch?v=HZ_0qk7Yd7A.  This robot and program is a good launching point for other projects, and it's a good introduction to PID control, which is used throughout robotics and engineering in general.
4 1 Edward Getz
5 1 Edward Getz
h2. *Building the robot*
6 1 Edward Getz
7 2 Edward Getz
The robot we're going to build can be found in Laurens Valk's book "LEGO Mindstorms EV3 Discovery Book":http://robotsquare.com/books/ev3discovery.  The robot is called the Explor3r and it uses only parts that are available in the retail Mindstorms EV3 kit.  
8 1 Edward Getz
9 7 Edward Getz
# Assemble Explor3r through step 11. "Click here for instructions on how to build Explor3r":http://robotsquare.com/2015/10/06/explor3r-building-instructions/."  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.  
10 4 Edward Getz
# Locate these LEGO parts and your LEGO-mounted Pixy. (There are instructions on how to mount Pixy to LEGO "here":/projects/cmucam5/wiki/Mounting_Pixy_with_LEGO.)   
11 3 Edward Getz
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/IMG_4994_zpsds83y5lm.jpg!
12 3 Edward Getz
 
13 3 Edward Getz
# 
14 1 Edward Getz
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/IMG_4996_zps8njal9ea.jpg!
15 4 Edward Getz
 
16 6 Edward Getz
# 
17 4 Edward Getz
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/IMG_4998_zpsj2rbmhch.jpg!
18 4 Edward Getz
 
19 6 Edward Getz
# 
20 5 Edward Getz
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/IMG_5000_zps63ambtvg.jpg!
21 5 Edward Getz
 
22 5 Edward Getz
# 
23 5 Edward Getz
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/IMG_5007_zps5956q7xb.jpg!
24 5 Edward Getz
 
25 5 Edward Getz
# Attach Pixy as shown.
26 5 Edward Getz
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/IMG_5008_zpsvosebvsq.jpg!
27 1 Edward Getz
 
28 7 Edward Getz
# Connect cable, and you're done with assembly!
29 1 Edward Getz
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/chase_ev3_zpsdnasr5hx.jpg!
30 7 Edward Getz
31 9 Edward Getz
h2. *Teach Pixy an object*
32 9 Edward Getz
33 9 Edward Getz
Go ahead and [[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!  
34 9 Edward Getz
35 7 Edward Getz
h2. *Load Chase example*
36 8 Edward Getz
37 7 Edward Getz
# Turn on your LEGO brick if you haven't done so, and hook up a USB cable between your computer and your LEGO brick.
38 7 Edward Getz
# From the LEGO Mindstorms EV3 Software, go to the *File* menu and select *Open Project*.  
39 7 Edward Getz
!http://i74.photobucket.com/albums/i241/cmucam/Image%20036_zpsxk0kqscq.png!
40 7 Edward Getz
 
41 7 Edward Getz
# Browse to the location where you unzipped the "LEGO blocks and examples" file (which you can download "on this page":/projects/cmucam5/wiki/Latest_release.), 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*.
42 9 Edward Getz
# Click on the "track" tab.  You should see a program that looks like this (below). 
43 10 Edward Getz
!{width: 50%}http://i74.photobucket.com/albums/i241/cmucam/Image%20042_zps3bvq1ehm.png!
44 9 Edward Getz
 
45 9 Edward Getz
# Run the program!  Click on the play icon in the lower right corner of your EV3 software window.  If 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.   
46 9 Edward Getz
!http://i74.photobucket.com/albums/i241/cmucam/Image%20038_zpscvm5bgys.png!
47 9 Edward Getz
 . 
48 9 Edward Getz
# If you are running PixyMon, make sure you are running the "Default program" by clicking on the home icon.
49 9 Edward Getz
!http://i74.photobucket.com/albums/i241/cmucam/Image%20039_zpssap8j5yb.png!
50 9 Edward Getz
 
51 11 Edward Getz
# Move the object that you taught Pixy in front its camera lens.  The 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!
52 11 Edward Getz
  
53 7 Edward Getz
54 9 Edward Getz
h2. *What's going on in the track program*
55 12 Edward Getz
56 13 Edward Getz
The track program uses *X centroid* output of the "Pixy block":/projects/cmucam5/wiki/Pixy_LEGO_Block
57 12 Edward Getz
!http://i74.photobucket.com/albums/i241/cmucam/Image%20045_zps7tmkn9xf.png!
58 12 Edward Getz
to adjust the heading of the robot by using a "PID controller":/projects/cmucam5/wiki/PID_LEGO_Block.
59 1 Edward Getz
!http://i74.photobucket.com/albums/i241/cmucam/Image%20046_zpsughyvxkm.png!
60 13 Edward Getz
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 controllers *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.  
61 13 Edward Getz
62 13 Edward Getz
       
63 9 Edward Getz
 
64 7 Edward Getz