Tasks on Adruino uno

I am building a quadcopter with the Adruino uno, and want it to self balance. Is there a way to have tasks running in the background of an arduino sketch? I know in a C/C++ hybrid language for programming FTC (FIRST Tech Challenge) robots called RobotC, you would make a header file with that task like so.

task TASKNAME() 

{

//Code here

}

And to start and stop the task you would use these commands inside the actual program

#include "HeaderName"

task main()
{

StartTask(TASKNAME);

StopTask(TASKNAME);

}

Is there a way to do that in arduino? If so, is possible to do with the Uno?

I know one thing There are MS timer and timer liberary. They say that you can start and stop task whenever you needed.

You can use interrupt also when interrupt enable you start task and interrupt disable stop task.

Is what your looking for??
http://playground.arduino.cc/Main/MsTimer2
http://playground.arduino.cc/Code/Timer
http://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/

There are a number of multitasking type systems available for the Arduino platform of different complexity, so it depends on your needs and memory constraints. Google "multitasking Arduino".

There are a number of multitasking type systems available for the Arduino platform of different complexity,

Keep in mind that the Arduino is not running an operating system with multi-threading capabilities and multiple processors to run those threads on.

The overhead of pretending to be a multi-threading system is going to cut into your performance, and is NOT going to enable two things to happen at once anyway.

True, they are usually API based and the same can be achieved through good overall design of a single threaded application.

Unless you have two or more processors, then the statement that you can only do one thing at a time is true of every multitasking system anyway.

Thanks guys for your fast replies. Greatly appreciated.

AMPS-N:
Is what your looking for??
Arduino Playground - HomePage
Arduino Playground - HomePage
gonium.net » Blog Archive » Handling external Interrupts with Arduino

I think the MS timer2 is the closest thing to what I want. From what I tested last night I could only run one "task" at a time, and I could only run for a specific amount of time. Is there a way to run multiple MS timers and make it run forever instead of a set time?

PaulS:

There are a number of multitasking type systems available for the Arduino platform of different complexity,

Keep in mind that the Arduino is not running an operating system with multi-threading capabilities and multiple processors to run those threads on.

The overhead of pretending to be a multi-threading system is going to cut into your performance, and is NOT going to enable two things to happen at once anyway.

Are there any arduino boards that can handle "multi-threading capabilities" ? I found this board from Intel http://www.mouser.com/ProductDetail/Intel/GALILEO/?qs=IN21uv1u6AnLUWebBkuQ2w%3D%3D Could this board support multiple task running?

Also, is there a way I can create a header file in arduino?

To be clear, any processor can run multi threaded/multi process applications if the software (OS usually) supports it. There is very little hardware support required other than a timer interrupt and sufficient memory to hold everything you need. Here is an example (from simple Google query): http://www.chrismoos.com/2012/12/05/avr-os-multitasking-on-arduino

For a long time Windows multi-tasking was just a co-operative scheme where each task gave up the CPU so others could use it. Those tasks that did not caused the system to 'freeze' up. At it's core this type of multitasking used a scheduler loop to call each task in turn when CPU was available.

If all you want to do is run every task in round-robin fashion forever then this 'scheduler' in loop() is sufficient:

void loop()
{
    task1();
    task2();
    task3();
   //etc
}

Just make sure that each of those tasks are self-sufficient and implemented with no side effects. A finite state machine style implementation is usually a good idea.

The loop shown by marco_c is the simplest way to go about things. But you could take a look at the Arduino Yun which does support multi processing and (of course) multi threading. It has a linux OS.

Mark

How would I define a task in Arduino? Is it like this?

void TASKNAME()
{

//code

}

Or is there another way?

Could you show me an example defining the task and then calling and stopping it?

Would the GALILEO board http://www.mouser.com/ProductDetail/Intel/GALILEO/?qs=IN21uv1u6AnLUWebBkuQ2w%3D%3D suport multi processing?

Sorry for all the questions, I'm new to Arduino the programming and circuit boards in general.

Thanks for the help :slight_smile:

Each task in what I showed above is just a standard function, nothing special. So you declare the function like you have shown.

You call it by calling the function and it stops when you exit the function. If you want a more sophistaicated mode of operation then you will need to invest in some knowledge about the libraries available for multitasking (like the link I posted earlier) or make your own (which is probably not feasible).

If this is your first Arduino project and you don't have much programming experience then I would suggest that you try some easier projects and build up experience. Quadcopters are quite complex ...

you will need to invest in some knowledge about the libraries available for multitasking

You can forget the so called multi tasking lib's just go with the FSM (see the playground) as you pointed out marco_c. Remmber to keep the paths through each state of your FSM short!.

Mark

A related thread Experience with ChibiOS RTOS? - Programming Questions - Arduino Forum

Thanks everyone for your input. I figured out that I'll just use if statements and functions instead of tasks. Its not as organized, but its the only way I could get it to work.

Thanks again :slight_smile: