mycobot_ros/mycobot_280/mycobot_280arduino/scripts/slider_control.py

62 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python2
"""[summary]
This file obtains the joint angle of the manipulator in ROS,
and then sends it directly to the real manipulator using `pymycobot` API.
This file is [slider_control.launch] related script.
Passable parameters:
port: serial prot string. Defaults is '/dev/ttyUSB0'
baud: serial prot baudrate. Defaults is 1000000.
"""
import time
import math
import rospy
from sensor_msgs.msg import JointState
import pymycobot
from packaging import version
# min low version require
MIN_REQUIRE_VERSION = '3.6.1'
current_verison = pymycobot.__version__
print('current pymycobot library version: {}'.format(current_verison))
if version.parse(current_verison) < version.parse(MIN_REQUIRE_VERSION):
raise RuntimeError('The version of pymycobot library must be greater than {} or higher. The current version is {}. Please upgrade the library version.'.format(MIN_REQUIRE_VERSION, current_verison))
else:
print('pymycobot library version meets the requirements!')
from pymycobot import MyCobot280
mc = None
def callback(data):
global latest_data
latest_data = [round(math.degrees(value), 2) for value in data.position]
rospy.loginfo(f"Joint angles: {latest_data}")
def control_loop(event):
if latest_data:
rospy.loginfo(f"Sending angles: {latest_data}")
mc.send_angles(latest_data, 25)
def listener():
global mc
rospy.init_node("control_slider", anonymous=True)
port = rospy.get_param("~port", "/dev/ttyACM0")
baud = rospy.get_param("~baud", 115200)
print(port, baud)
mc = MyCobot280(port, baud)
time.sleep(1) # open port,need wait
rospy.Subscriber("joint_states", JointState, callback)
# 启动定时器每0.5秒执行一次控制循环
rospy.Timer(rospy.Duration(0.5), control_loop)
rospy.spin()
if __name__ == "__main__":
listener()