Page 2 of 2
Re: NXC Multitasking Tutorial
Posted: 04 Oct 2010, 20:25
by m-goldberg
mattallen37 wrote:Could you please provide an example program that shows what you mean? I would like to see the it in action, as well as the proper usage of the commands.
I'm not sure what you are asking for here because I think I already have provided such an example. The program that my tutorial discusses is intended to demonstrate how use a mutex. Since the full NXC source is included, you can compile and run it. You can also experiment with modifications and see what effect they have. I will be happy to answer any further questions that arise from your experiments.
Re: NXC Multitasking Tutorial
Posted: 04 Oct 2010, 20:33
by mattallen37
I know, but that program had a TON of extra "stuff", and I want a working code that is much smaller, but big enough to demonstrate. Often it helps me if I can see a command used in several ways (in different programs), so I better understand it, and am able to know more of what it can do.
Re: NXC Multitasking Tutorial
Posted: 04 Oct 2010, 20:38
by HaWe
just erase all the LCD-like 7-segment display code and substitute it by a simple NumOut procedure. maybe that'll help already.
Re: NXC Multitasking Tutorial
Posted: 04 Oct 2010, 20:41
by mattallen37
Well, I did try modifying the code (not exactly how you said though), and I know what the values returned (displayed) are, and all that, but I want another example that uses a mutex. It would be nice if it also had nothing to do with the button interface.
Re: NXC Multitasking Tutorial
Posted: 04 Oct 2010, 22:18
by afanofosc
Here's an example from
NXT Power Programming:
Code: Select all
#define BUMPER_PORT S1
#define BUMPER SENSOR_1
#define BAT_PORT S4
#define BAT_THRESHOLD 30
mutex motorMutex;
task Move() {
while(true) {
Acquire(motorMutex);
OnFwdSync(OUT_AC, 75, 0);
Release(motorMutex);
Wait(500);
}
}
task WatchUltra() {
while (true) {
if (SensorUS(BAT_PORT) < BAT_THRESHOLD) {
// too close so back up and turn
Acquire(motorMutex);
PlayTone(440, 500);
// backup and then spin
OnRevSync(OUT_AC, 40+Random(60), 0);
Wait(500+Random(500));
OnFwdSync(OUT_AC, 40+Random(60), 100);
Wait(500+Random(1000));
Release(motorMutex);
}
}
}
task WatchBumper() {
while (true) {
if (BUMPER) {
// sensor is pressed so back up and turn
Acquire(motorMutex);
PlayTone(880, 500);
OnRev(OUT_A, 40+Random(60));
OnRev(OUT_C, 60+Random(40));
Wait(500+Random(1000));
Release(motorMutex);
}
}
}
task main() {
SetSensorTouch(BUMPER_PORT);
SetSensorLowspeed(BAT_PORT);
Precedes(WatchBumper, WatchUltra, Move);
}
John Hansen
Re: NXC Multitasking Tutorial
Posted: 04 Oct 2010, 22:33
by mattallen37
Oh, I see now why it is referred to as a "token". That makes sense now. Thanks for posting the code.