ROS2在MyCobot320Pi2022和PC之間通訊(都是Ubuntu20.04系統)

susiezsf發表於2024-10-28

參考:

  1. Running ROS across multiple machines http://wiki.ros.org/ROS/Tutorials/MultipleMachines
  2. SLAM+語音機器人DIY系列:(五)樹莓派3開發環境搭建——4.PC端與robot端ROS網路通訊 https://www.cnblogs.com/hiram-zhang/p/10410168.html
  3. 在多臺PC上進行ROS通訊(在多臺遠端機器人或電腦上執行ROS)-學習筆記 https://blog.51cto.com/u_12369060/5172376
  4. [基於ROS構建移動機器人]第二篇:設定ROS主從網路和遠端開發環境 https://zhuanlan.zhihu.com/p/52005221/

示例程式碼:使用PC驅動Raspberry機械臂,並以10Hz的頻率釋出機械臂末端法蘭盤相對於基座座標系的座標:

------ Notes ------
Aim to control and monitor robot arm (MyCobot320pi2022) through PC. Both with Ubuntu 20.04 and ROS 2 Galactic installed.
The command comes from the MECHREV computer (PC), whose hostname is `zsfmec`. IP: 192.168.117.142, passcode:zsf2603
The executor is Raspberry with hostname `er`. IP: 192.168.117.179, passcode: 123

------ Topic list defined in this packgae ------
1. /robot_arm/coordinates
2. 
3. 



------ Usage ------
1. Connect the two devices in the same WiFi: phone hotspot called: Redmi K40, passcode: zsf20220912
2. Raspberry: open the folder ~/Desktop/pymycobot/demo in terminal, and execute:
    `python3 Server.py`, press Enter
    open another terminal inside the folder ~/Desktop/pymycobot/demo, and execute:
    `python3 Server1.py`, press Enter
3. PC: 
    open a terminal and run /home/zsfmec/ros2_ws/src/robot_control/robot_control/coordinate_publisher.py
    open another terminal and run /home/zsfmec/ros2_ws/src/robot_control/robot_control/actuate_robot.py

------ 我的筆記 ------
PC與MyCobot通訊可以參考我的CSDN的文章: https://blog.csdn.net/weixin_42776565/article/details/137049334

/home/zsfmec/ros2_ws/src/robot_control/robot_control/coordinate_publisher.py

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from std_msgs.msg import Float32MultiArray  # Replace String with appropriate message type for coordinates
from pymycobot import MyCobotSocket  # Import the MyCobotSocket library for TCP/IP connection
import time

class CoordinatePublisher(Node):
    def __init__(self):
        super().__init__('coordinate_publisher')
        self.publisher_ = self.create_publisher(Float32MultiArray, '/robot_arm/coordinates', 10)
        self.timer = self.create_timer(0.1, self.publish_coordinates)  # 0.1 seconds corresponds to 10Hz
        
        # Initialize MyCobotSocket connection to the robot arm
        self.tcp_ip = '192.168.117.179'  # Replace with the IP address of the robot arm
        self.tcp_port = 9000  # Default MyCobot port
        
        try:
            self.mc = MyCobotSocket(self.tcp_ip, self.tcp_port)
            self.get_logger().info('Connected to robot arm via TCP/IP')
        except Exception as e:
            self.get_logger().error(f'Failed to connect to robot arm: {e}')

    def publish_coordinates(self):
        try:
            # Request angles from the robot arm via TCP/IP
            coords = self.mc.get_coords()  # Get the end-effector coordinates
            if coords:
                msg = Float32MultiArray()  # Using Float32MultiArray to hold coordinate values
                msg.data = coords
                self.publisher_.publish(msg)
                self.get_logger().info(f'Publishing coordinates: {coords}')
        except Exception as e:
            self.get_logger().error(f'Failed to get coordinates: {e}')


def main(args=None):
    rclpy.init(args=args)
    node = CoordinatePublisher()
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

/home/zsfmec/ros2_ws/src/robot_control/robot_control/actuate_robot.py

from pymycobot import MyCobotSocket
import time
#其中"192.168.11.15"為機械臂IP,請自行輸入你的機械臂IP
mc = MyCobotSocket("192.168.117.179",9001)  

#連線正常就可以對機械臂進行控制操作
mc.send_angles([0,0,0,0,0,0],20)
time.sleep(3)
mc.send_angles([30,20,0,0,0,0],20)
time.sleep(3)
mc.send_angles([0,0,0,0,0,0],20)
time.sleep(3)
mc.send_angles([-30,-20,0,0,0,0],20)
time.sleep(3)

image

image

相關文章