dynamic_graph_manager
dynamic_graph_manager.hh
Go to the documentation of this file.
1 
10 #ifndef DYNAMIC_GRAPH_MANAGER_HH
11 #define DYNAMIC_GRAPH_MANAGER_HH
12 
13 // used to join the different processes
14 #include <unistd.h>
15 #ifndef __APPLE__
16 #include <wait.h>
17 #endif // __APPLE__
18 
19 // use of std::bind
20 #include <functional>
21 
22 // used to synchronise the control loop
23 #include <chrono>
24 
25 // here this is used to use atomic (thread safe objects)
26 #include <atomic>
27 
28 // use to protect the add and consumption of the user commands
29 #include <mutex>
30 
31 // get the yaml configuration
32 #include "yaml_cpp_catkin/yaml_cpp_fwd.hpp"
33 
34 // ROS includes
35 #include <ros/ros.h>
36 #include <std_srvs/Empty.h>
37 
38 // used to spawn the real time thread
39 #include "real_time_tools/thread.hpp"
40 
41 // use the realtime spinner to time the loops
42 #include <real_time_tools/spinner.hpp>
43 
44 // time measurement
45 #include "real_time_tools/timer.hpp"
46 
47 // used to deal with shared memory
48 #include "shared_memory/locked_condition_variable.hpp"
49 
50 // import the python interpreter ros binding
52 
53 // some useful tools like the yaml parsing
55 
56 // the device of the dynamic-graph
58 
59 namespace dynamic_graph
60 {
65 typedef std::chrono::steady_clock clock;
66 
93 {
94  /*******************
95  * Public methods *
96  *******************/
97 public:
102 
106  virtual ~DynamicGraphManager();
107 
111  void initialize(YAML::Node param);
112 
119  void run();
120 
126 
132 
138 
144 
150  void run_python_command(std::ostream& file, const std::string& command);
151 
156  void python_prologue();
157 
163  virtual void run_dynamic_graph_process();
164 
171  virtual void run_hardware_communication_process();
172 
179  virtual void run_single_process();
180 
181  /***************************
182  * method to be overloaded *
183  ***************************/
184 
192  {
193  throw(
194  std::runtime_error(std::string("DynamicGraphManager::") +
195  "initialize_hardware_communication_process(): " +
196  "this method needs to be overloaded"));
197  }
198 
207  {
208  throw(
209  std::runtime_error("DynamicGraphManager::"
210  "get_sensors_to_map(VectorDGMap&): "
211  "this method needs to be overloaded"));
212  }
213 
222  {
223  throw(std::runtime_error(
224  "DynamicGraphManager::"
225  "set_motor_controls_from_map(const VectorDGMap&): "
226  "this method needs to be overloaded"));
227  }
228 
233  virtual void compute_safety_controls();
234 
235  /************************
236  * gettters and setters *
237  ************************/
238 
243  {
245  }
246 
251  {
253  }
254 
260  {
262  }
263 
268  {
270  }
271 
276  {
278  }
279 
285  {
287  }
288 
294  {
296  }
297 
304  {
306  }
307 
313  {
314  if (device_ != nullptr) return *device_;
315  throw(
316  std::runtime_error("DynamicGraphManager::device():Try"
317  "accessing a device that has not been created"));
318  }
319 
326 
332  virtual bool is_in_safety_mode()
333  {
334  bool too_much_missed_control =
336  static bool once_tmmc = false;
337  if (too_much_missed_control && !once_tmmc)
338  {
339  rt_printf(
340  "HARDWARE: Too much missed control (%d/%d), going in safe "
341  "mode\n",
344  once_tmmc = true;
345  }
346 
347  bool dg_died = has_dynamic_graph_process_died();
348  static bool once_hdgd = false;
349  if (dg_died && !once_hdgd)
350  {
351  rt_printf(
352  "HARDWARE: Dynamic Graph process died, going in safe mode\n");
353  once_hdgd = true;
354  }
355  return too_much_missed_control || dg_died;
356  }
357 
362  static const std::string dg_ros_node_name_;
363 
368  static const std::string hw_com_ros_node_name_;
369 
374  static const std::string shared_memory_name_;
375 
380  static const std::string sensors_map_name_;
381 
386  static const std::string motor_controls_map_name_;
387 
392  static const std::string cond_var_name_;
393 
397 protected:
401  void add_user_command(std::function<void(void)> func);
402 
406 private:
412  bool start_dynamic_graph(std_srvs::Empty::Request&,
413  std_srvs::Empty::Response&)
414  {
416  return true;
417  }
418 
424  bool stop_dynamic_graph(std_srvs::Empty::Request&,
425  std_srvs::Empty::Response&)
426  {
428  return true;
429  }
430 
435  void start_ros_service(ros::NodeHandle& ros_node_handle);
436 
442 
449  static void* dynamic_graph_real_time_loop_helper(void* context)
450  {
451  return static_cast<DynamicGraphManager*>(context)
453  }
454 
461 
469  {
470  return static_cast<DynamicGraphManager*>(context)
472  }
473 
479 
486  static void* single_process_real_time_loop_helper(void* context)
487  {
488  return static_cast<DynamicGraphManager*>(context)
490  }
491 
492  /***********************
493  * Private attributes *
494  ***********************/
495 protected:
501  ros::ServiceServer ros_service_start_dg_;
502 
508  ros::ServiceServer ros_service_stop_dg_;
509 
517  std::atomic<bool> is_dynamic_graph_stopped_;
518 
527 
532  std::unique_ptr<dynamic_graph::RosPythonInterpreter>
534 
539  std::unique_ptr<real_time_tools::RealTimeThread> thread_dynamic_graph_;
540 
545  std::unique_ptr<real_time_tools::RealTimeThread>
547 
553 
560 
565  std::unique_ptr<Device> device_;
566 
572 
578 
583  std::unique_ptr<shared_memory::LockedConditionVariable> cond_var_;
584 
591 
597 
603 
607  clock::duration control_period_;
608 
613  clock::time_point hw_time_loop_before_sleep_;
614 
619  clock::time_point hw_time_loop_after_sleep_;
620 
625  clock::duration hw_meas_sleep_time_;
626 
631  clock::duration hw_ref_sleep_time_;
632 
637  clock::duration hw_meas_active_time_;
638 
644 
651 
656  std::string dg_sleep_timer_file_;
657 
662  std::string dg_timer_file_;
663 
670 
676 
682  std::string hwc_timer_file_;
683 
688  std::string log_dir_;
689 
693  std::string python_log_file_;
694 
698  std::string app_dir_;
699 
704  real_time_tools::Timer dg_active_timer_;
705 
710  real_time_tools::Timer dg_sleep_timer_;
711 
716  real_time_tools::Timer dg_timer_;
717 
722  real_time_tools::Timer hwc_active_timer_;
723 
728  real_time_tools::Timer hwc_sleep_timer_;
729 
733  real_time_tools::Timer hwc_timer_;
734 
740 
745  real_time_tools::Spinner hwc_spinner_;
746 
753 
758 
762  std::deque<std::function<void(void)> > user_commands_;
763 
767 protected:
772  std::deque<ros::ServiceServer> ros_user_commands_;
773 
779 
783  YAML::Node params_;
784 
785  std::mutex hwc_mutex_;
786 };
787 
788 } // namespace dynamic_graph
789 
790 #endif // DYNAMIC_GRAPH_MANAGER_HH
clock::time_point hw_time_loop_before_sleep_
hw_time_loop_before_sleep_ is the time measurement just before the hardware communication loop goes t...
Definition: dynamic_graph_manager.hh:613
Device & device()
device is a getter method on the Device internal pointer.
Definition: dynamic_graph_manager.hh:312
YAML::Node params_
params_ is the pool of parameters in a yaml tree
Definition: dynamic_graph_manager.hh:783
virtual void run_hardware_communication_process()
run_hardware_communication_process spawns the real time thread.
Definition: dynamic_graph_manager.cpp:431
void wait_stop_dynamic_graph()
wait_stop_dynamic_graph put the current thread to sleep until the user stop the dynamic graph ...
Definition: dynamic_graph_manager.cpp:273
virtual void get_sensors_to_map(VectorDGMap &)
get_sensors_to_map is the fonction that get the motor command from a map and that uses the drivers to...
Definition: dynamic_graph_manager.hh:206
std::deque< ros::ServiceServer > ros_user_commands_
Attribute shared with the daughter class.
Definition: dynamic_graph_manager.hh:772
std::string dg_active_timer_file_
dg_active_timer_file_ this is the path to the file that will contain the computation time of each of ...
Definition: dynamic_graph_manager.hh:650
unsigned max_missed_control_
max_missed_control_ if the missed_control_count_ reach the value of max_missed_control_ then we switc...
Definition: dynamic_graph_manager.hh:602
static const std::string dg_ros_node_name_
dg_ros_node_name_ this is the ros node name of the dynamic graph process
Definition: dynamic_graph_manager.hh:362
std::unique_ptr< real_time_tools::RealTimeThread > thread_hardware_communication_
thread_hardware_communication_ is the real thread that communicate with the hardware.
Definition: dynamic_graph_manager.hh:546
void * hardware_communication_real_time_loop()
hardware_communication_real_time_loop is the method that communicate with the hardware and send the c...
Definition: dynamic_graph_manager.cpp:541
ros::ServiceServer ros_service_start_dg_
ros_service_start_dg_ allows to start the dynamic graph on call.
Definition: dynamic_graph_manager.hh:501
virtual void run_single_process()
run_single_process spawns the real time thread.
Definition: dynamic_graph_manager.cpp:455
void python_prologue()
python_prologue get the pointer of the device in the the python interpretor.
Definition: dynamic_graph_manager.cpp:380
virtual void set_motor_controls_from_map(const VectorDGMap &)
set_motor_controls_from_map is the fonction that get the motor command from a map and that uses the d...
Definition: dynamic_graph_manager.hh:221
clock::duration hw_meas_active_time_
hw_meas_active_time_ is the time during which the hardware communication process is supposed to sleep...
Definition: dynamic_graph_manager.hh:637
real_time_tools::Timer dg_timer_
dg_timer_ is the timer measuring the duration time of the dynamic graph loop.
Definition: dynamic_graph_manager.hh:716
std::unique_ptr< Device > device_
device_ is the DynamicGraph device that manages the computation of the graph.
Definition: dynamic_graph_manager.hh:565
clock::time_point hw_time_loop_after_sleep_
hw_time_loop_after_sleep_ is the time measurement just after the hardware communication loop goes to ...
Definition: dynamic_graph_manager.hh:619
std::string hwc_timer_file_
hwc_timer_file_ this is the path to the file that will contain the computation time of each of the ha...
Definition: dynamic_graph_manager.hh:682
double maximum_time_for_user_cmd_
This the duration during which a user command can be executed.
Definition: dynamic_graph_manager.hh:757
bool is_dynamic_graph_stopped()
get the status of the dynamic graph (is running or not)
Definition: dynamic_graph_manager.hh:259
void initialize_dynamic_graph_process()
initialize_dynamic_graph_process instanciates all variables related to the dynamic_graph and user int...
Definition: dynamic_graph_manager.cpp:330
std::string hwc_active_timer_file_
hwc_active_timer_file_ this is the path to the file that will contain the computation time of each ac...
Definition: dynamic_graph_manager.hh:669
bool has_been_waken_by_dg_
has_been_waken_by_dg_ is a flag that indicates if the hardware communication process has been awaken ...
Definition: dynamic_graph_manager.hh:590
std::atomic< bool > is_dynamic_graph_stopped_
is_dynamic_graph_stopped_ is the flag reflecting the state of the dynamic graph.
Definition: dynamic_graph_manager.hh:517
static const std::string hw_com_ros_node_name_
hw_com_ros_node_name_ this is the ros node name of the harware communication process ...
Definition: dynamic_graph_manager.hh:368
real_time_tools::Timer hwc_active_timer_
hwc_active_timer is measuring the active time of the hardware communication loop
Definition: dynamic_graph_manager.hh:722
std::unique_ptr< real_time_tools::RealTimeThread > thread_dynamic_graph_
thread_dynamic_graph_ is the real time thread that runs the dynamic graph.
Definition: dynamic_graph_manager.hh:539
pid_t pid_hardware_communication_process_
pid_hardware_communication_process_ is the pid of the hardware communication process.
Definition: dynamic_graph_manager.hh:559
bool start_dynamic_graph(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
Method NOT inherited.
Definition: dynamic_graph_manager.hh:412
virtual void run_dynamic_graph_process()
run_dynamic_graph_process spawns the real time thread and becomes a ros spinner (thread in charge of ...
Definition: dynamic_graph_manager.cpp:411
bool stop_dynamic_graph(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
stop_dg is the callback method of the ROS service stop dynamic graph
Definition: dynamic_graph_manager.hh:424
Definition: device.hh:29
clock::duration hw_meas_sleep_time_
hw_measured_sleep_time_ is the time during which the hardware communication process actually slept...
Definition: dynamic_graph_manager.hh:625
static const std::string cond_var_name_
cond_var_sensors_name_ is the name of the condition variable in the shared memory ...
Definition: dynamic_graph_manager.hh:392
void * dynamic_graph_real_time_loop()
dynamic_graph_real_time_loop is the method used to execute the dynamic graph.
Definition: dynamic_graph_manager.cpp:479
std::map< std::string, dynamicgraph::Vector > VectorDGMap
VectorDGMap is a shortcut for the very long type of the map.
Definition: tools.hh:22
double control_period_sec_
control_period_sec_ this is the control period in Seconds (S.I.
Definition: dynamic_graph_manager.hh:778
unsigned memory_buffer_timers_
memory_buffer_timers_ is the size of the memory buffers for the real_time_tools timers.
Definition: dynamic_graph_manager.hh:739
void stop_hardware_communication()
stop_hardware_communication stops the hardware communication.
Definition: dynamic_graph_manager.hh:267
virtual void compute_safety_controls()
compute_safety_controls computes safety controls very fast in case the dynamic graph is taking to muc...
Definition: dynamic_graph_manager.cpp:685
std::chrono::steady_clock clock
clock is the std::chrono::high_resolution_clock object.
Definition: dynamic_graph_manager.hh:65
this is this package namespace in order to avoid naming conflict
Definition: device.hh:22
real_time_tools::Timer hwc_sleep_timer_
hwc_sleep_timer is measuring the sleeping time of the hardware communication loop ...
Definition: dynamic_graph_manager.hh:728
std::string python_log_file_
This file will contain the python interpreter output.
Definition: dynamic_graph_manager.hh:693
virtual void initialize_hardware_communication_process()
initialize_hardware_communication_process instanciate all variables related to the hardware communica...
Definition: dynamic_graph_manager.hh:191
std::deque< std::function< void(void)> > user_commands_
This is the list of the user commands.
Definition: dynamic_graph_manager.hh:762
std::string log_dir_
log_folder_ is the folder where all the data of the current experiment will be saved.
Definition: dynamic_graph_manager.hh:688
real_time_tools::Timer dg_active_timer_
dg_active_timer_ is the timer measuring the computation time of the dynamic graph loop...
Definition: dynamic_graph_manager.hh:704
This class has for purpose to manage the different processes during run time.
Definition: dynamic_graph_manager.hh:92
std::string dg_sleep_timer_file_
dg_sleep_timer_file_ this is the path to the file that will contain the sleep duration of the dynamic...
Definition: dynamic_graph_manager.hh:656
std::unique_ptr< shared_memory::LockedConditionVariable > cond_var_
cond_var_sensors_ this condition variable allow the computation of the dynamic graph just after the a...
Definition: dynamic_graph_manager.hh:583
clock::duration hw_ref_sleep_time_
hw_ref_sleep_time_ is the time during which the hardware communication process is supposed to sleep...
Definition: dynamic_graph_manager.hh:631
unsigned missed_control_count_
missed_control_count_ is counting the number of iteration when the dynamic_graph failed to provide da...
Definition: dynamic_graph_manager.hh:596
static void * single_process_real_time_loop_helper(void *context)
dynamic_graph_real_time_loop_helper is a static member allowing to use the posix pthread_create.
Definition: dynamic_graph_manager.hh:486
static const std::string motor_controls_map_name_
motor_controls_map_name is the name of the motor controls map inside the shared memory segment ...
Definition: dynamic_graph_manager.hh:386
bool is_real_robot_
is_real_robot this boolean is a parameter to indicate if yes or no we are in simulation or in a real ...
Definition: dynamic_graph_manager.hh:643
ros::ServiceServer ros_service_stop_dg_
ros_service_stop_dg_ allows to stop the dynamic graph on call.
Definition: dynamic_graph_manager.hh:508
real_time_tools::Timer dg_sleep_timer_
dg_sleep_timer_ is the timer measuring the time during which the dynamic graph loop sleeps...
Definition: dynamic_graph_manager.hh:710
VectorDGMap motor_controls_map_
motor_controls_map_ is a map of dynamicgraph::Vector.
Definition: dynamic_graph_manager.hh:577
virtual ~DynamicGraphManager()
DynamicGraphManager, destructor of the class.
Definition: dynamic_graph_manager.cpp:69
real_time_tools::Spinner hwc_spinner_
This class allows us to time the real time thread for the hardware communication. ...
Definition: dynamic_graph_manager.hh:745
static const std::string shared_memory_name_
shared_memory_name is the name of the shared memory segment to be used
Definition: dynamic_graph_manager.hh:374
pid_t pid_dynamic_graph_process()
pid_dynamic_graph_process is an accessor on the pid of the process
Definition: dynamic_graph_manager.hh:293
clock::duration control_period_
control_period_ this is the control period in nanoseconds.
Definition: dynamic_graph_manager.hh:607
real_time_tools::Timer hwc_timer_
hwc_timer is measuring the time of the hardware communication loop
Definition: dynamic_graph_manager.hh:733
std::string dg_timer_file_
dg_timer_file_ this is the path to the file that will contain the time of the dynamic graph loop...
Definition: dynamic_graph_manager.hh:662
pid_t pid_hardware_communication_process()
pid_hardware_communication_process is an accessor on the pid of the process
Definition: dynamic_graph_manager.hh:303
void run_python_command(std::ostream &file, const std::string &command)
run_python_command
Definition: dynamic_graph_manager.cpp:359
std::string app_dir_
This is the application directory in the home directory.
Definition: dynamic_graph_manager.hh:698
double hwc_predicted_sleeping_time_
This corresponds to the predicted sleeping time for the hardware communication process.
Definition: dynamic_graph_manager.hh:752
void start_hardware_communication()
start_hardware_communication starts the hardware communication.
Definition: dynamic_graph_manager.hh:275
void run()
run() splits the process in the dynamic_graph process and the hadware_communication process...
Definition: dynamic_graph_manager.cpp:221
DynamicGraphManager()
DynamicGraphManager, constructor of the class.
Definition: dynamic_graph_manager.cpp:34
pid_t pid_dynamic_graph_process_
pid_dynamic_graph_process_ is the pid of the DynamicGraph process.
Definition: dynamic_graph_manager.hh:552
static void * dynamic_graph_real_time_loop_helper(void *context)
dynamic_graph_real_time_loop_helper is a static member allowing to use the posix pthread_create.
Definition: dynamic_graph_manager.hh:449
std::atomic< bool > is_hardware_communication_stopped_
is_hardware_communication_stopped_ is the flag reflecting the state of the hardware communication thr...
Definition: dynamic_graph_manager.hh:526
void initialize(YAML::Node param)
initialize the basic variables
Definition: dynamic_graph_manager.cpp:104
void start_ros_service(ros::NodeHandle &ros_node_handle)
start_ros_service is the method that advertise the different ros services.
Definition: dynamic_graph_manager.cpp:468
std::unique_ptr< dynamic_graph::RosPythonInterpreter > ros_python_interpreter_
ros_python_interpreter_ptr_ is a ROS wrapper around a python interpreter.
Definition: dynamic_graph_manager.hh:533
void add_user_command(std::function< void(void)> func)
Method inherited.
Definition: dynamic_graph_manager.cpp:722
static const std::string sensors_map_name_
sensors_map_name is the name of the sensor map inside the shared memory segment
Definition: dynamic_graph_manager.hh:380
virtual bool is_in_safety_mode()
is_in_safety_mode check if the dynamic graph is still alive and sending commands at a descent frequen...
Definition: dynamic_graph_manager.hh:332
void wait_stop_hardware_communication()
wait_stop_hardware_communication put the current thread to sleep until the user stop the hardware com...
Definition: dynamic_graph_manager.cpp:288
void stop_dynamic_graph()
stop_dynamic_graph stop the DynamicGraph.
Definition: dynamic_graph_manager.hh:242
void * single_process_real_time_loop()
single_process_real_time_loop is the method that performs the control but in one single process...
Definition: dynamic_graph_manager.cpp:696
void start_dynamic_graph()
start_dynamic_graph start the DynamicGraph.
Definition: dynamic_graph_manager.hh:250
static void * hardware_communication_real_time_loop_helper(void *context)
dynamic_graph_real_time_loop_helper is a static member allowing to use the posix pthread_create.
Definition: dynamic_graph_manager.hh:468
std::string hwc_sleep_timer_file_
hwc_sleep_timer_file_ this is the path to the file that will contain the sleeping time of the hardwar...
Definition: dynamic_graph_manager.hh:675
VectorDGMap sensors_map_
sensors_map_ is a map of dynamicgraph::Vector.
Definition: dynamic_graph_manager.hh:571
void wait_start_dynamic_graph()
wait_start_dynamic_graph put the current thread to sleep until the user start the dynamic graph ...
Definition: dynamic_graph_manager.cpp:264
bool is_hardware_communication_stopped()
get the status of the hardware communication (is running or not).
Definition: dynamic_graph_manager.hh:284
bool has_dynamic_graph_process_died()
has_dynamic_graph_process_died check if the process of the DynamicGraph has died or not...
Definition: dynamic_graph_manager.cpp:303