ROBOTC.net forums
http://robotc.net/forums/

Help with concurrent programming with NXC ...
http://robotc.net/forums/viewtopic.php?f=15&t=10445
Page 1 of 1

Author:  chandrurajaram [ Tue Feb 03, 2015 1:04 am ]
Post subject:  Help with concurrent programming with NXC ...

Hi:

I was trying to code the Lego Mindstorms NXT 8527 to work on a small
program that uses concurrent programming. Basically, all it does is to
toggle between move forward and stop on pressing the touch sensor.

Code as below:

mutex catchMe, mutexStop, mutexRun;
bool running;

//
// runRobot: Start robot if stopped
//

task runRobot()
{
while (true)
{
if (!running)
{
Acquire (mutexRun);
Acquire(catchMe);

OnFwd (OUT_AC, 75);
running = true;
until (SENSOR_1 == 1);

Release (catchMe);
Release (mutexStop);
}

}
}

//
// stopRobot: Stop robot if running
//

task stopRobot()
{
while (true)
{
if (running)
{
Acquire (mutexStop);
Acquire (catchMe);

Off(OUT_AC);
running = false;
until (SENSOR_1 == 1);

Release (catchMe);
Release (mutexRun);
}

}
}

// Main task

task main ()
{
running = false;

Precedes (runRobot, stopRobot);
SetSensor(IN_1, SENSOR_TOUCH);
}

However, this doesn't quite work as required and there is some problem
related to concurrency. Would be glad to hear from others if there is a
better solution.

Thanks

Chandru

Author:  BurningLights [ Tue Feb 03, 2015 10:46 pm ]
Post subject:  Re: Help with concurrent programming with NXC ...

An easier way to do this would be to combine the functionality of your two separate tasks into one big while (true) loop. So, it would be executing the one behavior, then on the touch sensor press go to the other one, and then on the next touch sensor press loop back around to the beginning and the first behavior.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/