Subscriber Callback not being triggered when JointState message is passed
Dear all,
I have come across a strange problem; maybe it's just something simple, but I am not really good at ROS programming.
Explaining the problem: I am using the rosserial package on Arduino Mega to log data and apply control inputs to a set of servos.
In order to do that, I created a subscriber as follows:
void controlCallback(const sensor_msgs::JointState& servo_des_states);
ros::Subscriber<sensor_msgs::JointState> control_sub("controlInput", controlCallback);
void setup(){
...
nh.subscribe(control_sub);
...
}
...
void controlCallback(const sensor_msgs::JointState& servo_des_states){
servoRadians = servo_des_states.position;
sendControlInput(RadToPWM(servoRadians), controlSpeed);
digitalWrite(13, HIGH-digitalRead(13));
}
when invoking the serial_node.py it lists that the subscriber is active and listening on the right topic:
cr055@cr055:~/catkin_ws$ rosrun rosserial_python serial_node.py /dev/ttyACM0 _baud:=115200
[INFO] [1548676899.466166]: ROS Serial Python Node
[INFO] [1548676899.471357]: Connecting to /dev/ttyACM0 at 115200 baud
[INFO] [1548676901.575863]: Requesting topics...
[INFO] [1548676904.505507]: Note: publish buffer size is 512 bytes
[INFO] [1548676904.505995]: Setup publisher on /tf [tf/tfMessage]
[INFO] [1548676904.509778]: Note: subscribe buffer size is 1024 bytes
[INFO] [1548676904.510289]: Setup subscriber on controlInput [sensor_msgs/JointState]
Although it seems everything correctly set up, the callback does not get triggered when I publish the mentioned messages.
If I try to send another kind of message (redefining the subscriber type and callback), for instance Float32, it works flawlessly.
It seems that the problem comes out when I use more complex kinds of messages (like sensor_msgs or MultiArray).
I hope someone has any clue about what's wrong here.
TL;DR: the callback function in the subscriber does not get invoked if the message type is multidimensional like JointState or Float32MultiArray. If I use simple floats I get the callback working.
UPDATE which might help: if I publish only a vector of two elements e.g. {position:[0,0]} the callback works. Am I missing something in the declaration of the published message?
UPDATE #2: I think that the arduino serial cannot handle packages as big as JointState if I publish 18 servos all together. Still, a workaround / alternative to publish 18 float values is welcome.
Sincerely,
Alessandro
Asked by cr055 on 2019-01-28 07:33:38 UTC
Answers
To access the data from the message, you need to use:
servoRadians = servo_des_states->position;
Asked by curi_ROS on 2019-01-28 08:50:59 UTC
Comments
Thanks!The modification you suggested gives me the following error:
base operand of '->' has non-pointer type 'const sensor_msgs::JointState'
As you can see from the code I toggle a led if I enter the callback. The led never goes on even if the RX serial pin flashes. The problem is somewhere else
Asked by cr055 on 2019-01-28 11:05:55 UTC
servo_des_states
is not a pointer here so the dot operator must be used, original code is correct.
Asked by VictorLamoine on 2019-01-28 17:37:57 UTC
Comments
have you looked at increasing the buffer size on the Arduino side? By default it's only 512 bytes IIRC.
Search for posts with similar topics. It should be clear from those.
Asked by gvdhoorn on 2019-01-28 13:07:51 UTC
Hi, I'm experiences exactly the same problem. Increasing the buffer does not make a difference. Has anyone been able to solve this?
Asked by Bifi on 2020-01-20 07:27:27 UTC