ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Strange data in arduino and ROS serial communication

asked 2019-12-24 13:24:42 -0500

ehsan_amp gravatar image

updated 2019-12-24 14:26:10 -0500

gvdhoorn gravatar image

I'm using AX-12+ dynamixel motor as an encoder in my robot joint. I receive data with dynamixel packages in ros kinetic. Then by the aim of a python node, I subscribe to the data and publish it to the Arduino DUE serial. I printed the encoder position in Arduino serial monitor, and among my correct data sometimes my motor send some unknown data like

⸮⸮⸮Notor2_arduinostd_msgs/Int16 8524586e34fbd7cb1c08c5f5f1ca0e57⸮

The error that is shown in the terminal while I run the publisher node:

[ERROR] [1577099602.767875]: bad callback: <function callback_receive at 0x7f5266c9a9b0>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/", line 750, in _invoke_callback
  File "/home/ehsan/catkin_ws/src/gripper/scripts/", line 11, in callback_receive
IndexError: list index out of range

I want to use the motor encoder data in my calculations, so I need to filter the data. How can I extract only the numbers and filter the unknown ones?

Python Subscriber and Publisher node:

#!/usr/bin/env python

import rospy
import time
from std_msgs.msg import Int16

from dynamixel_msgs.msg import MotorStateList
def callback_receive(msg):

if __name__ == '__main__':
sub = rospy.Subscriber("/motor_states/pan_tilt_port",MotorStateList,callback_receive)


Arduino Code:

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#include <WProgram.h>
#define USE_USBCON
#include <ros.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Empty.h>
#include <std_msgs/Float64.h>
int position2;
ros::NodeHandle  nh;

void servo_cb2(const std_msgs::Int16& cmd_msg2){
position2 =;


ros::Subscriber<std_msgs::Int16> sub1("motor2_arduino", &servo_cb2 );

void setup() {



void loop() {
int val = map(position2, 0, 1023, 0, 300);
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2019-12-27 15:32:30 -0500

updated 2019-12-27 15:34:20 -0500

Don't use serial print while using rosserial, they will interfere with each other. Comment out the Serial lines in the Arduino code.

To send data back use a publisher within the Arduino code, and subscribe to that topic within ROS

edit flag offensive delete link more


Thanks. Actually, I want to control another servo motor with Arduino, so I don't need to publish the data again. I'm using 3 IMU sensors through serial communication of Arduino mega 2560 (Serial1, Serial2, Serial3 of the board). Is it ok if I use those serials in my Arduino code to have IMU values?

ehsan_amp gravatar image ehsan_amp  ( 2019-12-29 11:13:59 -0500 )edit

Yes that is fine. If a board has multiple serial ports then you can use the others to do other serial communication without interfering with rosserial.

nickw gravatar image nickw  ( 2019-12-29 11:36:40 -0500 )edit

Thanks. I did that, and it works well

ehsan_amp gravatar image ehsan_amp  ( 2020-01-03 07:30:39 -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: 2019-12-24 13:24:42 -0500

Seen: 311 times

Last updated: Dec 27 '19