add rcl lifecycle

* (refactor) add rcl_lifecycle package

* (refactor) cleanup free calls

* (fix) initialize state machine with external node handle

* (dev) use external typesupport

* (fix) use external typesupport

* (fix) cleanup states

* (fix) no pointer comparison in state machine

* (test) refactor for rcl testing purpose

* (test) test suite for lifecycle sequence

* (dev) change to rcl_ret_t

* (test) wrong transition test

* (fix) dependency for isolated build

* (clean) remove std_msgs as a dep

* (fix) enable correct visibility control

* (fix) correct test for initialization

* (fix) correct visibility attributes

* (dev) change default value to lifecycle_msgs

* (clean) style and lifecycle prefix

* (dev) cmake macro get_rcl_lifecycle_information

* (cleanup) remove unused files'

* (cleanup) remove callback pointer

* (debug) add print state machine function

* (review) address review comments

* (bugfix) correct export in information.cmake

* (fix) correct visibility control

* (vcs) fix convertion from size_t to unsigned int

* (typo) fix internal struct name

* const correctness

* get_available_states service

* new service msgs

* tes for multiple instances

* (dev) return codes

* initial refactor

* test default sequence

* refactor state machine

* apply upstream changes

* c++14

* disable state machine print

* address review comments

* uncrustify

* fix comparison with unsigned warning
This commit is contained in:
Karsten Knese 2016-12-14 09:28:54 -08:00 committed by GitHub
parent 4590fc5f77
commit a18ef97e5a
17 changed files with 2357 additions and 0 deletions

View file

@ -0,0 +1,93 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef RCL_LIFECYCLE__DATA_TYPES_H_
#define RCL_LIFECYCLE__DATA_TYPES_H_
#include <rcl/rcl.h>
#include "rcl_lifecycle/visibility_control.h"
#if __cplusplus
extern "C"
{
#endif
typedef int rcl_lifecycle_ret_t;
#define RCL_LIFECYCLE_RET_OK -1
#define RCL_LIFECYCLE_RET_FAILURE -2
#define RCL_LIFECYCLE_RET_ERROR -3
typedef struct rcl_lifecycle_transition_t rcl_lifecycle_transition_t;
typedef struct rcl_lifecycle_state_t
{
const char * label;
unsigned int id;
// a valid key is a generic identifier for deciding
// which transitions to trigger.
// This serves the purpose of hiding a unique ID from
// users prospective.
// e.g. shutdown
// the concrete transition for the state "unconfigured"
// is "unconfigured_shutdown". However, the user only specifies
// "shutdown". So we register the "unconfigured_shutdown"
// transition with the impuls "shutdown".
rcl_lifecycle_ret_t * valid_transition_keys;
rcl_lifecycle_transition_t * valid_transitions;
unsigned int valid_transition_size;
} rcl_lifecycle_state_t;
typedef struct rcl_lifecycle_transition_t
{
const char * label;
unsigned int id;
const rcl_lifecycle_state_t * start;
const rcl_lifecycle_state_t * goal;
} rcl_lifecycle_transition_t;
// TODO(karsten1987): Rename map as it is no
// associated array any longer
typedef struct rcl_lifecycle_transition_map_t
{
rcl_lifecycle_state_t * states;
unsigned int states_size;
rcl_lifecycle_transition_t * transitions;
unsigned int transitions_size;
} rcl_lifecycle_transition_map_t;
typedef struct rcl_lifecycle_com_interface_t
{
rcl_node_t * node_handle;
rcl_publisher_t pub_transition_event;
rcl_service_t srv_change_state;
rcl_service_t srv_get_state;
rcl_service_t srv_get_available_states;
rcl_service_t srv_get_available_transitions;
} rcl_lifecycle_com_interface_t;
typedef struct rcl_lifecycle_state_machine_t
{
const rcl_lifecycle_state_t * current_state;
// Map/Associated array of registered states and transitions
rcl_lifecycle_transition_map_t transition_map;
// Communication interface into a ROS world
rcl_lifecycle_com_interface_t com_interface;
} rcl_lifecycle_state_machine_t;
#if __cplusplus
}
#endif
#endif // RCL_LIFECYCLE__DATA_TYPES_H_

View file

@ -0,0 +1,86 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef RCL_LIFECYCLE__RCL_LIFECYCLE_H_
#define RCL_LIFECYCLE__RCL_LIFECYCLE_H_
#if __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
#include <rcl_lifecycle/visibility_control.h>
#include <rcl_lifecycle/data_types.h>
RCL_LIFECYCLE_PUBLIC
rcl_lifecycle_state_machine_t
rcl_lifecycle_get_zero_initialized_state_machine();
RCL_LIFECYCLE_PUBLIC
rcl_ret_t
rcl_lifecycle_state_machine_init(
rcl_lifecycle_state_machine_t * state_machine,
rcl_node_t * node_handle,
const rosidl_message_type_support_t * ts_pub_notify,
const rosidl_service_type_support_t * ts_srv_change_state,
const rosidl_service_type_support_t * ts_srv_get_state,
const rosidl_service_type_support_t * ts_srv_get_available_states,
const rosidl_service_type_support_t * ts_srv_get_available_transitions,
bool default_states);
RCL_LIFECYCLE_PUBLIC
rcl_ret_t
rcl_lifecycle_state_machine_fini(
rcl_lifecycle_state_machine_t * state_machine,
rcl_node_t * node_handle);
RCL_LIFECYCLE_PUBLIC
rcl_ret_t
rcl_lifecycle_state_machine_is_initialized(
const rcl_lifecycle_state_machine_t * state_machine);
RCL_LIFECYCLE_PUBLIC
const rcl_lifecycle_transition_t *
rcl_lifecycle_is_valid_callback_transition(
rcl_lifecycle_state_machine_t * state_machine,
rcl_lifecycle_ret_t key);
/// Execute a transition
/*
* Important note for \param key here:
* This is meant as feedback from the high level
* callback associated with this transition.
* The key is the index for the valid transitions
* associated with the current state.
* This key may either be a valid external stimuli
* such as "configure" or direct return codes from
* callbacks such as RCL_LIFECYCLE_RET_OK et. al.
*/
RCL_LIFECYCLE_PUBLIC
rcl_ret_t
rcl_lifecycle_trigger_transition(
rcl_lifecycle_state_machine_t * state_machine,
rcl_lifecycle_ret_t key, bool publish_notification);
RCL_LIFECYCLE_PUBLIC
void
rcl_print_state_machine(const rcl_lifecycle_state_machine_t * state_machine);
#if __cplusplus
}
#endif // extern "C"
#endif // RCL_LIFECYCLE__RCL_LIFECYCLE_H_

View file

@ -0,0 +1,55 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef RCL_LIFECYCLE__TRANSITION_MAP_H_
#define RCL_LIFECYCLE__TRANSITION_MAP_H_
#include <rcl_lifecycle/data_types.h>
#if __cplusplus
extern "C"
{
#endif
RCL_LIFECYCLE_PUBLIC
void
rcl_lifecycle_register_state(
rcl_lifecycle_transition_map_t * map,
rcl_lifecycle_state_t state);
RCL_LIFECYCLE_PUBLIC
void
rcl_lifecycle_register_transition(
rcl_lifecycle_transition_map_t * transition_map,
rcl_lifecycle_transition_t transition,
rcl_lifecycle_ret_t key);
RCL_LIFECYCLE_PUBLIC
rcl_lifecycle_state_t *
rcl_lifecycle_get_state(
rcl_lifecycle_transition_map_t * transition_map,
unsigned int state_id);
RCL_LIFECYCLE_PUBLIC
rcl_lifecycle_transition_t *
rcl_lifecycle_get_transitions(
rcl_lifecycle_transition_map_t * transition_map,
unsigned int state_id);
#if __cplusplus
}
#endif
#endif // RCL_LIFECYCLE__TRANSITION_MAP_H_

View file

@ -0,0 +1,56 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* This header must be included by all rclcpp headers which declare symbols
* which are defined in the rclcpp library. When not building the rclcpp
* library, i.e. when using the headers in other package's code, the contents
* of this header change the visibility of certain symbols which the rclcpp
* library cannot have, but the consuming code must have inorder to link.
*/
#ifndef RCL_LIFECYCLE__VISIBILITY_CONTROL_H_
#define RCL_LIFECYCLE__VISIBILITY_CONTROL_H_
// This logic was borrowed (then namespaced) from the examples on the gcc wiki:
// https://gcc.gnu.org/wiki/Visibility
#if defined _WIN32 || defined __CYGWIN__
#ifdef __GNUC__
#define RCL_LIFECYCLE_EXPORT __attribute__ ((dllexport))
#define RCL_LIFECYCLE_IMPORT __attribute__ ((dllimport))
#else
#define RCL_LIFECYCLE_EXPORT __declspec(dllexport)
#define RCL_LIFECYCLE_IMPORT __declspec(dllimport)
#endif
#ifdef RCL_LIFECYCLE_BUILDING_DLL
#define RCL_LIFECYCLE_PUBLIC RCL_LIFECYCLE_EXPORT
#else
#define RCL_LIFECYCLE_PUBLIC RCL_LIFECYCLE_IMPORT
#endif
#define RCL_LIFECYCLE_PUBLIC_TYPE RCL_LIFECYCLE_PUBLIC
#define RCL_LIFECYCLE_LOCAL
#else
#define RCL_LIFECYCLE_EXPORT __attribute__ ((visibility("default")))
#define RCL_LIFECYCLE_IMPORT
#if __GNUC__ >= 4
#define RCL_LIFECYCLE_PUBLIC __attribute__ ((visibility("default")))
#define RCL_LIFECYCLE_LOCAL __attribute__ ((visibility("hidden")))
#else
#define RCL_LIFECYCLE_PUBLIC
#define RCL_LIFECYCLE_LOCAL
#endif
#define RCL_LIFECYCLE_PUBLIC_TYPE
#endif
#endif // RCL_LIFECYCLE__VISIBILITY_CONTROL_H_