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");
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
#define ROS_WARN(...)
std::array< double, 7 > q_d
std::map< std::string, std::shared_ptr< Joint > > JointMap
Definition: statemachine.h:14
std::array< double, 7 > q
std::array< double, 7 > dq_d
std::array< double, 7 > ddq_d
RobotMode robot_mode


franka_gazebo
Author(s): Franka Emika GmbH
autogenerated on Mon Sep 19 2022 03:06:05