View unanswered posts | View active topics It is currently Fri Aug 01, 2014 3:47 am






Reply to topic  [ 11 posts ] 
Background task. 
Author Message
Novice

Joined: Mon Oct 13, 2008 6:29 pm
Posts: 75
Location: Florida, USA
Post Background task.
how would one create a function that runs "in the background" continuously while other parts of the code excecute, such as to control power steering?

_________________
PHUHS Robotics
Team 516 FTW

Daytona Robofest Winning Alliance Captain
Daytona Robofest Inspire Award


Sun Jan 25, 2009 1:11 pm
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: Background task.
Code:
task Drive()
{
    // code to drive the robot around.
}

task main()
{
    // initialization code, etc would go here

    StartTask(Drive);  // Background task to drive the robot around.

    // Other stuff you want to do goes here
}

_________________
Jeff McBride
Benson Robotics Club


Sun Jan 25, 2009 1:36 pm
Profile
Novice

Joined: Thu Oct 09, 2008 7:58 pm
Posts: 79
Post Re: Background task.
So, essentially, we could do the drive task, and then it would run the same as if you were doing it in the while loop?

I know that in the short term, it doesn't matter, but in the long run, which way would be more efficient?


Sun Jan 25, 2009 9:17 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 601
Post Re: Background task.
You will need to make sure that you have a loop inside of your task, or else the code will execute once and then the task will end.

The other thing to remember is that you have to "free up" CPU time. Since the NXT doesn't actually have multiple processors, it shares the processor time. So if you start a new task and have it continuously loop, the rest of your code might not execute because that task will be using all of the processor time.

You can get around that issue by using wait1Msec commands throughout your new task (like at the end of a loop, have it wait 10ms before looping again)... when you use a wait1Msec command, it will free that task's CPU time to other tasks. Look at the joystick driver source code for an example and how the wait statements are used.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Mon Jan 26, 2009 10:45 am
Profile
Rookie

Joined: Thu Jan 08, 2009 7:51 pm
Posts: 25
Post Re: Background task.
Our robot has about 8 threads running while loops at one time and the NXT handles it just fine.

_________________
It must be user error.


Mon Jan 26, 2009 1:49 pm
Profile WWW
Novice

Joined: Thu Oct 09, 2008 7:58 pm
Posts: 79
Post Re: Background task.
hm...

So if i were to do something like

task drive()
{
while(true)
{
//Drive
}
}

task arm()
{

while (true)
{
// arm
}
}

task main()
{

StartTask(drive);
StartTask(arm);
}


The code would run just the same? Assuming we have all of the FMS stuff and junk.


Mon Jan 26, 2009 5:29 pm
Profile
Rookie

Joined: Thu Jan 08, 2009 7:51 pm
Posts: 25
Post Re: Background task.
It's probably not the best way to do it but we haven't had any problems using that way so far.

_________________
It must be user error.


Mon Jan 26, 2009 7:01 pm
Profile WWW
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: Background task.
Yes. Make sure that each of your loops have a wait at the bottom like so:

Code:
while (true)
   // my code goes here

   wait10Msec(1);
}


That will ensure that all tasks get as much CPU as they want.
Note: Since both of your tasks rely on the joystick values I suggest reading them in the main task like so:

Code:
task main()
{
    long count;

    WaitForStart();

    StartTask(Drive);
    StartTask(Arm);

    count = 0;   // Current number of bluetooth messages we've received.

    while(true)
    {
        if (count != nMessageCount)   // Check against the number received by the driver.
        {
            GetJoystickSettings(joystick);    // New message available, get it.
            count = nMessageCount;
        }

        wait10Msec(1);
    }
}


That means you will not need to poll the joysticks in your Drive() and Arm() tasks. You can simply refer to the joystick structure directly and know that it always has the most recent values.

_________________
Jeff McBride
Benson Robotics Club


Mon Jan 26, 2009 8:57 pm
Profile
Novice

Joined: Mon Oct 13, 2008 6:29 pm
Posts: 75
Location: Florida, USA
Post Re: Background task.
Thanks !

_________________
PHUHS Robotics
Team 516 FTW

Daytona Robofest Winning Alliance Captain
Daytona Robofest Inspire Award


Tue Jan 27, 2009 4:49 pm
Profile
Novice

Joined: Mon Oct 13, 2008 6:29 pm
Posts: 75
Location: Florida, USA
Post Re: Background task.
i get these errors

Code:
**Error**:Functions must be defined at main scope level
**Error**:Task 'main' is not defined at global scope level
**Error**:Tasks must be defined at main scope level

this only happens for one function

_________________
PHUHS Robotics
Team 516 FTW

Daytona Robofest Winning Alliance Captain
Daytona Robofest Inspire Award


Tue Jan 27, 2009 6:00 pm
Profile
Novice

Joined: Mon Oct 13, 2008 6:29 pm
Posts: 75
Location: Florida, USA
Post Re: Background task.
never mind i fixed it, if you put one task inside task main you must only have tasks, or i did something wrong the first time, so i have used sunny's style of coding.

_________________
PHUHS Robotics
Team 516 FTW

Daytona Robofest Winning Alliance Captain
Daytona Robofest Inspire Award


Tue Jan 27, 2009 6:06 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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.