Messages between tasks

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
thomdaley
Posts: 11
Joined: 13 Jan 2011, 03:48

Messages between tasks

Post by thomdaley »

Hi,
Is it possible to send messages between tasks in NXC? I know about the StopAllTasks routine but was wondering if it can be done.

Thanks
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: Messages between tasks

Post by timpattinson »

If you mean using a mutex (mutually exclusive) variable to control using a shared resource(motors, display etc) then yes
NXC Guide wrote:In NXC the mutex type is a 32-bit value that is used to synchronize access to resources shared across multiple threads.

For this reason there is never a reason to declare a mutex variable inside a task or a function. It is designed for global variables that all tasks or functions can Acquire or Release in order to obtain exclusive access to a resource that other tasks or functions are also trying to use

Code: Select all

  mutex motorMutex;
  task t1()
  {
     while (true) {
       Acquire(motorMutex);
       // use the motor(s) protected by this mutex.
       Release(motorMutex);
       Wait(MS_500);
     }
  }
  task t2()
  {
     while (true) {
       Acquire(motorMutex);
       // use the motor(s) protected by this mutex.
       Release(motorMutex);
       Wait(MS_200);
     }
  }
  task main()
  {
    Precedes(t1, t2);
  }
-Tim
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Messages between tasks

Post by mattallen37 »

You want to have a variable that can be accessed by all tasks? Just use global variables, not local.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Messages between tasks

Post by muntoo »

mattallen37 wrote:You want to have a variable that can be accessed by all tasks? Just use global variables, not local.
Yes, as Matt said, you can do this:

Code: Select all

int g_iCharVal; // declare global variable (that will store the value of the current ASCII character position)


// Displays stuff!
task display()
{
    string cOut; // lol :) Technically, it should be szOut, but I like messing things up

    while(1)
    {
        // Convert g_iCharVal to string and store in cOut
        cOut = FlattenVar(g_iCharVal);

        // Display information on screen
        ClearScreen();
        NumOut(0, LCD_LINE1, g_iCharVal, 0);
        TextOut(0, LCD_LINE2, cOut, 0);
    }
}


// Changes the variable according to user input
task change()
{
    while(1)
    {
        if(ButtonPressed(BTNLEFT, 0))
            g_iCharVal--;

        if(ButtonPressed(BTNRIGHT, 0))
            g_iCharVal++;

        if(g_iCharVal < 0x20) // 0x20 == ' '
            g_iCharVal = 0x7F;

        if(g_iCharVal > 0x7F) // 0x7F == '!', IIRC
            g_iCharVal = 0x20;
    }
}


task main()
{
    g_iCharVal = 0x20;
    // start other 2 tasks
    Precedes(display, change);

//    doNotUse = 4; // ERROR!
}


int doNotUse = 3;
Make sure your global variables are at the top, though, if you want all your tasks to be able to access them.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
thomdaley
Posts: 11
Joined: 13 Jan 2011, 03:48

Re: Messages between tasks

Post by thomdaley »

Thanks for the quick replies! I did know about mutex, I used it in a simple line following program.

Thanks again
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests