Ask Your Question
0

Why does this rosserial arduino code only permits a single subscriber?

asked 2018-09-05 01:55:20 -0500

J. J. gravatar image

updated 2018-09-06 00:41:49 -0500

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(){
  nh.initNode();
  nh.subscribe(override_sub);
  //nh.subscribe(joy_sub);
  nh.advertise(override_pub);
  nh.advertise(pub_button);
  last_reading = false;
  currentReading = false;
  flag=true;
}


void loop(){
  override_msg.data = joy_button;

  if (override_msg.data == 0.0){
    currentReading = false;
  }else currentReading = true;

  if (currentReading && !last_reading) {
    flag=!flag;
    if (flag) {
      published = true;
    }
    else {
      published = false;
    }
  }


  last_reading = currentReading;
  pushed_msg.data = published;
  pub_button.publish(&pushed_msg);  
  override_pub.publish(&override_msg);
  nh.spinOnce();
  delay(5);
}

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-09-05 02:34:27 -0500

gvdhoorn gravatar image

updated 2018-09-07 03:05:35 -0500

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.


Edit:

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.

edit flag offensive delete link more

Comments

I don't know why but adding this: ros::NodeHandle_<ArduinoHardware, 2, 2, 150, 150> nh; the code now works.

J. J. gravatar image J. J.  ( 2018-09-05 03:11:45 -0500 )edit

Does that increase the buffer size?

gvdhoorn gravatar image gvdhoorn  ( 2018-09-05 03:14:40 -0500 )edit

Are you still having problems? From your comment it would appear things are working now.

gvdhoorn gravatar image gvdhoorn  ( 2018-09-06 01:23:21 -0500 )edit

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

J. J. gravatar image J. J.  ( 2018-09-07 01:42:30 -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

Stats

Asked: 2018-09-05 01:55:20 -0500

Seen: 212 times

Last updated: Sep 07 '18