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
Messages between tasks
-
- Posts: 224
- Joined: 30 Oct 2010, 04:10
- Location: 127.0.0.1
- Contact:
Re: Messages between tasks
If you mean using a mutex (mutually exclusive) variable to control using a shared resource(motors, display etc) then yes
-TimNXC 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); }
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/
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: Messages between tasks
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: Messages between tasks
Yes, as Matt said, you can do this:mattallen37 wrote:You want to have a variable that can be accessed by all tasks? Just use global variables, not local.
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;
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: Messages between tasks
Thanks for the quick replies! I did know about mutex, I used it in a simple line following program.
Thanks again
Thanks again
Who is online
Users browsing this forum: No registered users and 0 guests