Welcome to RDK X5 Robot repository
8.Controlling robot movement
Controlling robot movement
Controlling robot movement8.1. Experimental Objectives8.2. Experimental Preparation8.3. Program source code8.4. Experimental effect
8.1. Experimental Objectives
Control the robot to move forward and backward, left and right, and set the relevant parameters of the car.
8.2. Experimental Preparation
MOTOR 1 is connected to the left front motor of the car, MOTOR 2 is connected to the left rear motor of the car, MOTOR 3 is connected to the right front motor of the car, and MOTOR 4 is connected to the right rear motor of the car.
SunriseRobotLib library functions needed to control the robot motors:
xxxxxxxxxxset_car_motion(v_x, v_y, v_z)Parameter explanation: Car motion control, this function will read the encoder pulse data to calculate the speed of the car. v_x represents the longitudinal speed of the car, in m/s, positive means forward, negative means backward; v_y represents the lateral speed of the car, in m/s, positive means left, negative means right; v_z represents the rotation speed of the car, in rad/s, positive means left, negative means right.
v_x=[-1.0, 1.0], v_y=[-1.0, 1.0], v_z=[-5.0, 5.0]
Return value: None.
xxxxxxxxxxset_pid_param(kp, ki, kd, forever=False)Parameter explanation: PID parameter control will affect the change of the movement speed of the car controlled by the set_car_motion function. By default, it can be left unchanged.
kp ki kd = [0, 10.00], decimals can be entered.
forever=True saves permanently, =False acts temporarily.
Permanent storage is to write data into the Flash of the MCU chip. The data will not be lost after restarting. The writing time is long, so a delay time is added to avoid the problem of packet loss in the MCU. Temporary effect is fast and effective once. The data will not be retained after restarting the chip.
Return value: None.
xxxxxxxxxxset_auto_report_state(enable, forever=False)Parameter explanation: The MCU automatically returns the data status bit, which is enabled by default. If it is set to off, it will affect the data reading function.
enable=True, the underlying expansion board will send data every 40 milliseconds. enable=False, it will not send.
forever=True permanent storage, =False temporary effect.
Return value: None.
xxxxxxxxxxclear_auto_report_data()Parameter explanation: Clear the cached data automatically sent by the MCU.
Return value: None.
xxxxxxxxxxreset_flash_value()Parameter explanation: reset the data saved in the car's flash and restore the factory default values. This function can also be achieved by pressing and holding the K2 button on the expansion board for about 10 seconds.
Return value: None.
The following functions all return data, and data can only be read when create_receive_threading() is started normally:
xxxxxxxxxxget_accelerometer_data()Parameter explanation: Get the three-axis data of the accelerometer
Return value: a_x, a_y, a_z
xxxxxxxxxxget_gyroscope_data()Parameter explanation: Get the three-axis data of the gyroscope
Return value: g_x, g_y, g_z
xxxxxxxxxxget_motion_data()Parameter explanation: Get the speed of the car, return val_vx, val_vy, val_vz
Return value: val_vx, val_vy, val_vz
xxxxxxxxxxget_motion_pid()Parameter explanation: Get the motion PID parameters of the car, read error returns [-1, -1, -1]
Return value: kp, ki, kd
8.3. Program source code
Turn on the robot power switch, and open the robot system or the browser of the remote computer to enter the Jupyter lab editor.
Reference code path: /home/sunrise/sunriseRobot/Samples/1_Basic/5_car_motion.ipynb
8.4. Experimental effect
When running to control the motor movement, three sliders will be generated. Changing the value of the slider can change the speed of the robot. Please suspend the car before modifying the value to prevent the car from running around. V_x controls the speed of forward and backward movement, V_y controls the speed of left and right translation, and V_z controls the speed of left and right rotation.

You can also run the stop cell to stop the car.

For other contents, please refer to the comments in the program.
Note: If you modify part of the configuration in this course and set it to be saved permanently, it will cause abnormal situations later (for example: set the automatic return data status enable=False and save it permanently as True, resulting in failure to read data normally). Please call the reset_flash_value() function, or press and hold the KEY1 button on the expansion board for about 10 seconds to restore the factory configuration, and then set the car type to 6 after restarting.