User Tools

Site Tools


legovision

LegoVision

Vision Subsystem v4 for NXT (NXTCam-v4) Tutorial

trackingstation.jpg

The photo depicts my NXT camera setup which allows you to track objects 360 degrees around. The big picture problem is tracking objects quickly, accurately and keeping them in view. Solving this partially or completely is important because this capability will increase the versatility of the NXT Cam. This tutorial shows you how to setup the NXT Cam, and achieve fast and reliable tracking and takes approximately 1.5 hours to complete.

Motivation and Audience

This tutorial's motivation is to help others achieve good tracking results with the NXT Cam easily and quickly. Readers of this tutorial assumes the reader has the following background and interests:

  • Know how to use Lego NXT Kit
  • Also know how to use Bricx Command Center and basic coding
  • Desire to learn about vision tracking
  • In addition, would like to learn more about robotics
  • Windows Computer (Bricx Command Center is currently only a windows program although this tutorial is still a good resouce)

Parts List and Sources

To complete this tutorial, you'll need the following items in the table below.

Parts needed to build NXT Cam Tracking station

PART DESCRIPTION VENDOR PART PRICE (2013) QTY URL
Lego NXT kit Lego 8547 279.99 1 #Lego NXT Kit
NXTCam-v4 Mindsensors N/A 149.75 1 #Mind Sensors NXT Cam
Micro USB to USB Cable Amazon N/A 2.07 1 #Micro USB to USB

Setup and Construction

Software

To download the several packages needed to use the NXT cam follow the steps below:

Step 1

Download Bricx Command Center 3.3

  • Find Downloading on the Bricx Command Center site
  • Click latest version
  • Click the latest folder (3.3.8.9 - 2013)
  • Click the file with .exe at the end
  • Install the program after it finished downloading

Step 2

GoTo MindSensors NXTCam-v4 Download then unzip the following files/folder to a place of your choosing such as a new folder “NXTCam in your Documents.

  • User Guide for NXTCam-v4
  • USB Drivers for NXTCam-v4
  • NXC Sample Programs
  • Feel Free to take a look at the Related Posts and videos on this site

Step 3

Open the User Guide for NXTCam-v4 and Follow the User Guide to install the 2 USB Drivers (Due the same procedure twice for two different drivers). This is crucial!

  • View and Configuration Software

Step 4

1. Using the MicroUSB to USB cable plug the micro side into the NXTCam and the USB into your computer. 
2. Open the NXTCamView Software and select the proper serial port
3. Hit the Connect Button
4. Take the lenscover off your camera if it isn't already
5. Hit the Capture button to verify the camera works

Hardware

trackingstation_1_.jpg

To build the setup in the above picture please follow these steps. (This setup is only one possibility feel free to edit or modify it at your convenience)

Firstly acquire the materials in the parts list if you haven't already and verify everything is in good working order. Please go through the software section of Setup before hardware to verify the camera works.

Disclaimer: I won't go into extensive detail about building although i will provide decent picture and explanation. As long as your design is close you should receive similar results.

WARNING!!!! - DO NOT PLUG THE CAMERA INTO A MOTOR PORT LABELED A,B,C THIS MAY RUIN IT. ONLY PLUG THE CAMERA INTO SENSOR PORTS 1,2,3,4.

Step 1

1. Open your Lego NXT kit and familiarize yourself with it.
2. Build the Base First. (Click on the pictures for larger images)

trackingstation_8_.jpgtrackingstation_9_.jpgtrackingstation_10_.jpg

3. Build the Camera turret next.

300pxtrackingstation_12_.jpg

4. Attach the camera using the pieces mindsensors provided

Step 2

Attach the lego brick to the base and then the camera turret to the base. (Note cables shouldn't be attached yet)

trackingstation_13_.jpgtrackingstation_14_.jpgtrackingstation_2_.jpg

Step 3

WARNING!!!! - DO NOT PLUG THE CAMERA INTO A MOTOR PORT LABELED A,B,C THIS MAY RUIN IT. ONLY PLUG THE CAMERA INTO SENSOR PORTS 1,2,3,4.

  • Wire everything up making sure not to put the camera in the motor port.

trackingstation_15_.jpg

“You should have something that looks like the following:

trackingstation_3_.jpgtrackingstation_4_.jpgtrackingstation_5_.jpgtrackingstation_6_.jpg

Hello world

This section gives step-by-step instructions along with photos to start working with the NXT cam.

Step 1=

1. Build the Tracking Station shown above or use your own
2. Follow the User Guide to correctly wire the NXTCam. WARNING!!!!-DO NOT PLUG THE CAMERA INTO A MOTOR PORT LABELED A,B,C THIS MAY RUIN IT. ONLY PLUG THE CAMERA INTO SENSOR PORTS 1,2,3,4.
3. Holding one of the colored balls provided in the kit capture an image in NXTCamView
4. Follow the User Guide to Configure a ColorMap or Color For Tracking

Step 2

1. Open Bricx and connect to your NXT
2. Open the NXC Sample Program File camtest-v3 placed in your NXTCam folder
3. Download this program to the NXT
4. Wave the colored ball used in the previous step around in fron of the camera and a blob should appear on the scree 

Congratulations on Completing the Hello World Tutorial!!!

The Rest of this Tutorial is under development check back later.

Advanced Tracking

After building the tracking station and completing advanced tracking upload the code below and with a little fine tuning you will be tracking the red or blue ball provided with the lego kit.

Programming

The source code to (fill in the blank) is provided below:

camtest-v3.nxc
/************************************************************************/
/*                                                                      */
/* Program Name: camtest-v3.nxc                                            */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2008 by mindsensors.com                                */
/* Email: info (<at>) mindsensors (<dot>) com                           */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; version 3 of the License.              */
/* Read the license at: http://www.gnu.org/licenses/gpl.txt             */
/*                                                                      */
/************************************************************************/
 
const byte camPort  =  IN_1;
 
#define CAMADDR     0x02
 
#define P 50
#define I 50
#define D 50
 
#include "nxtcamlib-default.nxc"
// Global variables.
int cb; // Current blob index to display
int nblobs;  // number of blobs that are in view of NXTCam (value between 0 and 8)
int stop_flag=0;
int bc[10];
int bl[10];
int bt[10];
int br[10];
int bb[10];
int xcent=0;
int ycent=0;
float xdif=0;
float ydif=0;
int xmove=0;
int ymove=0;
string msg;
string str;
string buf2;
int count=0;
int i=0;
int n;
int stop_now = 0;
int init;
 
//paresh junk
int xmove_previous = 0;
int ymove_previous = 0;
 
//Print sensor information from its register
void ShowSensorInfo(byte prt, byte Addres )
{
   if( i2cread(prt,Addres,0x00,1)>1)
   {
     // sensor name
     TextOut(0, LCD_LINE8, i2cReadString(prt, Addres, 0x10, 8), false);
     // sensor firmware version.
     TextOut(50, LCD_LINE8, i2cReadString(prt, Addres, 0x00, 8), false);
   }
}
 
 
 
task move()
{
Wait(2000);
while (true)
{
NXTCam_GetBlobs(camPort, CAMADDR, nblobs, bc, bl, bt, br, bb);
//                            top-left X, top left Y, bottom right X, bottom right Y
if(nblobs>0){
xcent=((br[i]-bl[i])/2)+bl[i];
ycent=((bt[i]-bb[i])/2)+bb[i];
xdif=xcent-88;
ydif=ycent-72;
xmove=(xdif/88)*100;
ymove=(ydif/72)*20;
if(abs(xdif)>20)
OnFwdRegPID(OUT_A, xmove, OUT_REGMODE_SPEED, 40, 40, 90); // regulate speed
if(abs(ydif)>15)
OnFwdRegPID(OUT_B, ymove, OUT_REGMODE_SPEED, 40, 40, 90); // regulate speed
}
}
}
 
 
task print()
{
   while (true) {
    ClearScreen();
    DrawRectangle(0, 0, 176/3, 144/3);
    msg = "xc:";
    msg += NumToStr(xcent);
    msg += "      ";
    TextOut(60, LCD_LINE3, msg, false);
    msg = "yc:";
    msg += NumToStr(ycent);
    msg += "      ";
    TextOut(60, LCD_LINE4, msg, false);
    msg = "xd:";
    msg += NumToStr(xdif);
    msg += "      ";
    TextOut(60, LCD_LINE5, msg, false);
    msg = "yd:";
    msg += NumToStr(ydif);
    msg += "      ";
    TextOut(60, LCD_LINE6, msg, false);
    msg = "clr:";
    msg += NumToStr(bc[i]);
    msg += "      ";
    TextOut(60, LCD_LINE7, msg, false);
    DrawRectangle(bl[i]/3,bt[i]/3, br[i]/3,bb[i]/3);
    if ( stop_flag > 1 ) {  // if stop is indicated by pressing button
                            // twice or more,
       Wait(100);          // wait for a second for all the communicaiton
                            // to finish
       stop_now = 1;
       Off(OUT_ABC);        // and break from the loop to exit.
       PlayTone(440, 30);
       NXTCam_SendCommand(camPort, CAMADDR, 'D'); // disable tracking
       Wait(500);
       StopAllTasks();
    }
}
}
 
task button_handler()
{
  while (true) {
 
     if ( ButtonPressed(BTNCENTER, true) ) {
         while (ButtonPressed(BTNCENTER, true)) ;            //debounce the switch
      stop_flag ++;
    }
 
    if ( ButtonPressed(BTNLEFT, true) ) {
         while (ButtonPressed(BTNLEFT, true)) ;            //debounce the switch
        cb = 1;
    }
    if (ButtonPressed(BTNRIGHT, true) ) {
        while(ButtonPressed(BTNRIGHT, true));              //debounce the switch
        cb = 2;
    }
 
  }// end of while
    Wait(500);   // don't hog the CPU.
}
 
task main ()
{
  ShowSensorInfo(camPort, CAMADDR);
  Wait(130);
  // Initialise the camera
  init = NXTCam_Init(camPort, CAMADDR);
 
  NXTCam_SendCommand(camPort, CAMADDR, 'E'); // enable tracking
 
  Wait(100);
 
  cb = 0;
  // Setup button handler
  //StartTask(button_handler);
  n = 0;
  Precedes(move);
}
legovision.txt · Last modified: 2016/11/09 15:17 by dwallace