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!