@@ -35,9 +35,9 @@ struct Callback;
3535template <typename Ret, typename ... Params, size_t State_Index, size_t Transition_Index>
3636struct Callback <Ret(Params...), State_Index, Transition_Index>
3737{
38- template <typename ... Args>
39- static Ret callback (Args... args) { return func (args...); }
40- static std::function<Ret(Params...)> func;
38+ template <typename ... Args>
39+ static Ret callback (Args... args) { return func (args...); }
40+ static std::function<Ret(Params...)> func;
4141};
4242
4343// Initialize the static member.
@@ -51,7 +51,10 @@ std::function<Ret(Params...)> Callback<Ret(Params...), State_Index, Transition_I
5151 */
5252class NodeInterface
5353{
54- protected:
54+ std::map<size_t , std::function<bool (void )>> callback_map;
55+ rcl_state_machine_t state_machine_;
56+
57+ protected:
5558 virtual bool on_configure () { return true ; };
5659 virtual bool on_cleanup () { return true ; };
5760 virtual bool on_shutdown () { return true ; };
@@ -68,11 +71,10 @@ class NodeInterface
6871 setup_state_machine ()
6972 {
7073 state_machine_ = rcl_get_default_state_machine ();
71- // register callback functions here so that a generic
72- // move_to function in rcl can actually makes the change
73- // register_callback(&state_machine, state.label, fcn)
7474 }
7575
76+ // In case with want to register the callbacks directly in c
77+ /*
7678 LIFECYCLE_EXPORT
7779 template<typename T, size_t State_Index, size_t Transition_Index>
7880 void
@@ -82,8 +84,46 @@ class NodeInterface
8284 bool(*c_function_pointer)(void) = static_cast<decltype(c_function_pointer)>(Callback<bool(), State_Index, Transition_Index>::callback);
8385 rcl_register_callback(&state_machine_, (unsigned int)State_Index, (unsigned int)Transition_Index, c_function_pointer);
8486 }
87+ */
8588
86- rcl_state_machine_t state_machine_;
89+ LIFECYCLE_EXPORT
90+ template <typename T>
91+ void
92+ register_transition_callback (bool (T::*method)(), T* instance, size_t transition_state_index)
93+ {
94+ const rcl_state_transition_t * transition_state
95+ = rcl_get_registered_transition_by_index (&state_machine_, transition_state_index);
96+ if (!transition_state)
97+ {
98+ // TODO do something smarter here
99+ throw std::runtime_error (" Transition is not valid" );
100+ }
101+ callback_map[transition_state_index] = std::bind (method, instance);
102+ }
103+
104+ LIFECYCLE_EXPORT
105+ template <typename T>
106+ void
107+ register_transition_callback (bool (T::*method)(), T* instance, const std::string& transition_state_label)
108+ {
109+ const rcl_state_transition_t * transition_state
110+ = rcl_get_registered_transition_by_label (&state_machine_, transition_state_label.c_str ());
111+ if (!transition_state)
112+ {
113+ // TODO do something smarter here
114+ throw std::runtime_error (" Transition is not valid" );
115+ }
116+ callback_map[transition_state->transition_state .index ] = std::bind (method, instance);
117+ }
118+
119+ public:
120+ LIFECYCLE_EXPORT
121+ void
122+ print_state_machine ()
123+ {
124+ printf (" current state is %s\n " , state_machine_.current_state ->label );
125+ rcl_print_transition_map (&state_machine_.transition_map );
126+ }
87127
88128public:
89129 // virtual bool create() = 0;
@@ -92,7 +132,9 @@ class NodeInterface
92132 if (state_machine_.current_state ->index == rcl_state_unconfigured.index )
93133 {
94134 // given the current state machine, specify a transition and go for it
95- auto ret = rcl_invoke_transition (&state_machine_, rcl_state_configuring);
135+ // auto ret = rcl_invoke_transition(&state_machine_, rcl_state_configuring);
136+ auto cb = callback_map[rcl_state_configuring.index ];
137+ auto ret = cb ();
96138 printf (" %s\n " , (ret)?" Callback was successful" :" Callback unsuccessful" );
97139 // change state here to "Configuring"
98140 // if (on_configure())
@@ -181,11 +223,9 @@ class LifecycleNode : public rclcpp::node::Node, public lifecycle_interface::Nod
181223 {
182224 lifecycle_interface::NodeInterface::setup_state_machine ();
183225
184- register_state_callback<LifecycleNode, 0 , 4 >(
185- &LifecycleNode::on_configure, this );
186- // FAAAAAAIL !!!!!
187- // rcl_register_callback(&state_machine_, rcl_state_unconfigured.index, rcl_state_configuring.index, &LifecycleNode::on_configure);
226+ register_transition_callback (&LifecycleNode::on_configure, this , rcl_state_configuring.index );
188227 }
228+
189229 /* *
190230 * @brief same API for creating publisher as regular Node
191231 */
@@ -206,14 +246,6 @@ class LifecycleNode : public rclcpp::node::Node, public lifecycle_interface::Nod
206246 return pub;
207247 }
208248
209- LIFECYCLE_EXPORT
210- void
211- print_state_machine ()
212- {
213- printf (" current state is %s\n " , state_machine_.current_state ->label );
214- rcl_print_transition_map (&state_machine_.transition_map );
215- }
216-
217249 LIFECYCLE_EXPORT
218250 virtual bool
219251 on_configure ()
0 commit comments