Friday, July 3, 2009

NXTCamView

-


NXTCamView configures and controls the NXTCam made by Mindsensors. NXTCam is a camera sensor that provides real-time vision recognition to NXT robots. NXTCam uses simple color recognition to detect objects in its view.

The main purpose of NXTCamView is to teach the NXTCam which colors to track. Colors are setup in ranges that are uploaded and stored in the NXTCam.

To teach the NXTCam colors -

1. Download, install and start NXTCamView on your PC.

· http://sourceforge.net/projects/nxtcamview/files/NXTCamView%20install/NXTCamView-0.2.1.msi

2. Ensure the two NXTCam drivers (USB-to-Serial and NXTCam) have been installed on your PC. I mentioned in an earlier blog how to install them.

3. Ensure the NXTCam is plugged into one of your PCs USB ports.

4. In NXTCamView, setup communications to the NXTCam via Tools\Options. Often only the COMPort needs to be set.

5. Connect to your NXTCam.

6. Capture an image (or two) to check the focus and lighting conditions.

7. Select some colors to track by clicking on the image you captured. Colors are shown as ranges of red, green and blue (min/max values of each). You can add or remove colors from a range by holding down the "CTRL" key or "SHIFT-CTRL" keys while clicking.

8. Upload the color ranges into the NXTCam.

9. Test your colors in the Tracking window to see what object blobs are returned.

10. Disconnect NXTCamView from your NXTCam.

11. If you don’t feel comfortable enough to start coding, then you need to read this guide at http://www.juanantonio.info/p_articles/archive/2007/NXTCamView.pdf its helpful.

12. There are many demos that are helpful when using the nxt camera:

· how to configure the COMPort to connect to the NXTCam http://nxtcamview.sourceforge.net/DemoScreenCam_SettingCOMPort.htm

· how to connect to the NXTCam, capture an image, and select colors to track http://nxtcamview.sourceforge.net/DemoScreenCam_CaptureAndSelect.htm

· sample run of a capture, color selection and tracking http://nxtcamview.sourceforge.net/DemoScreenCam_SampleCapture.htm

13. Also, you can visit http://nxtcamview.wiki.sourceforge.net/. This is an open public wiki you can visit to help with issues with the nxtcam.

Now you're ready to start coding!


Time: 6 hrs.

Thursday, July 2, 2009

Programming NXT camera in C

-

As I mentioned in previously you need to download a new library called nxtcamlib in order for RobotC to be able to control the camera. This library has many functions that are useful when trying to write a program for the camera in C. “nxtcamlib.c” library was created by Gordon Wyeth to simplify the use of nxtcam and provide an easy to use interface. The two key functions are:

void init_camera (tSensors camera) - Initialises the camera so it is ready to find blobs. Assumes that the color map has been set up using nxtcamview or similar.

void get_blobs(tSensors camera, int &nblobs, int_array &color, int_array &left, int_array &top, int_array &right, int_array &bottom) - Fills the variables nblobs and bounding box coordinate arrays with current values from the camera. nblobs is loaded with the number of visible blobs. The arrays carry the color, leftmost pixel, topmost pixel, rightmost pixel, and bottommost pixel in each blob. Only the first nblobs in each array are valid. Old data will persist in the rest.

The functions camera_cmd() and camera_flush() might also be useful, but were more intended to make writing the above functions easier.

After downloading the library you need to add it to your RobotC directory in the include file. The path for mine is “C:\Program Files\Robotics Academy\ROBOTC for Mindstorms\Includes”. In case you cannot find it on the internet all you have to do is create a .c file and past this in it:

#define MAX_BLOBS 8

// I2C Constants associated with camera

const int I2C_addr = 0x02;

const int cmd_reg = 0x41;

const int count_reg = 0x42;

const int data_reg = 0x43;

// Data structure for storing blob information

typedef int int_array[MAX_BLOBS];

// void camera_flush() - flushes any pending reply bytes from camera.

void camera_flush(tSensors camera)

{

int n;

byte dump[8];

while (nI2CStatus[camera] == STAT_COMM_PENDING) ;

n = nI2CBytesReady[camera];

while (n > 0) {

while (nI2CStatus[camera] == STAT_COMM_PENDING);

readI2CReply(camera, dump[0], n);

while (nI2CStatus[camera] == STAT_COMM_PENDING);

n = nI2CBytesReady[camera];

}

}

// void camera_cmd(byte cmd) - sends cmd to the camera over I2C.

void camera_cmd(tSensors camera, byte cmd) {

const byte msg[] = {3, I2C_addr, cmd_reg, cmd};

while (nI2CStatus[camera] == STAT_COMM_PENDING);

sendI2CMsg(camera, msg[0], 0);

camera_flush(camera);

}

// void init_camera() - Initialises camera ready to find blobs.

void init_camera(tSensors camera)

{

SensorType[camera] = sensorI2CCustomFast;

camera_cmd(camera, 'A'); // Sort by size

camera_cmd(camera,'E'); // Start finding

}

// void get_blobs() - loads the current blobs into the global data structure over I2C

void get_blobs(tSensors camera, int &nblobs, int_array &color, int_array &left, int_array &top, int_array &right, int_array &bottom)

{

byte msg[3];

byte reply[5];

int i;

camera_flush(camera);

// Request number of blobs from the count register

msg[0] = 2;

msg[1] = I2C_addr;

msg[2] = count_reg;

while (nI2CStatus[camera] == STAT_COMM_PENDING);

sendI2CMsg(camera, msg[0], 1);

// Get the reply and put into nblobs global

while (nI2CStatus[camera] == STAT_COMM_PENDING);

while (nI2CBytesReady[camera] != 1);

readI2CReply(camera, reply[0], 1);

nblobs = reply[0];

camera_flush(camera);

// Get nblobs of blob data from the camera

for (i = 0; i <>

// Request blob data

msg[0] = 2;

msg[1] = I2C_addr;

msg[2] = data_reg + i * 5;

while (nI2CStatus[camera] == STAT_COMM_PENDING);

sendI2CMsg(camera, msg[0], 5);

// Get blob data reply

while (nI2CStatus[camera] == STAT_COMM_PENDING);

while (nI2CBytesReady[camera] != 5);

readI2CReply(camera, reply[0], 5);

// Put data into global variables.

// Casting to unsigned int throws a warning BUT casting a byte to int is quite

// different to casting to unsigned int, as the former performs sign extension

// (copies first bit of byte to higher bits to make it negative) and latter

// does not. Not sure what Robot C is doing now, but leave it for when they

// fix it. :)

color[i] = ((unsigned int)reply[0]) & 0x00FF;

left[i] = ((unsigned int)reply[1]) & 0x00FF;

top[i] = ((unsigned int)reply[2]) & 0x00FF;

right[i] = ((unsigned int)reply[3]) & 0x00FF;

bottom[i] = ((unsigned int)reply[4]) & 0x00FF;

camera_flush(camera);

}

}

Time: 7.5 hrs.

Wednesday, July 1, 2009

Install the NXTCam-v2

-

Downloads

· User guide, the documentation is not that great so you might not get a lot of help

· http://www.totalrobots.com/pdf/mindsensors/NXTCam-v2-User-Guide.pdf

· http://www.robotshop.ca/content/PDF/NXTCam-v2-User-Guide.pdf

· http://www.mindsensors.com/index.php?module=documents&JAS_Document_op=downloadFile&JAS_File_id=480

· USB drivers, here is installation instructions of USB Drivers

· Download USB Driver ZIP file to your disk from:

· http://www.mindsensors.com/index.php?module=documents&JAS_DocumentManager_op=viewDocument&JAS_Document_id=44

· http://www.totalrobots.com/pdf/mindsensors/nxtcamV1p1-windows.zip

· Unzip in a folder, say c:\nxtcam_v1p1

· Connect NXTCam to USB Port.

· The New Hardware Wizard will pop up, in this wizard, select:
Install from a list or specific location.

· Next, uncheck search removable media and check include this location to search.

· Depending on configuration of your NXTCam firmware you may have to use subfolder 'nxtcam' or 'ftdichip'.

· Specify path with 'nxtcam' subfolder. e.g. c:\nxtcam_v1p1\nxtcam

· In the next step, a dialog may inform you that the driver has not passed the Windows Logo test for compatibility. Select 'Continue Anyway'.

· Files Needed Dialog box:
In the next step, if a dialog box appears asking for path of sys files, provide the path you gave in prior step followed by \i386. (or directory for your processor) e.g. c:\nxtcam_v1p1\nxtcam\i386

· When the New Hardware Wizard pops up again, follow the same steps.

NXTCam is ready to use upon completion of these steps.

· RobotC library, you need to download nxtcam library which gives RobotC the ability to control the Cam-v2. It comes with some simple demos, you can get it at:

· http://www.mindsensors.com/index.php?module=documents&JAS_Document_op=downloadFile&JAS_File_id=181

· http://www.totalrobots.com/pdf/mindsensors/nxtcamlib.zip

Time: 7 hrs.