Ask Your Question
1

Publishing String from Terminal?

asked 2018-03-12 06:58:33 -0500

updated 2018-03-12 07:03:52 -0500

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);
}
edit retag flag offensive close merge delete

Comments

1

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.

lucasw gravatar image lucasw  ( 2018-03-12 11:34:23 -0500 )edit
1

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

Maarten gravatar image Maarten  ( 2018-03-12 11:47:06 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-03-13 01:25:08 -0500

updated 2018-03-13 01:26:57 -0500

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

edit flag offensive delete link more

Comments

try to add servo library also :)

lagankapoor gravatar image lagankapoor  ( 2018-03-13 05:22:41 -0500 )edit
1

answered 2018-03-12 11:47:50 -0500

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

edit flag offensive delete link more

Comments

1

+1 for taking time to test his code on a machine.

billy gravatar image billy  ( 2018-03-12 18:23:07 -0500 )edit

@billy Thank you Sir :)

lagankapoor gravatar image lagankapoor  ( 2018-03-12 20:03:35 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-03-12 06:58:33 -0500

Seen: 944 times

Last updated: Mar 13 '18