ROBOTC.net Blog  

ROBOTC News

ROBOTC with the Mindsensors NXTCam

4 comments

The Mindsensors NXTCam can be configured to track up to 8 colored objects.  It connects to an NXT sensor port, and reports the coordinates of each object to the NXT, enabling fast line tracking, color recognition, and object tracking. Watch this cool video showing ROBOTC and the NXTCam: http://www.education.rec.ri.cmu.edu/downloads/lego/resources/NXTCam/NXTCam.html

Sample code for Color Recognition:

#pragma config(Sensor, S1,     camera1,             sensorI2CCustomFast)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
/*
Color Recognition - ROBOTC for the NXT
 
Description: The NXT displays the name of the color returned by the Mindsensors NXTCam v2
 
NXT Configuration:
- Mindsensors NXTCam v2 plugged into NXT Sensor Port 1
 
Camera Configuration:
- Color Index 0: Red
- Color Index 1: Orange
- Color Index 2: Yellow
- Color Index 3: Green
- Color Index 4: Blue
- Color Index 5: Purple
- Color Index 6: Black
- Color Index 7: Brown
 
Notes:
- As of October 2008, NXTCam color index must be configured through
NXTCamView, found at: http://nxtcamview.sourceforge.net/
- More information can be found at: http://mindsensors.com/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=78
*/
 
//Include NXTCam Library
#include "RAnxtCamLib.c"
 
//Define global variables
string colorName;
int numBlobs;
int_array blobColor;
int_array blobX1;
int_array blobTop;
int_array blobX2;
int_array blobBottom;
 
void printColorName();
 
task main ()
{
  //Initialize the NXTCam
  enableTracking(camera1);
  wait1Msec(500);
  sortBySize(camera1);
  wait1Msec(500);
  useObjectTracking(camera1);
  wait1Msec(500);
 
  //Recognize Colors Forever
  while(true)
  {
    printColorName();
    wait1Msec(100);
  }
}
 
//void printColorName() - Writes the color returned from the camera to the NXT screen
void printColorName()
{
  nxtDisplayString(1, "The color is:");
  //Retrieves Camera Data
  getCameraData(camera1, numBlobs, blobColor, blobX1, blobTop, blobX2, blobBottom);
 
  if(numBlobs > 0)  //If at least 1 "blob" or color is seen...
  {
    switch(blobColor) //...check the blobColor index.
    {
    case 0:
      colorName = "RED     ";
      break;
    case 1:
      colorName = "ORANGE  ";
      break;
    case 2:
      colorName = "YELLOW  ";
      break;
    case 3:
      colorName = "GREEN   ";
      break;
    case 4:
      colorName = "BLUE    ";
      break;
    case 5:
      colorName = "PURPLE  ";
      break;
    case 6:
      colorName = "BLACK   ";
      break;
    case 7:
      colorName = "BROWN   ";
      break;
    default:
      colorName = "UNKNONWN";
      break;
    }
  }
  nxtDisplayString(3, colorName);
}

Written by Jesse Flot

November 24th, 2009 at 12:32 pm