Using Tasks in Autonomous

I understand the use of tasks in the main portion of the competition template. Now I’d like to have some tasks run simultaneously in autonomous mode (for example, driving while retracting a catapult). I have done multiple searches but can’t find a discussion of the use of tasks in autonomous anywhere. So here are some specific questions:

  1. Can tasks even be used in autonomous mode?
  2. If used in autonomous, does the task need a “while(1==1)” loop?
  3. If a task does not have a while(1==1) loop, will it simply end when it finishes executing the task’s code?
  4. Will the tasks autonomatically end at the end of autonomous or do all tasks called in autonomous need to be stoppped?

Any tips or tricks on using tasks in autonomous?? Words of caution…?

  1. Yes, they actually can. Someone else in another forum about a week or so ago told me it was possible.
  2. It depends… If you want something to end, then don’t have the “while(1==1)” loop. Otherwise, in autonomous, at a certain point you have to use the “StopTask(Task);” function/command.
  3. If it doesn’t have a “while(1==1)” loop, then it will end after it does the given program.
  4. Yes, ALL tasks will end end after autonomous except the pre-autonomous task (Or whatever it’s called).
    So basically on creating tasks, you don’t have those tasks inside of the “task autonomous” task. Usually it is above or below it. Whenever you want to call that task from autonomous, you will use the command “StartTask(Task)” in order to start it. And if you want to stop that task at any time while running, or if it’s a while loop, you will use the command “StopTask(Task)”
    I highly suggest commenting a ton because it will be fairly confusing if there were a beginner trying to read the program.

Technically there’s no exception here. Pre-autonomous is a function, not a task, so it should be over well before autonomous even starts. And, to clarify, RobotC (by default) literally runs a ‘stopAllTasks’ command after autonomous ends.

Oh I thought it was an exception but I was wrong throughout the months :stuck_out_tongue:
And oops I forgot about the “stopAllTasks” command too…

Here is an example of some code I made for a demo robot last year. It uses tasks and infinite while loops and is fairly simple to follow. Pretty short and self explanatory (I think)

While you can keep tasks running in the background via the global variable, I find it easier to have the kids start the tasks they want in each section individually. This helps them keep straight what is happening in each section of the code.

It also helps when going right into driver mode without hitting auton mode in practice.

And to help context switch, place wait1Msec statements in the while loop to let the Cortex do its job easier. You never know when you will be switched between task executions.