statemachine.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <franka/robot_state.h>
4 #include <franka_gazebo/joint.h>
5 
6 #include <ros/ros.h>
7 #include <boost_sml/sml.hpp>
8 #include <map>
9 #include <memory>
10 #include <string>
11 
12 namespace franka_gazebo {
13 
14 using JointMap = std::map<std::string, std::shared_ptr<Joint>>;
15 
16 // States
17 struct Idle {};
18 struct Move {};
19 struct UserStopped {};
20 
21 // Events
22 struct ErrorRecovery {};
23 struct SwitchControl {};
24 struct UserStop {
25  bool pressed;
26 };
27 
28 // Guards
29 auto contains = [](const auto& haystack, const auto& needle) {
30  return haystack.find(needle) != std::string::npos;
31 };
32 auto isPressed = [](const UserStop& event) { return event.pressed; };
33 auto isReleased = [](const UserStop& event, const JointMap& joints) { return not event.pressed; };
34 auto isStarting = [](const SwitchControl& event, const JointMap& joints) {
35  for (auto& joint : joints) {
36  if (contains(joint.first, "_finger_joint")) {
37  continue;
38  }
39  if (joint.second->control_method) {
40  return true;
41  }
42  }
43  return false;
44 };
45 auto isStopping = [](const SwitchControl& event, const JointMap& joints) {
46  return not isStarting(event, joints);
47 };
48 
49 // Actions
50 auto start = [](franka::RobotState& state) { state.robot_mode = franka::RobotMode::kMove; };
51 auto idle = [](franka::RobotState& state) { state.robot_mode = franka::RobotMode::kIdle; };
52 auto stop = [](franka::RobotState& state, JointMap& joints) {
53  ROS_WARN("User stop pressed, stopping robot");
54  state.robot_mode = franka::RobotMode::kUserStopped;
55  state.q_d = state.q;
56  state.dq_d = {0};
57  state.ddq_d = {0};
58 
59  for (auto& joint : joints) {
60  if (contains(joint.first, "_finger_joint")) {
61  continue;
62  }
63  joint.second->stop_position = joint.second->position;
64  joint.second->desired_position = joint.second->position;
65  joint.second->desired_velocity = 0;
66  }
67 };
68 
69 struct StateMachine {
70  auto operator()() const {
71  using namespace boost::sml;
72  return make_transition_table(
73  // clang-format off
74  *state<Idle> + event<SwitchControl>[isStarting] / start = state<Move>,
75  state<Idle> + event<UserStop>[isPressed] / stop = state<UserStopped>,
76  state<Idle> + event<ErrorRecovery> / start = state<Move>,
77  state<Move> + event<SwitchControl>[isStopping] / idle = state<Idle>,
78  state<Move> + event<UserStop>[isPressed] / stop = state<UserStopped>,
79  state<UserStopped> + event<UserStop>[isReleased] / idle = state<Idle>
80  // clang-format on
81  );
82  }
83 };
84 } // namespace franka_gazebo
franka_gazebo::UserStop::pressed
bool pressed
Definition: statemachine.h:25
franka_gazebo
Definition: controller_verifier.h:8
franka_gazebo::Move
Definition: statemachine.h:18
franka_gazebo::Idle
Definition: statemachine.h:17
ros.h
franka_gazebo::isStopping
auto isStopping
Definition: statemachine.h:45
franka_gazebo::isStarting
auto isStarting
Definition: statemachine.h:34
joint.h
franka_gazebo::JointMap
std::map< std::string, std::shared_ptr< Joint > > JointMap
Definition: statemachine.h:14
franka_gazebo::UserStopped
Definition: statemachine.h:19
franka_gazebo::StateMachine::operator()
auto operator()() const
Definition: statemachine.h:70
franka_gazebo::idle
auto idle
Definition: statemachine.h:51
ROS_WARN
#define ROS_WARN(...)
franka_gazebo::isPressed
auto isPressed
Definition: statemachine.h:32
franka_gazebo::ErrorRecovery
Definition: statemachine.h:22
franka_gazebo::isReleased
auto isReleased
Definition: statemachine.h:33
franka_gazebo::contains
auto contains
Definition: statemachine.h:29
franka_gazebo::start
auto start
Definition: statemachine.h:50
franka_gazebo::SwitchControl
Definition: statemachine.h:23
franka_gazebo::StateMachine
Definition: statemachine.h:69
franka_gazebo::UserStop
Definition: statemachine.h:24
sml.hpp
franka_gazebo::stop
auto stop
Definition: statemachine.h:52


franka_gazebo
Author(s): Franka Emika GmbH
autogenerated on Mon Sep 19 2022 02:33:28