8. Controlling reBot Arm Using Python SDK
Chapter 8 of the Seeed Embodied Intelligence Beginner's Course — control the reBot Arm with the Python SDK: parameters, context manager connection, motion, zero point, and joint state.
1. If the environment is not installed, refer to section 7.2 for the installation environment.
2. The parameters of each joint controller of the Python SDK robotic arm need to be adjusted according to actual usage requirements. The current parameters can only satisfy scenarios with low precision requirements.
Install required dependencies:
python3 -m pip install pyyaml motorbridge
Pull example code:
git clone https://github.com/hopcan/rebotArm_ctrl.git
- The Python examples for controlling reBot DM are in
rebotArm_ctrl/example/rebotDM. - The configuration file for the reBot DM robotic arm is in
rebotArm_ctrl/config. - The Python examples for controlling reBot RS are in
rebotArm_ctrl/example/rebotRS. - The configuration file for the reBot RS robotic arm is in
rebotArm_ctrl/config.
8.1 Modify Parameters and Switch Modes
8.1 Modify Parameters and Switch Modes
The recommended control mode for reBot DM is POS_VEL. Switching the robotic arm joint control mode and configuring parameters can both be achieved by modifying the corresponding parameters in rebotDM.yaml under rebotArm_ctrl/config.
For example:
- name: Shoulder Pan
motor_can_id: 1
MIT:
kp: 10.0
kd: 1.0
POS_VEL:
vel_kp: 0.0125
vel_ki: 0.004
pos_kp: 150.0
pos_ki: 0.5
vlim: 5.0
posmax: 2.6
posmin: -2.6
use_mode: POS_VEL
MIT's kp and kd, POS_VEL's vel_kp, vel_ki, pos_kp, pos_ki, and vlim are the parameters of the corresponding mode, which can be changed according to the control effect of the robotic arm. use_mode can switch the control mode of the corresponding joint, and can be changed to MIT or POS_VEL.
8.2 Connect/Disconnect the Robotic Arm (Using Context Manager)
8.2 Connect/Disconnect the Robotic Arm (Using Context Manager)
Refer to example/rebotDM/1_rebotDM_connect.py or example/rebotRS/1_rebotRS_connect.py.
- First create the bus controller.
- Check whether the port exists.
- Port permissions must be granted before the program runs.
reBot DM uses a serial port, created as follows:
channel = "/dev/ttyACM0"
ctrl = Controller.from_dm_serial(channel, 921600)
reBot RS uses PCAN:
channel = "can0"
ctrl = Controller(channel)
- Implemented through a safe context manager:
with reBotArm_handle(ctrl, "rebotDM") as handle:
with reBotArm_handle(ctrl, "rebotRS") as handle:
Where reBotArm_handle also supports the config_path parameter. This parameter can specify the imported configuration file, and the default robotic arm configuration file will no longer be imported. You can refer to the configuration files in config to write your own configuration file.
with reBotArm_handle(ctrl, "rebotDM", config_path="absolute path of yaml") as handle:
with reBotArm_handle(ctrl, "rebotRS", config_path="absolute path of yaml") as handle:
Core implementation:
- The
__enter__function will call theconnectfunction to automatically connect to the robotic arm. If the connection fails, the corresponding log will be output. - The
__exit__function will call thedisconnectfunction to automatically disconnect from the robotic arm when the program exits. - Connecting to the robotic arm will add motors to the bus controller, check motor communication on power-up, verify whether motor CAN ID and master ID are valid, verify whether the configuration file is valid, and change the motor control mode to the target control mode.
- Disconnecting from the robotic arm will first automatically restore the initial state, then disable.
After using Ctrl+C to exit the program, wait a few seconds. Do not keep entering Ctrl+C; you need to wait for the robotic arm to automatically return to its home position and then disable.
If you do not want to use the context manager, you can directly call the connect function and disconnect function to connect/disconnect the robotic arm.
8.3 Control Robotic Arm Motion
8.3 Control Robotic Arm Motion
Refer to example/rebotDM/3_rebotDM_move_joint.py or example/rebotRS/3_rebotRS_move_joint.py.
while True:
handle.move_to_joint_positions([0, 0, 0, 0.5, 0.5, 0, -1])
for motor_id in list(range(1, 8)):
print(f"motor {motor_id}")
print(f"pos: {handle.motor_state[motor_id].pos:.3f} rad")
print(f"vel: {handle.motor_state[motor_id].vel:.3f} rad/s")
print(f"torque: {handle.motor_state[motor_id].torq:.3f} Nm\n")
time.sleep(0.002)
handle.motor_state is a dictionary containing the state information of all joints. The reading method is as above.
8.4 Set Zero Point for the Robotic Arm
8.4 Set Zero Point for the Robotic Arm
Refer to example/rebotDM/2_rebotDM_set_zero.py or example/rebotRS/2_rebotRS_set_zero.py.
with reBotArm_handle(ctrl, "rebotRS") as handle:
handle.set_zero_position()
with reBotArm_handle(ctrl, "rebotDM") as handle:
handle.set_zero_position()
Calling the set_zero_position function through the robotic arm control class can set the corresponding joint ID for all joints of the robotic arm.
8.5 Update Robotic Arm Joint State
8.5 Update Robotic Arm Joint State
Refer to example/rebotDM/5_rebotDM_request_joints_data.py or example/rebotRS/5_rebotRS_request_joints_data.py.
with reBotArm_handle(ctrl, "rebotDM") as handle:
if handle.is_connected:
print("Controller is connected and ready.")
print("Motor Use Modes:", handle.use_mode)
else:
print("Controller failed to connect.")
handle.ctrl.disable_all()
while True:
print(handle.get_joints_state())
time.sleep(0.002)
with reBotArm_handle(ctrl, "rebotRS") as handle:
if handle.is_connected:
print("Controller is connected and ready.")
print("Motor Use Modes:", handle.use_mode)
else:
print("Controller failed to connect.")
handle.ctrl.disable_all()
while True:
print(handle.get_joints_state())
time.sleep(0.002)
get_joints_state(): Actively updates the state of each joint of the robotic arm and returns the current joint angles.