NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
ctl_ros.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef CTL_CTL_ROS_H_
20 #define CTL_CTL_ROS_H_
21 
22 #include <ctl/ctl.h>
23 
24 // For includes
25 #include <ros/ros.h>
26 
27 // Standard messages
28 #include <geometry_msgs/InertiaStamped.h>
29 #include <geometry_msgs/PoseStamped.h>
30 #include <geometry_msgs/TwistStamped.h>
31 
32 // Service message
33 #include <std_srvs/SetBool.h>
34 
35 // FSW actions
36 #include <ff_msgs/ControlAction.h>
37 
38 // FSW messages
39 #include <ff_msgs/ControlCommand.h>
40 #include <ff_msgs/FlightMode.h>
41 #include <ff_msgs/EkfState.h>
42 
43 // Libraries to handle flight config and segment processing
44 #include <ff_util/ff_flight.h>
45 #include <ff_util/ff_action.h>
46 #include <ff_util/ff_fsm.h>
47 #include <ff_util/perf_timer.h>
48 
49 // Libraries to read LIA config file
51 
52 // STL includes
53 #include <string>
54 #include <vector>
55 #include <mutex>
56 #include <memory>
57 
58 namespace ctl {
59 
60 using FSM = ff_util::FSM;
61 
66 class Ctl {
67  public:
68  // Declaration of all possible states
69  enum : ff_util::FSM::State {
70  WAITING = 1,
71  NOMINAL = 2,
73  };
74 
75  // Declaration of all possible events
76  enum : ff_util::FSM::Event {
77  GOAL_COMPLETE = (1<<0),
78  GOAL_NOMINAL = (1<<1),
79  GOAL_CANCEL = (1<<2),
80  GOAL_STOP = (1<<3)
81  };
82 
83  // Maximum acceptable latency
84  static constexpr double MAX_LATENCY = 0.5;
85 
89  explicit Ctl(ros::NodeHandle* nh, std::string const& name);
93  ~Ctl();
94 
95  // Terminate execution in either IDLE or STOP mode
96  FSM::State Result(int32_t response);
97 
98  // SERVICE CALLBACKS
99 
100  // Enable/Disable Onboard Controller
101  bool EnableCtl(std_srvs::SetBoolRequest&req, std_srvs::SetBoolResponse &response);
102 
103  // GENERAL MESSAGE CALLBACKS
104 
105  // Called when the internal state changes
106  void UpdateCallback(FSM::State const& state, FSM::Event const& event);
107 
108  // Called when a pose estimate is available
109  void EkfCallback(const ff_msgs::EkfState::ConstPtr& state);
110 
111  // Called when localization has a new pose data
112  void PoseCallback(const geometry_msgs::PoseStamped::ConstPtr& truth);
113 
114  // Called when localization has new twist data
115  void TwistCallback(const geometry_msgs::TwistStamped::ConstPtr& truth);
116 
117  // Called when management updates inertial info
118  void InertiaCallback(const geometry_msgs::InertiaStamped::ConstPtr& inertia);
119 
120  // Called when a timer has called back to progress control to next setpoint
121  void ControlTimerCallback(const ros::TimerEvent & event);
122 
123  // Called when the choreographer updates flight modes
124  void FlightModeCallback(const ff_msgs::FlightMode::ConstPtr& mode);
125 
126  // Command GNC directly, bypassing the action-base dinterface
127  void SetpointCallback(const ff_msgs::ControlState::ConstPtr& command);
128 
129  // Used to feed segments
130  void TimerCallback(const ros::TimerEvent& event);
131 
132  // ACTION CLIENT
133 
134  // Called when a new goal arrives
135  void GoalCallback(ff_msgs::ControlGoalConstPtr const& control_goal);
136 
137  // Called when a goal is preempted
138  void PreemptCallback();
139 
140  // Called when a goal is cancelled
141  void CancelCallback();
142 
143  // ORIGINAL GNC FUNCTIONS
144 
145  // Update control to take a new setpoint
146  bool Command(uint8_t const mode,
147  ff_msgs::ControlCommand const& poseVel = ff_msgs::ControlCommand());
148 
149  // Step control forward
150  bool Step(ros::Time curr_time);
151 
152  // Read the control parameters from the LUA config file
153  void ReadParams(void);
154 
155  // Simple extension to allow NODELET_* logging calls
156  std::string getName();
157 
158  private:
159  // Proxy to gnc
160  Control controller_;
161 
162  std::mutex mutex_cmd_msg_, mutex_segment_;
163 
164  ros::Subscriber truth_pose_sub_, inertia_sub_, flight_mode_sub_;
165  ros::Subscriber twist_sub_, pose_sub_, ekf_sub_, command_sub_;
166  ros::Publisher ctl_pub_, traj_pub_, segment_pub_, progress_pub_;
167  ros::ServiceServer enable_srv_;
168  ros::Timer timer_;
169 
171  ff_util::FSM fsm_;
172  ff_util::Segment segment_;
173  ff_util::Segment::iterator setpoint_;
174  ff_msgs::ControlFeedback feedback_;
175  ff_msgs::ControlCommand command_;
176 
178  ff_util::PerfTimer pt_ctl_;
179  ros::Timer config_timer_;
180 
181  uint8_t mode_;
182  ControlState state_;
183 
184  std::string name_;
185  bool inertia_received_;
186  bool control_enabled_;
187  bool flight_enabled_;
188  bool use_truth_;
189  float stopping_vel_thresh_squared_;
190  float stopping_omega_thresh_squared_;
191 
192  float GetCommand(ControlCommand* cmd, ros::Time tim);
193 };
194 
195 } // end namespace ctl
196 
197 #endif // CTL_CTL_ROS_H_
mode
uint8_t mode
Definition: signal_lights.h:74
ff_util::FreeFlyerActionServer< ff_msgs::ControlAction >
ctl::Ctl::Ctl
Ctl(ros::NodeHandle *nh, std::string const &name)
Definition: ctl_ros.cc:39
ctl::Ctl::PoseCallback
void PoseCallback(const geometry_msgs::PoseStamped::ConstPtr &truth)
Definition: ctl_ros.cc:270
ctl::Ctl::NOMINAL
@ NOMINAL
Definition: ctl_ros.h:71
ctl::Ctl::GoalCallback
void GoalCallback(ff_msgs::ControlGoalConstPtr const &control_goal)
Definition: ctl_ros.cc:386
ctl::Ctl::CancelCallback
void CancelCallback()
Definition: ctl_ros.cc:434
ctl::ControlState
Definition: ctl.h:37
ctl::Ctl::SetpointCallback
void SetpointCallback(const ff_msgs::ControlState::ConstPtr &command)
Definition: ctl_ros.cc:318
ctl.h
ctl::Ctl::~Ctl
~Ctl()
Definition: ctl_ros.cc:153
ctl::Ctl::EkfCallback
void EkfCallback(const ff_msgs::EkfState::ConstPtr &state)
Definition: ctl_ros.cc:235
ctl::Ctl::STOPPING
@ STOPPING
Definition: ctl_ros.h:72
ctl::Ctl::WAITING
@ WAITING
Definition: ctl_ros.h:70
perf_timer.h
ctl::Ctl::getName
std::string getName()
Definition: ctl_ros.cc:560
ctl::Ctl::GOAL_CANCEL
@ GOAL_CANCEL
Definition: ctl_ros.h:79
ff_flight.h
ff_util::PerfTimer
Definition: perf_timer.h:31
ctl::Ctl::ControlTimerCallback
void ControlTimerCallback(const ros::TimerEvent &event)
ctl::Ctl::GOAL_COMPLETE
@ GOAL_COMPLETE
Definition: ctl_ros.h:77
ff_util::Segment
std::vector< Setpoint > Segment
Definition: ff_flight.h:47
ctl::ControlCommand
Definition: ctl.h:57
ctl::Ctl::TwistCallback
void TwistCallback(const geometry_msgs::TwistStamped::ConstPtr &truth)
Definition: ctl_ros.cc:285
ctl::Ctl::PreemptCallback
void PreemptCallback()
Definition: ctl_ros.cc:440
ff_util::FSM::Event
uint32_t Event
Definition: ff_fsm.h:33
ctl::Ctl::ReadParams
void ReadParams(void)
Definition: ctl_ros.cc:563
name
std::string name
Definition: eps_simulator.cc:48
ctl
Definition: ctl.h:35
ctl::Ctl::GOAL_STOP
@ GOAL_STOP
Definition: ctl_ros.h:80
ctl::Ctl::UpdateCallback
void UpdateCallback(FSM::State const &state, FSM::Event const &event)
Definition: ctl_ros.cc:215
ctl::Ctl::TimerCallback
void TimerCallback(const ros::TimerEvent &event)
Definition: ctl_ros.cc:335
ctl::Ctl::EnableCtl
bool EnableCtl(std_srvs::SetBoolRequest &req, std_srvs::SetBoolResponse &response)
Definition: ctl_ros.cc:192
config_reader::ConfigReader
Definition: config_reader.h:48
ff_util::FSM::State
int8_t State
Definition: ff_fsm.h:32
ctl::Ctl::Step
bool Step(ros::Time curr_time)
Definition: ctl_ros.cc:472
ff_action.h
config_reader.h
ctl::Ctl::InertiaCallback
void InertiaCallback(const geometry_msgs::InertiaStamped::ConstPtr &inertia)
Definition: ctl_ros.cc:296
ctl::Ctl::Result
FSM::State Result(int32_t response)
Definition: ctl_ros.cc:155
ctl::Ctl::FlightModeCallback
void FlightModeCallback(const ff_msgs::FlightMode::ConstPtr &mode)
Definition: ctl_ros.cc:305
state
uint8_t state
Definition: signal_lights.h:90
ff_util::FSM
Definition: ff_fsm.h:30
ctl::FSM
ff_util::FSM FSM
Definition: ctl_ros.h:60
ctl::Ctl::GOAL_NOMINAL
@ GOAL_NOMINAL
Definition: ctl_ros.h:78
ctl::Control
Definition: ctl.h:95
localization_common::Time
double Time
Definition: time.h:23
ctl::Ctl
Controller implementation using GNC module.
Definition: ctl_ros.h:66
ctl::Ctl::Command
bool Command(uint8_t const mode, ff_msgs::ControlCommand const &poseVel=ff_msgs::ControlCommand())
Definition: ctl_ros.cc:446
ff_fsm.h
ctl::Ctl::MAX_LATENCY
static constexpr double MAX_LATENCY
Definition: ctl_ros.h:84