|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import rclpy |
| 3 | +from rclpy.node import Node |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import threading |
| 7 | +import os |
| 8 | +import yaml |
| 9 | +import time |
| 10 | +import math |
| 11 | + |
| 12 | +import geometry_msgs.msg |
| 13 | +import sensor_msgs.msg |
| 14 | + |
| 15 | + |
| 16 | +def sort_list(list_target, list_source, list_in): |
| 17 | + assert set(list_target) <= set(list_source) |
| 18 | + mapping = dict(zip(list_source, list_in)) |
| 19 | + return np.array([mapping[i] for i in list_target]) |
| 20 | + |
| 21 | + |
| 22 | +def jpos_to_msg(q, name_list): |
| 23 | + msg = sensor_msgs.msg.JointState() |
| 24 | + msg.name = name_list |
| 25 | + msg.position = q |
| 26 | + return msg |
| 27 | + |
| 28 | + |
| 29 | +def msg_to_jpos(msg, name_list): |
| 30 | + return sort_list(name_list, msg.name, msg.position) |
| 31 | + |
| 32 | + |
| 33 | +class ControlROS(Node): |
| 34 | + def __init__(self, controller_file) -> None: |
| 35 | + super().__init__("robot_control_tool") |
| 36 | + |
| 37 | + with open(controller_file, "r", encoding="utf-8") as file: |
| 38 | + config = yaml.safe_load(file) |
| 39 | + self.control_joint = config["control_joints"] |
| 40 | + self.joint_name = self.control_joint.keys() |
| 41 | + self.joint_command = None |
| 42 | + self.joint_command_stamp = None |
| 43 | + self.joint_state = None |
| 44 | + self.init_flag = False |
| 45 | + self.msg = None |
| 46 | + self.dt = 1 / config["rate"] |
| 47 | + self.t = 0 |
| 48 | + |
| 49 | + # ROS2 |
| 50 | + self.rate = self.create_rate(config["rate"]) |
| 51 | + self.joint_state_sub = self.create_subscription( |
| 52 | + sensor_msgs.msg.JointState, |
| 53 | + config["joint_state"], |
| 54 | + self.joint_state_callback, |
| 55 | + 10, |
| 56 | + ) |
| 57 | + self.ee_state_sub = self.create_subscription( |
| 58 | + geometry_msgs.msg.PoseArray, config["ee_state"], self.ee_state_callback, 10 |
| 59 | + ) |
| 60 | + |
| 61 | + self.joint_cmd_pub = self.create_publisher( |
| 62 | + sensor_msgs.msg.JointState, config["joint_cmd"], 10 |
| 63 | + ) |
| 64 | + |
| 65 | + update_robot_cmd = threading.Thread(target=self.update_robot_cmd) |
| 66 | + update_robot_cmd.daemon = True |
| 67 | + update_robot_cmd.start() |
| 68 | + |
| 69 | + def joint_state_callback(self, msg): |
| 70 | + self.joint_state = np.array(msg_to_jpos(msg, self.joint_name)) |
| 71 | + if not self.init_flag: |
| 72 | + self.joint_command_stamp = list(self.joint_state) |
| 73 | + self.joint_command = list(self.joint_state) |
| 74 | + self.init_flag = True |
| 75 | + |
| 76 | + def ee_state_callback(self, msg): |
| 77 | + pass |
| 78 | + |
| 79 | + def update_robot_cmd(self): |
| 80 | + while rclpy.ok(): |
| 81 | + if not self.init_flag: |
| 82 | + continue |
| 83 | + # pub joint command |
| 84 | + if self.msg is not None: |
| 85 | + self.msg.header.stamp = self.get_clock().now().to_msg() |
| 86 | + self.joint_cmd_pub.publish(self.msg) |
| 87 | + self.t += self.dt |
| 88 | + |
| 89 | + for idx, joint in enumerate(self.control_joint.values()): |
| 90 | + mag = joint["magnitude"] |
| 91 | + f = joint["frequence"] |
| 92 | + if joint["type"] == "sine": |
| 93 | + self.joint_command[idx] = self.joint_command_stamp[ |
| 94 | + idx |
| 95 | + ] + mag * math.sin(2 * math.pi * f * self.t) |
| 96 | + if joint["type"] == "step": |
| 97 | + self.joint_command[idx] = self.joint_command_stamp[idx] + mag * ( |
| 98 | + int(self.t * f * 2) % 2 * -2 + 1 |
| 99 | + ) |
| 100 | + if joint["type"] == "ramp": |
| 101 | + self.joint_command[idx] = ( |
| 102 | + self.joint_command_stamp[idx] |
| 103 | + + 2 * mag * abs((self.t * f % 1) / 0.5 - 1) |
| 104 | + - mag |
| 105 | + ) |
| 106 | + self.msg = jpos_to_msg(self.joint_command, self.joint_name) |
| 107 | + |
| 108 | + self.rate.sleep() |
| 109 | + |
| 110 | + |
| 111 | +def main(args=None): |
| 112 | + rclpy.init(args=args) |
| 113 | + |
| 114 | + config_file = os.path.join(os.path.dirname(__file__), "../config/" + "tool.yml") |
| 115 | + control_ros = ControlROS(config_file) |
| 116 | + |
| 117 | + rclpy.spin(control_ros) |
| 118 | + |
| 119 | + control_ros.destroy_node() |
| 120 | + rclpy.shutdown() |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments