Ask Your Question
0

Unable to operate servo with rosserial_arduino

asked 2020-01-14 09:42:31 -0600

alam121 gravatar image

updated 2020-01-14 10:03:54 -0600

gvdhoorn gravatar image

I'm using ros kinetic on ubuntu 16.04. I'm able to perform the hello world code from rosserial tutorials http://wiki.ros.org/rosserial_arduino... However not being able to operate the servo example. Any ideas? no error on Arduino IDE. After i perform the test on the terminal roscore rostopic pub servo std_msgs/UInt16 8

the terminal output:publishing and latching message. Press ctrl-C to terminate Nothing after that

/*
 * rosserial Servo Control Example

#if (ARDUINO >= 100)
 #include <Arduino.h>
#else
 #include <WProgram.h>

#endif

#include <Servo.h> 
#include <ros.h>
#include <std_msgs/UInt16.h>

ros::NodeHandle  nh;

Servo servo;

void servo_cb( const std_msgs::UInt16& cmd_msg){
  servo.write(cmd_msg.data); //set servo angle, should be from 0-180  
  digitalWrite(13, HIGH-digitalRead(13));  //toggle  led  
}


ros::Subscriber<std_msgs::UInt16> sub("servo", servo_cb);

void setup(){
pinMode(13, OUTPUT);

nh.initNode();

nh.subscribe(sub);

servo.attach(9); //attach it to pin 9
}

void loop(){
  nh.spinOnce();
  delay(1);
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-01-14 10:04:45 -0600

gvdhoorn gravatar image

delay(1) is a very small delay. You may want to try and change it to something a little higher. I've seen previous Q&As where using a larger delay makes things work better.

Not saying it will solve your problem though. Just something to keep in mind.

edit flag offensive delete link more

Comments

This actually worked. I'm not sure why i did not think of it earlier. Many thanks!

alam121 gravatar imagealam121 ( 2020-01-14 10:09:44 -0600 )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

1 follower

Stats

Asked: 2020-01-14 09:42:31 -0600

Seen: 10 times

Last updated: Jan 14