ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/ehsan/catkin_ws/src/gripper/scripts/dynamixel_servo.py", line 11, in callback_receive
    motor_data2=msg.motor_states[1]
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):
motor_data1=msg.motor_states[0]
pub1.publish(motor_data1.position)
motor_data2=msg.motor_states[1]
pub2.publish(motor_data2.position)



if __name__ == '__main__':
rospy.init_node('dynamixel_servo')
sub = rospy.Subscriber("/motor_states/pan_tilt_port",MotorStateList,callback_receive)
pub1=rospy.Publisher("/motor1_arduino",Int16,queue_size=30)
pub2=rospy.Publisher("/motor2_arduino",Int16,queue_size=10)

rospy.spin()

Arduino Code:

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include <WProgram.h>
#endif
#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){
//Serial.println(cmd_msg2.data); 
position2 = cmd_msg2.data;

}

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

void setup() {

Serial.begin(57600);
nh.initNode();
nh.subscribe(sub1);

}

void loop() {
int val = map(position2, 0, 1023, 0, 300);
val=val+30;
Serial.println(val);
nh.spinOnce();
delay(1);
}
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

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

Comments

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
1

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

Question Tools

1 follower

Stats

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

Seen: 374 times

Last updated: Dec 27 '19