Introduction: Motion Tracker

About: I am passionate about anything and everything engineering and physics. I am interested in much of chemistry and biology and I enjoy most art and craft + cooking and baking. I have completed my bachelor's in me…
Motion tracking done with a webcam and some Lego
This is just a very crude design (especially the hardware), but you can apply the things you learn here to a more polish and solid setup too. You can also add a gun/rocket launcher/cannon as many of you would think about it

For this project the main things i used is a webcam, 2 NXT motors and an NXT "brick"
the software used is Roborealm (I am using their 30 day trial) with a custom VBscript

I learned of the software from this video and the VBscript is a modified version of the one he provided, so credits to him

Please note there is more information in the picture annotations 

Step 1: Hardware

The hardware setup is pretty straightforward, mount the camera to the 2 motors where 1 allows it to tilt up and down while the other allows it to pan left and right. Initially i did not use the worm-gears, but decided to do so to slow the motor down since its a bit of a problem controlling the speed through Roborealm.

Step 2: Software

The software used is Roborealm you can get a free trial version at their page here
There are 4 main steps to setting up the software

1)Motion detection
Examples ->motion detection
Add that as your first filter/module
You will want the background image to be set to Previous frame.
For the technique used and difference threshold, you really need to play around with that as it depends on your environment, lighting and what you are trying to track.
Also remember to check "set still to black" its important for the next step

2)Center of Gravity
Analysis-> Center_of_gravity
Add that as your second filter/module
For this part, you also need to play around with the filter settings.
The display settings do not affect the performance, just what is shows 

3)Lego_NXT (if you are using some other hardware select that instead)
For this part you would want to check "Enable Motors"
Then define 2 variables by typing into the boxes, here I used "Aa" and "Cc", you will be using these variables in your VBscript

4)VBScript Program
You will need to load your VBScript (We will get there in the next step) and run it

Step 3: VBscript and Endword

Personally I have never done anything in Visual Basic but it worked out fine since I am familiar with some other languages
The script was modified off the script that I got from the video on youtube (link at intro step) so credits to him.
For the script, remember to make sure the variables you use matches the ones you defined at the Lego_NXT module, there I used "Aa" and "Bb" so I use the same thing here. For the NXT motors, 0 means full power in one direction and 255 is full power in the other direction while 128 is stop
And I am only using a resolution of 352x288 since thats the maximum resolution i could select while getting 30fps, it might be different for your setup. This means the pixel at which you want your robot to start turning left, right, up or down depends on your resolution.
you can check and change your resolution at Options->Camera->Video Format

Here is the code i used, excluding the dashed lines
' initialize all values
Aa = 128
Bb = 128
Cc = 128

' get the size (width or height) of the current bounding box
size = GetVariable("COG_BOX_SIZE")

' if it is equal to "" then no object was detected
if size <> 0 and size < 150  then

' get the horizontal center of gravity
cogX = GetVariable("COG_X")

' pan left
if cogX < 150 then
StartTime = Timer
Aa = 40
Elapsed = Timer - StartTime
end if

' pan right
if cogX > 200 then
Aa= 220
end if

' stop x
if cogX = 0  then
Aa = 128
end if

' get the vertical center of gravity
cogY = GetVariable("COG_Y")

' tilt down
if cogY < 134 then
Cc = 40
end if

' tilt up
if cogY > 154 then
Cc = 220
end if

' stop y
if cogY = 0 then
Cc = 128
end if
end if

SetVariable "Aa", Aa
SetVariable "Cc", Cc

Thank you for reading, feel free to leave you comments, opinions and feedback. Questions are also welcome
and if you like it please vote

Hardware Hacking

Participated in the
Hardware Hacking

Build My Lab Contest

Participated in the
Build My Lab Contest