Tutorial - Making Module
Start:
''Table of Contents''
#contents
* Overview [#w6685947]
Making a module can be done through the following steps:
+ Create a configuration class [optional]
-- Copy and paste the template
-- Add some variables
+ Create a memory class [optional]
-- Copy and paste the template
-- Add some variables
+ Create the module class
-- Copy and paste the template
-- Add ports (signal, slot, in, out)
+ Write a SkyAI module declaration
-- Just write a single line macro
You can easily modify any parts later; take it easy!
* Include [#q5ca70df]
The header skyai/skyai.h is needed:
#codeh(cpp){{
#include <skyai/skyai.h>
}}
* Configuration Class [#u98bbe9f]
+ Copy and paste the template
#codeh(cpp){{
//=======================================================...
class TXxConfigurations
//=======================================================...
{
public:
TReal TestC;
TXxConfigurations (var_space::TVariableMap &mmap) :
TestC (0.0l)
{
Register(mmap);
}
void Register (var_space::TVariableMap &mmap)
{
#define ADD(x_member) AddToVarMap(mmap, #x_member,...
ADD( TestC );
#undef ADD
}
};
//-------------------------------------------------------...
}}
+ Add some variables
-- In the above template, a variable ''TestC'' is added. ...
-- You need to write each variable three times:
+++ Declaration (e.g. TReal TestC;)
+++ Initialization at the constructor [optional] (e.g. Te...
+++ Register (e.g. ADD(TestC);)
:Note|
If you want to add std::vector etc., include lora/variabl...
#codeh(cpp){{
#include <lora/variable_space_impl.h>
}}
* Memory Class [#t5fd156f]
+ Copy and paste the template
#codeh(cpp){{
//=======================================================...
class TXxMemory
//=======================================================...
{
public:
TReal TestM;
TXxMemory (var_space::TVariableMap &mmap) :
TestM (0.0l)
{
Register(mmap);
}
void Register (var_space::TVariableMap &mmap)
{
#define ADD(x_member) AddToVarMap(mmap, #x_member,...
ADD( TestM );
#undef ADD
}
};
//-------------------------------------------------------...
}}
+ Add some variables
-- Same as the configuration class
* Module class [#ff1a8bc9]
+ Copy and paste the template&br;
Simple template:
#codeh(cpp){{
//=======================================================...
//!\brief XX module
class MXxModule
: public MParentModule
//=======================================================...
{
public:
typedef MParentModule TParent;
typedef MXxModule TThis;
SKYAI_MODULE_NAMES(MXxModule)
MXxModule (const std::string &v_instance_name)
: TParent (v_instance_name),
conf_ (TParent::param_box_config_map()),
mem_ (TParent::param_box_memory_map())
{
}
protected:
TXxConfigurations conf_;
TXxMemory mem_;
}; // end of MXxModule
//-------------------------------------------------------...
}}
Verbose template:
#codeh(cpp){{
//=======================================================...
//!\brief XX module
class MXxModule
: public MParentModule
//=======================================================...
{
public:
typedef MParentModule TParent; // optional
typedef MXxModule TThis; // optional
SKYAI_MODULE_NAMES(MXxModule) // mandatory
MXxModule (const std::string &v_instance_name)
: TParent (v_instance_name), // mandatory
conf_ (TParent::param_box_config_map()),
mem_ (TParent::param_box_memory_map()),
slot_slot1 (*this), // optional
signal_signal1 (*this), // optional
out_out1 (*this), // optional
in_in1 (*this) // optional
{
add_slot_port (slot_slot1); // optional
add_signal_port (signal_signal1); // optional
add_out_port (out_out1); // optional
add_in_port (in_in1); // optional
}
protected:
TXxConfigurations conf_; // optional
TXxMemory mem_; // optional
MAKE_SLOT_PORT(slot_slot1, TOutType, (const TInType &x)...
MAKE_SIGNAL_PORT(signal_signal1, const TOutType& (const...
MAKE_OUT_PORT(out_out1, const TOutType&, (const TInType...
MAKE_IN_PORT(in_in1, const TOutType& (const TInType &),...
virtual TOutType slot_slot1_exec (const TInType &x) ...
{
...
}
virtual const TOutType& out_out1_get (const TInType &x)...
{
...
}
}; // end of MXxModule
//-------------------------------------------------------...
}}
+ If you do not use the configuration box or the memory b...
+ Replace every MXxModule by the name of your module.
+ Replace every MParentModule by the parent module (usual...
+ Add ports
-- Adding a ''slot port'':
+++ Declare at the protected section:
#codeh(cpp){{
MAKE_SLOT_PORT(slot_slot1, TOutType, (const TInType &x), ...
// change slot1 to the name of the slot port
// change TOutType to the return type of the callback fun...
// change const TInType &x to the parameter list of the c...
// change x to the argument list of the callback function
// e.g. MAKE_SLOT_PORT(slot_action, void, (const TReal &...
}}
+++ Write the initialization at the constructor:
#codeh(cpp){{
MXxModule (const std::string &v_instance_name)
: ...
slot_slot1 (*this), // like this
...
}}
+++ Use the add_slot_port function at the constructor:
#codeh(cpp){{
add_slot_port (slot_slot1);
}}
+++ Implement the callback function:
#codeh(cpp){{
// change slot1 to the name of the slot port
virtual TOutType slot_slot1_exec (const TInType &x)
{
...
}
}}
-- Adding a ''signal port'':
+++ Declare at the protected section:
#codeh(cpp){{
MAKE_SIGNAL_PORT(signal_signal1, const TOutType& (const T...
// change signal1 to the name of the signal port
// change TOutType to the return type of the callback fun...
// change const TInType & to the parameter list of the ca...
// e.g. MAKE_SIGNAL_PORT(signal_action, void (const TRea...
}}
+++ Write the initialization at the constructor:
#codeh(cpp){{
MXxModule (const std::string &v_instance_name)
: ...
signal_signal1 (*this), // like this
...
}}
+++ Use the add_signal_port function at the constructor:
#codeh(cpp){{
add_signal_port (signal_signal1);
}}
-- Adding an ''out port'':
+++ Declare at the protected section:
#codeh(cpp){{
MAKE_OUT_PORT(out_out1, const TOutType&, (const TInType &...
// change out1 to the name of the out port
// change TOutType to the return type of the function
// change const TInType &x to the parameter list of the f...
// change x to the argument list of the function
// e.g. MAKE_OUT_PORT(out_state, const TRealVector&, (vo...
}}
+++ Write the initialization at the constructor:
#codeh(cpp){{
MXxModule (const std::string &v_instance_name)
: ...
out_out1 (*this), // like this
...
}}
+++ Use the add_out_port function at the constructor:
#codeh(cpp){{
add_out_port (out_out1);
}}
+++ Implement the function:
#codeh(cpp){{
// change out1 to the name of the out port
virtual const TOutType& out_out1_get (const TInType &x) c...
{
...
}
}}
-- Adding an ''in port'':
+++ Declare at the protected section:
#codeh(cpp){{
MAKE_IN_PORT(in_in1, const TOutType& (const TInType &), T...
// change in1 to the name of the in port
// change TOutType to the return type of the function
// change const TInType &x to the parameter list of the f...
// e.g. MAKE_IN_PORT(in_state, const TRealVector& (void)...
}}
+++ Write the initialization at the constructor:
#codeh(cpp){{
MXxModule (const std::string &v_instance_name)
: ...
in_in1 (*this), // like this
...
}}
+++ Use the add_in_port function at the constructor:
#codeh(cpp){{
add_in_port (in_in1);
}}
* SkyAI Module Declaration [#sfeabb06]
+ Just write a single line macro at the source code (do n...
#codeh(cpp){{
SKYAI_ADD_MODULE(MXxModule)
}}
* Implementing Functions [#u58a602b]
In the member functions of a module including the callbac...
** Emit Signal [#wb00f568]
In order to emit a signal at a signal port, write as foll...
#codeh(cpp){{
signal_signal1.ExecAll(x,y);
}}
The member function ExecAll emits a signal to every slot ...
If no slot port is connected, nothing happens.
** Call Function [#o37d37cc]
In order to use a function connected to an in port, write...
#codeh(cpp){{
TRealVector y= in_in1.GetFirst(x);
}}
Here, the member function GetFirst execute the function o...
Recall that in default, only one out port can be connecte...
If no out port is connected, GetFirst causes an error.
In order to avoid the error, you need to check the size o...
#codeh(cpp){{
in_in1.ConnectionSize()
}}
this returns the number of connected out ports.
We suggest to wrap the GetFirst by using a macro as follo...
#codeh(cpp){{
MAKE_IN_PORT(in_state, const TRealVector& (void), TThis);
MAKE_IN_PORT(in_func, const TReal& (const TReal &x), TThi...
#define GET_FROM_IN_PORT(x_in,x_return_type,x_arg_list,x_...
x_return_type get_##x_in x_arg_list const ...
{ ...
if (in_##x_in.ConnectionSize()==0) ...
{LERROR("in "<<ModuleUniqueCode()<<", in_" #x_in ...
return in_##x_in.GetFirst x_param_list; ...
}
GET_FROM_IN_PORT(state, const TRealVector&, (void), ())
GET_FROM_IN_PORT(func, const TReal&, (const TReal &x), (x))
#undef GET_FROM_IN_PORT
}}
Then, we can use get_state and get_func like a normal fun...
#codeh(cpp){{
TRealVector x= get_state();
TReal y= get_func(1.2);
}}
The macro GET_FROM_IN_PORT is generic, so you can copy an...
End:
''Table of Contents''
#contents
* Overview [#w6685947]
Making a module can be done through the following steps:
+ Create a configuration class [optional]
-- Copy and paste the template
-- Add some variables
+ Create a memory class [optional]
-- Copy and paste the template
-- Add some variables
+ Create the module class
-- Copy and paste the template
-- Add ports (signal, slot, in, out)
+ Write a SkyAI module declaration
-- Just write a single line macro
You can easily modify any parts later; take it easy!
* Include [#q5ca70df]
The header skyai/skyai.h is needed:
#codeh(cpp){{
#include <skyai/skyai.h>
}}
* Configuration Class [#u98bbe9f]
+ Copy and paste the template
#codeh(cpp){{
//=======================================================...
class TXxConfigurations
//=======================================================...
{
public:
TReal TestC;
TXxConfigurations (var_space::TVariableMap &mmap) :
TestC (0.0l)
{
Register(mmap);
}
void Register (var_space::TVariableMap &mmap)
{
#define ADD(x_member) AddToVarMap(mmap, #x_member,...
ADD( TestC );
#undef ADD
}
};
//-------------------------------------------------------...
}}
+ Add some variables
-- In the above template, a variable ''TestC'' is added. ...
-- You need to write each variable three times:
+++ Declaration (e.g. TReal TestC;)
+++ Initialization at the constructor [optional] (e.g. Te...
+++ Register (e.g. ADD(TestC);)
:Note|
If you want to add std::vector etc., include lora/variabl...
#codeh(cpp){{
#include <lora/variable_space_impl.h>
}}
* Memory Class [#t5fd156f]
+ Copy and paste the template
#codeh(cpp){{
//=======================================================...
class TXxMemory
//=======================================================...
{
public:
TReal TestM;
TXxMemory (var_space::TVariableMap &mmap) :
TestM (0.0l)
{
Register(mmap);
}
void Register (var_space::TVariableMap &mmap)
{
#define ADD(x_member) AddToVarMap(mmap, #x_member,...
ADD( TestM );
#undef ADD
}
};
//-------------------------------------------------------...
}}
+ Add some variables
-- Same as the configuration class
* Module class [#ff1a8bc9]
+ Copy and paste the template&br;
Simple template:
#codeh(cpp){{
//=======================================================...
//!\brief XX module
class MXxModule
: public MParentModule
//=======================================================...
{
public:
typedef MParentModule TParent;
typedef MXxModule TThis;
SKYAI_MODULE_NAMES(MXxModule)
MXxModule (const std::string &v_instance_name)
: TParent (v_instance_name),
conf_ (TParent::param_box_config_map()),
mem_ (TParent::param_box_memory_map())
{
}
protected:
TXxConfigurations conf_;
TXxMemory mem_;
}; // end of MXxModule
//-------------------------------------------------------...
}}
Verbose template:
#codeh(cpp){{
//=======================================================...
//!\brief XX module
class MXxModule
: public MParentModule
//=======================================================...
{
public:
typedef MParentModule TParent; // optional
typedef MXxModule TThis; // optional
SKYAI_MODULE_NAMES(MXxModule) // mandatory
MXxModule (const std::string &v_instance_name)
: TParent (v_instance_name), // mandatory
conf_ (TParent::param_box_config_map()),
mem_ (TParent::param_box_memory_map()),
slot_slot1 (*this), // optional
signal_signal1 (*this), // optional
out_out1 (*this), // optional
in_in1 (*this) // optional
{
add_slot_port (slot_slot1); // optional
add_signal_port (signal_signal1); // optional
add_out_port (out_out1); // optional
add_in_port (in_in1); // optional
}
protected:
TXxConfigurations conf_; // optional
TXxMemory mem_; // optional
MAKE_SLOT_PORT(slot_slot1, TOutType, (const TInType &x)...
MAKE_SIGNAL_PORT(signal_signal1, const TOutType& (const...
MAKE_OUT_PORT(out_out1, const TOutType&, (const TInType...
MAKE_IN_PORT(in_in1, const TOutType& (const TInType &),...
virtual TOutType slot_slot1_exec (const TInType &x) ...
{
...
}
virtual const TOutType& out_out1_get (const TInType &x)...
{
...
}
}; // end of MXxModule
//-------------------------------------------------------...
}}
+ If you do not use the configuration box or the memory b...
+ Replace every MXxModule by the name of your module.
+ Replace every MParentModule by the parent module (usual...
+ Add ports
-- Adding a ''slot port'':
+++ Declare at the protected section:
#codeh(cpp){{
MAKE_SLOT_PORT(slot_slot1, TOutType, (const TInType &x), ...
// change slot1 to the name of the slot port
// change TOutType to the return type of the callback fun...
// change const TInType &x to the parameter list of the c...
// change x to the argument list of the callback function
// e.g. MAKE_SLOT_PORT(slot_action, void, (const TReal &...
}}
+++ Write the initialization at the constructor:
#codeh(cpp){{
MXxModule (const std::string &v_instance_name)
: ...
slot_slot1 (*this), // like this
...
}}
+++ Use the add_slot_port function at the constructor:
#codeh(cpp){{
add_slot_port (slot_slot1);
}}
+++ Implement the callback function:
#codeh(cpp){{
// change slot1 to the name of the slot port
virtual TOutType slot_slot1_exec (const TInType &x)
{
...
}
}}
-- Adding a ''signal port'':
+++ Declare at the protected section:
#codeh(cpp){{
MAKE_SIGNAL_PORT(signal_signal1, const TOutType& (const T...
// change signal1 to the name of the signal port
// change TOutType to the return type of the callback fun...
// change const TInType & to the parameter list of the ca...
// e.g. MAKE_SIGNAL_PORT(signal_action, void (const TRea...
}}
+++ Write the initialization at the constructor:
#codeh(cpp){{
MXxModule (const std::string &v_instance_name)
: ...
signal_signal1 (*this), // like this
...
}}
+++ Use the add_signal_port function at the constructor:
#codeh(cpp){{
add_signal_port (signal_signal1);
}}
-- Adding an ''out port'':
+++ Declare at the protected section:
#codeh(cpp){{
MAKE_OUT_PORT(out_out1, const TOutType&, (const TInType &...
// change out1 to the name of the out port
// change TOutType to the return type of the function
// change const TInType &x to the parameter list of the f...
// change x to the argument list of the function
// e.g. MAKE_OUT_PORT(out_state, const TRealVector&, (vo...
}}
+++ Write the initialization at the constructor:
#codeh(cpp){{
MXxModule (const std::string &v_instance_name)
: ...
out_out1 (*this), // like this
...
}}
+++ Use the add_out_port function at the constructor:
#codeh(cpp){{
add_out_port (out_out1);
}}
+++ Implement the function:
#codeh(cpp){{
// change out1 to the name of the out port
virtual const TOutType& out_out1_get (const TInType &x) c...
{
...
}
}}
-- Adding an ''in port'':
+++ Declare at the protected section:
#codeh(cpp){{
MAKE_IN_PORT(in_in1, const TOutType& (const TInType &), T...
// change in1 to the name of the in port
// change TOutType to the return type of the function
// change const TInType &x to the parameter list of the f...
// e.g. MAKE_IN_PORT(in_state, const TRealVector& (void)...
}}
+++ Write the initialization at the constructor:
#codeh(cpp){{
MXxModule (const std::string &v_instance_name)
: ...
in_in1 (*this), // like this
...
}}
+++ Use the add_in_port function at the constructor:
#codeh(cpp){{
add_in_port (in_in1);
}}
* SkyAI Module Declaration [#sfeabb06]
+ Just write a single line macro at the source code (do n...
#codeh(cpp){{
SKYAI_ADD_MODULE(MXxModule)
}}
* Implementing Functions [#u58a602b]
In the member functions of a module including the callbac...
** Emit Signal [#wb00f568]
In order to emit a signal at a signal port, write as foll...
#codeh(cpp){{
signal_signal1.ExecAll(x,y);
}}
The member function ExecAll emits a signal to every slot ...
If no slot port is connected, nothing happens.
** Call Function [#o37d37cc]
In order to use a function connected to an in port, write...
#codeh(cpp){{
TRealVector y= in_in1.GetFirst(x);
}}
Here, the member function GetFirst execute the function o...
Recall that in default, only one out port can be connecte...
If no out port is connected, GetFirst causes an error.
In order to avoid the error, you need to check the size o...
#codeh(cpp){{
in_in1.ConnectionSize()
}}
this returns the number of connected out ports.
We suggest to wrap the GetFirst by using a macro as follo...
#codeh(cpp){{
MAKE_IN_PORT(in_state, const TRealVector& (void), TThis);
MAKE_IN_PORT(in_func, const TReal& (const TReal &x), TThi...
#define GET_FROM_IN_PORT(x_in,x_return_type,x_arg_list,x_...
x_return_type get_##x_in x_arg_list const ...
{ ...
if (in_##x_in.ConnectionSize()==0) ...
{LERROR("in "<<ModuleUniqueCode()<<", in_" #x_in ...
return in_##x_in.GetFirst x_param_list; ...
}
GET_FROM_IN_PORT(state, const TRealVector&, (void), ())
GET_FROM_IN_PORT(func, const TReal&, (const TReal &x), (x))
#undef GET_FROM_IN_PORT
}}
Then, we can use get_state and get_func like a normal fun...
#codeh(cpp){{
TRealVector x= get_state();
TReal y= get_func(1.2);
}}
The macro GET_FROM_IN_PORT is generic, so you can copy an...
Page: