SCARA Robot Term Project
cotask.Task Class Reference

Implements multitasking with scheduling and some performance logging. More...

Public Member Functions

def __init__ (self, run_fun, name="NoName", priority=0, period=None, profile=False, trace=False)
 Initialize a task object so it may be run by the scheduler. More...
 
bool schedule (self)
 
bool ready (self)
 
def reset_profile (self)
 ! This method resets the variables used for execution time profiling. More...
 
def get_trace (self)
 
def go (self)
 
def __repr__ (self)
 

Public Attributes

 name
 The name of the task, hopefully a short and descriptive string.
 
 priority
 The task's priority, an integer with higher numbers meaning higher priority.
 
 period
 The period, in milliseconds, between runs of the task's run() method. More...
 
 go_flag
 Flag which is set true when the task is ready to be run by the scheduler.
 

Detailed Description

Implements multitasking with scheduling and some performance logging.

This class implements behavior common to tasks in a cooperative multitasking system which runs in MicroPython. The ability to be scheduled on the basis of time or an external software trigger or interrupt is implemented, state transitions can be recorded, and run times can be profiled. The user's task code must be implemented in a generator which yields the state (and the CPU) after it has run for a short and bounded period of time.

Examples

@code
def task1_fun ():
''' This function switches states repeatedly for no reason '''
state = 0
while True:
if state == 0:
state = 1
elif state == 1:
state = 0
yield (state)
# In main routine, create this task and set it to run twice per second
task1 = cotask.Task (task1_fun, name = 'Task 1', priority = 1,
period = 500, profile = True, trace = True)
# Add the task to the list (so it will be run) and run scheduler
cotask.task_list.append (task1)
while True:
cotask.task_list.pri_sched ()
Implements multitasking with scheduling and some performance logging.
Definition: cotask.py:61

Constructor & Destructor Documentation

◆ __init__()

def cotask.Task.__init__ (   self,
  run_fun,
  name = "NoName",
  priority = 0,
  period = None,
  profile = False,
  trace = False 
)

Initialize a task object so it may be run by the scheduler.

   This method initializes a task object, saving copies of constructor
   parameters and preparing an empty dictionary for states.

   @param run_fun The function which implements the task's code. It must
          be a generator which yields the current state.
   @param name The name of the task, by default @c NoName. This should
          be overridden with a more descriptive name by the programmer.
   @param priority The priority of the task, a positive integer with
          higher numbers meaning higher priority (default 0)
   @param period The time in milliseconds between runs of the task if it's
          run by a timer or @c None if the task is not run by a timer.
          The time can be given in a @c float or @c int; it will be
          converted to microseconds for internal use by the scheduler.
   @param profile Set to @c True to enable run-time profiling
   @param trace Set to @c True to generate a list of transitions between
          states. @b Note: This slows things down and allocates memory.

Member Function Documentation

◆ reset_profile()

def cotask.Task.reset_profile (   self)

! This method resets the variables used for execution time profiling.

This method is also used by init() to create the variables.

Member Data Documentation

◆ period

cotask.Task.period

The period, in milliseconds, between runs of the task's run() method.

If the period is None, the run() method won't be run on a time basis but will instead be run by the scheduler as soon as feasible after code such as an interrupt handler calls the go() method.


The documentation for this class was generated from the following file: