Publishing std_msgs int32 ROS Melodic , I need help
Hello,
Firstly im just begginer on ROS.My Professor asked me to do these task;
1-Create two nodes, a chatter which publishes std_msgs and a listener to listen to chatter.
2-The message you are required to send will be assigned for each one separately.
3-The listener should get the message and print it using ROS_INFO function.
I created 2 nodes with following instructions.But i can not go further.
Chatter codes (python) ;
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def talker():
pub = rospy.Publisher('chatter', String, queue_size=1)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
hello_str = "hello world %s" % rospy.get_time()
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
and listener codes (python) ;
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo("I heard %s", data.data)
def listener():
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("chatter", String, callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
listener()
Problem here how can i complete task 3 and also i need to replace string std_msgs with int32 data type. On here http://docs.ros.org/api/std_msgs/html...
I get the logic that how the system gonna work.It will be like listener will write massage , this massage will go to chatter and chatter will publish it with int32 data type. But i don't know how to write these codes.Can you help me with writing and replacing these codes.
############################Edit
I changed the data type from string to int32 on C++ file;
// %Tag(FULLTEXT)%
// %Tag(ROS_HEADER)%
#include "ros/ros.h"
// %EndTag(ROS_HEADER)%
// %Tag(MSG_HEADER)%
#include "std_msgs/int32.h"
// %EndTag(MSG_HEADER)%
#include <sstream>
int main(int argc, char **argv)
{
// %Tag(INIT)%
ros::init(argc, argv, "talker");
// %EndTag(INIT)%
// %Tag(NODEHANDLE)%
ros::NodeHandle n;
// %EndTag(NODEHANDLE)%
// %Tag(PUBLISHER)%
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1);
// %EndTag(PUBLISHER)%
// %Tag(LOOP_RATE)%
ros::Rate loop_rate(10);
// %EndTag(LOOP_RATE)%
// %Tag(ROS_OK)%
int count = 0;
while (ros::ok())
{
// %EndTag(ROS_OK)%
// %Tag(FILL_MESSAGE)%
std_msgs::int32 msg;
std::stringstream ss;
ss << "Hey " << count;
msg.data = count;
msg.data = ss.str();
// %EndTag(FILL_MESSAGE)%
// %Tag(ROSCONSOLE)%
ROS_INFO("%s", msg.data.c_str());
// %EndTag(ROSCONSOLE)%
// %Tag(PUBLISH)%
chatter_pub.publish(msg);
// %EndTag(PUBLISH)%
// %Tag(SPINONCE)%
ros::spinOnce();
// %EndTag(SPINONCE)%
// %Tag(RATE_SLEEP)%
loop_rate.sleep();
// %EndTag(RATE_SLEEP)%
++count;
}
return 0;
}
// %EndTag(FULLTEXT)%
And saved it but when i run catkin_make command i get this error ;
burak@burak:~/catkin_ws$ catkin_make
Base path: /home/burak/catkin_ws
Source space: /home/burak/catkin_ws/src
Build space: /home/burak/catkin_ws/build
Devel space: /home/burak/catkin_ws/devel
Install space: /home/burak/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/burak/catkin_ws/build"
####
####
#### Running command: "make -j8 -l8" in "/home/burak/catkin_ws/build"
####
[ 25%] Building CXX object my_package/CMakeFiles/name_of_node_listener.dir/src/02.cpp.o
[ 50%] Building CXX object my_package/CMakeFiles/name_of_node_chatter.dir/src/01.cpp.o
/home/burak/catkin_ws/src/my_package/src ...
Can you show me your CMakeLists.txt and package.xml file? Be sure to include roscpp in both files.