Ask Your Question
0

Publishing and Subscribing at the same time

asked 2015-02-11 07:28:21 -0500

zuygar gravatar image

updated 2015-02-11 07:39:47 -0500

I am trying to send constant velocity commands to turtlesim and at the same time want to recieve pose information. I am only recieving pose data in terminal when i runing both turtlesim and roscore.

My code is following:

#include <ros/ros.h>
#include <turtlesim/Pose.h>
#include <iomanip>
#include <geometry_msgs/Twist.h>
#include <stdlib.h>

void poseMessageRecieved(const turtlesim::Pose& msg) {

ROS_INFO_STREAM(std::setprecision(2) << std::fixed
<< "position=(" << msg.x << "," << msg.y << ")"
<< "direction=" << msg.theta); 

}

int main(int argc,char **argv) {
ros::init(argc, argv, "sub2");
ros::NodeHandle nh;
ros::Subscriber sub=nh.subscribe("turtle1/pose", 1000, &poseMessageRecieved);
ros::spin();

ros::init(argc, argv, "pub2");
ros::Publisher pub=nh.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000);
ros::Rate rate(2);

while(ros::ok()) {
geometry_msgs::Twist msg;

msg.linear.x=2.0;
msg.angular.z=1.0;

pub.publish(msg);
ROS_INFO_STREAM("Sending constant velocity command: "<< "linear=" <<msg.linear.x << "angular=" << 
msg.angular.z);

ros::spinOnce();
rate.sleep();
}

//ros::spin();
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-02-11 09:48:57 -0500

zuygar gravatar image

Ok i fixed the problem. My code is following, but i have a confusion about "delete pub" command. If i wouldn'd use this, what happened ?

#include <ros/ros.h>
#include <turtlesim/Pose.h>
#include <iomanip>
#include <geometry_msgs/Twist.h>
#include <stdlib.h>

ros::Publisher *pub;

void poseMessageRecieved(const turtlesim::Pose& msgIn) {
geometry_msgs::Twist msg;
ROS_INFO_STREAM(std::setprecision(2) << std::fixed
<< "position=(" << msgIn.x << "," << msgIn.y << ")"
<< "direction=" << msgIn.theta); 
msg.linear.x=0.2;
msg.angular.z=0.1;
pub->publish(msg);
ROS_INFO_STREAM("Sending constant velocity command: "<< "linear=" <<msg.linear.x << "angular=" << msg.angular.z);
}

int main(int argc,char **argv) {
ros::init(argc, argv, "sub_and_pub");
ros::NodeHandle nh;

pub = new ros::Publisher(nh.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000));
ros::Subscriber sub = nh.subscribe("turtle1/pose", 1000, &poseMessageRecieved);

ros::spin();

delete pub;
}
edit flag offensive delete link more

Comments

pub will get cleaned up by the end of the scope anyway.

tfoote gravatar image tfoote  ( 2015-02-11 20:24:31 -0500 )edit
2

answered 2015-02-11 08:33:10 -0500

kramer gravatar image

If your code really is as posted, the 4th line into your main is a ros::spin; that's a non-returning call, so nothing after it will be executed.

Just to point it out -- you have 2 ros::init calls, which is a no-no.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-02-11 07:28:21 -0500

Seen: 945 times

Last updated: Feb 11 '15