| Author |
Message |
|
10nisman94
Novice
Joined: Mon Oct 13, 2008 6:29 pm Posts: 75 Location: Florida, USA
|
 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 |
|
 |
|
Jeff McBride
Professor
Joined: Fri Sep 19, 2008 1:22 am Posts: 200
|
 Re: Background task.
_________________ Jeff McBride Benson Robotics Club
|
| Sun Jan 25, 2009 1:36 pm |
|
 |
|
Sunny1261
Novice
Joined: Thu Oct 09, 2008 7:58 pm Posts: 79
|
 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 |
|
 |
|
tfriez
Site Admin
Joined: Wed Jan 24, 2007 10:42 am Posts: 537
|
 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 |
|
 |
|
hobbseltoff
Rookie
Joined: Thu Jan 08, 2009 7:51 pm Posts: 25
|
 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 |
|
 |
|
Sunny1261
Novice
Joined: Thu Oct 09, 2008 7:58 pm Posts: 79
|
 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 |
|
 |
|
hobbseltoff
Rookie
Joined: Thu Jan 08, 2009 7:51 pm Posts: 25
|
 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 |
|
 |
|
Jeff McBride
Professor
Joined: Fri Sep 19, 2008 1:22 am Posts: 200
|
 Re: Background task.
Yes. Make sure that each of your loops have a wait at the bottom like so: 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: 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 |
|
 |
|
10nisman94
Novice
Joined: Mon Oct 13, 2008 6:29 pm Posts: 75 Location: Florida, USA
|
 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 |
|
 |
|
10nisman94
Novice
Joined: Mon Oct 13, 2008 6:29 pm Posts: 75 Location: Florida, USA
|
 Re: Background task.
i get these errors 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 |
|
 |
|
10nisman94
Novice
Joined: Mon Oct 13, 2008 6:29 pm Posts: 75 Location: Florida, USA
|
 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 |
|
|