Sunday, July 5, 2009

Understanding C Code for the NXTcam

-

I put my hands on come code written for the NXT in C using the NXTCam so I decieded to read it and understand it before I start writing my own and it was very helpful. Also, if I read the library for the NXTCam and it was a bit hard to understand but it gave me a better understand of NXTCam programming background.

One of the sample codes was camtest.c”, which is a simple program that displays the blobs returned from the camera on the NXT display as text. Left and right buttons can be used to choose which blob you would like to look at. Another code was “cam_display.c”, which is a program that Displays the blobs returned from the camera on the NXT display. Note the scaling functions below reflect the actual scaling required - not the scaling from the documented values of camera coordinates. These program were wriiten by Gordon Wyeth.

Camtest.c

const tSensors cam = (tSensors) S1; //sensorI2CCustomFast

#include "nxtcamlib.c"

// Global

int cb; // Current blob index to display

// task button_handler() - increments blob index when right button is pressed, decrements

// when left button is pressed. Keeps values between 0 and 7.

task button_handler()

{

while (true) {

while (nNxtButtonPressed == -1){

;

}

if (nNxtButtonPressed == 2) {

if (cb == 0) {

cb = 7;

} else {

cb--;

}

} else if (nNxtButtonPressed == 1) {

if (cb == 7) {

cb = 0;

} else {

cb++;

}

}

while (nNxtButtonPressed != -1)

;

}

}

task main ()

{

int nblobs;

int_array bc;

int_array bl;

int_array bt;

int_array br;

int_array bb;

// Initialise the camera

init_camera(cam);

// Start with blob 0

cb = 0;

// Setup button handler

nNxtButtonTask = -2;

StartTask(button_handler);

while (true) {

// Get the current blob data from the camera

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

// Print the data on the screen

nxtDisplayTextLine(1, "Blob %d of %d", cb + 1, nblobs);

nxtDisplayTextLine(2, "Color: %d", bc[cb]);

nxtDisplayTextLine(3, "Left: %d", bl[cb]);

nxtDisplayTextLine(4, "Top: %d", bt[cb]);

nxtDisplayTextLine(5, "Right: %d", br[cb]);

nxtDisplayTextLine(6, "Bottom: %d", bb[cb]);

}

}

Cam_display.c

const tSensors cam = (tSensors) S1; //sensorI2CCustomStd

#include "nxtcamlib.c"

// int xscale(int x) - Scales x values from camera coordinates to screen coordinates.

int xscale(int x) {

return ((x - 12) * 99) / 176;

}

// int yscale(int y) - Scales y values from camera coordinates to screen coordinates.

int yscale(int y) {

return ((143 - y) * 63) / 143;

}

task main ()

{

int n; // Number of blobs

int i;

int_array bc, bl, bt, br, bb; // Intermediate values for scaled corners

int l, t, r, b; // Intermediate values for scaled corners

// Initialises the camera

init_camera(cam);

while (true) {

// Get the blobs into the arrays for display

get_blobs(cam, n, bc, bl, bt, br, bb);

// Clear the display

eraseDisplay();

for (i = 0; i <>

// Draw the scaled blobs

l = xscale(bl[i]);

t = yscale(bt[i]);

r = xscale(br[i]);

b = yscale(bb[i]);

nxtFillRect(l, t, r, b);

}

}

}

After reading these codes and understanding them, I have a way better understanding of what I am doing, and I feel ready to start writing and modifying my own code for the Leader-Follower algorithm.

Time: 6 hrs.

No comments:

Post a Comment