mycobot_ros/mycobot_280/mycobot_280/scripts/slider_control_gripper.py

53 lines
1.5 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 python3
# -*- coding: utf-8 -*-
"""[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 115200.
"""
import rospy
from sensor_msgs.msg import JointState
from pymycobot.mycobot import MyCobot
mc = None
gripper_value = []
def callback(data):
# print(data.position)
data_list = []
for index, value in enumerate(data.position):
data_list.append(round(value,3))
# print(data_list[6:])
data_list = data_list[:7]
print("radians:%s"%data_list[:6])
mc.send_radians(data_list[:6], 80)
gripper_value = int(abs(-0.7-data_list[6])* 117)
print("gripper_value:%s"%gripper_value)
mc.set_gripper_value(gripper_value, 80, 1)
def listener():
global mc
global gripper_value
rospy.init_node("control_slider", anonymous=True)
rospy.Subscriber("joint_states", JointState, callback)
port = rospy.get_param("~port", "/dev/ttyUSB0")
baud = rospy.get_param("~baud", 115200)
print(port, baud)
mc = MyCobot(port, baud)
# spin() simply keeps python from exiting until this node is stopped
# spin()只是阻止python退出直到该节点停止
print("spin ...")
rospy.spin()
if __name__ == "__main__":
listener()