How to send messages from a node running python to arduino?
I upload the following code to Arduino.
#include <ros.h>
#include <std_msgs/Float64.h>
ros::NodeHandle nh;
void messageCb(const std_msgs::Float64& msg)
{
if(msg.data > 1.0)
digitalWrite(13, HIGH); // blink the led
else
digitalWrite(13, LOW); // Turn off led
}
ros::Subscriber<std_msgs::Float64> sub("your_topic", &messageCb);
void setup()
{
pinMode(13, OUTPUT);
nh.initNode();
nh.subscribe(sub);
}
void loop()
{
nh.spinOnce();
delay(1);
}
I use this code to turn on a LED connected to PIN13. The code receives a signal from the topic "your_topic", then if the signal is bigger than 1.0 it turns the LED.
From the terminal, I use the next code to publish to "your_topic": rostopic pub your_topic std_msgs/Float64 2
Though this code works, I now want to publish using python. I tried the succeeding code.
#!/usr/bin/env python
import rospy
from project_1.msg import *
from random import randint
rospy.init_node('Arduino_hear_my_whispers')
pub = rospy.Publisher('your_topic', Arduino_message, queue_size=10)
rnd = randint(0,2)
print "the number is " + str(rnd)
message=Arduino_message(x=rnd)
pub.publish(message)
rate = rospy.Rate(10)
rate.sleep()
Although the code worked, the LED did not turn on. Can someone help me?