View unanswered posts | View active topics It is currently Sat Oct 25, 2014 11:13 pm






Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Multitasking 
Author Message
Novice

Joined: Thu Oct 09, 2008 7:58 pm
Posts: 79
Post Multitasking
Hello,

Could someone please post some sample code for multitasking? Or different "tasks"?


Fri Oct 10, 2008 9:36 am
Profile
Rookie
User avatar

Joined: Wed Oct 08, 2008 2:09 pm
Posts: 5
Location: Brasil
Post Re: Multitasking
It is very simple:

task MyTask();

task main()
{
startTask(MyTask).
}

task MyTask()
{
...Put your code here...
}


At the right side of RobotC program there is the scope of procedures, including Tasks procedures: starting, stoping change prioryts and others.


Fri Oct 10, 2008 10:22 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post Re: Multitasking
Fun fact: you can have up to 8 tasks running at the same time on the NXT, thought I would throw that tidbit it there lol :-)

Scott B-)

_________________
Mmmm Legos B-)

My Robot Projects:
http://www.freewebs.com/robotprojects/


Fri Oct 10, 2008 1:13 pm
Profile WWW
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: Multitasking
There are quite a few sample programs that come with the ROBOTC distribution. Did you look for samples in there?

Hin: Search for files containing the keyword "StartTask".

Look at the file "NXT Songs.c" for a simple example. It shows how to use a secnd task to play a song while your main task does whatever else you want.


Fri Oct 10, 2008 4:17 pm
Profile
Novice

Joined: Thu Oct 09, 2008 7:58 pm
Posts: 79
Post Re: Multitasking
Ah, So it's almost like a function, but not really. :D

Sorry about the double post. :D, I wasn't sure where to post the question.


Fri Oct 10, 2008 7:55 pm
Profile
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia
Post Re: Multitasking
This is the simplest 'useful' task test I created to gain a basic understanding of tasks.
Plug a touch sensor into port 4

Code:
#pragma config(Sensor, S4,     senTouch,            sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                 Program to Demonstrate Use of Tasks on The NXT
//
////////////////////////////////////////////////////////////////////////////////////////////////////


task MainAction()
{
  while (true)
  {
    nxtDisplayTextLine(4,  "Task1 is running");
    wait1Msec(200);
    eraseDisplay();
    nxtDisplayTextLine(5,  "Task1 is running");
    wait1Msec(200);
    eraseDisplay();
  }
}

task CheckForSomething()
{
  while (true)
  {
    if (SensorValue[senTouch]==0)
    {
      // Do nothing
    }
    else
    {
      StopTask(MainAction);
      while (SensorValue[senTouch]==1)
      {
        nxtDisplayTextLine(0,  "Task 2 in control");
        wait1Msec(200);
        eraseDisplay();
        wait1Msec(200);
      }
      StartTask(MainAction);
    }
  }
}

task main()
{

  PlaySound(soundBeepBeep);
  StartTask(MainAction);
  StartTask(CheckForSomething);

}


Fri Oct 17, 2008 11:58 pm
Profile
Rookie

Joined: Wed Jan 14, 2009 11:42 am
Posts: 7
Post Re: Multitasking
thx for your explanation mate :bigthumb:

but the code jus works for me if i add a wait after starting the tasks in the main task. otherwise the program will stop after 1 sec, without doing anything. can somebody see my misthake?

_________________
o
L_/
OL
This is Schäuble.
Copy Schäuble into your signature to help him on his way to Überwachungsstaat.


Tue Jan 20, 2009 5:32 am
Profile
Rookie

Joined: Sun Nov 16, 2008 3:07 pm
Posts: 45
Post Re: Multitasking
Probably that once task main() exits, the whole program stops.
Since task main() doesn't loop in any way, it will start the other tasks, then exit, and that will tear down the others.

(there is a "Preference" which sounds somewhat related to this (View->Preferences->Compiler->Create 'Stop All Tasks' epilogue on 'main' exit), but I think the right answer is to not have your task main() exit until you are really done. )
e.g. you could put a wait as you discussed, or you could put a loop that does some other useful thing until the Exit button is pressed, stopping the program.

Also note that the other threads in the above examples look like they might have paths where they loop without ever waiting, and if that is the case, they might prevent other tasks from running for a while (depending on priority).

And also be aware of the HogCPU and ReleseCPU functions which should be used by BOTH threads around access to shared things.
(and note that some of the example programs neglect to do this for BOTH threads).


Tue Jan 20, 2009 12:06 pm
Profile
Rookie

Joined: Wed Jan 14, 2009 11:42 am
Posts: 7
Post Re: Multitasking
thanks a lot.

thats must have been the error. now everythin works fine for me ;) (waits at the end of tasks are not the best way to solve the problem)

_________________
o
L_/
OL
This is Schäuble.
Copy Schäuble into your signature to help him on his way to Überwachungsstaat.


Tue Jan 20, 2009 12:40 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Multitasking
starwarslegokid wrote:
Fun fact: you can have up to 8 tasks running at the same time on the NXT

it should be 10, not 8 according to D. Swan. (But unfortunataly both is far too poor e.g., for subsumption programs)

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Tue Jan 20, 2009 3:33 pm
Profile
Rookie

Joined: Sun Nov 16, 2008 3:07 pm
Posts: 45
Post Re: Multitasking
I've been asked for a practical example of using the HogCPU and ReleaseCPU functions, so I'm posting some snippets from Brush Robotics FTC Team 1001 gyro code that the kids wrote (with me making them worry about the multithreaded problems).

Caveat - I don't have any visibility into the RobotC internal workings, so questions like "should access to the display from different threads be serialized by Hog/Release CPU?" are beyond my level of knowledge. (my Windows based paranoia says yes).

Here are some functions and a task that keep track of the approximate heading by periodically reading the gyro (turn rate), which is more or less in degrees per second:
#define GYRO_ONE_TURN 13475.0
#define GYRO_ZERO_RATE 615.5
#define GYRO_INIT_SAMPLE_COUNT 50
#define GYRO_SAMPLE_PERIOD 26 // for old time's sake
float gyroHeading=0; // integrated raw Analog to Digital Counts
int rdegreeHeading;
float degreeHeading;
float gyroZeroBias = GYRO_ZERO_RATE;
task gyroUpdate()
{
while(true)
{
hogCPU();
gyroHeading+=(SensorValue[gyro]-gyroZeroBias);

// keep the integrated Heading value between
// the equivalent of 0 and 359 degrees.
if (gyroHeading >= GYRO_ONE_TURN)
{
gyroHeading -= GYRO_ONE_TURN;
}
else if (gyroHeading <0)
{
gyroHeading += GYRO_ONE_TURN;
}

degreeHeading = gyroHeading*(360.0/GYRO_ONE_TURN);
// ASSERT: degreeHeading is between 0 and 359
rdegreeHeading = degreeHeading;

// checking it here shouldn't be necessary, because we unwound above.
if(rdegreeHeading>360)
{
PlaySoundFile("Woops.rso");
rdegreeHeading -=360;
}
else if(rdegreeHeading <0)
{
PlaySoundFile("Woops.rso");
rdegreeHeading += 360;
}
releaseCPU();
wait1Msec(GYRO_SAMPLE_PERIOD);
}
}
void gyroSetDegrees(int degrees)
{
hogCPU();
gyroHeading = degrees*(GYRO_ONE_TURN/360.0);
releaseCPU();
}
int gyroGetHeading()
{
// return heading on 0 to 360 scale
int temp;
hogCPU();
temp = rdegreeHeading;
releaseCPU();
return temp;
}

Then in our main task, we read the heading like this:
task main()
{
StartTask(gyroUpdate);
gyroSetDegrees(180);


while (true)
{
// the stuff in this next section happens even when disabled
getJoystickSettings(joystick);
CurrentHeading = CompassGetCorrectedValue(); // Hog and Release down inside here prevent the gyro thread from updating it while we are reading it.

yada yada
}
}


btw., JoystickDriver.c is an example of what I think is a bad or incomplete use of Hog/ReleaseCPU.
In there, the joystick thread does a HogCPU while it accesses the bluetooth stuff and writes it to a local copy, but when the Client calls getJoystickSettings, that function does NOT do a HogCPU, so I think it is possible that in the middle of that memcpy, that Client thread could get swapped out, the joystick thread start to run, do a HogCPU, and change some of the values in the midst of them being memcpy'd, resulting in an inconsistent set of joystick data. Admittedly anal, and perhaps covered through some other mechanism in the depths of RobotC, but this sort of half locking is fairly common in industry and sometimes results in a difficult to find bug.

Best regards,
David Fort
Mentor, FTC Team 1001
Charles F. Brush High School
Lyndhurst, Ohio, USA


Wed Jan 21, 2009 10:16 pm
Profile
Rookie

Joined: Wed Jan 14, 2009 11:42 am
Posts: 7
Post Re: Multitasking
thank you for this very good example on using the hog- and releaseCPU commands.

i think, now i unersand the basics of multitasking :bigthumb:

_________________
o
L_/
OL
This is Schäuble.
Copy Schäuble into your signature to help him on his way to Überwachungsstaat.


Thu Jan 22, 2009 2:09 pm
Profile
Rookie

Joined: Wed Jan 14, 2009 11:42 am
Posts: 7
Post Re: Multitasking
hey there ;)

a new day, a new problem....

Code:
#pragma config(Sensor, S4,     us,                  sensorSONAR)

task drive();
task watch();
task avoid();

task avoid()
{
           StopTasl(watch);
   nSyncedTurnRatio = -100;
   nMotorEncoderTarget[motorA] = 90;
  motor[motorA] = 100;
}

task watch()
{
   while(1)
   {
    if(SensorValue[S4]<25)
    {
      StopTask(drive);
      motor[motorA]=0;
      StartTask(avoid);
     EndTimeSlice();
    }
   wait1Msec(100);
  }
}

task drive()
{
   while(1)
   {
      motor[motorA]=100;
      wait1Msec(1000);
  }
}

task main()
{
   nSyncedMotors = synchAC;
   nSyncedTurnRatio = 100;
   StartTask(watch);
  StartTask(drive);
}


in this programm, the robot should recognize obstacles and do a 90° turn afterwards. the robot does stop but then it says "error 0" on the screen. if i leave out the "StopTask(watch);" in the beginning of task avoid(), there is no error, but the task watch() ist still active and senses objekts while making the 90° turn. why does the StopTask command not work?

greetz Stryder

_________________
o
L_/
OL
This is Schäuble.
Copy Schäuble into your signature to help him on his way to Überwachungsstaat.


Sun Jan 25, 2009 4:59 pm
Profile
Rookie

Joined: Wed Jan 14, 2009 11:42 am
Posts: 7
Post Re: Multitasking
sry for the double post, in the beginning of task avoid, there has to be a "StopTask(watch)" instead of "StopTas1(watch)" :breakcomputer:

of course this does not solve the problem :lol:

_________________
o
L_/
OL
This is Schäuble.
Copy Schäuble into your signature to help him on his way to Überwachungsstaat.


Sun Jan 25, 2009 5:01 pm
Profile
Rookie

Joined: Mon Aug 20, 2007 4:01 am
Posts: 28
Post Re: Multitasking
Aren't you only supposed to control tasks from main()? Programming without testing, what about something like:

Code:
void avoid();
task watch();

int monitor;

task main()
{
   nSyncedMotors = synchAC;
   nSyncedTurnRatio = 100;
   StartTask(watch);

   while (1)  {
       if (monitor == 0) {
          motor[motorA]=100;
          wait1Msec(1000);
          }
      else {
         avoid();
         }
   }
}

task watch()
{
   while(1)
   {
    if(SensorValue[S4]<25) {
      monitor = 1;
    }
    else {
      monitor = 0;
      }
   wait1Msec(100);
  }
}

void avoid()
{
    monitor = 1;
    nSyncedTurnRatio = -100;
    nMotorEncoderTarget[motorA] = 90;
    motor[motorA] = 100;

    nSyncedTurnRatio = 100;
    monitor = 0;
    return;
}



I wouldn't expect the code above to work without some modifications.

Karl.


Tue Jan 27, 2009 7:58 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 16 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.