roboteq_control.cpp
Go to the documentation of this file.
1 
31 #include <ros/ros.h>
33 #include <ros/callback_queue.h>
34 #include <signal.h>
35 
37 #include "roboteq/roboteq.h"
38 
39 #include <boost/chrono.hpp>
40 
41 using namespace std;
42 
43 typedef boost::chrono::steady_clock time_source;
44 
47 
49 
50 // >>>>> Ctrl+C handler
51 void siginthandler(int param)
52 {
53  ROS_INFO("User pressed Ctrl+C Shutting down...");
54  control_loop.stop();
55  diagnostic_loop.stop();
56  rSerial->stop();
57  ROS_INFO("Control and diagnostic loop stopped");
58  ROS_INFO_STREAM("--------- ROBOTEQ_NODE STOPPED ---------");
59  ros::shutdown();
60 
61 }
62 // <<<<< Ctrl+C handler
68  time_source::time_point &last_time)
69 {
70  // Calculate monotonic time difference
71  time_source::time_point this_time = time_source::now();
72  boost::chrono::duration<double> elapsed_duration = this_time - last_time;
73  ros::Duration elapsed(elapsed_duration.count());
74  last_time = this_time;
75 
76  //ROS_INFO_STREAM("CONTROL - running");
77  /* process control loop */
78  roboteq.read(ros::Time::now(), elapsed);
79  cm.update(ros::Time::now(), elapsed);
80  roboteq.write(ros::Time::now(), elapsed);
81 }
82 
87 {
88  //ROS_INFO_STREAM("DIAGNOSTIC - running");
89  roboteq.updateDiagnostics();
90 }
91 
92 int main(int argc, char **argv) {
93 
94  ros::init(argc, argv, "roboteq_control");
95  ros::NodeHandle nh, private_nh("~");
96 
97  signal(SIGINT, siginthandler);
98  ROS_INFO_STREAM("----------------------------------------");
99  ROS_INFO_STREAM("------------- ROBOTEQ_NODE -------------");
100  //Hardware information
101  double control_frequency, diagnostic_frequency;
102  private_nh.param<double>("control_frequency", control_frequency, 50.0);
103  private_nh.param<double>("diagnostic_frequency", diagnostic_frequency, 1.0);
104  ROS_INFO_STREAM("Control:" << control_frequency << "Hz - Diagnostic:" << diagnostic_frequency << "Hz");
105 
106  string serial_port_string;
107  int32_t baud_rate;
108 
109  private_nh.param<string>("serial_port", serial_port_string, "/dev/ttyACM0");
110  private_nh.param<int32_t>("serial_rate", baud_rate, 115200);
111  ROS_INFO_STREAM("Open Serial " << serial_port_string << ":" << baud_rate);
112 
113  rSerial = new roboteq::serial_controller(serial_port_string, baud_rate);
114  // Run the serial controller
115  bool start = rSerial->start();
116  // Check connection started
117  if(start) {
118  // Initialize roboteq controller
119  roboteq::Roboteq interface(nh, private_nh, rSerial);
120  // Initialize the motor parameters
121  interface.initialize();
122  //Initialize all interfaces and setup diagnostic messages
123  interface.initializeInterfaces();
124 
125  controller_manager::ControllerManager cm(&interface, nh);
126 
127  // Setup separate queue and single-threaded spinner to process timer callbacks
128  // that interface with RoboTeq hardware.
129  // This avoids having to lock around hardware access, but precludes realtime safety
130  // in the control loop.
131  ros::CallbackQueue unav_queue;
132  ros::AsyncSpinner unav_spinner(1, &unav_queue);
133 
134  time_source::time_point last_time = time_source::now();
135  ros::TimerOptions control_timer(
136  ros::Duration(1 / control_frequency),
137  boost::bind(controlLoop, boost::ref(interface), boost::ref(cm), boost::ref(last_time)),
138  &unav_queue);
139  // Global variable
140  control_loop = nh.createTimer(control_timer);
141 
142  ros::TimerOptions diagnostic_timer(
143  ros::Duration(1 / diagnostic_frequency),
144  boost::bind(diagnosticLoop, boost::ref(interface)),
145  &unav_queue);
146  diagnostic_loop = nh.createTimer(diagnostic_timer);
147 
148  unav_spinner.start();
149 
150  std::string name_node = ros::this_node::getName();
151  ROS_INFO("Started %s", name_node.c_str());
152 
153  // Process remainder of ROS callbacks separately, mainly ControlManager related
154  ros::spin();
155  } else {
156 
157  ROS_ERROR_STREAM("Error connection, shutting down");
158  }
159  return 0;
160 
161 }
void siginthandler(int param)
void read(const ros::Time &time, const ros::Duration &period)
Definition: roboteq.cpp:301
boost::chrono::steady_clock time_source
void stop()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL const std::string & getName()
void diagnosticLoop(roboteq::Roboteq &roboteq)
void write(const ros::Time &time, const ros::Duration &period)
Definition: roboteq.cpp:473
ROSCPP_DECL void spin(Spinner &spinner)
void updateDiagnostics()
updateDiagnostics
Definition: roboteq.cpp:264
ros::Timer diagnostic_loop
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
roboteq::serial_controller * rSerial
void controlLoop(roboteq::Roboteq &roboteq, controller_manager::ControllerManager &cm, time_source::time_point &last_time)
#define ROS_INFO_STREAM(args)
Definition: motor.h:51
void initialize()
initialize the roboteq controller
Definition: roboteq.cpp:165
static Time now()
ROSCPP_DECL void shutdown()
void initializeInterfaces(hardware_interface::JointStateInterface &joint_state_interface, hardware_interface::VelocityJointInterface &velocity_joint_interface)
initializeInterfaces Initialize all motors. Add all Control Interface availbles and add in diagnostic...
Definition: roboteq.cpp:203
#define ROS_ERROR_STREAM(args)
void update(const ros::Time &time, const ros::Duration &period, bool reset_controllers=false)
ros::Timer control_loop
int main(int argc, char **argv)
bool start()
start Initialize the serial communcation


roboteq_control
Author(s): Raffaello Bonghi
autogenerated on Wed Jan 3 2018 03:48:23