想要实现如下功能:(假装实现,主要是为了练习action编程)
客户端发给服务器一个坐标
服务器控制机器人前往坐标
相关信息打印出来(这个比较随便)
action文件
#goal
uint32 x
#uint32 y
---
#result
uint32 b
---
#feedback
uint32 c
客户端
#include <ros/ros.h>
#include<actionlib/client/simple_action_client.h>
#include"homework/workAction.h"
typedef actionlib::SimpleActionClient<homework::workAction> Client;
void activeCb()
{
ROS_INFO("I am Client : server active just now");
}
void feedbackCb(const homework::workFeedbackConstPtr& fback)
{
ROS_INFO("I am Client : server has complete %d ",fback->c);
}
void doneCb(const actionlib::SimpleClientGoalState& state,
const homework::workResultConstPtr& result)
{
ROS_INFO("I am Client : it's done");
ros::shutdown();
}
int main(int argc,char** argv)
{
ros::init(argc,argv,"jzmoveclient");
//Client 是typedef出来的,Client 是个类型,然后client是个对象,后面跟着() 是构造函数
Client client("jzmove",true);
ROS_INFO("I am Client : I am waiting for server");
client.waitForServer();
ROS_INFO("I am Client : Action server started ,send goal ");
homework::workGoal goal;
goal.x=3;
goal.y = 5;
client.sendGoal(goal, &doneCb,&activeCb,&feedbackCb);// &doneCb,&activeCb,&feedbackCb 这三个函数固定这个名字,不要改动
//猜想:activeCb是以服务端进入回调函数为信号的
// as->setSucceeded();是doneCb的信号
ros::spin();
return 0;
}
服务端
#include <ros/ros.h>
#include<actionlib/server/simple_action_server.h>
#include "homework/workAction.h"
typedef actionlib::SimpleActionServer<homework::workAction> Server;
void shuchu(const homework::workGoalConstPtr& goal, Server* as) //const 功能包::文件名GoalConstPtr
{
ros::Rate r(1);
ROS_INFO("I am Server : I know the goal X: %d Y: %d ",goal->x,goal->y);
//动作客户端要发布一个目标点,,, 服务器需要让假想的机器人运动到那个点
//并且动作服务器要定时返回feedback
homework::workFeedback feedback;
for(int i =0;i<=10;i++)
{
feedback.c = i*10;
// ROS_INFO("have move %d ", i*10);
as->publishFeedback(feedback);
r.sleep();
}
ROS_INFO("I am Server : (%d %d ) had arraved",goal->x,goal->y);
as->setSucceeded();
}
int main(int argc,char** argv)
{
ros::init(argc , argv,"jzmoveserver");
ros::NodeHandle n;
Server server(n,"jzmove",boost::bind(&shuchu, _1 ,&server) ,false);
// Server server(n,"do_dish",boost::bind(&execute, _1 , &server ) , false);
//服务器开始运行
server.start();
//循环等待
ros::spin();
return 0;
}
大坑:注意这里的客户端的
client.sendGoal(goal, &doneCb,&activeCb,&feedbackCb);
// &doneCb,&activeCb,&feedbackCb 这三个函数固定这个名字,不要改动,顺序也不要动
如果改动会报错(编译错误)
[ 98%] Built target jzmoveserver
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47,
from /usr/include/boost/function.hpp:64,
from /opt/ros/melodic/include/ros/forwards.h:40,
from /opt/ros/melodic/include/ros/common.h:37,
from /opt/ros/melodic/include/ros/ros.h:43,
from /home/xrh/ROStest/src/homework/src/jzmoveclient.cpp:1:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘static void boost::detail::function::void_function_invoker0<FunctionPtr, R>::invoke(boost::detail::function::function_buffer&) [with FunctionPtr = void (*)(const boost::shared_ptr<const homework::workFeedback_<std::allocator<void> > >&); R = void]’:
/usr/include/boost/function/function_template.hpp:925:38: required from ‘void boost::function0<R>::assign_to(Functor) [with Functor = void (*)(const boost::shared_ptr<const homework::workFeedback_<std::allocator<void> > >&); R = void]’
/usr/include/boost/function/function_template.hpp:716:7: required from ‘boost::function0<R>::function0(Functor, typename boost::enable_if_c<(! boost::is_integral<Functor>::value), int>::type) [with Functor = void (*)(const boost::shared_ptr<const homework::workFeedback_<std::allocator<void> > >&); R = void; typename boost::enable_if_c<(! boost::is_integral<Functor>::value), int>::type = int]’
/usr/include/boost/function/function_template.hpp:1061:16: required from ‘boost::function<R()>::function(Functor, typename boost::enable_if_c<(! boost::is_integral<Functor>::value), int>::type) [with Functor = void (*)(const boost::shared_ptr<const homework::workFeedback_<std::allocator<void> > >&); R = void; typename boost::enable_if_c<(! boost::is_integral<Functor>::value), int>::type = int]’
/home/xrh/ROStest/src/homework/src/jzmoveclient.cpp:40:58: required from here
/usr/include/boost/function/function_template.hpp:118:11: error: too few arguments to function
BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS));
^
In file included from /usr/include/boost/function/detail/maybe_include.hpp:18:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52,
from /usr/include/boost/function.hpp:64,
from /opt/ros/melodic/include/ros/forwards.h:40,
from /opt/ros/melodic/include/ros/common.h:37,
from /opt/ros/melodic/include/ros/ros.h:43,
from /home/xrh/ROStest/src/homework/src/jzmoveclient.cpp:1:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘static void boost::detail::function::void_function_invoker1<FunctionPtr, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with FunctionPtr = void (*)(); R = void; T0 = const boost::shared_ptr<const homework::workFeedback_<std::allocator<void> > >&]’:
/usr/include/boost/function/function_template.hpp:925:38: required from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const homework::workFeedback_<std::allocator<void> > >&]’
/usr/include/boost/function/function_template.hpp:716:7: required from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<(! boost::is_integral<Functor>::value), int>::type) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const homework::workFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<(! boost::is_integral<Functor>::value), int>::type = int]’
/usr/include/boost/function/function_template.hpp:1061:16: required from ‘boost::function<R(T0)>::function(Functor, typename boost::enable_if_c<(! boost::is_integral<Functor>::value), int>::type) [with Functor = void (*)(); R = void; T0 = const boost::shared_ptr<const homework::workFeedback_<std::allocator<void> > >&; typename boost::enable_if_c<(! boost::is_integral<Functor>::value), int>::type = int]’
/home/xrh/ROStest/src/homework/src/jzmoveclient.cpp:40:58: required from here
/usr/include/boost/function/function_template.hpp:118:11: error: too many arguments to function
BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS));
^
homework/CMakeFiles/jzmoveclient.dir/build.make:62: recipe for target 'homework/CMakeFiles/jzmoveclient.dir/src/jzmoveclient.cpp.o' failed
make[2]: *** [homework/CMakeFiles/jzmoveclient.dir/src/jzmoveclient.cpp.o] Error 1
CMakeFiles/Makefile2:2277: recipe for target 'homework/CMakeFiles/jzmoveclient.dir/all' failed
make[1]: *** [homework/CMakeFiles/jzmoveclient.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j16 -l16" failed
把三个函数码好了之后就编译成功了
执行一下自己编写的launch文件,虽然很简单,但是成就感满满。
把执行进程的 情况分析一下:
?
|