Rosanswers logo

Hi, I have a simple arduino code that takes in inputs from my joystick where a button on my joystick would act as a switch ( pressing once will turn 'on' and pressing it again turns it 'off'). My code basically publish the output as true or false and it works. However the problem arises when I add another subscriber to the cmd_vel topic. My output doesn't change and later a lost sync error occurs.

#include <ros.h>
#include <std_msgs/Float32.h>
#include <sensor_msgs/Joy.h>
#include <std_msgs/String.h>
#include <geometry_msgs/Twist.h>
#include <std_msgs/Bool.h>
ros::NodeHandle nh;

float tempx;
float tempz;
float joy_button;
boolean last_reading;
boolean currentReading;
boolean flag;
boolean published;

void overrideCallback(const sensor_msgs::Joy& joy_msg){
  joy_button = joy_msg.buttons[4];

void joyCallback(const geometry_msgs::Twist& cmd_msg){
    tempx = cmd_msg.linear.x;
    tempz = cmd_msg.angular.z;

//ros::Subscriber<geometry_msgs::Twist> joy_sub("cmd_vel", &joyCallback );
ros::Subscriber<sensor_msgs::Joy> override_sub("joy", &overrideCallback);

std_msgs::Float32 override_msg;
ros::Publisher override_pub("override_status", &override_msg);

std_msgs::Bool pushed_msg;
ros::Publisher pub_button("pushed", &pushed_msg);

void setup(){
  last_reading = false;
  currentReading = false;

void loop(){
  override_msg.data = joy_button;
  if (override_msg.data == 0.0){
    currentReading = false;
  }else currentReading = true;
  if (currentReading && !last_reading) {
    if (flag) {
      published = true;
    else {
      published = false;

  last_reading = currentReading;
  pushed_msg.data = published;

The above code works without the commented lines. Any help would be appreciated. Thank you.

Originally posted by J. J. on ROS Answers with karma: 60 on 2018-09-05

Post score: 0


1 Answer 1


Rosanswers logo

Twist msgs are rather large. Try increasing the buffer on the Arduino side. There are multiple Q&As on this site about that (use Google: your_search_terms site:answers.ros.org). The Hardware class needs changes.


But what does the 2,2, 150,150 indicate? Does it mean (#of sub, # of pub, buffer size sub, buffer size pub)?

This appears to be documented on the rosserial/Overview/Initialization page:

The rosserial_client library does not actually provide a NodeHandle object directly. Instead it provides:

ros::NodeHandle_<HardwareType, MAX_PUBLISHERS=25, MAX_SUBSCRIBERS=25, IN_BUFFER_SIZE=512, OUT_BUFFER_SIZE=512> nh;

It's most likely also documented in the API documentation for rosserial.

Originally posted by gvdhoorn with karma: 86574 on 2018-09-05

This answer was ACCEPTED on the original site

Post score: 0

Original comments

Comment by J. J. on 2018-09-05:
I don't know why but adding this: ros::NodeHandle_<ArduinoHardware, 2, 2, 150, 150> nh; the code now works.

Comment by gvdhoorn on 2018-09-05:
Does that increase the buffer size?

Comment by gvdhoorn on 2018-09-06:
Are you still having problems? From your comment it would appear things are working now.

Comment by J. J. on 2018-09-07:
Yes it now works. Thank you. But what does the 2,2, 150,150 indicate? Does it mean (#of sub, # of pub, buffer size sub, buffer size pub)?


Your Answer

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