Control the Transbot to move forward and backward, turn left and right, and set the relevant parameters of the car.
The corresponding interface line sequence of the left and right motors is shown below:
Transbot_Lib library function is needed to control the motor of Transbot:
set_car_motion(velocity, angular)
Parameter explanation: control car movement, this function will read the pulse data of the encoder to calculate the speed of the car movement. Velocity represents the speed of the car, the unit is m/s, the positive number is forward, the negative number is backward, the angular represents the rotation speed of the car, the unit is rad/s, the positive number is left-handed, and the negative number is right-handed.
velocity=[-0.45, 0.45]
angular=[-2.00, 2.00]
Return value: None.
xxxxxxxxxx
set_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. No adjustment is required by default.
kp ki kd = [0, 10.00], decimals can be entered.
forever=True for permanent preservation, =False for temporary use.
Permanent preservation is to write data into the Flash of MCU. The data will not be lost after restarting, and the writing time will be longer. Therefore, the delay delay time is added to avoid the problem of packet loss in the MCU.
Temporary is single-time effective, and the data will no longer be retained after restarting the MCU.
Return value: None.
xxxxxxxxxx
set_speed_limit(line_limit, angular_limit, forever=False)
Parameter explanation: Set the minimum speed limit value (minimum value) of the trolley movement. It can not be set by default.
For example: when the linear speed limit value is set to 0.1, the next time the set_car_motion(0.05, 0) function is called, the microcontroller judges that 0.05 is less than 0.1, and automatically sets the input 0.05 to 0.1, if it is set_car_motion(-0.05, 0) , It will set -0.05 to -0.1. The angular velocity limit value also has the same function.
line_limit=[0, 0.20], angular_limit=[0, 1.00]
forever=True for permanent preservation,=False for temporary use.
Return value: None.
xxxxxxxxxx
set_imu_adjust(enable, forever=False)
Parameter explanation: Set the gyroscope to assist in adjusting the direction of the car movement. After the setting is turned on, the set_car_motion function will work when the angular velocity is set to 0. When the car is moving forward, due to the external force causing its moving direction to deviate from the beginning, the gyroscope will adjust the moving direction of the car to the original forward direction by adjusting the left and right speeds of the motor. This function is only an aid, and it cannot guarantee that the car will advance completely in a straight line. The same principle applies to backwards.
enable=True turns on,=False turns off.
forever=True for permanent preservation,=False for temporary use.
Return value: None.
xxxxxxxxxx
set_auto_report_state(enable, forever=False)
Parameter explanation: The MCU automatically returns the data status bit, the default is on, if the setting is off, it will affect the data reading function.
enable=True, the bottom expansion board will send data every 40 milliseconds. enable=False, do not send.
forever=True for permanent preservation,=False for temporary use.
Return value: None.
xxxxxxxxxx
clear_auto_report_data()
Parameter explanation: Clear the buffered data sent by the MCU automatically.
Return value: None.
xxxxxxxxxx
reset_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 the data can be read only when create_receive_threading() is started normally:
xxxxxxxxxx
get_accelerometer_data()
Parameter explanation: Obtain the three-axis data of the gyroscope
Return value: g_x, g_y, g_z
xxxxxxxxxx
get_gyroscope_data()
Parameter explanation: Obtain the three-axis data of the gyroscope
Return value: g_x, g_y, g_z
xxxxxxxxxx
get_motion_data()
Parameter explanation: get the speed of the trolley, return the linear velocity v, the angular velocity a
Return value: v, a
xxxxxxxxxx
get_motion_pid()
Parameter explanation: Get the motion PID parameters of the car, and return when reading error [-1, -1, -1]
Return value: kp, ki, kd
xxxxxxxxxx
get_imu_state()
Parameter explanation: Get the state of the gyroscope-assisted car movement, return True to indicate open, return False to indicate close, and return -1 if reading fails.
Return value: the state of gyroscope adjustment
Please check video
Code path: Transbot/Samples/9.car_motion.ipynb