View unanswered posts | View active topics It is currently Fri Dec 19, 2014 7:57 am






Reply to topic  [ 6 posts ] 
[nxc]Multitasking issue.. :( 
Author Message
Rookie

Joined: Sun Oct 06, 2013 12:59 pm
Posts: 2
Post [nxc]Multitasking issue.. :(
Hello everyone !

So I would like to do multitasking on my robot (lego mindstorm), and for that I'll need multitasking later, so I need to know how to do it, I know that you're against multitasking but later I'd like to do a real complex robot (not for now of course, I'm beginning, but later in this year I hope or at worst, next year), so that's why I need multitasking.

I don't understand how it really works... For example :
Code:
int folie = 0;
task testage1()
{
      while(1){
      folie = folie + 3;
     Wait(3000);
      }

}

task testage2()
{
   while(1){
   folie = folie + 30;
   Wait(1000);
   }

}

task main()
{
   folie++;
   while(true)
   {

   StartTask(testage1);
   StartTask(testage2);
   Wait(100);
   }

}


If I do that, the robot is spamming like hell (the variable reaches 4000 in less than 10 secondes...)

So I did that :
Code:
#include "nxc4rvw.h"


int folie = 0;
task testage1()
{
      while(1){
      folie = folie + 3;
     Wait(3000);
      }
}

task testage2()
{
   while(1){
   folie = folie + 30;
   Wait(1000);
   }

}

task main()
{
   folie++;

   StartTask(testage1);
   StartTask(testage2);

}


And this time it works only one time... What...? (the variable folie reach 34, so +1 + 30 + 3....) I don't even understand because when I read the code, it reads the startask but in the code, no reason to do it only one time, there is a while(1) lol. Really strange.

May someone help me ? Why is it doing that ? And how to do this simple multitask ?

Thanks for help in advance, good bye!

PS : By the way, it's off-topic but I'm using the robot virtual worlds and I need to program in nxc, so that's why I added this library .(nothing important anyways)


Mon Oct 07, 2013 3:05 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1362
Post Re: [nxc]Multitasking issue.. :(
You need to know two things:
1. You should not call StartTask inside of a loop. You only need to call StartTask once and the task will keep running until either the task terminates itself or you explicitly stop the task.
2. When task main runs to the end, it will terminate all the tasks it started.

Knowing these two things, does it answer your question on why it behaved the way it did?

BTW, you can perform very complex concurrent operations without using tasks. An example is in this thread.
viewtopic.php?f=52&t=3152


Mon Oct 07, 2013 4:15 am
Profile
Rookie

Joined: Sun Oct 06, 2013 12:59 pm
Posts: 2
Post Re: [nxc]Multitasking issue.. :(
Woh perfect ! Finally I've found an answer ! Thanks a lot ! Yes, understood.


And by the way, why everyone is so "upset" to see someone doing multitasking ? Why is it that bad ? I can understand that it's a bad idea to do that when it's not needed at all...but, sometimes it can't be avoided no ? :s
And furthermore, it's easier to read the code when there are different tasks than a huge one...


Mon Oct 07, 2013 5:24 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1362
Post Re: [nxc]Multitasking issue.. :(
No, doing multitasking is not bad. It is mostly required because you simply need the robot to do multiple things at the same time. Having said that, it is how you do multitasking the real debate. In the past, RobotC "task" has a lot of limitations. For example, I believe you cannot call the same function from multiple tasks etc. On top of that, if you are a beginner in programming, I would recommend avoiding "task". It has a lot of pitfalls to look out for. It's in the area of contention when multiple tasks accessing shared resources. Your code demonstrated that. Imagine multiple tasks are accessing the same variable 'folie'. RobotC tasks are implemented by "preemptive multi-tasking". It means the code in the task could be interrupted at any random point to switch to another task. If you have code that depends on the execution of the previous lines of code, you may be surprise to find a variable that you just read no longer contains the same value in the next line of code. That may cause problem. In order to avoid that, you need to put hogCPU()/releaseCPU() pair around the code that needs to be executed "atomically". This even trips a lot of professional programmers. The resulting bug is sometimes very difficult to pinpoint.


Mon Oct 07, 2013 12:03 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1362
Post Re: [nxc]Multitasking issue.. :(
BTW, the other type of multi-tasking instead of "preemptive multitasking" is "cooperative multitasking". In this type of multi-tasking, you control when to switch task. Therefore, it is more predictable especially in resource contention. The basic concept of cooperative multitasking is:
Code:
void task1()
{
    static int state = 0;
    switch (state)
    {
        case 0:
            if ((nMotorEncoder[leftMotor] + nMotorEncoder[rightEncoder])/2 >= 3000)
            {
                // reached target distance, stop the motors.
                motor[leftMotor] = 0;
                motor[rightMotor] = 0;
                // raise the arm.
                nMotorEncoder[armMotor] = 0;
                motor[armMotor] = 50;
                state++;
            }
            break;
        case 1:
            if (nMotorEncoder[armMotor] >= 1000)
            {
                // reached target, stop arm motor.
                motor[armMotor] = 0;
            }
            break;
            ...
    }
}

void task2()
{
    static int state = 0;
    switch (state)
    {
        ...
    }
}

task main()
{
    while (true)
    {
        task1();
        task2();
        ...
        EndTimeSlice();
    }
}

Your own main loop is the task scheduler. It calls each task in a round robin fashion. Cooperative means each task has to play nice and must return as soon as possible without blocking (i.e. No wait loops or wait1Msec() type of code in the task). Whenever a task cannot continue (blocked waiting for something to happen), the task must remember the state and just return to the main loop so other tasks can move on. When the task is called again, it should examine the last state it was in and then continue from there. In this case, all tasks can make progress at the same time.


Mon Oct 07, 2013 4:38 pm
Profile
Professor
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
Location: Olympia, WA
Post Re: [nxc]Multitasking issue.. :(
This is nitpicking, really, but it's generally better practice to write `while (true)` instead of `while (1)` :)
Also, is there a reason for the post title including "NXC"? Because that's a different programming language than RobotC...

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!


Mon Oct 07, 2013 8:46 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

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.