ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Publishing std_msgs int32 ROS Melodic , I need help

asked 2020-04-01 11:24:34 -0500

Karandiru gravatar image

updated 2020-04-02 12:06:00 -0500

billy gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

Comments

Can you show me your CMakeLists.txt and package.xml file? Be sure to include roscpp in both files.

petitpoulet gravatar image petitpoulet  ( 2020-04-08 19:34:28 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-04-01 17:08:05 -0500

petitpoulet gravatar image

Actually, the listener will listen to your chatter. That's done when calling:

rospy.Subscriber("chatter", String, callback)

Each time a new message is received, your listener execute callback(), which will print the data in the terminal. Basically, you've already completed step 1 and step 3.

By sending the message in only one place, which is in the chatter, you complete the second step. If you want to send another message, you just have to change the string in hello-str.

This brings me to the int32 data. Instead of importing a string, you can import int32. This will allow you to send int32 messages, but you will need to change the type in the Publish() and Subscribe() function.

You can find a lot of tutorials in order to get used to ROS. Here is the link to the rospy tutorials.

http://wiki.ros.org/rospy_tutorials/T...

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-04-01 11:24:34 -0500

Seen: 2,253 times

Last updated: Apr 02 '20