Strange data in arduino and ROS serial communication
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);
}