Archive for the ‘nxtcam’ tag
Tracking a Colored Ball with the NXTCam
Vision systems are one of the more useful, albeit trickier, sensors that can be used in a robotics system. They allow a microcontroller to literally ‘see’ an object, its color, shape, and (in some cases) the material it is made from. They are used extensively almost anywhere an automated system needs to make a decision based on an object’s visual properties.
Fortunately, MindSensor’s NXTCam combined with Xander’s driver suite allows NXT users to quickly and easily program a vision system for their robots. ROBOTC forum member alain has recently created one of the basic NXTCam robots (a robot that will track a colored ball with relatively high accuracy) and was kind enough to share his programming journey on the ROBOTC forum and the video below.
If you’re interested in building your own color-tracking robot or have other, unique ideas for an NXT cam with ROBOTC, be sure to check out the Robotics Academy demo video for ideas on how the NXTCam can be used and the ROBOTC forum for coding help.
Colored Object Tracking using the VEX Cortex and NXTCam
No official vision system available for the VEX Cortex? No problem.
Thanks to some really cool work by Xander, I2C sensors for the NXT can now be used on the Cortex as well (with a little bit of effort). As we’ve covered in some of our previous posts, ROBOTC and the VEX Cortex unlock a ton of cool robotic applications – controlling pneumatics, controlling high torque and high speed motors through relays, outputting to the LCD screen, ect. Being able to use the repository of I2C sensors available for the NXT on the Cortex unlocks many, many more.
To demonstrate this, we’ve chosen the Mindsensors NXTCam. With the NXTCam, you’re able to connect it to a computer over USB and “teach” it to track up to 8 different colors. Once it’s hooked up to your robot, it returns data related to the colors it’s tracking – size, x and y positions, ect. This make it ideal for tracking a colored object, distinguishing between multiple different colors, and even advanced line tracking (more on that in a future post).
In this video, we’ve trained the NXTCam to track the color red. ROBOTC code uses the data from the camera to center the robot on the red ball and drive up to it until it’s within a certain distance.
ROBOTC with the Mindsensors NXTCam
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);
} |





