denso_robot_core_test.cpp
Go to the documentation of this file.
1 
25 #include <ros/ros.h>
27 
28 #include <iostream>
29 
30 #include <boost/algorithm/string.hpp>
31 #include <boost/foreach.hpp>
32 #include <boost/thread.hpp>
33 
34 #include "bcap_core/dn_common.h"
35 
36 // Message
37 #include <std_msgs/Int32.h>
38 #include <std_msgs/Float32.h>
39 #include <std_msgs/Float64.h>
40 #include <std_msgs/String.h>
41 #include <std_msgs/Bool.h>
42 #include <std_msgs/Float32MultiArray.h>
43 #include <std_msgs/Float64MultiArray.h>
44 #include "denso_robot_core/Joints.h"
45 #include "denso_robot_core/ExJoints.h"
46 #include "denso_robot_core/PoseData.h"
47 #include "denso_robot_core/MoveStringAction.h"
48 #include "denso_robot_core/MoveValueAction.h"
49 #include "denso_robot_core/DriveStringAction.h"
50 #include "denso_robot_core/DriveValueAction.h"
51 
52 using namespace std_msgs;
53 using namespace actionlib;
54 using namespace denso_robot_core;
55 
56 #define MESSAGE_QUEUE (100)
57 #define DEFAULT_TIMEOUT (0.0)
58 
59 enum {
69 };
70 
71 volatile bool bConn;
72 
74 {
75  bConn = true;
76 }
77 
79 {
80  ros::NodeHandle nh;
81  while(!bConn && ros::ok()) {
82  ros::spinOnce();
83  }
84 }
85 
86 volatile bool bPrint;
87 
88 void PrintFeedback(const std::vector<double>& pose)
89 {
90  std::cout << ros::Time::now() << " ";
91 
92  for(int i = 0; i <pose.size(); i++) {
93  std::cout << pose[i];
94  if(i != pose.size() - 1) {
95  std::cout << ", ";
96  } else {
97  std::cout << std::endl;
98  }
99  }
100 }
101 
103 {
104  ros::NodeHandle nh;
105  while(!bPrint && ros::ok()) {
106  ros::spinOnce();
107  }
108 }
109 
110 void ReadLine(std::string &str, std::string erase = "")
111 {
112  char cline[1024];
113  std::cin.getline(cline, sizeof(cline));
114  std::getline(std::cin, str);
115  std::cin.putback('\n');
116 
117  if(!erase.compare("")) {
118  size_t c;
119  while((c = str.find_first_of(erase)) != std::string::npos) {
120  str.erase(c,1);
121  }
122  }
123 }
124 
125 void WriteVariable(const std::string& name)
126 {
127  ros::NodeHandle nh;
128  ros::Publisher pub;
129 
130  int i, vt;
131  std::string strTmp;
132  std::vector<std::string> vecStr;
133 
134  Int32 ival; Float32 fval; Float64 dval;
135  String sval; Bool bval;
136  Float32MultiArray faryval;
137  Float64MultiArray daryval;
138 
139  std::cout << "Please input variable type: ";
140  std::cin >> vt;
141 
142  std::cout << "Please input value: ";
143  ReadLine(strTmp, " ");
144  boost::split(vecStr, strTmp, boost::is_any_of(","));
145 
146  switch(vt) {
147  case VT_I4:
148  pub = nh.advertise<Int32>(name, MESSAGE_QUEUE,
150  ival.data = atoi(vecStr.front().c_str());
151  WaitForConnect();
152  pub.publish(ival);
153  break;
154  case VT_R4:
155  pub = nh.advertise<Float32>(name, MESSAGE_QUEUE,
157  fval.data = atof(vecStr.front().c_str());
158  WaitForConnect();
159  pub.publish(fval);
160  break;
161  case VT_R8:
162  pub = nh.advertise<Float64>(name, MESSAGE_QUEUE,
164  dval.data = atof(vecStr.front().c_str());
165  WaitForConnect();
166  pub.publish(dval);
167  break;
168  case VT_BSTR:
169  pub = nh.advertise<String>(name, MESSAGE_QUEUE,
171  sval.data = vecStr.front();
172  WaitForConnect();
173  pub.publish(sval);
174  break;
175  case VT_BOOL:
176  pub = nh.advertise<Bool>(name, MESSAGE_QUEUE,
178  bval.data = atoi(vecStr.front().c_str());
179  WaitForConnect();
180  pub.publish(bval);
181  break;
182  case (VT_ARRAY | VT_R4):
183  pub = nh.advertise<Float32MultiArray>(name, MESSAGE_QUEUE,
185  BOOST_FOREACH(std::string str, vecStr) {
186  faryval.data.push_back(atof(str.c_str()));
187  }
188  WaitForConnect();
189  pub.publish(faryval);
190  break;
191  case (VT_ARRAY | VT_R8):
192  pub = nh.advertise<Float64MultiArray>(name, MESSAGE_QUEUE,
194  BOOST_FOREACH(std::string str, vecStr) {
195  daryval.data.push_back(atof(str.c_str()));
196  }
197  WaitForConnect();
198  pub.publish(daryval);
199  break;
200  }
201 }
202 
203 void WriteInt32(const std::string& name)
204 {
205  ros::NodeHandle nh;
206  ros::Publisher pub;
207 
208  Int32 ival;
209 
210  std::cout << "Please input value/number: ";
211  std::cin >> ival.data;
212  pub = nh.advertise<Int32>(name, MESSAGE_QUEUE,
214  WaitForConnect();
215  pub.publish(ival);
216 }
217 
218 void WriteFloat32(const std::string& name)
219 {
220  ros::NodeHandle nh;
221  ros::Publisher pub;
222 
223  Float32 fltval;
224 
225  std::cout << "Please input value/number: ";
226  std::cin >> fltval.data;
227  pub = nh.advertise<Float32>(name, MESSAGE_QUEUE,
229  WaitForConnect();
230  pub.publish(fltval);
231 }
232 
233 void Callback_MoveStringFeedback(const MoveStringActionFeedbackConstPtr& msg)
234 {
235  PrintFeedback(msg->feedback.pose);
236 }
237 
238 void MoveString(const std::string& name, float timeout)
239 {
240  ros::NodeHandle nh;
241  SimpleClientGoalState gs(SimpleClientGoalState::PENDING, "");
242 
243  ros::Subscriber sub = nh.subscribe<MoveStringActionFeedback>
244  (name + "/feedback", MESSAGE_QUEUE, &Callback_MoveStringFeedback);
245 
246  SimpleActionClient<MoveStringAction> acMvStr(name, true);
247  MoveStringGoal mvStrGoal;
248  MoveStringResultConstPtr mvStrRes;
249 
250  std::cout << "Please input interpolation (PTP:=1, CP:=2): ";
251  std::cin >> mvStrGoal.comp;
252  std::cout << "Please input pose (ex.: J(0,0,0,0,0,0,0,0)): ";
253  ReadLine(mvStrGoal.pose);
254  std::cout << "Please input option (If skip, input _): ";
255  ReadLine(mvStrGoal.option);
256  if(!mvStrGoal.option.compare("_")) mvStrGoal.option = "";
257 
258  boost::thread th(&PrintThread);
259 
260  acMvStr.sendGoal(mvStrGoal);
261  if(!acMvStr.waitForResult(ros::Duration(timeout))) {
262  acMvStr.cancelGoal();
263  }
264 
265  bPrint = true;
266  th.join();
267 
268  while(ros::ok()) {
269  gs = acMvStr.getState();
270  if(gs.isDone()) break;
271  ros::Duration(1).sleep();
272  }
273 
274  mvStrRes = acMvStr.getResult();
275 
276  ROS_INFO("State: %s, Result: %X", gs.toString().c_str(), mvStrRes->HRESULT);
277 }
278 
279 void Callback_MoveValueFeedback(const MoveValueActionFeedbackConstPtr& msg)
280 {
281  PrintFeedback(msg->feedback.pose);
282 }
283 
284 void MoveValue(const std::string& name, float timeout)
285 {
286  ros::NodeHandle nh;
287  SimpleClientGoalState gs(SimpleClientGoalState::PENDING, "");
288 
289  std::string strTmp;
290  std::vector<std::string> vecStr;
291 
292  ros::Subscriber sub = nh.subscribe<MoveValueActionFeedback>
293  (name + "/feedback", MESSAGE_QUEUE, &Callback_MoveValueFeedback);
294 
295  SimpleActionClient<MoveValueAction> acMvVal(name, true);
296  MoveValueGoal mvValGoal;
297  MoveValueResultConstPtr mvValRes;
298 
299  std::cout << "Please input interpolation (PTP:=1, CP:=2): ";
300  std::cin >> mvValGoal.comp;
301  std::cout << "Please input pose (ex.: 0,0,0,0,0,0,0,0): ";
302  ReadLine(strTmp, " ");
303  boost::split(vecStr, strTmp, boost::is_any_of(","));
304  BOOST_FOREACH(std::string str, vecStr) {
305  mvValGoal.pose.value.push_back(atof(str.c_str()));
306  }
307  std::cout << "Please input type (P: 0, T: 1, J: 2, V: 3): ";
308  std::cin >> mvValGoal.pose.type;
309  std::cout << "Please input pass (@P: -1, @E: -2, @0: 0, @n: n): ";
310  std::cin >> mvValGoal.pose.pass;
311  std::cout << "Please input exjoints mode (EX: 1, EXA: 2, No exjoints: 0): ";
312  std::cin >> mvValGoal.pose.exjoints.mode;
313  if(mvValGoal.pose.exjoints.mode != 0) {
314  while(true) {
315  std::cout << "Please input exjoint value (ex.: 7,30.5, If skip input _): ";
316  ReadLine(strTmp, " ");
317  if(!strTmp.compare("_")) break;
318  boost::split(vecStr, strTmp, boost::is_any_of(","));
319  Joints jnt;
320  jnt.joint = atoi(vecStr.at(0).c_str());
321  jnt.value = atof(vecStr.at(1).c_str());
322  mvValGoal.pose.exjoints.joints.push_back(jnt);
323  }
324  }
325  std::cout << "Please input option (If skip, input _): ";
326  ReadLine(mvValGoal.option);
327  if(!mvValGoal.option.compare("_")) mvValGoal.option = "";
328 
329  boost::thread th(&PrintThread);
330 
331  acMvVal.sendGoal(mvValGoal);
332  if(!acMvVal.waitForResult(ros::Duration(timeout))) {
333  acMvVal.cancelGoal();
334  }
335 
336  bPrint = true;
337  th.join();
338 
339  while(ros::ok()) {
340  gs = acMvVal.getState();
341  if(gs.isDone()) break;
342  ros::Duration(1).sleep();
343  }
344 
345  mvValRes = acMvVal.getResult();
346 
347  ROS_INFO("State: %s, Result: %X", gs.toString().c_str(), mvValRes->HRESULT);
348 }
349 
350 void Callback_DriveStringFeedback(const DriveStringActionFeedbackConstPtr& msg)
351 {
352  PrintFeedback(msg->feedback.pose);
353 }
354 
355 void DriveString(const std::string& name, float timeout)
356 {
357  ros::NodeHandle nh;
358  SimpleClientGoalState gs(SimpleClientGoalState::PENDING, "");
359 
360  ros::Subscriber sub = nh.subscribe<DriveStringActionFeedback>
361  (name + "/feedback", MESSAGE_QUEUE, &Callback_DriveStringFeedback);
362 
363  SimpleActionClient<DriveStringAction> acDrvVal(name, true);
364  DriveStringGoal drvStrGoal;
365  DriveStringResultConstPtr drvStrRes;
366 
367  std::cout << "Please input pose (ex.: @0 (1, 10), (2, 20)): ";
368  ReadLine(drvStrGoal.pose);
369  std::cout << "Please input option (If skip, input _): ";
370  ReadLine(drvStrGoal.option);
371  if(!drvStrGoal.option.compare("_")) drvStrGoal.option = "";
372 
373  boost::thread th(&PrintThread);
374 
375  acDrvVal.sendGoal(drvStrGoal);
376  if(!acDrvVal.waitForResult(ros::Duration(timeout))) {
377  acDrvVal.cancelGoal();
378  }
379 
380  bPrint = true;
381  th.join();
382 
383  while(ros::ok()) {
384  gs = acDrvVal.getState();
385  if(gs.isDone()) break;
386  ros::Duration(1).sleep();
387  }
388 
389  drvStrRes = acDrvVal.getResult();
390 
391  ROS_INFO("State: %s, Result: %X", gs.toString().c_str(), drvStrRes->HRESULT);
392 }
393 
394 void Callback_DriveValueFeedback(const DriveValueActionFeedbackConstPtr& msg)
395 {
396  PrintFeedback(msg->feedback.pose);
397 }
398 
399 void DriveValue(const std::string& name, float timeout)
400 {
401  ros::NodeHandle nh;
402  SimpleClientGoalState gs(SimpleClientGoalState::PENDING, "");
403 
404  std::string strTmp;
405  std::vector<std::string> vecStr;
406 
407  ros::Subscriber sub = nh.subscribe<DriveValueActionFeedback>
408  (name + "/feedback", MESSAGE_QUEUE, &Callback_DriveValueFeedback);
409 
410  SimpleActionClient<DriveValueAction> acDrvVal(name, true);
411  DriveValueGoal drvValGoal;
412  DriveValueResultConstPtr drvValRes;
413 
414  std::cout << "Please input pass (@P: -1, @E: -2, @0: 0, @n: n): ";
415  std::cin >> drvValGoal.pass;
416  while(true) {
417  std::cout << "Please input joint value (ex.: 7,30.5, If skip input _): ";
418  ReadLine(strTmp, " ");
419  if(!strTmp.compare("_")) break;
420  boost::split(vecStr, strTmp, boost::is_any_of(","));
421  Joints jnt;
422  jnt.joint = atoi(vecStr.at(0).c_str());
423  jnt.value = atof(vecStr.at(1).c_str());
424  drvValGoal.pose.push_back(jnt);
425  }
426  std::cout << "Please input option (If skip, input _): ";
427  ReadLine(drvValGoal.option);
428  if(!drvValGoal.option.compare("_")) drvValGoal.option = "";
429 
430  boost::thread th(&PrintThread);
431 
432  acDrvVal.sendGoal(drvValGoal);
433  if(!acDrvVal.waitForResult(ros::Duration(timeout))) {
434  acDrvVal.cancelGoal();
435  }
436 
437  bPrint = true;
438  th.join();
439 
440  while(ros::ok()) {
441  gs = acDrvVal.getState();
442  if(gs.isDone()) break;
443  ros::Duration(1).sleep();
444  }
445 
446  drvValRes = acDrvVal.getResult();
447 
448  ROS_INFO("State: %s, Result: %X", gs.toString().c_str(), drvValRes->HRESULT);
449 }
450 
451 int main(int argc, char** argv)
452 {
453  if(argc < 3) {
454  std::cout << "Usage: denso_robot_core mode topic [timeout]" << std::endl;
455  std::cout << "mode: 1:= Write variable." << std::endl;
456  std::cout << " 2:= Write ID." << std::endl;
457  std::cout << " 3:= Change arm group." << std::endl;
458  std::cout << " 4:= Move robot with string." << std::endl;
459  std::cout << " 5:= Move robot with value." << std::endl;
460  std::cout << " 6:= Drive robot with string." << std::endl;
461  std::cout << " 7:= Drive robot with value." << std::endl;
462  std::cout << " 8:= Change robot speed." << std::endl;
463  std::cout << " 9:= Change tool or work number." << std::endl;
464  return -1;
465  }
466 
467  ros::init(argc, argv, "denso_robot_core_test");
468 
469  int mode = atoi(argv[1]);
470 
471  float timeout = DEFAULT_TIMEOUT;
472  if(3 < argc) {
473  timeout = atof(argv[3]);
474  }
475 
476  std::cin.putback('\n');
477 
478  switch(mode) {
479  case WRITE_VARIABLE:
480  WriteVariable(argv[2]);
481  break;
482  case WRITE_ID:
483  case ARM_GROUP:
484  case CHANGE_TOOLWORK:
485  WriteInt32(argv[2]);
486  break;
487  case SPEED:
488  WriteFloat32(argv[2]);
489  break;
490  case MOVE_STRING:
491  MoveString(argv[2], timeout);
492  break;
493  case MOVE_VALUE:
494  MoveValue(argv[2], timeout);
495  break;
496  case DRIVE_STRING:
497  DriveString(argv[2], timeout);
498  break;
499  case DRIVE_VALUE:
500  DriveValue(argv[2], timeout);
501  break;
502  }
503 
504  return 0;
505 }
void Callback_DriveValueFeedback(const DriveValueActionFeedbackConstPtr &msg)
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
void publish(const boost::shared_ptr< M > &message) const
void DriveString(const std::string &name, float timeout)
void ReadLine(std::string &str, std::string erase="")
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
volatile bool bConn
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
VT_I4
VT_BSTR
void Connected(const ros::SingleSubscriberPublisher &pub)
std::string toString() const
void Callback_MoveStringFeedback(const MoveStringActionFeedbackConstPtr &msg)
void WriteVariable(const std::string &name)
void PrintThread()
VT_ARRAY
VT_BOOL
#define ROS_INFO(...)
volatile bool bPrint
ROSCPP_DECL bool ok()
#define MESSAGE_QUEUE
void WaitForConnect()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
VT_R8
void WriteInt32(const std::string &name)
void Callback_DriveStringFeedback(const DriveStringActionFeedbackConstPtr &msg)
void Callback_MoveValueFeedback(const MoveValueActionFeedbackConstPtr &msg)
void PrintFeedback(const std::vector< double > &pose)
int main(int argc, char **argv)
void MoveValue(const std::string &name, float timeout)
void WriteFloat32(const std::string &name)
static Time now()
void MoveString(const std::string &name, float timeout)
VT_R4
void DriveValue(const std::string &name, float timeout)
ROSCPP_DECL void spinOnce()
#define DEFAULT_TIMEOUT


denso_robot_core_test
Author(s): DENSO WAVE INCORPORATED
autogenerated on Mon Jun 10 2019 13:12:35