Move Forward
1. Working Principle
According to the characteristic of mecanum wheel, when the car moves
forward, the four wheels must rotate clockwise. The force analysis is shown in
the following figure:
According to physical kinematics, when forces are equal and opposite to each
other, they will counteract each other. Any force can be decomposed into two
perpendicular vectors. Suppose the speed of wheel A and wheel B rotates at
the same speed, a right force decomposed by wheel A and a left force
decomposed by wheel B will counteract each other, which the direction of
resultant velocity is forward.
Based on Newton’s second law (F=ma), if the direction of acceleration is
forward, the final resultant force is also forward.
1
2. Program Flowchart
3. Program Download
Note: Remove the Bluetooth module before downloading the program. Or the program will
fail to download because of the serial port conflict.
1) Locate and open “forward_and_back/forward_and_back.ino” program file in
the same directory of this section.
2) Connect Arduino to the computer with the Type-B cable .
3) Click “Select Board”, and the software will automatically detect the current
Arduino serial port. Next, click to connect.
4) Click to download the program into Arduino. Then just wait for it to
complete.
2
4. Project Outcome
After the program is downloaded, turn on the robot. It will automatically move
forward for 1 second, turn left for 1 second, and turn right for 1 second.
5. Program Analysis
The source code of the program is located in the file “forward_and_back” in the
same directory as this lesson.
5.1 Define Pin and Create Object
Define the “motorpwmPin” array to store the PWM control pin numbers of the 4
motors. Define the “motordirectionPin” array to store the direction control pin
numbers of the 4 motors.
Declare three function prototype. “Motor_Init” for motor initialization;
“Velocity_Controller” for velocity control; “Motors_Set” for setting motor
speeds.
5.2 “setup()” Initialization and Motion Control
1) Initialize serial communication. Set the timeout for serial communication
data reading to 500ms. Call the “Motor_Init()” function to initialize the motors.
2) Call the “Velocity_Controller” function to set the motors to move forward at
a speed of 100 for 1 second.
3
3) Call the “Velocity_Controller” function to set the motors to turn left in place
at a rotation speed of 100 for 1 second.
4) Call the “Velocity_Controller” function to set the motors to turn right in place
at a rotation speed of -100 for 1 second.
5) Call the “Velocity_Controller” function to stop the motors.
5.3 “Motor_Init()” Motor Initialization Function
Set the direction pins of the motors to output mode. Call the “Velocity_
Controller” function to stop the motors.
5.4 “Velocity_Controller()” Velocity Control Function
1) Initialize the speed values of the 4 motors as integers. Set the speed factor
to 1. Set the “angle” to 90° to correct the direction of the robot. Then,
convert “angle” from degrees to radians.
2) Check if “rot” is 0. If it is, set “speed” to 1. Otherwise, set “speed” to 0.5.
4
3) Decrease the speed values.
4) Calculate the speed of the 4 motors based on the value of “drift”.
5) Use the calculated speed values of the four motors to set motors.
5.5 “Motors_Set()” Motor Speed Setting Function
1) Define arrays to store the PWM values, motor speed values, and motor
direction for each motor.
2) Loop through the 4 motors. Determine the motor direction based on the
motor speed value.
5
6. Function Extension
The default movement speed is 100. In this section, the robot speed will be
modified to 50. Please refer to the following specific steps:
1) Modify the speed and rotation speed parameters in the “Velocity_
Controller()” to 50.
2) After modifying, please refer to “4.Program Download” to download the
program. Turn on the robot again to check the outcome.
Note: The adjustable range of speed for going straight and turning is “-100~100”. When
the going straight speed value is positive, the robot car will move forward; when the value
is negative, it moves backward. When the turning speed value is positive, the car will
rotate counterclockwise; When the value is negative, it will rotate clockwise.