Skip to content

The EMA Task Example

Daniele Alessandrelli edited this page Aug 23, 2013 · 11 revisions

The EMA task computes the Exponential Moving Average (EMA) of the data coming from the monitored input.

The processing function of the EMA task is the following:

from tres_pymite import *                                                        
                                                                                 
class state:                                                                     
  def __init__(self):                                                            
    self.m = 0.0                                                                 
                                                                                 
print "Exponential Moving Average:",                                             
s = getState(state)                                                              
x = getFloatInput()                                                                
s.m = 0.1 * x + 0.9 * s.m                                                        
print s.m                                                                        
setOutput(s.m)                                                                   
saveState(s) 

As you can see, the task uses a state variable for storing the last value of the EMA (needed to compute the new value). Refer to the T-Res Python API to learn more about state handling in T-Res.

You are now going to install the EMA task in node #2 setting the output of the previously-created Halve task as input.

node #3 (sensor) --> node #2 (Halve task) --> node #2 (EMA task)

You will not set any output destination. Instead, you will monitor the output of the EMA task using CoAP observe.

Create the task

To create the EMA task on node #2 you must issue a PUT request for its URI, i.e., coap://[aaaa::200:0:0:2]/tasks/ema.

The node should reply with 2.01 Created.

As usual, perform a GET request on the same URI to update the resource tree with the task resource just created and its sub-resources. You should end up with something like this.

EMA task created

Set the input of the task

To set the Halve task as input of the EMA task, send a PUT request to the EMA task's /is sub-resource (i.e., coap://[aaaa::200:0:0:2]/tasks/ema/is) with the following content:

<coap://[aaaa::200:0:0:2]/tasks/halve/lo>

Indeed, the /lo sub-resource of the Halve task contains the output produced by the Halve task.

Set the processing function of the task

To set the processing function of the EMA task, send a PUT request to the task's /pf sub-resource (i.e., coap://[aaaa::200:0:0:2]/tasks/ema/pf) setting the contiki-tres/examples/tres/bin/ema.pyc file as payload.

Remember to use block-wise transfer by enabling both the Debug options checkbox and the auto checkbox, and setting the Block1 (Req.) field to 0. Also check that the block size is equal to or less than 64 (under the Behavior drop-down menu).

Once you have successfully performed the PUT request, the node should reply with 2.04 Changed (Blockwise) (Upload finished).

If the response is different, please double check that:

  • The Payload field is set to File.
  • The Debug options checkbox is enabled.
  • The Block1 (Req.) field is set to 0.
  • The auto checkbox is enabled.
  • The Block size is set to 64 (or less).

After the successful upload of the processing function, change payload back to Text and disable Debug options.

Activate the task

To activate the halve task send a POST request to the task URI (i.e., coap://[aaaa::200:0:0:2]/tasks/ema).

The node should reply with the following payload: Task now running.

In the Cooja simulation you should see something similar to the following picture.

Cooja output for the Halve task

Please note that the Halve task is still sending its output to the actuator on node #4.

Observing the output of the task

Since no output destination for the EMA task was set, its output is not sent to anybody.

However, you can still monitor such output by observing the last output resource of the task. To do that using Copper, select the /lo sub-resource of the EMA task (i.e., coap://[aaaa::200:0:0:2]/tasks/ema/lo) and click Observe.

When you are done, delete the two tasks by sending DELETE requests to their URI. Then, try running the CED task example.