
Is there any problem in my code? i just want, if i publish "q" Blue LED glow. Red for "a" and blink for "z" . i use
rostopic pub /servo std_msgs/String "data: 'z'"
command to publish topic.but nothing happening.the code running without error. my code is
#include <ros.h>
#include <std_msgs/String.h>
ros::NodeHandle nh;
void pwm( const std_msgs::String& cmd_msg)
{
if (cmd_msg.data=="q")
{
digitalWrite (13, HIGH); //BLUE LED ON
}
else if (cmd_msg.data=="a")
{
digitalWrite (12, HIGH); //RED LED ON
}
else if (cmd_msg.data=="z")
{
for(int i=0;i<1000;i++)
{
digitalWrite (13, HIGH); //LED BLINKING
delay(100);
digitalWrite (13, LOW);
digitalWrite (12, HIGH);
delay(100);
digitalWrite (12, LOW);
}
}
}
ros::Subscriber<std_msgs::String> sub("servo", pwm);
void setup()
{
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
nh.initNode();
nh.subscribe(sub);
}
void loop()
{
nh.spinOnce();
delay(10);
}
Originally posted by sudo_melvinyesudas on ROS Answers with karma: 54 on 2018-03-12
Post score: 1
Original comments
Comment by lucasw on 2018-03-12:
Try things like rostopic echo /servo to see if you see the message there, rostopic info /servo to see if your receiving node is a subscriber, and try rosnode info name_of_receiving_node - update the question with output if you need more help. Also details on how you launch the receiving node.
Comment by Maarten on 2018-03-12:
Your rostopic pub command seems to be correct (it sends "data: 'z'", not just "z" as tested in your if). We can't say much about your code sample, since it doesn't include the main with (or and) full initialisation and the call to the loop function (there could be an issue with one of those).