Community
cancel
Showing results for 
Search instead for 
Did you mean: 
FPaci1
Novice
1,923 Views

I'm interested is writing Arduino C code that uses Multitasking

I've had experience with SPLat embedded controllers that encourage multitasking in programme design. Please read this before you provide me with an answer. http://www.splatco.com/32_processes_for_the_price_of_one.htm SPLat Controls - Embedded microcontroller multitasking in electronic controls

You may want to read this too. http://www.splatco.com/rtos_1.htm SPLat Controls - Embedded microcontroller RTOS cooperative and preemptive multitasking in electronic controls

Along with structured programming, multitasking provides a very powerful method of programming. Each task is an independent piece of code that can run on itself. In the example below, there are four independent programmes. We can have global variables to share data. Whenever a task goes into a timing phase, the task is switched to the next. If there is no timing phase in a task, YIELDTASK is placed at the beginning of the loop for obvious reasons. It will loop once and switch to the next task. When it receives the focus again, loop once and yield again.

I would like to be able to do the vary same thing with Arduino C with either Arduino IDE or VS Visual Micro.

Any ideas?

For example

Launchtask CheckEmergencyPushButton

Launchtask ControlTankTemp

Launchtask MonitorPushbuttons

Launchtask LCDDisplayUpdate

Runtasksforever

CheckEmergencyPushbutton

LoopCode here

ControlTankTemp

LoopCode here

MonitorPushbuttons

LoopCode here

LCDDisplayUpdate

LoopCode here

8 Replies
Carlos_M_Intel
Employee
97 Views

Hi Fred,

The Arduino IDE uploads one sketch and the board can remember it, it also saves the sketch before the latest one; you can find the sketches on /sketch with the names sketch.elf and sketch.elf.old

When you say:

Launchtask CheckEmergencyPushButton

Launchtask ControlTankTemp

Launchtask MonitorPushbuttons

Launchtask LCDDisplayUpdate

Are you planning to run different .ino sketches at the same time? If this is the case you will have to upload them in different rounds so you can have all the sketches in one specific folder and then be able to run them at the same time. Is this what you want to do?

If you want to run different parts of code that are located inside the same .ino sketch I suggest you to use pthread, this should allow you to run different tasks at the same time.

You need to use # include

https://computing.llnl.gov/tutorials/pthreads/ POSIX Threads Programming

You can find the library in C:\Users\YourUser\AppData\Local\Arduino15\packages\Intel\tools\core2-32-poky-linux\1.6.2+1.0\core2-3...

Regards,

Charlie

FPaci1
Novice
97 Views

Thank you for your reply, CMata_Intel. Judging from your questions I assume you didn't read the links. If you did read the links, especially the first shorth one, you'd understand what I'm up to. Instead or writing a spaghetti string programme, I break it down to small bite size programmes and run then all in one file. It's almost like a sequencer with subroutines. The exception is in a sequencer, you will stay in the routine until the timing function times out. Or you have have to set an elaborate logic scheme to leave the subroutine and return every now and then.

With multitasking, the microprocessor is clocking so fast you get the impression that their running simultaneously. Much like when you have a number of programmes open and running on Windows 10 or Linux. Even a Mac. A takeoff in object programming. They are all self contained. If you need to change one, you don't have to worry about affecting the others. That's one terrific payoff right there.

Look at my example and look at pThread. Very straight forward. Very simple. You can disable one or more tasks by simply placing a comment character in front and the rest will work as happy as can be. By the way, SPLat has a feel like BASIC. By comparison, pThread if very complex to set up.

I was hoping that someone came up with an eloquent approach.

By the way, I know the gentleman who wrote the SPLat language and Multitasking. I'm trying to convince him to work a form of Multitasking out for Arduino C.

Oh, Charlie, I did read the second link. I wasn't very impressed. I guess I'm spoiled. Sorry.

VIvan7
New Contributor III
97 Views

Multitasking is easy in Linux, just check the fork function.

Let's go back to microcontrollers and Arduino.

Your example:

Launchtask CheckEmergencyPushButton

Launchtask ControlTankTemp

Launchtask MonitorPushbuttons

Launchtask LCDDisplayUpdate

Runtasksforever

ControlTankTemp and LCDDisplayUpdate can be run sequentially in the main loop.

You want to constantly poll the state of the push buttons.

It's better to connect the push buttons to GPIO pins and generate interrupts when the user pushes on them. Then handle these interrupts in Arduino.

FPaci1
Novice
97 Views

Hello Vincenze. About the example I used.... I simply threw something together. The idea was you take a long speghetti type programme and break it down into its verious components. It's like machine state mapping, with subroutines that can be treated as objects. Polling is like replacing a fuel injector with a carburetor. Interrupts only handle inputs and you are limited. Multitasking can handle anything. How would I use interrupts on LCD display, real time clock monitoring, timing functions, arithmetic computations and so on? Did you read my links?

The trouble, Vicenze, is I read everything there is and the closest I can get to Multitasking is pThreads. They're not eloquent. Have you writen Visual C or Basic? You design the interface and then code the objects. There the module you write code that loops, not event driven. In effect, you have multitasking. That's what I'm after.

VIvan7
New Contributor III
97 Views

Nobody wants multitasking on Arduino.

Arduino is intended for small, slow and cheap microcontrollers. Typically, they have less than 16kB of RAM.

When you connect an LED to a pin of a microcontroller and change the pin from LOW to HIGH, you want it to be done almost instantaneously. You don't want the microcontroller to wait 2 seconds while another thread reads the RTC and consumes all microcontroller power.

When one microcontroller can't process simultaneous tasks, you just add a second microcontroller that costs $2. The first microcontroller flashes the first LED, the second flashes the second LED.

Then you have the Edison that costs $50, but its Linux natively supports multiprocessing and multitasking. Then you can download a file in one process, read the RTC in the second, update the screen in the third. If you are not satisfied with Linux, install something else, but you'll have problems with drivers.

What can be easier than multiprocessing with fork() in Linux? Using pthreads has its benefits, but it's more complicated.

Example:

# include  

# include

int main(int argc, char **argv) 

{

 

printf("--beginning of program\n");

int counter = 0; 

pid_t pid = fork();

if (pid == 0) 

{

 

// child process

 

int i = 0;

 

for (; i < 5; ++i)

 

{

 

printf("child process: counter=%d\n", ++counter);

 

}

 

}

 

else if (pid > 0)

 

{

 

// parent process

 

int j = 0;

 

for (; j < 5; ++j)

 

{

 

printf("parent process: counter=%d\n", ++counter);

 

}

 

}

 

else

 

{

 

// fork failed

 

printf("fork() failed!\n");

 

return 1;

 

}

printf("--end of program--\n");

return 0; 

}
FPaci1
Novice
97 Views

You have something like 18 lines of code for two elements (assumed). My example used four lines of code for three element. And it's intuitive.

If I wasn't aware of this product, http://www.splatco.com/ec_p1.htm SPLat Controls - SPLat alternative to Arduino, I might agree with you. $21 gets you one.

I used to represent SPLat in USA and Canada. I'm retired and decided to play with Intel/Edison with the Arduino board and I programme the lot with Visual Studio 2015/Micro. It's lots of fun. But, I wish I could multitask as I can with EC1. I can also do this on their more sophisticated HMI430. But the thought that I can do this on EC1 without event lag as you identified above is amazing and a natural for simplified FSM programming.

About my silly programme: As one element of the programme waits for someone to push a button, it is running the flashing element and monitoring the water temperature control element all at the same time. And you don't notice a delay. The controller clocks so fast through the machine codes you simply don't notice it. As soon as you hit that push-button, Bingo. If I decide to skip with the Flash_The_Light element, I simply place a comment (; semi-colon) in front of Luanchtask Flash_The_Light. You can launch a task from within a task and kill it as well. You will note that the language is very much like BASIC but a bit more complex. You don't have libraries to include. I'm not trying to push the language, but I do wish to demonstrate how simple multitasking can be employed on a dirt cheap controller like EC1.

 

If you wish to see something cute, watch what I did with a automatic drink mixing machine. http://www.somabarkickstarter.com/ Somabar | Robotic Bartender for your Home This was the original proof of concept controller I put together for them. http://drinkdispenseremoteandroid.blogspot.com/ Drink Programme with Remote on Android You should be able to connect to this Blogger. There are three videos here. I need to control Bluetooth, Remote HMI on an Android using SimpleHMI, and the control itself. It would not have been possible without multitasking and I could have used that cheapie EC1. I used a CC18 OEM controller ($54) because of its robust I/Os. http://www.splatco.com/cc16p1.htm SPLat Controls - CC18 - Embedded PLC programmable logic controller

Vincenze, I understand where you're coming from but I'm asking you to think outside of the box. With the computing power available on the Intel/Edison processor, surely, we can come up with something akin to Multitask. Or is a small Australian company more savvy than Intel? And I've been told that certain Arduino micro-controllers can handle it. And whilst we're at it... wouldn't it be nice to have a version of SimpleHMI to use an Android phones for HMI applications via Bluetooth? http://www.splatco.com/simplehmi_p1.htm SPLat Controls - SimpleHMI - Free Android HMI app

My_Silly_Programme: ;This is an actual programme. It can be downloaded into a SPLat controller with their IDE. 27 lines of code including Multitasking.

Launchtask Pushbutton

Launchtask Flash_The_Light

Launchtask Keep_Water_Warm

RunTasksForever

WaitPushButton = 1 ; input

PushbuttonLight = 6 ;output

FlashingLight = 7 ;output

WaterThermoSwitch = 2 ;input

WaterHeater = 8 ;output

Pushbutton: ;This programme element will wait for someone to hit that button.

waiton WaitPushbutton

on PushbuttonLight

waitoff WaitPushbutton

off PushbuttonLight

goto Pushbutton

Flash_The_Light: ;This programme element will continuously run, flashing a light.

on FlashingLight

pause 1000 ;pause for 1 second

off FlashingLight

pause 1000

goto Flash_The_Light

Keep_Water_Warm: ;This will wait for heat demand from a water bath.

waiton WaterThermoSwitch

on WaterHeater

waitoff WaterThermoSwitch

off WaterHeater

goto Keep_Water_Warm

Carlos_M_Intel
Employee
97 Views

Hi Fred,

I would like to know if you have found another approach or if you started to try with pthread.

I've been searching but I've only found pthread as an option right now, did you find some other way?

Regards,

Charlie

FPaci1
Novice
97 Views

Hi Charlie,

 

Nope, I haven't. pthread is too heavy to deal with. It shouldn't have to be like that. Someone should be able to boil it down to something more manageable. Imagine having to go through similar steps just to print a line of text. By the way, you may be interested in a neat way to programme in finite state machine (FSM). https://www.google.com/search?q=finite+state+machine&oq=finite+state+machine&aqs=chrome..69i57j0l5.1... Definition FSM The man who came up with Multitasking and FSM programming is Mr. David Stonier-Gibson. If we had his Multitasking library function and this https://www.youtube.com/watch?v=uqwmMFCMx3k FSM Tabula for Arduino, we would take Intel/Edison and Arduino into a realm no hobby embedded controller has ever gone. Ideal for university courses and research. Someone should talk to the man. Have any ideas?

How about starting a design group here on pthread made easy?

Take Care,

Fred

Reply