Bluetooth Message Control
Posted: 18 Mar 2012, 19:24
So, I've been working on a simple project where one robot, using and ultrasonic sensor, completes a simple maze and records its encoder values of its right wheel. Every time it stops and turns, it sends to messages: 1 for how far it moved forward, and one for how much it turned, left or right. I'm having an issue though, with the receiving of a messages on my clone, which will run the maze blind. I use the debug stream to see what values my clone is receiving. However, as soon as one message is sent, it writes it on the debug stream an infinite amount of times. I'm thinking it should write it once, clear the message, and await for the next message. Any help please?
Here's the code:
MASTER:
CLONE:
The writing to the debug stream occurs in the clone's first if statement. The array is there because I was going to save all the values in an array and execute the items from the array later on, but I'm having separate problems with that, so I'm tackling my issues one at a time.
Thanks!
Here's the code:
MASTER:
Code: Select all
#pragma config(Sensor, S1, sonar, sensorSONAR)
#pragma config(Motor, motorA, right, tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor, motorB, left, tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor, motorC, look, tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*Maze communication
Goes throug a simple maze (i.e, no dead ends, just one route, start to finish, but with turns) and
sends the encoder amounts to simple clone, which navigates without sensors.*/
void angleTurn(string t)
{
float arcLength = 2.2 * 2 * PI * 0.25;
float degreesNeeded = 1.3 * 2 * PI;
degreesNeeded =arcLength * 360/degreesNeeded;
nMotorEncoder[right] = 0;
if(t == "right")
{
while(nMotorEncoder[right] < degreesNeeded)
{
motor[right] = 10;
motor[left] = -10;
}
sendMessageWithParm(1, 1, nMotorEncoder[right]);
wait1Msec(10);
motor[right] = 0;
motor[left] = 0;
wait1Msec(50);
}
else
{
while(nMotorEncoder[right] > -degreesNeeded)
{
motor[right] = -10;
motor[left] = 10;
}
sendMessageWithParm(2, 2, nMotorEncoder[right]);
wait1Msec(10);
motor[right] = 0;
motor[left] = 0;
wait1Msec(50);
}
}
void checkSides()
{
int x;
int y;
x = SensorValue[sonar];
if(x < 20)
{
motor[right] = 0;
motor[left] = 0;
y = nMotorEncoder[right];
sendMessageWithParm(3, 0,y);
wait1Msec(10);
nxtDisplayCenteredTextLine(4, "%4d", y);
wait1Msec(1000);
nMotorEncoder[look] = 0;
while(nMotorEncoder[look] < 90)
{
motor[look] = 8;
}
motor[look] = 0;
wait1Msec(50);
writeDebugStreamLine("checked forward");
int f = SensorValue[sonar];
nMotorEncoder[look] = 0;
while(nMotorEncoder[look] > -180)
{
motor[look] = -8;
nxtDisplayCenteredTextLine(3, "%4d", nMotorEncoder[look]);
}
motor[look] = 0;
wait1Msec(50);
writeDebugStreamLine("Checked Reverse");
int b = SensorValue[sonar];
nMotorEncoder[look] = 0;
while(nMotorEncoder[look] < 90)
{
motor[look] = 8;
}
motor[look] = 0;
wait1Msec(50);
if(f > b)
{
angleTurn("right");
}
else
{
angleTurn("left");
}
writeDebugStreamLine("%4d", SensorValue[sonar]);
nMotorEncoder[right] = 0;
wait1Msec(1000);
}
else
{
motor[right] = 30;
motor[left] = 30;
}
}
task main()
{
btConnect(3, "Wifi 2");
wait1Msec(2000);
nMotorEncoder[right] = 0;
while(true)
{
if(nNxtButtonPressed == 3)
{
nxtDisplayCenteredTextLine(4, "DONE");
motor[right] = 0;
motor[left] = 0;
sendMessageWithParm(1,3,0);
wait1Msec(10);
break;
}
checkSides();
}
}
Code: Select all
#pragma config(Sensor, S1, sonar, sensorSONAR)
#pragma config(Motor, motorA, right, tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor, motorB, left, tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor, motorC, look, tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*Maze communication
Goes throug a simple maze (i.e, no dead ends, just one route, start to finish, but with turns) and
sends the encoder amounts to simple clone, which navigates without sensors.*/
task main()
{
int messages[30];
int x = 0;
while(true)
{
if(bQueuedMsgAvailable())
{
writeDebugStreamLine("%4d", messageParm[1]);
ClearMessage();
wait1Msec(50);
}
}
writeDebugStreamLine("Done from master");
for(int i = 0; i < 30; i++)
{
if(messageParm[1]==0)
{
nMotorEncoder[right] = 0;
while(nMotorEncoder[right] < messageParm[2])
{
motor[right] = 10;
motor[left] = 10;
}
}
else if(messageParm[1]==1)
{
nMotorEncoder[right] = 0;
while(nMotorEncoder[right] < messageParm[2])
{
motor[right] = 10;
motor[left] = -10;
}
}
else
{
nMotorEncoder[right] = 0;
while(nMotorEncoder[right] > messageParm[2])
{
motor[right] = -10;
motor[left] = 10;
}
}
}
btDisconnect(3);
}
Thanks!