Welcome to DOFBOT-JetsonNANO repository
1. Robotic arm ROS control
Robotic Arm ROS Control
1. Function Description
After the program runs, you can control the robotic arm to move to specified positions by publishing arm control topics through the terminal; you can control the buzzer by publishing buzzer control topics through the terminal.
2. Function Startup
After entering the docker container, enter the following command in the docker terminal:
ros2 run dofbot_ctrl arm_ctrlPress Enter, and the robotic arm will move to the set position.
2.1. Control the Robotic Arm
2.1.1. Control a Single Servo
After entering the docker container, enter the following command in the docker terminal:
xxxxxxxxxxros2 topic pub /TargetAngle dofbot_interface/ArmJoint "{id: 1,run_time: 1000,angle: 150.0,joints: []}" --onceAdjust the angle of servo 1 of the robotic arm to 150 degrees, with an arm running time of 1000 milliseconds (1 second).
2.1.2. Control Six Servos
After entering the docker container, enter the following command in the docker terminal:
xxxxxxxxxxros2 topic pub /TargetAngle dofbot_interface/ArmJoint "{id: 1,run_time: 2000,angle: 120.0,joints: [90,120,30,30,90,90]}" --onceAdjust servo values 1-6 to 90, 120, 30, 30, 90, 90 respectively, with an arm running time of 2000 milliseconds (2 seconds).
2.1.3. Command Analysis
ros2 topic pub: A common ROS control command used to publish topic messages, followed by the topic name to be controlled, the topic message type, topic message data, and some common parameters.
/TargetAngle: The name of the robotic arm control topic set by the program
dofbot_interface/ArmJoint: The message type of the robotic arm control topic
"{id: 1,run_time: 2000,angle: 120.0,joints: [90,120,30,30,90,90]}": Topic message data
- id: Represents the servo number to be controlled, value range is 1-6, this value is only meaningful when the following joints is [];
- run_time: The running time of the servo, unit is milliseconds, indicating that the servo angle control should be completed within this time;
- angle: Represents the angle of the servo to be controlled, unit is degrees, the value range for servos 1-4 and 6 is [0, 180], the control angle of servo 5 is [0, 270], similarly this value is only meaningful when the following joints is [];
- joints: Represents the angle values of six servos, when this value is [], it means controlling the angle of a single servo, and the values of id and angle need to be provided.
--once: Indicates that the topic is published only once.
2.2. Control the Buzzer
After entering the docker container, enter the following command in the docker terminal:
xxxxxxxxxxros2 topic pub /Beep std_msgs/msg/Bool "data: True" --onceControl the buzzer to respond once for 0.5 seconds and then stop. Where, /Beep is the buzzer control topic set by the program; std_msgs/msg/Bool is the topic message type; "data: True" indicates that the topic data message is True, enabling the buzzer for 0.5 seconds and then disabling it; --once indicates that the topic is published only once.
3. Code Analysis
After entering the docker container, the source code path is located at:
xxxxxxxxxx/root/dofbot_ws/src/dofbot_ctrl/dofbot_ctrl/arm_ctrl.pyImport necessary library files:
xxxxxxxxxximport rclpyfrom rclpy.node import Nodefrom std_msgs.msg import Boolfrom Arm_Lib import Arm_Devicefrom dofbot_interface.msg import ArmJointCreate robotic arm control object:
xxxxxxxxxxself.Arm = Arm_Device()Create subscribers for robotic arm servo control topics and buzzer topic control:
xxxxxxxxxxself.sub_arm_angle = self.create_subscription(ArmJoint,'/TargetAngle',self.GetAngleCallback,1)self.sub_beep = self.create_subscription(Bool,'/Beep',self.GetBeepCallback,1) Robotic arm control topic callback function:
xxxxxxxxxxdef GetAngleCallback(self,msg): arm_joint = ArmJoint() if len(msg.joints) != 0: self.get_logger().info(f"Received joints: {msg.joints}") for _ in range(2): #Call the robotic arm control function Arm_serial_servo_write6 to control six servo angles self.Arm.Arm_serial_servo_write6( msg.joints[0], msg.joints[1], msg.joints[2], msg.joints[3], msg.joints[4], msg.joints[5], time=msg.run_time ) else: self.get_logger().info(f"Moving joint {msg.id} to {msg.angle}") arm_joint.id = msg.id arm_joint.angle = msg.angle for _ in range(2): #Call the robotic arm control function Arm_serial_servo_write to control single servo angle self.Arm.Arm_serial_servo_write(msg.id, msg.angle, msg.run_time)Buzzer control topic callback function:
xxxxxxxxxxdef GetBeepCallback(self,msg): if msg.data == True: #Buzzer sounds for 0.5 seconds then turns off self.Arm.Arm_Buzzer_On(5) else: self.Arm.Arm_Buzzer_Off()