0
$\begingroup$

Rosanswers logo

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).

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

I run your code its all right on my Arduino Uno Do Two things 1).Add servo library 2).rostopic pub servo std_msgs/String z

and not forget to use serial node


Originally posted by lagankapoor with karma: 216 on 2018-03-12

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by billy on 2018-03-12:
+1 for taking time to test his code on a machine.

Comment by lagankapoor on 2018-03-12:
@billy Thank you Sir :)

$\endgroup$
0
$\begingroup$

Rosanswers logo

i figure it out.the code should be like this.

void pwm( const std_msgs::String& cmd_msg)
{
  if (cmd_msg.data[0]=='q')
  {
    digitalWrite (13, HIGH); 
     digitalWrite (12, LOW);
  }
  else if (cmd_msg.data[0]=='a')
  {
    digitalWrite (12, HIGH);
    digitalWrite (13,LOW); 
  }

thank you for all.for the effort. @lucasw @billy @maarten and @lagankapoor


Originally posted by sudo_melvinyesudas with karma: 54 on 2018-03-13

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by lagankapoor on 2018-03-13:
try to add servo library also :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.