I have been tasked with compiling a program which will allow a robot to perform several functions at the same time. From the research I have done on google, Mr. Benedettelli's site and here I have come up with a program which I hope almost does what I want it to, I am however having a few bugs with it and dont really have anyone else to turn to. I hope someone here can help.
Below is program as I have written it so far, any advice, alterations and or comments would be appreciated. I would like to achieve the robot staying on a large white page (SOP) using its light level detector, avoiding obstacles using the ultrasonic detector (dodge) and stop when it "hears" a loud noise (noisy) via the mic sensor. All these programs need to run at the same time. My thanks for any input (providing it isnt - give up ).
Code: Select all
//Exp program for moving robot to stay on page and avoid obstacles
#define forwards(s,t) \
OnFwdReg(OUT_AC, s,OUT_REGMODE_SYNC);Wait(t);
#define backwards(s,t) \
OnRevReg(OUT_AC, s,OUT_REGMODE_SYNC);Wait(t);
#define turn_right(s,t) \
OnFwd(OUT_A, s);OnRev(OUT_C, s);Wait(t);
#define turn_left(s,t) \
OnRev(OUT_A, s);OnFwd(OUT_C, s);Wait(t);
#define turn_around(s,t) \
OnRev(OUT_C, s); Wait(t);
#define Lightlevel 30
#define Near 20 //cm
#define Threshold 70
#define Mic (IN_3)
mutex moveMutex;
task SOP()
{
while (true)
{
if (Sensor(IN_2) < Lightlevel);
{
Acquire(moveMutex);
Wait(100);
backwards (30,100);
turn_right (45, 100);
until(Sensor(IN_2) >= Lightlevel);
Release(moveMutex);
}
}
}
task Dodge()
{
while (true)
{
if(SensorUS(IN_4)> Near);
{
Acquire(moveMutex);
turn_around(60,1000);
Wait(300);
Release(moveMutex);
}
}
}
task Noisy()
{
forwards(30,20000);
{
while (true)
{
if(Mic > Threshold);
{
Acquire(moveMutex);
Wait(300);
until(Mic > Threshold);
Off(OUT_AC);
Release(moveMutex);
}
}
}
}
task main()
{
SetSensorLight(IN_2,SENSOR_LIGHT);
SetSensorLowspeed(IN_4);
SetSensorSound(IN_3);
Precedes(Noisy, Dodge, SOP);
}