Motorcortex Core
version: 2.7.6
|
A building block of the Motorcortex components. More...
#include <ct_module.h>
Public Types | |
enum | ModuleStates { NOT_INITIALIZED = 0, PHASE_0 = 1, PHASE0 = 2, PHASE0_1 = 3, PHASE1 = 4, PHASE1_2 = 5, PHASE2 = 6, PHASE2_OP = 7, OP = 8, OP_PHASE2 = 9, PHASE2_NOT_INIT = 10 } |
enum | ModuleErrors { NO_ERROR = 0, NO_CALLBACK_DEFINED = 1, CALLBACK_FAILED = 2, WRONG_STATE = 3 } |
enum | ModuleEvents { EMPTY = -1, STOP = 0, START = 1, PAUSE = 2, ACK = 0xFF } |
Public Member Functions | |
Module () | |
Default constructor. | |
virtual | ~Module ()=default |
Default destructor. | |
Module (const Module &orig)=delete | |
Copy constructors are deleted. | |
Module & | operator= (const Module &)=delete |
Copy constructors are deleted. | |
void | create (const char *name, parameter_server::Parameter *parameter_server, uint64_t dt_micro_s=0) |
Executes 'Create' phase of initialization. More... | |
void | create (const char *name, parameter_server::Parameter *parameter_server, uint64_t dt_micro_s, parameter_server::UserGroup owner_group, uint32_t permissions=parameter_server::default_permissions) |
Extended interface with access permissions. More... | |
bool | initPhase1 () |
Executes initialization of the parameter tree phase. More... | |
bool | initPhase2 () |
Initialization, which require parameter tree to be complete and loaded. More... | |
bool | startOp () |
Final initialization before entering Operation mode. More... | |
bool | stopOp () |
Stops Operation mode, switches back to PHASE2. More... | |
bool | iterate (const TaskTime &task_time, UserTime *user_time) |
Iterates an execution cycle of the module. More... | |
void | setName (const char *name) |
Sets the name of the module. More... | |
const char * | getName () const |
Returns the name of the module. More... | |
void | setDtMicroSec (uint64_t micro_sec) |
Sets a cycle time of module and its submodules in microseconds. More... | |
uint64_t | getDtMicroSec () const |
Returns a cycle time as an integer in microseconds. More... | |
void | setDtSec (double sec) |
Sets a cycle time of module and its submodules in seconds. More... | |
double | getDtSec () const |
ModuleStates | getState () const |
Returns an actual state. More... | |
void | setEvent (ModuleEvents event) |
Command an event. More... | |
ModuleErrors | getError () const |
Returns active error code. More... | |
Protected Member Functions | |
virtual void | create_ (const char *name, parameter_server::Parameter *parameter_server, uint64_t dt_micro_s)=0 |
User-defined callback, which is executed during 'Create' phase. More... | |
virtual bool | initPhase1_ ()=0 |
User-defined callback to register parameters in the tree. More... | |
virtual bool | initPhase2_ ()=0 |
User-defined callback after the parameter tree is ready. More... | |
virtual bool | startOp_ ()=0 |
User-defined callback before entering Operation mode. More... | |
virtual bool | stopOp_ ()=0 |
User-defined callback before to stop Operation mode. More... | |
virtual bool | iteratePreOp_ (const TaskTime &system_time, UserTime *user_time) |
User-defined callback which is called during Phase 2. More... | |
virtual bool | iterateOp_ (const TaskTime &system_time, UserTime *user_time)=0 |
User-defined callback which is called during Operation mode. More... | |
void | createSubmodule (Module *module, const char *name) |
Creates and registers submodule. More... | |
template<typename T > | |
void | createSubmodules (utils::span< T > module_array, const char *basename) |
Creates and registers an array of submodules. More... | |
void | setType (const char *name) |
Sets a type of the module. More... | |
template<size_t buffer_size = parameter_server::DEFAULT_INPUT_BUFFER_LENGTH> | |
parameter_server::ParamHandle | addParameter (const char *id, parameter_server::ParameterType param_type, char *str_ptr, size_t length) |
Helper function to register C-string member variable in the parameter tree. More... | |
template<typename T , size_t buffer_size = parameter_server::DEFAULT_INPUT_BUFFER_LENGTH, add_visitable::EnableIfNotVisitable< T > = true> | |
parameter_server::ParamHandle | addParameter (const char *id, parameter_server::ParameterType param_type, T *value_ptr, size_t length=1, parameter_server::Unit param_unit=parameter_server::Unit::undefined) |
Helper function to register member variable of in the parameter tree. More... | |
template<typename T , size_t buffer_size = parameter_server::DEFAULT_INPUT_BUFFER_LENGTH, add_visitable::EnableIfVisitable< T > = true> | |
parameter_server::GroupHandle | addParameter (const char *id, parameter_server::ParameterType param_type, T *value_ptr, size_t length=1, parameter_server::Unit param_unit=parameter_server::Unit::undefined) |
Helper function to register visitable PODs in the parameter tree. More... | |
template<typename T , size_t buffer_size = parameter_server::DEFAULT_INPUT_BUFFER_LENGTH> | |
parameter_server::ParamHandle | addParameter (const char *id, parameter_server::ParameterType param_type, parameter_server::DataType data_type, T *value_ptr, size_t length=1, parameter_server::Unit param_unit=parameter_server::Unit::undefined) |
Helper function to register types, which cannot be deduced. More... | |
parameter_server::SubHandle | subscribe (const char *path) |
Helper function to subscribe for the parameter's update. More... | |
parameter_server::ReqHandle | request (const char *path) |
Helper function to request a parameter's value. More... | |
parameter_server::PubHandle | publish (const char *path) |
Helper function to update parameter's value. More... | |
parameter_server::Parameter * | getParameterRoot () |
Returns the root of the parameter tree. More... | |
parameter_server::Parameter * | getLocalBranch () |
Returns the root of the local branch. More... | |
Friends | |
class | Task |
A building block of the Motorcortex components.
State machine of the Motorcortex module
Module: Task: ┌──────────────────────────────┐ │ Not Initialized │ └───┬──────────────────────────┘ ┌─────────────────────────────────────┐ │ (Event: create) │ Creates modules and submodules. │ ┌───v───────┐ └───┬─────────────────────────────────┘ │ Phase0 │ │ (Event: configure) └───┬───────┘ ┌───v─────────────────────────────────┐ │ (Event: initPhase1) │ Add parameters to the tree │ │ │ │ ┌───v───────┐ └───┬─────────────────────────────────┘ │ Phase1 │ │ (Event: start) └───┬───────┘ ┌───v─────────────────────────────────┐ │ (Event: initPhase2) │ Parameter tree is ready, │ │ │ │ ┌───v──────────────────────────┐ └───┬─────────────────────────────────┘ │ Phase2 │ │ └───┬──────────────────────^───┘ ┌───v─────────────────────────────────┐ │ (Event: startOp) │ (Event: stopOp) │ Real-time event loop is │ │ │ │ ready to start. │ ┌───v──────────────────────┴───┐ └─────────────────────────────────────┘ │ Operation │ └───┬──────────────────────────┘ │ (Event: exit) ┌───v───────┐ │ Destroyed │ └───────────┘
|
strong |
Module Errors are available via parameter: 'module_error' or a member function call getErrors().
|
strong |
Module Events can be set via parameter: 'module_event' or a member function call setEvent().
|
strong |
Module States are available via parameter: 'module_state' or a member function call getState().
|
protected |
Helper function to register C-string member variable in the parameter tree.
id | - name of the module. |
param_type | - IO type of the parameter. |
str_ptr | - pointer to a C-string. |
length | - maximum length of the C-string. |
buffer_size | - size of the input buffer (default: DEFAULT_INPUT_BUFFER_LENGTH). |
|
protected |
Helper function to register types, which cannot be deduced.
id | - name of the module. |
param_type | - IO type of the parameter. |
data_type | - a data type from the parameter_server::DataType |
value_ptr | - pointer to a member variable. |
length | - number of elements in case of the array. |
param_unit | - measurement unit. |
T | - a data type of the registered variable. |
buffer_size | - size of the input buffer. (default: DEFAULT_INPUT_BUFFER_LENGTH). |
|
protected |
Helper function to register member variable of in the parameter tree.
A Helper function, which automatically deduce standard numerical types.
id | - name of the module. |
param_type | - IO type of the parameter. |
value_ptr | - pointer to a member variable. |
length | - number of elements in case of the array. |
param_unit | - measurement unit. |
T | - a data type of the registered variable. |
buffer_size | - size of the input buffer. (default: DEFAULT_INPUT_BUFFER_LENGTH). |
|
protected |
Helper function to register visitable PODs in the parameter tree.
A Helper function, which automatically deduce visitable PODs.
id | - name of the module. |
param_type | - IO type of the parameter. |
value_ptr | - pointer to a member variable. |
length | - number of elements in case of the array (Only length = 1 is supported). |
param_unit | - measurement unit. |
T | - a data type of the registered variable. |
buffer_size | - size of the input buffer. (default: DEFAULT_INPUT_BUFFER_LENGTH). |
void mcx::container::Module::create | ( | const char * | name, |
parameter_server::Parameter * | parameter_server, | ||
uint64_t | dt_micro_s, | ||
parameter_server::UserGroup | owner_group, | ||
uint32_t | permissions = parameter_server::default_permissions |
||
) |
Extended interface with access permissions.
Extended interface in addition to the previous one, which can set User Group and Permissions of the module branch.
name | - unique name. |
parameter_server | - pointer to the root. |
dt_micro_s | - cycle time in microseconds. |
owner_group | - owner group of the module's branch. |
permissions | - POSIX-like permissions for the parameter access. |
void mcx::container::Module::create | ( | const char * | name, |
parameter_server::Parameter * | parameter_server, | ||
uint64_t | dt_micro_s = 0 |
||
) |
Executes 'Create' phase of initialization.
Before any operations on the module can be performed, it must be created, assigned a unique name, set its location in the parameter tree and set its cycle time.
name | - unique name. |
parameter_server | - pointer to the root. |
dt_micro_s | - cycle time in microseconds. |
|
protectedpure virtual |
User-defined callback, which is executed during 'Create' phase.
name | - unique name. |
parameter_server | - pointer to the root. |
dt_micro_s | - cycle time in microseconds. |
Implemented in mcx::drive::Module, mcx::watchdog::Module, mcx::parameter_server::Persistence, mcx::drive::Diagnostics, and MyModule1.
|
protected |
Creates and registers submodule.
This is a helper function, which creates a submodule and registers it in the branch as a child node. If submodule is created via createSubmodule, its state machine during initialization and deinitialization will be managed automatically. However the user is responsible to call submodule's Module::iterate() and trigger its event cycle.
module | - pointer to the module instance. |
name | - name of the module in the parameter tree. |
|
inlineprotected |
Creates and registers an array of submodules.
This is a helper function, which creates an array of submodule and registers them in the branch as a child nodes. For more information check createSubmodule.
module_array | - iterable object with the array of modules. |
basename | - base name of the module in the parameter tree, index is added automatically |
|
inline |
Returns a cycle time as an integer in microseconds.
|
inline |
Returns a cycle time as a float in seconds.
Module::ModuleErrors mcx::container::Module::getError | ( | ) | const |
|
inlineprotected |
Returns the root of the local branch.
const char * mcx::container::Module::getName | ( | ) | const |
Returns the name of the module.
|
inlineprotected |
Returns the root of the parameter tree.
Module::ModuleStates mcx::container::Module::getState | ( | ) | const |
bool mcx::container::Module::initPhase1 | ( | ) |
Executes initialization of the parameter tree phase.
The next step after the Create. During initPhase1 parameters are added to the parameter tree. Parameter values are loaded from the configuration file
|
protectedpure virtual |
User-defined callback to register parameters in the tree.
Implemented in mcx::drive::Module, mcx::watchdog::Module, mcx::parameter_server::Persistence, mcx::drive::Diagnostics, and MyModule1.
bool mcx::container::Module::initPhase2 | ( | ) |
Initialization, which require parameter tree to be complete and loaded.
After the tree is built and values are loaded, additional initialization might be required. For example preparing the state machine, where state timeouts are the values from the parameter tree.
|
protectedpure virtual |
User-defined callback after the parameter tree is ready.
This callback could be used for additional initialization. During initPhase2_ tree structure is complete, initial values are NOT loaded.
Implemented in mcx::drive::Module, mcx::watchdog::Module, mcx::parameter_server::Persistence, mcx::drive::Diagnostics, and MyModule1.
Iterates an execution cycle of the module.
task_time | - various input timers, application and system time. |
user_time | - user defined timer, an external clock source. |
|
protectedpure virtual |
User-defined callback which is called during Operation mode.
Implemented in mcx::drive::Module, mcx::watchdog::Module, mcx::parameter_server::Persistence, and mcx::drive::Diagnostics.
|
inlineprotectedvirtual |
User-defined callback which is called during Phase 2.
Optional callback, which is useful for the modules which needs to start iterating before the system switches to OP. For example a Logger module start to print output before OP. Real-time behaviour is this callback is not guaranteed.
|
protected |
Helper function to update parameter's value.
|
protected |
Helper function to request a parameter's value.
|
inline |
Sets a cycle time of module and its submodules in microseconds.
micro_sec | - new cycle time of the module. |
|
inline |
Sets a cycle time of module and its submodules in seconds.
sec | - new cycle time of the module. |
void mcx::container::Module::setEvent | ( | Module::ModuleEvents | event | ) |
void mcx::container::Module::setName | ( | const char * | name | ) |
Sets the name of the module.
name | - name of the module. |
|
protected |
Sets a type of the module.
This is a helper function to set the type of the module, which can be used by the client to get additional information. If the type is not set, it will be generated automatically in the following form: Namespace::ClassName<Number of Channels>.
name | - type name. |
bool mcx::container::Module::startOp | ( | ) |
Final initialization before entering Operation mode.
Just before entering Operation mode startOp is called. Module could be started and stopped multiple times. startOp could be used to reset counter, state variable etc.
|
protectedpure virtual |
User-defined callback before entering Operation mode.
During startOp_ callback user can safely access initialized parameter tree. startOp_ operation is part of the first iterateOp_ cycle, that is why it should be relatively short. Otherwise fist cycle time will be violated.
Implemented in mcx::drive::Module, mcx::watchdog::Module, mcx::parameter_server::Persistence, mcx::drive::Diagnostics, and MyModule1.
bool mcx::container::Module::stopOp | ( | ) |
Stops Operation mode, switches back to PHASE2.
User may decide to stop the module by calling stopOp. iterateOp_ is not called anymore, but input requests are still processed. To restart the module startOp should be called.
|
protectedpure virtual |
User-defined callback before to stop Operation mode.
Implemented in mcx::drive::Module, mcx::watchdog::Module, mcx::parameter_server::Persistence, mcx::drive::Diagnostics, and MyModule1.
|
protected |
Helper function to subscribe for the parameter's update.