NXC Multitasking Tutorial

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: NXC Multitasking Tutorial

Post 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.
Regards, Morton
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC Multitasking Tutorial

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC Multitasking Tutorial

Post 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. :)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC Multitasking Tutorial

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC Multitasking Tutorial

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC Multitasking Tutorial

Post by mattallen37 »

Oh, I see now why it is referred to as a "token". That makes sense now. Thanks for posting the code.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest