top of page

DIFFERENTIAL DRIVE ROVER: PART I

1. Introduction

In this project, we'll develop and control this differential drive robot:

roverC.png

A differential drive robot is a vehicle whose movement is solely based on two separately driven wheels placed on each side of its body. Its heading direction isn't changed with a mechanism, such as the steering wheel of a car, but is done simply by altering the wheels' speeds.

​

For example, if both wheels turn in the same direction and at the same speed, it will drive forward or backward. If they turn at opposite same speeds, it will turn in place. By combining the speed of each wheel we can have different sets of linear and angular speeds.

rover_top_view.png

right wheel

left wheel

(top view without some of the parts)

Since it only has two wheels, we need a third support point in order for it to remain flat on the floor. For that purpose we use a caster ball that can roll in any direction:

rover3.png

caster ball

The wheels are driven by two independent 12V DC motors, which are fixed to the robot structure. Each one of them has an attached hall effect "encoder", which outputs electric pulses that we'll use to calculate the displacement and speed of the wheels.

rover_wheels.png

dc motor

hall sensors

disk with magnets

gearbox

The robot also has a forklift mechanism that we can use to lift objects. Its movement is given by an attached servo motor: 

rover_forklift_up.png
rover_forklift_down.png

The hardware that we'll use to control the robot is made of three boards: an Arduino MKR1000, which will handle the logic, an MKR Motor Carrier, which will drive the motors and the servo, and an MKR IMU Shield which we can use as a second source of data for the robot's speeds, positioning, and direction, although it's not essential to the project. The robot is powered by an 11V lipo battery which remains at the bottom of the structure.

rover_boards.png

IMU Shield

MKR1000

MKR Motor Carrier

Battery

rover_boards_2.png

The colored circles on the top will be used by our image processing algorithm to calculate the coordinates and heading of the robot. We also have an ultrasonic sensor in the front that we can use to prevent collisions and avoid obstacles:

Ultrasonic sensor

rover_front.png

2. Basic control using Matlab

Now that we understand how the robot works, we will begin to control it using Matlab. Basically, we will connect the Rover to the program and create objects for each motor that will allow us to put them in motion by setting its parameters and turning them on and off.

​

The first thing we need to do is to establish a connection between Matlab and the Arduino board. After connecting the board to the computer through the USB cable, we will type in the following command:

arduinosetup.PNG

This will bring up the setup window. In this particular case we want to control the robot wirelessly so that it can move freely on the ground, therefore we will select the WiFi type and fill in the boxes with the required information. The SSID is the name of our internet connection. The port number will be automatically filled and we can leave it as it is.

wifisetup.PNG

Next, we will select the board we are using, the port number, and the libraries we want to include when we create a generic Arduino object. In this particular project, the only one we will need is the MKRMotorCarrier. This library already contains all the functionality of the devices that can be attached to the Motor Carrier, such as the dc motors, encoders, and servo in this project. Afterward, click Program.

arduino uploading.PNG

It may take a few minutes to finish uploading the code to the board. When it is finished, you will see a success message below the button. If you get an error, make sure you entered the correct WiFi information and selected the correct board and port number and try again.

arduino success.PNG

We have finished setting up the communication between the board and Matlab! Click Next on the current window and Test connection on the next one to make sure it is really established:

arduino test connection.PNG

Finally, click Next and then Finish to exit the setup.

​

Now we can begin creating the objects we need. As you will notice, we will setup layers of communication. First, we will create an object for the board. Then, we will create a Motor Carrier object linked to the board, and finally, we will create motor and encoder objects linked to the Motor Carrier.

​

Let us start by creating the board and carrier objects:

arduino objects.PNG

Next, we create the dc motors and the encoders objects. Note that the second input parameter in each of these methods is the Motor Carrier pin to which they are connected.

arduino_object2.PNG

At this point we can disconnect the USB cable from the board since we have already established a wireless connection.

​

We now use the function Speed to set the voltage being fed to the motors and consequently how fast it will turn. The range goes from (full forward power) to -1 (full reverse power).

The functions start and stop are used to turn the motors on and off. Let us first try them on the right motor to make sure it is working.

​

First, we set the power to 25% going forward:

right motor speed.PNG

Note that the forward and reverse direction depend on the order we connected the motor voltage supply wires on the Motor Carrier. If we had inverted the order, then -1 would be forward and 1 backward.

​

Then, we type the following commands to turn on the right motor for 3 seconds.

dcmr start stop.PNG

3. Calculating the angular displacement

The next step is to to read the encoder data so that we can know how much the wheel has turned. This is the information we will use in future parts of the project to know the robot's position and speed at any given moment.

​

The encoder attached to the dc motor is a magnetic one with quadrature output. Its geometry is such that there are 3 full cycles of quadrature when the motor turns one full revolution. Each cycle of quadrature equals 4 signals changes or "pulses", therefore, in our case, one full motor shaft revolution equals 12 encoder pulses.

 

Moreover, there is a reduction gear with a ratio of 100:1 between the motor and the output shaft. Hence, the formula to calculate how many degrees the wheel will turn is:

formula.PNG

Let us test it with the right wheel as before. We declare the new constants that we are going to use and set the motor speed to 25%:

ppr and gear ratio.PNG

Then, we will reset the encoder count using the resetCount function, turn on the motor for 1 second, read the number of encoder pulses using the readCount function, and calculate how many degrees it has turned using the formula above. We will call it rwDisp (right wheel displacement):

right wheel displacement.PNG

In this case the wheel turned 372 degrees, which we can see is accurate from the yellow stick we placed on it as a reference.

​

To run it multiple times in a faster way and test the accuracy of the system, we can write all the commands in one line. That way we can just press Shift + Up Arrow and Enter in order to run it again: 

right wheel displacements.PNG

As we can see from the results, the wheel does not always turn the same exact number of degrees for a given power (in this case 25%). This comes from the fact that the motor, as with anything else in the real world, is not perfect. The results will also vary depending on the battery levels. The lower the battery, the less it will turn for the same specified power.

4. Calculating the angular speed

Since now we know how to calculate how much the wheel has turned, it should be pretty straightforward to obtain the angular speed. If we divide the angular displacement by the time it took to turn we will have its average speed for that time period. To get the exact time value we can use the Matlab functions tic and tac. It works like a stopwatch: the first one will start it and the second one will return how much time has passed since the beginning. We will store this value in the dt variable:

right wheel speed.PNG

In this test, the wheel speed was around 376.8 degrees/second, which we can confirm from the fact that the motor was on for 2 seconds and the wheel turned a bit more than two full revolutions.

5. Moving the robot around

Now that we know how to control the motors that turn the wheels, we can put the robot on the ground and see how it moves around. It should be intuitive that if both wheels turn forward or backward at the same speed, the robot will drive straight. Let us try that:

drive straight.PNG
drive straight.PNG

In these two examples, we can already see one of the problems that we will have to overcome: the robot will not drive in a straight line when the motors are supplied with the same voltage. In theory, it should, but the motors are not perfectly equal, so one of them may turn a little faster than the other. Mechanical problems such as the wheel not being aligned can also cause this problem. We will use closed-loop control to fix this issue in future chapters.

To make the robot turn in place, one wheel has to turn forward and the other backward, depending on the direction we want it to turn. To turn to the right, its the left wheel that has to turn forward, and vice-versa. Let us try that as well:

turn in place.PNG
turn in place.PNG

When the robot turns in place it is harder to notice, but here too the motors do not turn at the same speed. In this case, it will cause the robot to move a little. We will also address this problem in the next phases.

If we just turn one of the wheels, the robot will not turn in place. Instead, its center of rotation will be the other wheel:

turn on wheel.PNG
turn on wheel.PNG

Finally, we can combine the movement of both wheels to make it turn a lot or a little while at the same time going forward.

movements.PNG
movements.PNG
movements.PNG

6. Controlling the forklift

The forklift movement is performed by the servo motor. To control it, first we create a servo object connected to the motor carrier. Then we use its function writePosition to move it to the desired angle. The range goes from 0 (0°) to 1 (180°):

servo.PNG

7. Conclusion

The way we are controlling the robot so far is very intuitive and straightforward. If we want it to perform more complex tasks, such as driving and turning at a specified linear/angular speed, moving to a specific position, or following a designated trajectory, we will need to develop it further. Furthermore, we have a problem when we want it to drive straight or turn in place due to the fact that the motors are not perfectly equal.

 

The next step will be to obtain the equations of motion for this particular robot, technically called kinematics. From there, we can develop algorithms using control theory for it to perform specific tasks and also add closed-loop control to address the not turning/driving straight issue.

1. Introduction
2. Matlab
3. Displacement
4. Speed
5. Driving
6. Forklift
7. Conclusion

Projects

Part 2

© 2025 by Matheus Lino

bottom of page