chipKIT® Development Platform

Inspired by Arduino™

Task Manager

Created Sat, 23 Mar 2013 00:56:53 +0000 by jmlynesjr


jmlynesjr

Sat, 23 Mar 2013 00:56:53 +0000

Is the Task Manager real? I haven't found any recent mentions.

Anyone have any examples of usage?

James


Jacob Christ

Tue, 26 Mar 2013 21:39:45 +0000

The main.cpp file calls a task scheduler but I've never looked into in detail.

Jacob


guymc

Tue, 26 Mar 2013 23:56:49 +0000

Documentation on the Task Manager can be found here:

http://chipkit.net/started/learn-basics/chipkit-compatible-libraries/

Cheers


jmlynesjr

Wed, 27 Mar 2013 01:25:03 +0000

Thanks. I had read the referenced documentation previously and was wondering if anyone had used it yet. I've not stumbled across any examples.

James


swoozle

Fri, 29 Mar 2013 03:23:15 +0000

Yup, I used it in a very basic fashion in a project and it worked fine.


jmlynesjr

Sat, 30 Mar 2013 17:49:51 +0000

Swoozle:

Mind posting some test code?

Thanks, James


Jacob Christ

Sat, 06 Apr 2013 14:27:40 +0000

Documentation on the Task Manager can be found here: http://chipkit.net/started/learn-basics/chipkit-compatible-libraries/ Cheers

Guy,

Do you know which version of MPIDE that this functionality was first realized?

Do you need to include any headers for functionality?

Jacob


Jacob Christ

Sat, 06 Apr 2013 19:53:21 +0000

Swoozle: Mind posting some test code?

I figured it out how to use it and posted a blink sketch on the overview page:

http://chipkit.net/started/learn-basics/chipkit-compatible-libraries/task-manager-overview/

Jacob


jmlynesjr

Sat, 06 Apr 2013 23:37:20 +0000

Thanks Jacob.

James


Jacob Christ

Sun, 07 Apr 2013 03:04:06 +0000

Here is a more advanced blink sketch.

/*
  Blink Task, self altering period (tested in MPIDE version 20120903)
  
  - Blink LED1 and LED2 with different (beating) timebases.
  - Utilizes the task variable to keep track of LED to blink
  - Task alters its period (slowing) until period is greater than
    one second at which point it resets to 200ms and prints a
    rollover message out the serial port.
  - Utlizing the chipKIT task manager rather than delay functions.
*/

/*
Copyright (c) 2013, Jacob Scott Christ (jacob@pontech.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: 

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies, 
either expressed or implied, of any project or company.
 */

#include "System_Defs.h"
int blink1_id;
int blink2_id;
int blink1_var = PIN_LED1;
int blink2_var = PIN_LED2;

void blink_task(int id, void * tptr) {
  int led_pin;
  unsigned long period;
  
  led_pin = *(int *)getTaskVar(id);
  digitalWrite(led_pin, !digitalRead(led_pin));   // Toggle pin state
  period = getTaskPeriod(id);
  if( period < 1000 )
    setTaskPeriod(id,period+5);
  else {
    setTaskPeriod(id,200);
    Serial.print("Task ");
    Serial.print(id, DEC);
    Serial.println(" rolled over.");
  }
}

void setup() {
  // initialize the digital pin as an output.
  pinMode(blink1_var, OUTPUT);
  pinMode(blink2_var, OUTPUT);
  blink1_id = createTask(blink_task, 200, TASK_ENABLE, &blink1_var);
  blink2_id = createTask(blink_task, 405, TASK_ENABLE, &blink2_var);
  Serial.begin(115200);
  Serial.println("It takes a few seconds for the task period to rollover and print out a message.");
}

void loop() {
}

Jacob Christ

Sun, 07 Apr 2013 22:31:01 +0000

I posted a video of this sketch running here:

https://www.youtube.com/watch?v=sr3A8zGqnGw

Jacob


rasmadrak

Wed, 10 Apr 2013 17:43:30 +0000

Just curious, Jacob -

What are they major differences of running with interrupts instead of the task manager? I figure interrupts would hijack whenever/wherever and the task manager would let each task finish, correct?

For my pinball machine I'm currently running the solenoids in an interrupt-driven task and the rest of the gameplay sequentially, in a semi-fixed rate. If I would run a couple of the tasks on the task manager instead, would they be running in the same order every time or could the order be swapped in case one of the tasks takes longer to execute than normally? And more importantly - what happens when a task misses its mark, is it skipped or just delayed? I think I remember something about it being delayed, but I'm not sure.

Thanks in advance! :)

Edit: I would test this myself, but my USB-port is broken and I'm waiting for a replacement part. :)


Jacob Christ

Thu, 25 Apr 2013 20:25:11 +0000

The task manager is non-preemptive multitasking (think Windows 3.1). What this essentially means is that there is some clever code that makes it look as if multitasking is taking place when really all that is going on is that a timer keeps track if its time to run the "task". You could do this yourself by reading the ticks and some if statements in your loop() to decide if enough time has gone by for your task to run again. The tasks will wait a the minimum amount of time requested but if the currently running task does not give up control the next task will be delayed or worse never run.

Interrupts will do just what they say, interrupt,. Whatever is currently running is stopped until the interrupt service routine is complete.

Jacob


rasmadrak

Sun, 19 May 2013 01:33:00 +0000

Awesome, thanks! :)


pappastech

Sun, 30 Jun 2013 21:28:09 +0000

I've been playing with the task manager and the only issue I'm having is that when I try to create a task that runs every 1ms, the results are not reliable. Setting it up to run every 10ms results in reliable code. Can anyone suggest what could be tweaked to make the 1ms task more reliable?

To be clear, if I have 2 tasks, a heartbeat @500ms and my data collection task @1ms; the sketch runs without issue. When I start adding other code to get the Ethernet subsystem running for logging purposes, that's when the data collection task stops working. The heart beat continues working just fine, so I haven't caused the PIC to lock up or anything like that. In fact, any code in the loop() function runs without issue in this case as well, just the data collection task stops running.

I've tried things like removing all the code from the loop() function which still results in the data collection task not running. This also replicates if I remove all the data collection code from that task and simply have it flash LED2 on the MX7cK.

I'm writing my code in MPIDE 0023-20120903 on a Windows 7 Pro machine and the target platform is the MX7cK.

Any help on how to troubleshoot what's going on would be most appreciated.

Tom


Jacob Christ

Sun, 07 Jul 2013 18:12:21 +0000

I've tried things like removing all the code from the loop() function which still results in the data collection task not running. This also replicates if I remove all the data collection code from that task and simply have it flash LED2 on the MX7cK.

When you say that is replicates with just flashing LED2 do you mean that it is or is not working?

Also, from what I understand from your e-mail is if you set the task to run every 10ms it will work in every case?

Jacob