In this tutorial, we will read the two Rotary Encoders’ position in FlexiRover robot car with Raspberry Pi control board.

Prerequisite:
We suppose you have installed the 520 motor and connected the motor wires to Raspberry Pi I/O Shield as per following tutorial:
https://osoyoo.com/2024/05/06/v5-generic-robot-car-for-raspberry-pi-running-with-mecanum-wheel/

Left and Right Encoders’wire Connection:

Above positions are the pin map of Magic I/O Shield GPIO pins which exactly matches Raspberry Pi GPIO layout .

Software Installation:
Download the encoder.py program file by typing following command in terminal (either ssh or putty terminal):

wget https://osoyoo.com/driver/v5car/encoder.py

After download the code, type following command in terminal:

python encoder.py

Now rotate your left encoder wheel or right encoder wheel, you will see following message in your terminal:

Encoder 1 Position: -49
Encoder 1 Position: -50
Encoder 1 Position: -51
Encoder 1 Position: -52
Encoder 1 Position: -51
Encoder 1 Position: -52
Encoder 1 Position: -51
Encoder 1 Position: -52
Encoder 1 Position: -53
Encoder 2 Position: 1
Encoder 2 Position: 2
Encoder 2 Position: 3
Encoder 2 Position: 4
Encoder 2 Position: 5
Encoder 2 Position: 6
Encoder 2 Position: 7
Encoder 2 Position: 8
Encoder 2 Position: 9
Encoder 2 Position: 10

Encoder 1 is the right motor encoder, Encoder 2 is the left motor encoder. By reading the value, you can calculate the left motor and right motor rotation speed which is quite important to PID algorithm.

PID algorithm Sample Code:

Based on above hardware and encoder installation, we can apply a PID program to make the car move straightly forward.
Type following command in terminal and download pid.py python code:

wget https://osoyoo.com/driver/v5car/pid.py

 

Type following command to run pid.py in terminal

python pid.py