1,今天用rospy.Timer(Duaration, self.timer_callback)时一直报错:
_callback takes 1 positional argument but 2 were given
回调函数是这样的:
callback(self):
****
网上查了很多资料,都说类中的函数需要加self,就能解决这个问题,但我明明加了,google了一下:
加了event,完美解决~
2, 用launch文件launch节点后,发现print语句打印不到界面了
解决办法:<node pkg="**" type="**" name="**" output="screen" />, 加上output
3,同一个ros节点同时订阅和发布,并在订阅的回调函数中发布另一个话题,这里的答案:
#include <ros/ros.h>
class SubscribeAndPublish
{
public:
SubscribeAndPublish()
{
//Topic you want to publish
pub_ = n_.advertise<PUBLISHED_MESSAGE_TYPE>("/published_topic", 1);
//Topic you want to subscribe
sub_ = n_.subscribe("/subscribed_topic", 1, &SubscribeAndPublish::callback, this);
}
void callback(const SUBSCRIBED_MESSAGE_TYPE& input)
{
PUBLISHED_MESSAGE_TYPE output;
//.... do something with the input and generate the output...
pub_.publish(output);
}
private:
ros::NodeHandle n_;
ros::Publisher pub_;
ros::Subscriber sub_;
};//End of class SubscribeAndPublish
int main(int argc, char **argv)
{
//Initiate ROS
ros::init(argc, argv, "subscribe_and_publish");
//Create an object of class SubscribeAndPublish that will take care of everything
SubscribeAndPublish SAPObject;
ros::spin();
return 0;
}
持续更新中……