因为ur_modern_driver/ur_ros_wrapper.cpp中已经存在订阅器:
urscript_sub_ = nh_.subscribe("ur_driver/URScript", 1, &RosWrapper::urscriptInterface, this);
因此,首先想到的是写一个发布器calibration_glasgow/robot_helper.cpp:
ros::Publisher joint_state_pub = nh.advertise<std_msgs::String>("ur_driver/URScript", 10);
joint_state_pub.publish(msg);
不知道节点有没有成功收到消息,并且很大程度上存在丢包的现象,于是采用策略二.
//首先创建服务srv,在us_msgs的基础上添加srv/urscript.srv,内容如下:
string script
---
bool success
//改写CMakeLists.txt
CMakeLists.txt内容添加如下:
add_service_files(
FILES
urscript.srv
)
//编译
catkin_make -DCATKIN_WHITELIST_PACKAGES="ur_msgs"
//在catkin_ws/devel/include目录下生成urscript.h,要用到这个srv,添加如下头文件:
#include "ur_msgs/urscript.h"
//在RosWrapper定义一个类的成员变量
ros::ServiceServer urscript_srv_;
//初始化
urscript_srv_ = nh_.advertiseService("ur_driver/URScript_srv", &RosWrapper::urscriptInterface_srv,this);
//定义回调函数
bool urscriptInterface_srv(ur_msgs::urscript::Request &req, ur_msgs::urscript::Response &res)
{
robot_.rt_interface_->addCommandToQueue(req.script);
res.success = true;
return res.success;
}
//定义需要发送的urscript
cmd_str = "movej([1.57075, -1.396, -1.57075, 0.349, 1.396, 0.2094],1.4,1.05,5)\n";
//在calibration_glasgow/robot_helper.cpp中定义相应的客户端
ros::ServiceClient joint_state_client = nh.serviceClient<ur_msgs::urscript>("ur_driver/URScript_srv");
ur_msgs::urscript srv;
srv.request.script = cmd_str;
while(!joint_state_client.call(srv));//这个使得一直调用服务,直到成功call到服务.
参考
创建消息和服务类型roswiki
编写服务器和客户端roswiki