arduino和ROS串行通信中的不规则数据

2024-06-11 01:01:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我用AX-12+dynamixel马达作为我机器人关节的编码器。我收到的数据与dynamixel包在ros动能。然后根据python节点的目标,我订阅数据并将其发布到Arduino DUE serial。我在Arduino串行监视器中打印了编码器的位置,在我的正确数据中,有时我的电机会发送一些未知数据,如

⸮⸮⸮ ⸮⸮⸮Notor2_arduinostd_msgs/Int16 8524586e34fbd7cb1c08c5f5f1ca0e57⸮

运行发布服务器节点时在终端中显示的错误:

[ERROR] [1577099602.767875]: bad callback: 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

我想在我的计算中使用电机编码器的数据,所以我需要过滤数据。如何只提取数字并过滤未知的数字?
Python订阅服务器和发布服务器节点:

#!/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代码:

#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);
}

Tags: 数据importincludecallbackmsgrosdynamixelarduino