Ask Your Question

Joy to control servo

asked 2014-10-02 11:07:52 -0500

dshimano gravatar image

Hi, I'm using Hydro.

I've been working on and off with this code to control a servo using rosserial with an Arduino, using a PS3 controller and the Joy node.

I wrote my code based off of this tutorial. Here is my code

 #include <ros/ros.h>
#include <std_msgs/UInt16.h>
#include <sensor_msgs/Joy.h>

class Servo

  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);

  ros::NodeHandle nh_;

 // Joystick tuning params
  int linear_, angular_;
  double l_scale_, a_scale_;

  // Joystick dev to listen to
  ros::Subscriber joy_sub;

  // Robot bits to control
  ros::ServiceClient create_client;
  ros::ServiceClient servo_client;

  ros::Publisher deg_pub_;  // I want to publish an angle as std_msgs/UInt16
  ros::Subscriber joy_sub_;


  nh_.param("axis_linear", linear_, linear_);
  nh_.param("axis_angular", angular_, angular_);
  nh_.param("scale_angular", a_scale_, a_scale_);
  nh_.param("scale_linear", l_scale_, l_scale_);

  deg_pub_ = nh_.advertise<std_msgs::UInt16>("servo", 1);

  joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &Servo::joyCallback, this);


void Servo::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
  std_msgs::UInt16 deg; 
  deg = l_scale_*joy->axes[linear_];

int main(int argc, char** argv) 
  ros::init(argc, argv, "servo_joy");
  Servo servo_joy;


I get some errors when I try to build it.

One is

/home/donni/catkin_ws/src/rosberry_pichoptor/src/servo.cpp:55:35: error: no match for ‘operator=’ in ‘deg = (((Servo*)this)->Servo::l_scale_ * ((double)(& joy)->boost::shared_ptr<T>::operator-> [with T = const sensor_msgs::Joy_<std::allocator<void> >]()->sensor_msgs::Joy_<std::allocator<void> >::axes.std::vector<_Tp, _Alloc>::operator[] [with _Tp = float, _Alloc = std::allocator<float>, std::vector<_Tp, _Alloc>::const_reference = const float&, std::vector<_Tp, _Alloc>::size_type = unsigned int](((unsigned int)((Servo*)this)->Servo::linear_))))’

I guess this means you can't use = with UInt16?

The second error is

/opt/ros/hydro/include/std_msgs/UInt16.h:55:8: note:   no known conversion for argument 1 from ‘double’ to ‘const std_msgs::UInt16_<std::allocator<void> >&’

This is also about UInt. I think my code is using UInt and float interchangeably. I don’t know how to get around this. I'm also unclear on how the Arduino node will read the Joy values. Is there a way to do the calculations with doubles and change them to UInt?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2014-10-02 11:24:23 -0500

ahendrix gravatar image

The std_msgs/UInt16 message is a wrapper around an integer type, not an integer type. This means that you can't assign directly into it; rather you have to set its data field. Try: = l_scale_*joy->axes[linear_];
edit flag offensive delete link more


Thanks, that fixed it!

dshimano gravatar image dshimano  ( 2014-10-07 14:16:47 -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

1 follower


Asked: 2014-10-02 11:07:52 -0500

Seen: 1,768 times

Last updated: Oct 02 '14