Publishing and Subscribing at the same time
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();
}