embedded system library for e-puck  1.0.1
A redesigned API library for the e-puck robot platform
 All Data Structures Files Functions Variables Enumerations Enumerator Modules Pages
Files
Trigger

achieving "do something when some event happens" More...

Files

file  el_trigger.h
 

Detailed Description

achieving "do something when some event happens"

Introduction

A trigger can launch a process when a specified event occurs in the system. For example, a process can be launched automatically when the proximity sensors has a set of samples ready for use.

Example Usage

Feedback Control Loop

Example 4 used the trigger mechanism to realize a control algorithm that uses camera image as input and hence executes every time when the camera is refreshed.

Here is an example of a control loop that takes the proximity sensors as input. The example assumes that the stepper motor and the infrared proximity sensors are enabled beforehand and the proximity sensors are working in EL_IR_PROXIMITY_PULSE mode.

Related global variables and function prototypes:

el_handle trg_control_step;
void Trigger_ControStep_Setup();
void Trigger_ControStep_Clear();
void Trigger_ControStep_Process(el_handle this_trigger);

Code to setup/clear the trigger:

void Trigger_ControStep_Setup(){
trg_control_step = el_create_trigger();
// specify the event of triggering
// a condition callback is not essential for all triggers
el_trigger_set_condition( trg_control_step, NULL );
// specify the process to launch when the event occurs
el_trigger_set_process( trg_control_step, Trigger_ControStep_Process );
}
void Trigger_ControStep_Clear(){
el_delete_trigger( trg_control_step );
}

Code of the trigger's process:

/*
* Note:
* For a trigger-launched process, the argument given to the entry function
* is the handle of the trigger.
*/
void Trigger_ControStep_Process(el_handle this_trigger){
el_int16 prox_value[8];
el_int16 mean;
// grab the reflection readings into an array
el_ir_proximity_get( EL_IR_PROXIMITY_SENSOR_ALL, EL_IR_REFLECTION, prox_value );
// assume that our algorithm needs the average of the two front sensors
mean = (prox_value[0] + prox_value[7])/2;
// the following algorithm let the robot keep a distance to anything in front
if( mean >= 900 ){
el_set_wheel_speed( -500, -500 );// move backwards
}else
if( mean >= 300 ){// with an implict condition: mean < 900
el_set_wheel_speed( 0, 0 );// don't move (distance is OK)
}else
if( mean >= 60 ){// with an implict condition: mean < 300
el_set_wheel_speed( 500, 500 );// move forwards
}else{// with an implict condition: mean < 60
el_set_wheel_speed( 0, 0 );// no motion (nothing in front)
}
/*
* After a trigger's process is launched, the trigger will be disabled.
* So re-enable the trigger after the process is done is necessary
*/
el_trigger_enable( this_trigger );
}