Tuesday, July 21, 2009

Two NXT Cameras


I was thinking of way to decrease the chances of the NXT losing the leader and I decided to use two NXT cameras. Doing this provides the NXT with better tracking abilities and decreases the chances of losing the object when tracking. Using both NXT cameras was a great idea because it improved the NXT’s tracking ability showing a notable difference. More modifications need to be made to the source code to allow better communication between the cameras, and also provide more precise data to the motors.

Here the old source code:

#pragma config(Motor, motorA, r_motor, tmotorNormal, PIDControl, )

#pragma config(Motor, motorC, l_motor, tmotorNormal, PIDControl, )

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "nxtcamlib.c"

/************************************************************************************/

// When a single blob is found in the image the robot will try to centre the blob by moving

// the motors. When more the one blob is found the robot halts and displays a message.

// Tracked image resolution of 88 x 144 pixels at 30 frames/second

// Perform full-resolution (176 x 144) pixels.

/************************************************************************************/

task main () {

int nblobs; // Number of blobs

int_array bc, bl, bt, br, bb;

int x_centre, x_error;

int y_centre, y_error;

bool erased = false;

// Initialise the camera

init_camera(S1);

while (true) {

// Get the blobs from the camera into the array

get_blobs(S1, nblobs, bc, bl, bt, br, bb);

if (nblobs == 1) {

if (!erased) {

nxtDisplayTextLine(0,"Tracking ...");

erased = true;

}

// Find the centre of the blob using double resolution of camera

x_centre = bl[0] + br[0];

y_centre = bt[0] + bb[0];

// Compute the error from the desired position of the blob (using double resolution)

x_error = 176 - x_centre;

y_error = 144 - y_centre;

// Drive the motors proportional to the error

motor[l_motor] = (y_error - x_error) / 3;

motor[r_motor] = (y_error + x_error) / 3;

}

else {

motor[l_motor] = 0;

motor[r_motor] = 0;

nxtDisplayTextLine(0,"Found %d blobs.",nblobs);

erased = false;

} } }

Here is the new source code:

#pragma config(Motor, motorA, r_motor, tmotorNormal, PIDControl, )

#pragma config(Motor, motorC, l_motor, tmotorNormal, PIDControl, )

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "nxtcamlib.c"

/***********************************************************************************/

// When a single blob is found in the image the robot will try to centre the blob by moving

// the motors. When more the one blob is found the robot halts and displays a message.

// Tracked image resolution of 88 x 144 pixels at 30 frames/second

// Perform full-resolution (176 x 144) pixels.

/***********************************************************************************/

task main () {

int nblobs, nblobst; // Number of blobs

int_array bc, bl, bt, br, bb;

int_array bct, blt, btt, brt, bbt;

int x_centre, x_error;

int y_centre, y_error;

int x_centret, x_errort;

int y_centret, y_errort;

bool erased = false;

// Initialise the camera

init_camera(S1);

init_camera(S4);

while (true) {

// Get the blobs from the camera into the array

get_blobs(S1, nblobs, bc, bl, bt, br, bb);

get_blobs(S4, nblobst, bct, blt, btt, brt, bbt);

if (nblobs == 1 || nblobst ==1) {

if (!erased) {

nxtDisplayTextLine(0,"Tracking ...");

erased = true;

}

// Find the centre of the blob using double resolution of camera

x_centre = bl[0] + br[0];

y_centre = bt[0] + bb[0];

x_centret = blt[0] + brt[0];

y_centret = btt[0] + bbt[0];

// Compute the error from the desired position of the blob (using double resolution)

x_error = 176 - x_centre;

y_error = 144 - y_centre;

x_errort = 176 - x_centret;

y_errort = 144 - y_centret;

if (nblobs != 1) {

x_error = x_errort;

y_error = y_errort; }

if (nblobst != 1) {

x_errort = x_error;

y_errort = y_error; }

x_error = (x_error + x_errort) / 2;

y_error = (y_error + x_errort) / 2;

// Drive the motors proportional to the error

motor[l_motor] = (y_error - x_error) / 3;

motor[r_motor] = (y_error + x_error) / 3;

}

else {

motor[l_motor] = 5;

motor[r_motor] = -5;

nxtDisplayTextLine(0,"Found %d blobs.",nblobs);

erased = false;

} } }

Time: 7 hrs.

No comments:

Post a Comment