NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
ff_nodelet.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 FF_UTIL_FF_NODELET_H_
20 #define FF_UTIL_FF_NODELET_H_
21 
22 // ROS includes
23 #include <ros/ros.h>
24 #include <ros/callback_queue.h>
25 #include <nodelet/nodelet.h>
26 #include <pluginlib/class_list_macros.h>
27 #include <diagnostic_msgs/KeyValue.h>
28 
30 
31 #include <ff_msgs/Fault.h>
32 #include <ff_msgs/Heartbeat.h>
33 #include <ff_msgs/Trigger.h>
34 
35 #include <ff_util/ff_faults.h>
36 #include <ff_common/ff_names.h>
37 
38 #include <map>
39 #include <string>
40 #include <vector>
41 #include <thread>
42 
43 // Constants
44 #define DEFAULT_ACTION_WAIT_TIME 30.0
45 #define DEFAULT_SERVICE_WAIT_TIME 30.0
46 #define DEFAULT_DIAGNOSTICS_RATE 1.0
47 
48 // Logging wrappers
49 #define FF_DEBUG(msg) NODELET_DEBUG_STREAM(msg)
50 #define FF_INFO(msg) NODELET_INFO_STREAM(msg)
51 #define FF_WARN(msg) NODELET_WARN_STREAM(msg)
52 #define FF_ERROR(msg) NODELET_ERROR_STREAM(msg)
53 #define FF_FATAL(msg) NODELET_FATAL_STREAM(msg)
54 
55 namespace ff_util {
56 
57 class FreeFlyerNodelet : public nodelet::Nodelet {
58  public:
59  enum ResolveType : uint8_t {
60  NAMESPACE = 0,
61  TRANSFORM = 1,
63  };
64 
65  // Use default name from freeflyer
66  explicit FreeFlyerNodelet(bool autostart_hb_timer = true);
67  // Explicitly specift the name
68  explicit FreeFlyerNodelet(std::string const& name, bool autostart_hb_timer = true);
69  virtual ~FreeFlyerNodelet();
70 
71  void AssertFault(FaultKeys enum_key,
72  std::string const& message,
73  ros::Time time_fault_occurred = ros::Time::now());
74  void ClearAllFaults();
75  void ClearFault(FaultKeys enum_key);
76  void PrintFaults();
77 
78  // NodeHandle management
79  ros::NodeHandle* GetPlatformHandle(bool multithreaded = false);
80  ros::NodeHandle* GetPrivateHandle(bool multithreaded = false);
81 
82  // Get the name of this node (mainly useful for drivers)
83  std::string GetName();
84  std::string GetPlatform();
85  std::string GetTransform(std::string const& child);
86 
87  protected:
88  // Virtual methods that *can* be implemented by FF nodes. We don't make
89  // these mandatory, as there is already a load callback in Gazebo.
90  virtual void Initialize(ros::NodeHandle *nh) {}
91  virtual void Reset() {}
92  virtual void Sleep() {}
93  virtual void Wakeup() {}
94 
95  // Stop the heartbeat messages
96  void StopHeartbeat();
97 
98  // Diagnostic management
99  void SendDiagnostics(const std::vector<diagnostic_msgs::KeyValue> &keyval);
100 
101  // The set function does all of the internal work. We have moved this out
102  // of the onInit() call, so that it can be invoked when a nodelet is not used
103  // for example, in simulation, where the dynamic loading is within gazebo...
104  void Setup(ros::NodeHandle & nh, ros::NodeHandle & nh_mt, std::string plugin_name);
105 
106  std::map<std::string, int> faults_;
107 
108  private:
109  // Called on a heartbeat event
110  void HeartbeatCallback(ros::TimerEvent const& ev);
111 
112  // Called when nodelet should be initialized
113  void InitCallback(ros::TimerEvent const& ev);
114 
115  // Called when a trigger action is called
116  bool TriggerCallback(ff_msgs::Trigger::Request &req, ff_msgs::Trigger::Response &res);
117 
118  // Called in heartbeat callback or by nodes that do not to use the hb timer
119  void PublishHeartbeat();
120 
121  // We capture the init function and start up heartbeats, etc, then call Initialize()
122  void onInit();
123 
124  // Called in onInit to read in the config values associated with the node
125  void ReadConfig();
126 
127  // Heartbeat autostart
128  bool autostart_hb_timer_;
129  bool initialized_;
130  bool sleeping_;
131 
132  unsigned int heartbeat_queue_size_;
133 
134  config_reader::ConfigReader param_config_;
135 
136  // Heartbeat message, also used to report faults
137  ff_msgs::Heartbeat heartbeat_;
138 
139  // Node handles
140  ros::NodeHandle nh_;
141  ros::NodeHandle nh_mt_;
142  ros::NodeHandle nh_private_;
143  ros::NodeHandle nh_private_mt_;
144 
145  // Timers
146  ros::Timer timer_heartbeat_;
147  ros::Timer timer_deferred_init_;
148 
149  // Publishers
150  ros::Publisher pub_heartbeat_;
151  ros::Publisher pub_diagnostics_;
152 
153  // Reset service
154  ros::ServiceServer srv_trigger_;
155 
156  // Name and subsystem
157  std::string platform_;
158  std::string node_;
159 };
160 
161 } // namespace ff_util
162 
163 #endif // FF_UTIL_FF_NODELET_H_
ff_util::FreeFlyerNodelet::FreeFlyerNodelet
FreeFlyerNodelet(bool autostart_hb_timer=true)
Definition: ff_nodelet.cc:46
ff_util::FreeFlyerNodelet::GetTransform
std::string GetTransform(std::string const &child)
Definition: ff_nodelet.cc:434
ff_util::FreeFlyerNodelet::GetPrivateHandle
ros::NodeHandle * GetPrivateHandle(bool multithreaded=false)
Definition: ff_nodelet.cc:419
ff_util::FreeFlyerNodelet::SendDiagnostics
void SendDiagnostics(const std::vector< diagnostic_msgs::KeyValue > &keyval)
Definition: ff_nodelet.cc:377
ff_faults.h
ff_util::FreeFlyerNodelet::TRANSFORM
@ TRANSFORM
Definition: ff_nodelet.h:61
ff_util::FreeFlyerNodelet::GetPlatformHandle
ros::NodeHandle * GetPlatformHandle(bool multithreaded=false)
Definition: ff_nodelet.cc:414
ff_util::FreeFlyerNodelet::NAMESPACE
@ NAMESPACE
Definition: ff_nodelet.h:60
ff_util::FreeFlyerNodelet::~FreeFlyerNodelet
virtual ~FreeFlyerNodelet()
Definition: ff_nodelet.cc:53
ff_util::FreeFlyerNodelet::GetPlatform
std::string GetPlatform()
Definition: ff_nodelet.cc:429
name
std::string name
Definition: eps_simulator.cc:48
ff_util::FreeFlyerNodelet
Definition: ff_nodelet.h:57
ff_names.h
ff_util::FreeFlyerNodelet::GetName
std::string GetName()
Definition: ff_nodelet.cc:424
ff_util::FreeFlyerNodelet::Sleep
virtual void Sleep()
Definition: ff_nodelet.h:92
config_reader::ConfigReader
Definition: config_reader.h:48
ff_util::FreeFlyerNodelet::ClearFault
void ClearFault(FaultKeys enum_key)
Definition: ff_nodelet.cc:253
ff_util::FreeFlyerNodelet::Wakeup
virtual void Wakeup()
Definition: ff_nodelet.h:93
ff_util::FreeFlyerNodelet::PrintFaults
void PrintFaults()
Definition: ff_nodelet.cc:284
config_reader.h
ff_util::FreeFlyerNodelet::RESOURCE
@ RESOURCE
Definition: ff_nodelet.h:62
ff_util::FreeFlyerNodelet::faults_
std::map< std::string, int > faults_
Definition: ff_nodelet.h:106
ff_util::FreeFlyerNodelet::Reset
virtual void Reset()
Definition: ff_nodelet.h:91
ff_util::FreeFlyerNodelet::Setup
void Setup(ros::NodeHandle &nh, ros::NodeHandle &nh_mt, std::string plugin_name)
Definition: ff_nodelet.cc:57
ff_util::FreeFlyerNodelet::StopHeartbeat
void StopHeartbeat()
Definition: ff_nodelet.cc:293
ff_util
Definition: config_client.h:31
ff_util::FreeFlyerNodelet::ClearAllFaults
void ClearAllFaults()
Definition: ff_nodelet.cc:247
ff_util::FreeFlyerNodelet::AssertFault
void AssertFault(FaultKeys enum_key, std::string const &message, ros::Time time_fault_occurred=ros::Time::now())
Definition: ff_nodelet.cc:199
ff_util::FreeFlyerNodelet::Initialize
virtual void Initialize(ros::NodeHandle *nh)
Definition: ff_nodelet.h:90
localization_common::Time
double Time
Definition: time.h:23
ff_util::FaultKeys
FaultKeys
Definition: ff_faults.h:28
ff_util::FreeFlyerNodelet::ResolveType
ResolveType
Definition: ff_nodelet.h:59