ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Unable to operate servo with rosserial_arduino

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

alam121 gravatar image

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

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 -0500

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 image alam121  ( 2020-01-14 10:09:44 -0500 )edit

Question Tools

1 follower

Stats

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

Seen: 202 times

Last updated: Jan 14 '20