romeo.cpp
Go to the documentation of this file.
1 
23 #include <iostream>
24 #include "romeo_dcm_driver/romeo.h"
25 #include <alerror/alerror.h>
26 #include <alcommon/albroker.h>
27 #include <sensor_msgs/JointState.h>
28 #include <algorithm>
29 
30 #include <controller_manager_msgs/ControllerState.h>
31 
32 Romeo::Romeo(boost::shared_ptr<AL::ALBroker> broker, const string &name)
33  : AL::ALModule(broker,name),is_connected_(false)
34 {
35  setModuleDescription("Romeo Robot Module");
36 
37  functionName("brokerDisconnected", getName(), "Callback when broker disconnects!");
38  BIND_METHOD(Romeo::brokerDisconnected);
39 }
40 
42 {
43  if(is_connected_)
44  disconnect();
45 }
46 
48 {
49 
50  //Romeo Joints Initialization
51  const char* joint[] = { /*"NeckYaw",
52  "NeckPitch",
53  "HeadPitch",
54  "HeadRoll",*/
55  "LShoulderPitch",
56  "LShoulderYaw",
57  "LElbowRoll",
58  "LElbowYaw",
59  "LWristRoll",
60  "LWristYaw",
61  "LWristPitch",
62  "LHand",
63  /*"TrunkYaw",
64  "LHipYaw",
65  "LHipRoll",
66  "LHipPitch",
67  "LKneePitch",
68  "LAnklePitch",
69  "LAnkleRoll",
70  "RHipYaw",
71  "RHipRoll",
72  "RHipPitch",
73  "RKneePitch",
74  "RAnklePitch",
75  "RAnkleRoll",*/
76  "RShoulderPitch",
77  "RShoulderYaw",
78  "RElbowRoll",
79  "RElbowYaw",
80  "RWristRoll",
81  "RWristYaw",
82  "RWristPitch",
83  "RHand" };
84  joint_names_ = vector<string>(joint, end(joint));
85 
86  for(vector<string>::iterator it=joint_names_.begin();it!=joint_names_.end();it++)
87  {
88  joints_names_.push_back("Device/SubDeviceList/"+(*it)+"/Position/Sensor/Value");
89  }
91 
92  // DCM Motion Commands Initialization
93  try
94  {
95  // Create Motion Command
96  commands_.arraySetSize(4);
97  commands_[0] = string("Joints");
98  commands_[1] = string("ClearAll");
99  commands_[2] = string("time-mixed");
100  commands_[3].arraySetSize(number_of_joints_);
101 
102  // Create Joints Actuators Alias
103  AL::ALValue commandAlias;
104  commandAlias.arraySetSize(2);
105  commandAlias[0] = string("Joints");
106  commandAlias[1].arraySetSize(number_of_joints_);
107  for(int i=0;i<number_of_joints_;i++)
108  {
109  commandAlias[1][i] = string("Device/SubDeviceList/"+joint_names_[i]+"/Position/Actuator/Value");
110  commands_[3][i].arraySetSize(1);
111  commands_[3][i][0].arraySetSize(2);
112  }
113  dcm_proxy_->callVoid("createAlias",commandAlias);
114 
115  // Create Joints Hardness Alias
116  commandAlias[0] = string("JointsHardness");
117  commandAlias[1].arraySetSize(number_of_joints_/*-1*/);
118  for(int i=0;i<number_of_joints_;i++)
119  {
120  commandAlias[1][i] = string("Device/SubDeviceList/"+joint_names_[i]+"/Hardness/Actuator/Value");
121  }
122  dcm_proxy_->callVoid("createAlias",commandAlias);
123 
124  }
125  catch(const AL::ALError& e)
126  {
127  ROS_ERROR("Could not initialize dcm aliases!\n\tTrace: %s",e.what());
128  return false;
129  }
130 
131  stiffnesses_enabled_ = true;
132 
133  m_jointState.name = m_motionProxy->getBodyNames("Body");
134 
135  std::stringstream ss;
136  std::copy(m_jointState.name.begin(), m_jointState.name.end()-1, std::ostream_iterator<std::string>(ss,","));
137  std::copy(m_jointState.name.end()-1, m_jointState.name.end(), std::ostream_iterator<std::string>(ss));
138  ROS_INFO("Romeo joints found: %s",ss.str().c_str());
139 
140  return true;
141 }
142 
144 {
145  if(!initialize())
146  {
147  ROS_ERROR("Initialization method failed!");
148  return false;
149  }
150 
151  // Initialize Controllers' Interfaces
156 
157  try
158  {
159  for(int i=0;i<number_of_joints_;i++)
160  {
163  jnt_state_interface_.registerHandle(state_handle);
164 
166  &joint_commands_[i]);
168  }
169 
172  }
173  catch(const ros::Exception& e)
174  {
175  ROS_ERROR("Could not initialize hardware interfaces!\n\tTrace: %s",e.what());
176  return false;
177  }
178  ROS_INFO("Romeo Module initialized!");
179  return true;
180 }
181 
183 {
184  // Initialize ROS nodes
185  node_handle_ = nh;
186 
187  is_connected_ = false;
188 
189  // Load ROS Parameters
190  loadParams();
191 
192  // Needed for Error Checking
193  try
194  {
195  subscribeToMicroEvent("ClientDisconnected", "Romeo", "brokerDisconnected");
196  }
197  catch (const AL::ALError& e)
198  {
199  ROS_ERROR("Could not subscribe to brokerDisconnected!\n\tTrace: %s",e.what());
200  }
201 
202  // Initialize DCM_motion Proxy
203  try
204  {
205  dcm_proxy_ = boost::shared_ptr<AL::ALProxy>(new AL::ALProxy(getParentBroker(), "DCM_motion"));
206 
207  }
208  catch (const AL::ALError& e)
209  {
210  ROS_ERROR("Failed to connect to DCM Proxy!\n\tTrace: %s",e.what());
211  return false;
212  }
213 
214  // Initialize Memory Proxy
215  try
216  {
217  memory_proxy_ = AL::ALMemoryProxy(getParentBroker());
218  }
219  catch (const AL::ALError& e)
220  {
221  ROS_ERROR("Failed to connect to Memory Proxy!\n\tTrace: %s",e.what());
222  return false;
223  }
224  try
225  {
226  m_motionProxy = boost::shared_ptr<AL::ALMotionProxy>(new AL::ALMotionProxy(getParentBroker()));
227  }
228  catch (const AL::ALError& e)
229  {
230  ROS_ERROR("Could not create ALMotionProxy.");
231  return false;
232  }
233 
234  is_connected_ = true;
235 
236  // Subscribe/Publish ROS Topics/Services
237  subscribe();
238 
239  // Initialize Controller Manager and Controllers
242  {
243  ROS_ERROR("Could not load controllers!");
244  return false;
245  }
246  ROS_INFO("Controllers successfully loaded!");
247  return true;
248 }
249 
251 {
252  if(!is_connected_)
253  return;
254  try
255  {
256  unsubscribeFromMicroEvent("ClientDisconnected", "Romeo");
257  }
258  catch (const AL::ALError& e)
259  {
260  ROS_ERROR("Failed to unsubscribe from subscribed events!\n\tTrace: %s",e.what());
261  }
262  is_connected_ = false;
263 }
264 
266 {
267  // Subscribe/Publish ROS Topics/Services
269 
270  stiffness_pub_ = node_handle_.advertise<std_msgs::Float32>(prefix_+"stiffnesses", topic_queue_);
271  stiffness_.data = 1.0f;
272 /*
273  //ROS service to enable robot's stiffness, currently not used.
274  stiffness_switch_ = node_handle_.advertiseService<Romeo, romeo_dcm_msgs::BoolService::Request,
275  romeo_dcm_msgs::BoolService::Response>(prefix_+"Stiffnesses/Enable", &Romeo::switchStiffnesses, this);
276 */
277  m_jointStatePub = node_handle_.advertise<sensor_msgs::JointState>("joint_states",5);
278 }
279 
281 {
282  ros::NodeHandle n_p("~");
283  // Load Server Parameters
284  n_p.param("Version", version_, string("V4"));
285  n_p.param("BodyType", body_type_, string("H21"));
286 
287  n_p.param("TopicQueue", topic_queue_, 50);
288 
289  n_p.param("Prefix", prefix_, string("romeo_dcm"));
290  prefix_ = prefix_+"/";
291 
292  n_p.param("LowCommunicationFrequency", low_freq_, 10.0);
293  n_p.param("HighCommunicationFrequency", high_freq_, 10.0);
294  n_p.param("ControllerFrequency", controller_freq_, 15.0);
295  n_p.param("JointPrecision", joint_precision_, 0.0174532925);
296  n_p.param("OdomFrame", odom_frame_, string("odom"));
297 }
298 
299 void Romeo::brokerDisconnected(const string& event_name, const string &broker_name, const string& subscriber_identifier)
300 {
301  if(broker_name == "Romeo Driver Broker")
302  is_connected_ = false;
303 }
304 
305 void Romeo::DCMTimedCommand(const string &key, const AL::ALValue &value, const int &timeOffset, const string &type)
306 {
307  try
308  {
309  // Create timed-command
310  AL::ALValue command;
311  command.arraySetSize(3);
312  command[0] = key;
313  command[1] = type;
314  command[2].arraySetSize(1);
315  command[2][0].arraySetSize(2);
316  command[2][0][0] = value;
317  command[2][0][1] = dcm_proxy_->call<int>("getTime",timeOffset);
318 
319  // Execute timed-command
320  dcm_proxy_->callVoid("set",command);
321  }
322  catch(const AL::ALError& e)
323  {
324  ROS_ERROR("Could not execute DCM timed-command!\n\t%s\n\n\tTrace: %s", key.c_str(), e.what());
325  }
326 }
327 
328 void Romeo::DCMAliasTimedCommand(const string &alias, const vector<float> &values, const vector<int> &timeOffsets,
329  const string &type, const string &type2)
330 {
331  try
332  {
333  // Create Alias timed-command
334  AL::ALValue command;
335  command.arraySetSize(4);
336  command[0] = alias;
337  command[1] = type;
338  command[2] = type2;
339  command[3].arraySetSize(values.size());
340  int T = dcm_proxy_->call<int>("getTime",0);
341  for(int i=0;i<values.size();i++)
342  {
343  command[3][i].arraySetSize(1);
344  command[3][i][0].arraySetSize(2);
345  command[3][i][0][0] = values[i];
346  command[3][i][0][1] = T+timeOffsets[i];
347  }
348 
349  // Execute Alias timed-command
350  dcm_proxy_->callVoid("setAlias",command);
351  }
352  catch(const AL::ALError& e)
353  {
354  ROS_ERROR("Could not execute DCM timed-command!\n\t%s\n\n\tTrace: %s", alias.c_str(), e.what());
355  }
356 }
357 
358 void Romeo::insertDataToMemory(const string &key, const AL::ALValue &value)
359 {
360  memory_proxy_.insertData(key,value);
361 }
362 
363 AL::ALValue Romeo::getDataFromMemory(const string &key)
364 {
365  return memory_proxy_.getData(key);
366 }
367 
368 void Romeo::subscribeToEvent(const string &name, const string &callback_module, const string &callback_method)
369 {
370  try
371  {
372  memory_proxy_.subscribeToEvent(name,callback_module,"",callback_method);
373  }
374  catch(const AL::ALError& e)
375  {
376  ROS_ERROR("Could not subscribe to event '%s'.\n\tTrace: %s",name.c_str(),e.what());
377  }
378 }
379 
380 void Romeo::subscribeToMicroEvent(const string &name, const string &callback_module,
381  const string &callback_method, const string &callback_message)
382 {
383  try
384  {
385  memory_proxy_.subscribeToMicroEvent(name,callback_module,callback_message,callback_method);
386  }
387  catch(const AL::ALError& e)
388  {
389  ROS_ERROR("Could not subscribe to micro-event '%s'.\n\tTrace: %s",name.c_str(),e.what());
390  }
391 }
392 
393 void Romeo::unsubscribeFromEvent(const string &name, const string &callback_module)
394 {
395  try
396  {
397  memory_proxy_.unsubscribeToEvent(name,callback_module);
398  }
399  catch(const AL::ALError& e)
400  {
401  ROS_ERROR("Could not unsubscribe from event '%s'.\n\tTrace: %s",name.c_str(),e.what());
402  }
403 }
404 
405 void Romeo::unsubscribeFromMicroEvent(const string &name, const string &callback_module)
406 {
407  try
408  {
409  memory_proxy_.unsubscribeToMicroEvent(name,callback_module);
410  }
411  catch(const AL::ALError& e)
412  {
413  ROS_ERROR("Could not unsubscribe from micro-event '%s'.\n\tTrace: %s",name.c_str(),e.what());
414  }
415 }
416 
417 void Romeo::raiseEvent(const string &name, const AL::ALValue &value)
418 {
419  try
420  {
421  memory_proxy_.raiseEvent(name,value);
422  }
423  catch(const AL::ALError& e)
424  {
425  ROS_ERROR("Could not raise event '%s'.\n\tTrace: %s",name.c_str(),e.what());
426  }
427 }
428 
429 void Romeo::raiseMicroEvent(const string &name, const AL::ALValue &value)
430 {
431  try
432  {
433  memory_proxy_.raiseMicroEvent(name,value);
434  }
435  catch(const AL::ALError& e)
436  {
437  ROS_ERROR("Could not raise micro-event '%s'.\n\tTrace: %s",name.c_str(),e.what());
438  }
439 }
440 
441 void Romeo::declareEvent(const string &name)
442 {
443  try
444  {
445  memory_proxy_.declareEvent(name);
446  }
447  catch(AL::ALError& e)
448  {
449  ROS_ERROR("Could not declare event '%s'.\n\tTrace: %s",name.c_str(),e.what());
450  }
451 }
452 
454 {
455  boost::thread t1(&Romeo::controllerLoop,this);
456 // boost::thread t2(&Romeo::lowCommunicationLoop,this);
457 // boost::thread t3(&Romeo::highCommunicationLoop,this);
458  t1.join();
459 // t2.join();
460 // t3.join();
461 }
462 
464 {
465  static ros::Rate rate(low_freq_);
466  while(ros::ok())
467  {
468  ros::Time time = ros::Time::now();
469 
470  if(!is_connected_)
471  break;
472 
474  {
475  stiffness_.data = 1.0f;
476  }
477  else
478  {
479  stiffness_.data = 0.0f;
480  }
482 
483  rate.sleep();
484  }
485 }
486 
488 {
489  static ros::Rate rate(high_freq_);
490  while(ros::ok())
491  {
492  ros::Time time = ros::Time::now();
493 
494  if(!is_connected_)
495  break;
496 
497  //You can add publisher for sensors value here
498 
499  try
500  {
501  dcm_proxy_->callVoid("ping");
502  }
503  catch(const AL::ALError& e)
504  {
505  ROS_ERROR("Could not ping DCM proxy.\n\tTrace: %s",e.what());
506  is_connected_ = false;
507  }
508  rate.sleep();
509  }
510 }
511 
513 {
514  static ros::Rate rate(controller_freq_+10.0);
515  while(ros::ok())
516  {
517 
518 
519  ros::Time time = ros::Time::now();
520 
521  if(!is_connected_)
522  break;
523 
524  //You can add publisher for sensors value here
525 
526  try
527  {
528  dcm_proxy_->callVoid("ping");
529  }
530  catch(const AL::ALError& e)
531  {
532  ROS_ERROR("Could not ping DCM proxy.\n\tTrace: %s",e.what());
533  is_connected_ = false;
534  rate.sleep();
535  continue;
536  }
537 
538  readJoints();
539 
541 
542  writeJoints();
543 
545 
546  rate.sleep();
547  }
548 }
549 
551 {
552  return is_connected_;
553 }
554 
555 void Romeo::commandVelocity(const geometry_msgs::TwistConstPtr &msg)
556 {
557  ROS_WARN("This function does nothing at the moment..");
558 }
559 
561 {
562  vector<float> jointData;
563  try
564  {
565  jointData = memory_proxy_.getListData(joints_names_);
566  }
567  catch(const AL::ALError& e)
568  {
569  ROS_ERROR("Could not get joint data from Romeo.\n\tTrace: %s",e.what());
570  return;
571  }
572 
573  for(short i = 0; i<jointData.size(); i++)
574  {
575  joint_angles_[i] = jointData[i];
576  // Set commands to the read angles for when no command specified
577  joint_commands_[i] = jointData[i];
578  }
579 
580 
581 }
582 
584  std::vector<float> positionData;
585  positionData = m_motionProxy->getAngles("Body", true);
586  m_jointState.header.stamp = ros::Time::now();
587  m_jointState.header.frame_id = "base_link";
588  m_jointState.position.resize(positionData.size());
589  for(unsigned i = 0; i<positionData.size(); ++i)
590  {
591  m_jointState.position[i] = positionData[i];
592  }
593 
595 }
596 
598 {
599  // Update joints only when actual command is issued
600  bool changed = false;
601  for(int i=0;i<number_of_joints_;i++)
602  {
604  {
605  changed = true;
606  break;
607  }
608  }
609  // Do not write joints if no change in joint values
610  if(!changed)
611  {
612  return;
613  }
614 
615  try
616  {
617  int T = dcm_proxy_->call<int>("getTime",0);
618  for(int i=0;i<number_of_joints_;i++)
619  {
620  commands_[3][i][0][0] = float(joint_commands_[i]);
621  commands_[3][i][0][1] = T+(int)(800.0f/controller_freq_);
622  //ROS_INFO(<<i<<"\t"<<commands_[3][i][0][0]<<"\n");
623  }
624 
625  dcm_proxy_->callVoid("setAlias",commands_);
626  }
627  catch(const AL::ALError& e)
628  {
629  ROS_ERROR("Could not send joint commands to Romeo.\n\tTrace: %s",e.what());
630  return;
631  }
632 }
633 
634 
635 bool Romeo::switchStiffnesses(romeo_dcm_msgs::BoolService::Request &req, romeo_dcm_msgs::BoolService::Response &res)
636 {
637  if(stiffnesses_enabled_!=req.enable && req.enable)
638  {
639  DCMAliasTimedCommand("JointsHardness",vector<float>(number_of_joints_,1.0f), vector<int>(number_of_joints_,0));
640  }
641  else if(stiffnesses_enabled_!=req.enable)
642  {
643  DCMAliasTimedCommand("JointsHardness",vector<float>(number_of_joints_,0.0f), vector<int>(number_of_joints_,0));
644  }
645  stiffnesses_enabled_ = req.enable;
646 }
647 
vector< string > joint_names_
Definition: romeo.h:130
hardware_interface::JointStateInterface jnt_state_interface_
Definition: romeo.h:126
controller_manager::ControllerManager * manager_
Definition: romeo.h:99
string odom_frame_
Definition: romeo.h:114
double low_freq_
Definition: romeo.h:115
void publishJointStateFromAlMotion()
Definition: romeo.cpp:583
boost::shared_ptr< AL::ALProxy > dcm_proxy_
Definition: romeo.h:119
ros::Publisher stiffness_pub_
Definition: romeo.h:91
void publish(const boost::shared_ptr< M > &message) const
f
double high_freq_
Definition: romeo.h:115
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool connected()
Definition: romeo.cpp:550
double joint_precision_
Definition: romeo.h:115
std::string getName(void *handle)
void insertDataToMemory(const string &key, const AL::ALValue &value)
Definition: romeo.cpp:358
Definition: romeo.h:71
#define ROS_WARN(...)
bool initialize()
Definition: romeo.cpp:47
void subscribeToMicroEvent(const std::string &name, const std::string &callback_module, const std::string &callback_method, const string &callback_message="")
Definition: romeo.cpp:380
vector< double > joint_efforts_
Definition: romeo.h:134
void subscribeToEvent(const std::string &name, const std::string &callback_module, const std::string &callback_method)
Definition: romeo.cpp:368
hardware_interface::PositionJointInterface jnt_pos_interface_
Definition: romeo.h:127
void unsubscribeFromMicroEvent(const string &name, const string &callback_module)
Definition: romeo.cpp:405
sensor_msgs::JointState m_jointState
Definition: romeo.h:96
bool switchStiffnesses(romeo_dcm_msgs::BoolService::Request &req, romeo_dcm_msgs::BoolService::Response &res)
Definition: romeo.cpp:635
void subscribe()
Definition: romeo.cpp:265
void run()
Definition: romeo.cpp:453
AL::ALMemoryProxy memory_proxy_
Definition: romeo.h:118
ROSLIB_DECL std::string command(const std::string &cmd)
void lowCommunicationLoop()
Definition: romeo.cpp:463
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void unsubscribeFromEvent(const string &name, const string &callback_module)
Definition: romeo.cpp:393
vector< double > joint_commands_
Definition: romeo.h:131
void declareEvent(const string &name)
Definition: romeo.cpp:441
void raiseMicroEvent(const string &name, const AL::ALValue &value)
Definition: romeo.cpp:429
void loadParams()
Definition: romeo.cpp:280
boost::shared_ptr< AL::ALMotionProxy > m_motionProxy
Definition: romeo.h:120
std_msgs::Float32 stiffness_
Definition: romeo.h:95
ROSCPP_DECL bool ok()
void highCommunicationLoop()
Definition: romeo.cpp:487
void readJoints()
Definition: romeo.cpp:560
vector< double > joint_angles_
Definition: romeo.h:132
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Subscriber cmd_vel_sub_
Definition: romeo.h:89
void registerHandle(const JointStateHandle &handle)
void raiseEvent(const string &name, const AL::ALValue &value)
Definition: romeo.cpp:417
bool sleep()
vector< double > joint_velocities_
Definition: romeo.h:133
int topic_queue_
Definition: romeo.h:113
AL::ALValue getDataFromMemory(const string &key)
Definition: romeo.cpp:363
JointStateHandle getHandle(const std::string &name)
~Romeo()
Definition: romeo.cpp:41
string body_type_
Definition: romeo.h:111
bool is_connected_
Definition: romeo.h:108
bool initializeControllers(controller_manager::ControllerManager &cm)
Definition: romeo.cpp:143
Romeo(boost::shared_ptr< AL::ALBroker > broker, const std::string &name)
Definition: romeo.cpp:32
void DCMAliasTimedCommand(const string &alias, const vector< float > &values, const vector< int > &timeOffsets, const string &type="Merge", const string &type2="time-mixed")
Definition: romeo.cpp:328
void disconnect()
Definition: romeo.cpp:250
void brokerDisconnected(const string &event_name, const string &broker_name, const string &subscriber_identifier)
Definition: romeo.cpp:299
T * end(T(&ra)[N])
Definition: romeo.h:78
static Time now()
bool stiffnesses_enabled_
Definition: romeo.h:112
ros::Publisher m_jointStatePub
Definition: romeo.h:92
string prefix_
Definition: romeo.h:114
void controllerLoop()
Definition: romeo.cpp:512
int number_of_joints_
Definition: romeo.h:129
void update(const ros::Time &time, const ros::Duration &period, bool reset_controllers=false)
AL::ALValue commands_
Definition: romeo.h:105
double controller_freq_
Definition: romeo.h:115
void writeJoints()
Definition: romeo.cpp:597
bool connect(const ros::NodeHandle nh)
Definition: romeo.cpp:182
string version_
Definition: romeo.h:111
ros::NodeHandle node_handle_
Definition: romeo.h:86
#define ROS_ERROR(...)
void commandVelocity(const geometry_msgs::TwistConstPtr &msg)
Definition: romeo.cpp:555
void DCMTimedCommand(const string &key, const AL::ALValue &value, const int &timeOffset, const string &type="Merge")
Definition: romeo.cpp:305
vector< string > joints_names_
Definition: romeo.h:123


romeo_dcm_driver
Author(s): Konstantinos Chatzilygeroudis , Ha Dang
autogenerated on Sun Jul 3 2016 04:02:28