Monday, July 20, 2009

A different way to use NXTCam.

A different way to use NXTCam.

This code below is a new way of using the NXT cam. Its different because it allows more control over the NXTCam, but after experimenting it, it didn’t show enough potential for me to research more about it, nor use it in my programming.


#define I2C_PORT S1
#define MOVE_TIME 300
#define TURN_TIME 75
#define BIG_TURN_TIME 250

int err;
byte send[] = {0x40, 0xfe};
byte recv[] = {1};

sub turn_right()
{
OnFwd(OUT_A, 60);
OnRev(OUT_C,55);
Wait(TURN_TIME);
Off(OUT_AC);
}

sub sendrecvi2c (byte data)
{
ClearScreen ();
send[0] = 0x40;
send[1] = data;
while (I2CCheckStatus(I2C_PORT) != 0);
err = I2CWrite(I2C_PORT, 1, send);
while (I2CCheckStatus(I2C_PORT) != 0);
TextOut (0, LCD_LINE4,"Clear");
while (I2CBytesReady(I2C_PORT) < 1);
I2CRead(I2C_PORT, 1, recv);
TextOut (0, LCD_LINE5,"Read");
NumOut(60, LCD_LINE5, recv[0]);
Wait(500);
}

sub turn_left()
{
OnFwd(OUT_C, 60);
OnRev(OUT_A,55);
Wait(TURN_TIME);
Off(OUT_AC);
}

sub turn_big_right()
{
OnFwd(OUT_A, 60);
OnRev(OUT_C,55);
Wait(BIG_TURN_TIME);
Off(OUT_AC);
}

sub turn_big_left()
{
OnFwd(OUT_C, 60);
OnRev(OUT_A,55);
Wait(BIG_TURN_TIME);
Off(OUT_AC);
}

sub straight()
{
OnFwd(OUT_AC, 60);
Wait(MOVE_TIME);
Off(OUT_AC);
}


task main()
{
SetSensorType(I2C_PORT, SENSOR_TYPE_LOWSPEED);
SetSensorMode(I2C_PORT, IN_MODE_RAW);
ResetSensor(I2C_PORT);
while(true)
{
sendrecvi2c(0x62);
Wait(100);
sendrecvi2c(0x62);
if(recv[0]>247)
{
TextOut (0, LCD_LINE2,"Error");
turn_big_right();
}
else if(recv[0]>90)
{
TextOut (0, LCD_LINE2,"Turn Right");
turn_right();
}
else if(recv[0]<75)
{
TextOut (0, LCD_LINE2,"Turn Left");
turn_left();
}
else
{
TextOut (0, LCD_LINE2,"Ball found and centered");
straight();
}
Wait(100);
}
ClearScreen ();
TextOut (0, LCD_LINE2,"Done");
Wait(10000);
}

Time: 4 hrs

No comments:

Post a Comment