Ros publisher and arduino subscriber

35 Views Asked by At

Arduino subscriber code

#include <ros.h>
#include <std_msgs/String.h>

ros::NodeHandle nh;

void messageCallback(const std_msgs::String& msg) {
  String received_message = msg.data;
 
  // Print received message to serial monitor
  Serial.print("Received message: ");
  Serial.println(received_message);
}
//but i didn't get any message....
ros::Subscriber<std_msgs::String> sub("arduino_message", &messageCallback );

void setup() {
  Serial.begin(9600);
  nh.initNode();
  nh.subscribe(sub);
}

void loop() {
  nh.spinOnce();
  delay(1);
}

ros publisher code

#!/usr/bin/env python

import rospy
from std_msgs.msg import String

def ros_publisher():
    rospy.init_node('ros_publisher_node', anonymous=True)
    pub = rospy.Publisher('arduino_message', String, queue_size=10)
    rate = rospy.Rate(1)  # 1 Hz

    while not rospy.is_shutdown():
        message = "Hello, World!"
        rospy.loginfo("Publishing message: %s" % message)
        pub.publish(message)
        rate.sleep()


if __name__ == '__main__':
    try:
        ros_publisher()
    except rospy.ROSInterruptException:
        pass

I wanted to try using ros publisher and arduino subscriber I checked (rostopic list) showed that send publish info But arduino couldn't get that info (i checked serial 115200, 57600, 9600 and many kinds of methods) but it's not the problem they didn't show

And i checked rqt_graph It showed only publisher there was no subscriber...it means they didn't connect

Why?.... what is the problem? I set up subscriber and publisher and same arduino_message, message and Int 32.

0

There are 0 best solutions below