denso_robot_core_test.cpp
Go to the documentation of this file.
00001 
00025 #include <ros/ros.h>
00026 #include <actionlib/client/simple_action_client.h>
00027 
00028 #include <iostream>
00029 
00030 #include <boost/algorithm/string.hpp>
00031 #include <boost/foreach.hpp>
00032 #include <boost/thread.hpp>
00033 
00034 #include "bcap_core/dn_common.h"
00035 
00036 // Message
00037 #include <std_msgs/Int32.h>
00038 #include <std_msgs/Float32.h>
00039 #include <std_msgs/Float64.h>
00040 #include <std_msgs/String.h>
00041 #include <std_msgs/Bool.h>
00042 #include <std_msgs/Float32MultiArray.h>
00043 #include <std_msgs/Float64MultiArray.h>
00044 #include "denso_robot_core/Joints.h"
00045 #include "denso_robot_core/ExJoints.h"
00046 #include "denso_robot_core/PoseData.h"
00047 #include "denso_robot_core/MoveStringAction.h"
00048 #include "denso_robot_core/MoveValueAction.h"
00049 #include "denso_robot_core/DriveStringAction.h"
00050 #include "denso_robot_core/DriveValueAction.h"
00051 
00052 using namespace std_msgs;
00053 using namespace actionlib;
00054 using namespace denso_robot_core;
00055 
00056 #define MESSAGE_QUEUE   (100)
00057 #define DEFAULT_TIMEOUT (0.0)
00058 
00059 enum {
00060   WRITE_VARIABLE = 1,
00061   WRITE_ID,
00062   ARM_GROUP,
00063   MOVE_STRING,
00064   MOVE_VALUE,
00065   DRIVE_STRING,
00066   DRIVE_VALUE,
00067   SPEED,
00068   CHANGE_TOOLWORK,
00069 };
00070 
00071 volatile bool bConn;
00072 
00073 void Connected(const ros::SingleSubscriberPublisher& pub)
00074 {
00075   bConn = true;
00076 }
00077 
00078 void WaitForConnect()
00079 {
00080   ros::NodeHandle nh;
00081   while(!bConn && ros::ok()) {
00082     ros::spinOnce();
00083   }
00084 }
00085 
00086 volatile bool bPrint;
00087 
00088 void PrintFeedback(const std::vector<double>& pose)
00089 {
00090   std::cout << ros::Time::now() << " ";
00091 
00092   for(int i = 0; i <pose.size(); i++) {
00093     std::cout << pose[i];
00094     if(i != pose.size() - 1) {
00095       std::cout << ", ";
00096     } else {
00097       std::cout << std::endl;
00098     }
00099   }
00100 }
00101 
00102 void PrintThread()
00103 {
00104   ros::NodeHandle nh;
00105   while(!bPrint && ros::ok()) {
00106     ros::spinOnce();
00107   }
00108 }
00109 
00110 void ReadLine(std::string &str, std::string erase = "")
00111 {
00112   char cline[1024];
00113   std::cin.getline(cline, sizeof(cline));
00114   std::getline(std::cin, str);
00115   std::cin.putback('\n');
00116 
00117   if(!erase.compare("")) {
00118     size_t c;
00119     while((c = str.find_first_of(erase)) != std::string::npos) {
00120       str.erase(c,1);
00121     }
00122   }
00123 }
00124 
00125 void WriteVariable(const std::string& name)
00126 {
00127   ros::NodeHandle nh;
00128   ros::Publisher  pub;
00129 
00130   int i, vt;
00131   std::string strTmp;
00132   std::vector<std::string> vecStr;
00133 
00134   Int32  ival; Float32  fval; Float64 dval;
00135   String sval; Bool bval;
00136   Float32MultiArray faryval;
00137   Float64MultiArray daryval;
00138 
00139   std::cout << "Please input variable type: ";
00140   std::cin >> vt;
00141 
00142   std::cout << "Please input value: ";
00143   ReadLine(strTmp, " ");
00144   boost::split(vecStr, strTmp, boost::is_any_of(","));
00145 
00146   switch(vt) {
00147     case VT_I4:
00148       pub = nh.advertise<Int32>(name, MESSAGE_QUEUE,
00149           (ros::SubscriberStatusCallback)Connected);
00150       ival.data = atoi(vecStr.front().c_str());
00151       WaitForConnect();
00152       pub.publish(ival);
00153       break;
00154     case VT_R4:
00155       pub = nh.advertise<Float32>(name, MESSAGE_QUEUE,
00156           (ros::SubscriberStatusCallback)Connected);
00157       fval.data = atof(vecStr.front().c_str());
00158       WaitForConnect();
00159       pub.publish(fval);
00160       break;
00161     case VT_R8:
00162       pub = nh.advertise<Float64>(name, MESSAGE_QUEUE,
00163           (ros::SubscriberStatusCallback)Connected);
00164       dval.data = atof(vecStr.front().c_str());
00165       WaitForConnect();
00166       pub.publish(dval);
00167       break;
00168     case VT_BSTR:
00169       pub = nh.advertise<String>(name, MESSAGE_QUEUE,
00170           (ros::SubscriberStatusCallback)Connected);
00171       sval.data = vecStr.front();
00172       WaitForConnect();
00173       pub.publish(sval);
00174       break;
00175     case VT_BOOL:
00176       pub = nh.advertise<Bool>(name, MESSAGE_QUEUE,
00177           (ros::SubscriberStatusCallback)Connected);
00178       bval.data = atoi(vecStr.front().c_str());
00179       WaitForConnect();
00180       pub.publish(bval);
00181       break;
00182     case (VT_ARRAY | VT_R4):
00183       pub = nh.advertise<Float32MultiArray>(name, MESSAGE_QUEUE,
00184           (ros::SubscriberStatusCallback)Connected);
00185       BOOST_FOREACH(std::string str, vecStr) {
00186         faryval.data.push_back(atof(str.c_str()));
00187       }
00188       WaitForConnect();
00189       pub.publish(faryval);
00190       break;
00191     case (VT_ARRAY | VT_R8):
00192       pub = nh.advertise<Float64MultiArray>(name, MESSAGE_QUEUE,
00193           (ros::SubscriberStatusCallback)Connected);
00194       BOOST_FOREACH(std::string str, vecStr) {
00195         daryval.data.push_back(atof(str.c_str()));
00196       }
00197       WaitForConnect();
00198       pub.publish(daryval);
00199       break;
00200   }
00201 }
00202 
00203 void WriteInt32(const std::string& name)
00204 {
00205   ros::NodeHandle nh;
00206   ros::Publisher  pub;
00207 
00208   Int32 ival;
00209 
00210   std::cout << "Please input value/number: ";
00211   std::cin >> ival.data;
00212   pub = nh.advertise<Int32>(name, MESSAGE_QUEUE,
00213       (ros::SubscriberStatusCallback)Connected);
00214   WaitForConnect();
00215   pub.publish(ival);
00216 }
00217 
00218 void WriteFloat32(const std::string& name)
00219 {
00220   ros::NodeHandle nh;
00221   ros::Publisher  pub;
00222 
00223   Float32 fltval;
00224 
00225   std::cout << "Please input value/number: ";
00226   std::cin >> fltval.data;
00227   pub = nh.advertise<Float32>(name, MESSAGE_QUEUE,
00228       (ros::SubscriberStatusCallback)Connected);
00229   WaitForConnect();
00230   pub.publish(fltval);
00231 }
00232 
00233 void Callback_MoveStringFeedback(const MoveStringActionFeedbackConstPtr& msg)
00234 {
00235   PrintFeedback(msg->feedback.pose);
00236 }
00237 
00238 void MoveString(const std::string& name, float timeout)
00239 {
00240   ros::NodeHandle nh;
00241   SimpleClientGoalState gs(SimpleClientGoalState::PENDING, "");
00242 
00243   ros::Subscriber sub = nh.subscribe<MoveStringActionFeedback>
00244     (name + "/feedback", MESSAGE_QUEUE, &Callback_MoveStringFeedback);
00245 
00246   SimpleActionClient<MoveStringAction> acMvStr(name, true);
00247   MoveStringGoal           mvStrGoal;
00248   MoveStringResultConstPtr mvStrRes;
00249 
00250   std::cout << "Please input interpolation (PTP:=1, CP:=2): ";
00251   std::cin >> mvStrGoal.comp;
00252   std::cout << "Please input pose (ex.: J(0,0,0,0,0,0,0,0)): ";
00253   ReadLine(mvStrGoal.pose);
00254   std::cout << "Please input option (If skip, input _): ";
00255   ReadLine(mvStrGoal.option);
00256   if(!mvStrGoal.option.compare("_")) mvStrGoal.option = "";
00257 
00258   boost::thread th(&PrintThread);
00259 
00260   acMvStr.sendGoal(mvStrGoal);
00261   if(!acMvStr.waitForResult(ros::Duration(timeout))) {
00262     acMvStr.cancelGoal();
00263   }
00264 
00265   bPrint = true;
00266   th.join();
00267 
00268   while(ros::ok()) {
00269     gs = acMvStr.getState();
00270     if(gs.isDone()) break;
00271     ros::Duration(1).sleep();
00272   }
00273 
00274   mvStrRes = acMvStr.getResult();
00275 
00276   ROS_INFO("State: %s, Result: %X", gs.toString().c_str(), mvStrRes->HRESULT);
00277 }
00278 
00279 void Callback_MoveValueFeedback(const MoveValueActionFeedbackConstPtr& msg)
00280 {
00281   PrintFeedback(msg->feedback.pose);
00282 }
00283 
00284 void MoveValue(const std::string& name, float timeout)
00285 {
00286   ros::NodeHandle nh;
00287   SimpleClientGoalState gs(SimpleClientGoalState::PENDING, "");
00288 
00289   std::string strTmp;
00290   std::vector<std::string> vecStr;
00291 
00292   ros::Subscriber sub = nh.subscribe<MoveValueActionFeedback>
00293     (name + "/feedback", MESSAGE_QUEUE, &Callback_MoveValueFeedback);
00294 
00295   SimpleActionClient<MoveValueAction> acMvVal(name, true);
00296   MoveValueGoal           mvValGoal;
00297   MoveValueResultConstPtr mvValRes;
00298 
00299   std::cout << "Please input interpolation (PTP:=1, CP:=2): ";
00300   std::cin >> mvValGoal.comp;
00301   std::cout << "Please input pose (ex.: 0,0,0,0,0,0,0,0): ";
00302   ReadLine(strTmp, " ");
00303   boost::split(vecStr, strTmp, boost::is_any_of(","));
00304   BOOST_FOREACH(std::string str, vecStr) {
00305     mvValGoal.pose.value.push_back(atof(str.c_str()));
00306   }
00307   std::cout << "Please input type (P: 0, T: 1, J: 2, V: 3): ";
00308   std::cin >> mvValGoal.pose.type;
00309   std::cout << "Please input pass (@P: -1, @E: -2, @0: 0, @n: n): ";
00310   std::cin >> mvValGoal.pose.pass;
00311   std::cout << "Please input exjoints mode (EX: 1, EXA: 2, No exjoints: 0): ";
00312   std::cin >> mvValGoal.pose.exjoints.mode;
00313   if(mvValGoal.pose.exjoints.mode != 0) {
00314     while(true) {
00315       std::cout << "Please input exjoint value (ex.: 7,30.5, If skip input _): ";
00316       ReadLine(strTmp, " ");
00317       if(!strTmp.compare("_")) break;
00318       boost::split(vecStr, strTmp, boost::is_any_of(","));
00319       Joints jnt;
00320       jnt.joint = atoi(vecStr.at(0).c_str());
00321       jnt.value = atof(vecStr.at(1).c_str());
00322       mvValGoal.pose.exjoints.joints.push_back(jnt);
00323     }
00324   }
00325   std::cout << "Please input option (If skip, input _): ";
00326   ReadLine(mvValGoal.option);
00327   if(!mvValGoal.option.compare("_")) mvValGoal.option = "";
00328 
00329   boost::thread th(&PrintThread);
00330 
00331   acMvVal.sendGoal(mvValGoal);
00332   if(!acMvVal.waitForResult(ros::Duration(timeout))) {
00333     acMvVal.cancelGoal();
00334   }
00335 
00336   bPrint = true;
00337   th.join();
00338 
00339   while(ros::ok()) {
00340     gs = acMvVal.getState();
00341     if(gs.isDone()) break;
00342     ros::Duration(1).sleep();
00343   }
00344 
00345   mvValRes = acMvVal.getResult();
00346 
00347   ROS_INFO("State: %s, Result: %X", gs.toString().c_str(), mvValRes->HRESULT);
00348 }
00349 
00350 void Callback_DriveStringFeedback(const DriveStringActionFeedbackConstPtr& msg)
00351 {
00352   PrintFeedback(msg->feedback.pose);
00353 }
00354 
00355 void DriveString(const std::string& name, float timeout)
00356 {
00357   ros::NodeHandle nh;
00358   SimpleClientGoalState gs(SimpleClientGoalState::PENDING, "");
00359 
00360   ros::Subscriber sub = nh.subscribe<DriveStringActionFeedback>
00361     (name + "/feedback", MESSAGE_QUEUE, &Callback_DriveStringFeedback);
00362 
00363   SimpleActionClient<DriveStringAction> acDrvVal(name, true);
00364   DriveStringGoal           drvStrGoal;
00365   DriveStringResultConstPtr drvStrRes;
00366 
00367   std::cout << "Please input pose (ex.: @0 (1, 10), (2, 20)): ";
00368   ReadLine(drvStrGoal.pose);
00369   std::cout << "Please input option (If skip, input _): ";
00370   ReadLine(drvStrGoal.option);
00371   if(!drvStrGoal.option.compare("_")) drvStrGoal.option = "";
00372 
00373   boost::thread th(&PrintThread);
00374 
00375   acDrvVal.sendGoal(drvStrGoal);
00376   if(!acDrvVal.waitForResult(ros::Duration(timeout))) {
00377     acDrvVal.cancelGoal();
00378   }
00379 
00380   bPrint = true;
00381   th.join();
00382 
00383   while(ros::ok()) {
00384     gs = acDrvVal.getState();
00385     if(gs.isDone()) break;
00386     ros::Duration(1).sleep();
00387   }
00388 
00389   drvStrRes = acDrvVal.getResult();
00390 
00391   ROS_INFO("State: %s, Result: %X", gs.toString().c_str(), drvStrRes->HRESULT);
00392 }
00393 
00394 void Callback_DriveValueFeedback(const DriveValueActionFeedbackConstPtr& msg)
00395 {
00396   PrintFeedback(msg->feedback.pose);
00397 }
00398 
00399 void DriveValue(const std::string& name, float timeout)
00400 {
00401   ros::NodeHandle nh;
00402   SimpleClientGoalState gs(SimpleClientGoalState::PENDING, "");
00403 
00404   std::string strTmp;
00405   std::vector<std::string> vecStr;
00406 
00407   ros::Subscriber sub = nh.subscribe<DriveValueActionFeedback>
00408     (name + "/feedback", MESSAGE_QUEUE, &Callback_DriveValueFeedback);
00409 
00410   SimpleActionClient<DriveValueAction> acDrvVal(name, true);
00411   DriveValueGoal           drvValGoal;
00412   DriveValueResultConstPtr drvValRes;
00413 
00414   std::cout << "Please input pass (@P: -1, @E: -2, @0: 0, @n: n): ";
00415   std::cin >> drvValGoal.pass;
00416   while(true) {
00417     std::cout << "Please input joint value (ex.: 7,30.5, If skip input _): ";
00418     ReadLine(strTmp, " ");
00419     if(!strTmp.compare("_")) break;
00420     boost::split(vecStr, strTmp, boost::is_any_of(","));
00421     Joints jnt;
00422     jnt.joint = atoi(vecStr.at(0).c_str());
00423     jnt.value = atof(vecStr.at(1).c_str());
00424     drvValGoal.pose.push_back(jnt);
00425   }
00426   std::cout << "Please input option (If skip, input _): ";
00427   ReadLine(drvValGoal.option);
00428   if(!drvValGoal.option.compare("_")) drvValGoal.option = "";
00429 
00430   boost::thread th(&PrintThread);
00431 
00432   acDrvVal.sendGoal(drvValGoal);
00433   if(!acDrvVal.waitForResult(ros::Duration(timeout))) {
00434     acDrvVal.cancelGoal();
00435   }
00436 
00437   bPrint = true;
00438   th.join();
00439 
00440   while(ros::ok()) {
00441     gs = acDrvVal.getState();
00442     if(gs.isDone()) break;
00443     ros::Duration(1).sleep();
00444   }
00445 
00446   drvValRes = acDrvVal.getResult();
00447 
00448   ROS_INFO("State: %s, Result: %X", gs.toString().c_str(), drvValRes->HRESULT);
00449 }
00450 
00451 int main(int argc, char** argv)
00452 {
00453   if(argc < 3) {
00454     std::cout << "Usage: denso_robot_core mode topic [timeout]" << std::endl;
00455     std::cout << "mode: 1:= Write variable."                    << std::endl;
00456     std::cout << "      2:= Write ID."                          << std::endl;
00457     std::cout << "      3:= Change arm group."                  << std::endl;
00458     std::cout << "      4:= Move robot with string."            << std::endl;
00459     std::cout << "      5:= Move robot with value."             << std::endl;
00460     std::cout << "      6:= Drive robot with string."           << std::endl;
00461     std::cout << "      7:= Drive robot with value."            << std::endl;
00462     std::cout << "      8:= Change robot speed."                << std::endl;
00463     std::cout << "      9:= Change tool or work number."        << std::endl;
00464     return -1;
00465   }
00466 
00467   ros::init(argc, argv, "denso_robot_core_test");
00468 
00469   int mode = atoi(argv[1]);
00470 
00471   float timeout = DEFAULT_TIMEOUT;
00472   if(3 < argc) {
00473     timeout = atof(argv[3]);
00474   }
00475 
00476   std::cin.putback('\n');
00477 
00478   switch(mode) {
00479     case WRITE_VARIABLE:
00480       WriteVariable(argv[2]);
00481       break;
00482     case WRITE_ID:
00483     case ARM_GROUP:
00484     case CHANGE_TOOLWORK:
00485       WriteInt32(argv[2]);
00486       break;
00487     case SPEED:
00488       WriteFloat32(argv[2]);
00489       break;
00490     case MOVE_STRING:
00491       MoveString(argv[2], timeout);
00492       break;
00493     case MOVE_VALUE:
00494       MoveValue(argv[2], timeout);
00495       break;
00496     case DRIVE_STRING:
00497       DriveString(argv[2], timeout);
00498       break;
00499     case DRIVE_VALUE:
00500       DriveValue(argv[2], timeout);
00501       break;
00502   }
00503 
00504   return 0;
00505 }


denso_robot_core_test
Author(s): DENSO WAVE INCORPORATED
autogenerated on Thu Jun 6 2019 21:00:20