Line Following Robot For Weight-Based Goods Distribution: Mechatronics System Design Project
Line Following Robot For Weight-Based Goods Distribution: Mechatronics System Design Project
2
Image 1.3 AGV Tugger
1.2 Mechanical Structure of Some AGVs
1.2.1 Pioneer 3-DX
Developed by Adept MobileRobots in the USA, Pioneer 3-DX is a classic 3-wheel
differential-drive robot widely used for research in navigation, SLAM, and multi-robot
coordination. Its simple and reliable design makes it a popular choice for introductory
robotics projects and small research groups
3
platform designed for field robotics and human-robot interaction research. Its advanced
control system and higher cost mean it is usually seen in well-funded labs rather than hobby
projects.
4
- Most stable among configurations complex fabrication
- Reduces slippage when turning by shifting - Must resolve the issue of even contact
the instantaneous center of rotation between 4 wheels
5
1.3.1.1. Camera (Vision system)
6
An IR sensor uses an infrared LED as the emitter and a photodiode as the detector. It
measures reflected IR light to distinguish between black and white surfaces, making it
common in line-following robots.
Principles: The emitter is an IR LED and the detector is an IR photodiode. The IR
photodiode is sensitive to the IR light emitted by an IR LED. The photo-diode’s resistance
and output voltage change in proportion to the IR light received. This is the underlying
working principle of the IR sensor.
1.3.1.3. Phototransistor
A phototransistor functions like a normal transistor but is triggered by light instead of
electrical current at the base. It is simple, fast, and inexpensive, though sensitive to ambient
light.
Principles: Works like a normal transistor, but instead of using a base current to switch
it on, it is activated by incident light (visible or infrared). When photons strike the
7
semiconductor material, they generate electron–hole pairs, which increase the base current
and allow a larger current to flow between the collector and emitter.
9
Figure 5: Load cell's structure and principles
Pros:
High accuracy and repeatability.
Wide measurement range (grams to tons).
Reliable for long-term use.
Cons:
Requires external amplifier (HX711).
Sensitive to mounting and mechanical alignment.
More expensive than simple pressure or force film sensors.
10
Figure 6: Relationship between force and resistance
Pros:
• Thin, lightweight, and easy to integrate.
• Simple to interface (just a resistor divider).
• Low cost for basic force detection.
Cons:
• Non-linear response, not suitable for precise measurements.
• Limited repeatability and drift over time.
• Less accurate compared to load cells.
1.3.3. Motor:
Motor receives command from microcontrollers to follow the path after tracking lines
with sensors. For the robot projects, there are some common motors that have been used.
DC motor: An electrical machine that converts direct current (DC) electrical energy
into mechanical energy, typically in the form of rotation.
BLDC motor: A brushless DC motor that uses electronic commutation instead of
brushes, offering higher efficiency, longer lifespan, and lower maintenance.
Stepper motor: An electromechanical device that divides a full rotation into precise,
fixed steps, making it ideal for position control without feedback.
Servo motor: A motor equipped with a control circuit and feedback mechanism
(often using a potentiometer or encoder) to achieve precise position, speed, or torque
control.
11
Table 1: Motor comparison
Type Pros Cons
DC motor Simple, cheap Short lifespan
Easy control Hard for precise position
Good torque control
BLDC motor Quieter than DC motor Required advanced
Long lifespan controller
Good torque
Stepper motor Precise step movement Less efficiency at high
Suitable for slow torque speed speed
Servo motor Excellent precision High price
Fast response Heavy
Maintain accuracy over dynamic loads
Pros Cons
12
1. Use one MCU for tons of tasks is
1. It minimize the size of control
potentially risky. Because the CPU
system. It cost less resources.
have to simultaneously processes
2. Easier for mechanical design. The
various task. It may cause RAM or
space only use for one MCU instead
Flash problems.
of three.
2. Find obstacles in debugging. When
3. The size and power circuit and
the system confronted with an error,
electronics circuit is smaller.
you have to dive into a central MCU
Leading to less power consumption.
which processes everything.
- Based on the Pros and Cons, this architecture only suitable for small project or
system.
13
Pros Cons
- The main advantage is this - This architecture consume more
architecture reduce the load of resources.
program on one MCU. Each MCU - It needs more space for mechanical
will only process a specific task. By design. Leading to Increase the
doing that, it reduce the chance of overall size of the robot.
error occurring in the MCU. - It needs more power to supply
- It is easier for developer to debug various MCU compared to one
the program. And easier to navigate MCU.
the source of problem, when each - It reduces the reliability of the whole
MCU is assigned a specific task. system. Because when more
You can easily isolate a MCU for components are added, the reliability
testing. of the system diminished.
14
- The algorithm is designed for a line follower robot, that moving in a set of square line.
Therefore, they will use sensor to detect the junction (crossing point).
- The steps to control the Robot can be describe as followed:
o Read the input: “Sensor value” in this case.
o Solving and interpreting the sensor data: “Junction detected” in this case.
o After that, control the Robot based on the result of data, this is called
“Decision Making”.
o Repeat the process.
15
- This is a system architecture for an industrial or an autonomous car. This system is
more complex. They divided the system into 3 classes:
o Perception and environment mapping.
o Motion Planning.
o Control system.
- This architecture is using hierarchal control system structure, the system is controlled
from low to high.
- Dive into the decision-making part, the articles mentioned they are using FSM
(Finite – System Machine) for decision making.
16
1.5.1.2. Interpolation method
The purpose of the interpolation method is to find the estimated position of the center
of the line to the center of the robot, hence control the robot to track the line. The signal
collected from the array of sensors is a group of discrete data values. Interpolating here is to
create a group of new data about the position of the line from the signal of the sensor.
a. Quadratic interpolation
We begin with the process of defining the robot's position at the start point of the line.
Finding the sensor position with the highest values, we can determine the range of interest of
the line that the robot is following. Assuming that, in this range of interest, we can perform a
calculation of the quadratic curve from their output signal values (for example, a, b, c of
2
ax +bx +c ). Therefore, we can define the position of the line at the specific point in the range
of interest by calculating the position of the highest value of the curve.
b. Weighted Interpolation
17
-The coordinates of the 7 sensors are x 0 , x 1 , x 2 , x 3 , x 4 , x 5 , x 6 , respectively, and the
∑ xi yi 3 ( y 6− y 0 ) +2 ( y 5− y 1) +( y 4 − y 2 )
x= i=07 = 7
∑ yi ∑ yi
i=0 i =0
In the research "An intelligent line-following robot project for introductory robot
courses,", the line tracking of the quadratic interpolation and weighted interpolation are
5,4mm and 2,6mm, respectively. We conclude that Weighted Interpolation is the method that
provides the smallest tracking error.
18
The robot’s line-following control is formulated via an error model, and stability is
proven using the Lyapunov criterion.
After doing some kinematic analysis, we get the tracking error as following:
[][ ]
e1 cos ( φ) sin ( φ) 0
e 2 = −sin (φ) cos (φ) 0
e3 0 0 1
[ ] [ ][ ][ ]
ė 1 v R cos ( e3 ) −1 e2
= + v
ė 2 v r sin ( e 3 ) 0 −d−e 1
ω
ė 3 ωr 0 −1
( 1)
The PID controller works well on straight segments. However, at sharp turns the robot
drifts off the line. This happens because the sensors lose the line, leading to incorrect error
computation and thus deviation. In this case an additional open-loop mechanism is used:
when all sensors lose the line, only the two outermost left/right sensors are used. If the last
error comes from the rightmost sensor ⇒ the line is to the right, so turn right; similarly for
the left. The motor speeds are then tuned to execute fixed-angle turns.
Remark: This is suitable for straight runs but not for curved profiles. On different
tracks, the motor speeds must be recalculated and retuned, so the approach is not optimal.
19
fuzzification, or fuzzy inference process that converts crisp sensors value into fuzzy world,
computation, where the fuzzy value is being treated, and defuzzification, where the fuzzy
output is converted back into crisp values mainly for actuators.
In this research, the Fuzzy Controller gives a better performance than the PID controller. In
detail, it performs a faster and efficient tracking compared to the PID controller.
But the process of the fuzzy controller is quite complicated. In this research, the author
had to process the value of digital sensors in the fuzzification stage, build a proper rule-based
decision-making process, and then defuzzify to obtain the output crisp value for the
differential drive of the motor. It’s quite challenging to understand, and needs experience at
some extent to build the rule-based decision-making process.
20
Additionally, a line tracking algorithm based on Lyapunov stability theory was
integrated to ensure error convergence. With this approach, the system error asymptotically
approaches zero as t→∞, guaranteeing robust stability in the tracking process.
The Fuzzy Controller is not chosen here because the surface of the map is plain, which
couldn’t optimize the performance of the Fuzzy Controller. Moreover, building the rule-
based decision-making also requires experience to some extent.
The proposed block diagram for the control structure:
21
CHAPTER 2: ANALYZING AND DESIGN OPTIONS
2.1. Mechanical selection:
We chose a three-wheel configuration with two front-driven wheels and a free caster
wheel at the rear. This setup is simple, reliable, and sufficient for our line-following
task, where the robot does not need to move at very high speeds.
Using three wheels avoids the challenge of keeping four wheels perfectly coplanar,
which would otherwise require a suspension or leveling mechanism.
Front-wheel drive is preferred since the robot mainly moves forward; it also improves
stability during turns by reducing the tendency to tip diagonally forward.
23
Conclusion: IR reflectance sensor is most suitable choice for our line-following robot, it
offers good black/white distinction, fast response, minimal influence from outside light, and
cost-efficiency. Moreover, its weakness can be solved by calibrating in code or hardware.
Based on prior experience and supplier recommendations, we propose using the
TCRT5000 IR sensor, which offers reliable performance and cost-effectiveness for student
projects.
Table 2.2: Technical Specifications of TCRT5000 Sensor
Parameter Value Unit
Operating range 0.2 – 15 mm
Wavelength of emitted 950 nm
light
Emission angle 16 °
Reception angle 30 °
Maximum current through 100 mA
phototransistor
Maximum current through 60 mA
LED
24
Figure 7: IR sensor diagram
Sensor arrangement:
From document references and debate in robot forum, number of sensors should be at
least 3. With 1 at center and 2 sensors on two sides to detect direction and send to MCU.
26
Figure 2.10: Load cell
27
Figure 2.11: BT6612 driver
Table 2.4: BT 6612's technical specification
Parameter Value / Range
28
RPM
40
35
30
25
20
15
10
5
0
0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 00 10 20 30 40 50 60 70 80 90 00 10 20 30 40 50
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2
RPM
Efficiency Up to 80–90 %
29
Package TO-263 (LM2596S IC) / module available
Isolation:
DC Input Current 2 mA
30
31
CHAPTER 3: MECHANICAL DESIGN
3.1. Wheel selection:
Driving Wheels: Select V3 wheels with a diameter of 65 mm from Hshop.
Caster Wheel: Select a caster wheel with a height of 40 mm from Hshop. P
32
Fms2: Rolling friction on driven wheel
Pstop: Motor stopping force
L = L1 + L2 = 0,3m: Distance between front and rear wheel axles
h=0,0015m: Height from vehicle center of mass to ground
r: Wheel’s radius
M=4kg: robot’s mass
g = 9.8 𝑚/𝑠2: Gravitational acceleration
a=0,5 m/s2: Acceleration
Force balance equation when robot start to slow down:
{
∑ F x =0
∑ F y =0
∑ M x /C=0
{
2 P stop +2 F ms 1 + F ms2=2 P stop +2 μ 1 N 1 + μ2 N 2=Ma
2 N 1+ N 2 =Mg
N 2 L1−F ms 2 h−2 F ms1 h−2 N 1 L2=0
{
2 P stop +0 , 03 N 1 +0 , 04 N 2=2
→ 2 N 1 + N 2=39 , 2
N 2 L1−2 N 1 L2 + F ms2 h+2 F ms1 h+2 Pstop =0
{
N 1=40 Pstop −8 , 64
→ N 2=56 , 48−80 P stop
16,974−24 P stop−39 , 2 L2=0
In order to make the robot stay flat on surface, both N1 and N2 must grater than 0N
→
{ 0,216 N < Pstop < 0,706 N
16,974−24 P stop−39 , 2 L2=0
→ 0,002 m< L2 <0,301 m
We choose L2 = 200mm.
𝐺 𝐹𝑙𝑡
𝑁𝑤
𝑁𝑤
ℎ
𝑃
2
Pb Mv Mgb
F ¿ h− ≤0⇒ h− ≤0
2 R 2
gb R min 9 , 81× 0 ,18 × 0 ,5
⇒h≤ 2
= 2
=1 , 77(m)
2v max 2 ×0 , 5
In order for the car not to slide when cornering, the centripetal force, in this case, the
total frictional force acting on the vehicle is greater than the centrifugal force:
2
∑ F ms ≥ Ma ⇒ μMg ≥ MRv ⇒ v ≤ √ μgR
34
Figure 3.3: Vehicle force calculation diagram
Where Pk is Motor traction force
Newton’s Second Law:
{
2 P k −2 f r 1 −f r 2=2 P k −2 μ 1 N 1−μ 2 N 2=Ma
2 N 1 + N 2=Mg
2 N 1 L2=N 2 L1
𝑅 𝜏
𝑎
𝑚 𝑃
𝑁
𝑓𝑟 𝑃𝑘
35
Based on section 4.2 of [1], we choose motor’s safety factor is 1.5.
Motor power:
P=1 , 5 τω=1, 5 ×0 , 06 × 15 ,38=1 ,38 (W )
Table 3.1: Motor selection
Specification GA25 - 370 Required
Voltage 12 VDC /
No-load speed 280 rpm /
Loaded speed 215 rpm 112,4 rpm
Power ? 1,38 W
Loaded torque 0,2 0,07 Nm
Encoder pulse 11 /
Speed ratio 21,3 : 1 /
Price 195k /
To manufacture the robot frame and motor mounts, we use AWEA F-87 CNC Milling
machine, with the accuracy of 0,008mm.1
Based on Chapter 2 of book [2], we calculate:
T 8
a= = =5.4
i 1 , 48
With
T =8 μm:range of tolerance
i=0 , 45 √ D+0,001 × D=1 , 48
3
1
Raza N.Jazar, Vehicle Dynamic: Theory and Application
36
Figure 3. Dimensional chain formed along the y direction.
Where:
A1 and A2 are the increasing component link.
A3 and A4 are the decreasing component link.
AΣy is the closed loop link.
Therefore, we have:
{
E S 1=+0.08 mm
+ 0.008
A1=2 E I 1=0 mm
T 1=0.08 mm
{
E S 2=+0.08 mm
+0.008
A2=19.75 E I 2=0 mm
T 2=0.08 mm
{
e s3 =0 mm
A3 =2−0.008 e i 3=−0,008 mm
T 3 =0.08 mm
{
e s 4 =0 mm
A 4=19.75−0.008 e i 4 =−0,008 mm
T 4 =0.08 mm
{
A Σy =0 mm
→ E S Σy =+0,016 mm
E I Σy =−0,016 mm
+ 0.016
→ A Σy =0−0.016
{
E S 1=+0.08 mm
+0.008
A1=0 E I 1=0 mm
T 1=0.08 mm
37
{
E S 2=+0.08 mm
+0.008
A2=13.25 E I 2=0 mm
T 2=0.08 mm
{
e s 3=0 mm
A3 =0−0.008 e i3 =−0,008 mm
T 3=0.08 mm
{
e s 4 =0 mm
A 4=13.25−0.008 e i 4 =−0,008 mm
T 4 =0.08 mm
{
A Σx =0 mm
→ E S Σx =+0,016 mm
E I Σx =−0,016 mm
+ 0.016
→ A Σx =0−0.016
Concentricity error is √ 0.032 +0.0322=0.045 mm , which is acceptable.
2
38
Figure 3.5 Displacement on the chassis
39
3.6. 3D Model of the Vehicle Structure
40
CHAPTER 4: ELECTRICAL DESIGN
4.1. Sensor circuit design:
4.1.1. Resistor selection for line sensor:
From [tcrt50 datasheet], we have the basic structure for sensor operation as shown
below:
41
Figure 4.312: Current Transfer Ratio vs. Forward Current and Forward Current vs. Forward
Voltage of TCRT5000
Hence we choose I F =10 mA to achieve most efficient CTR, hence we choose
V F=1.12 V corresponding to the right figure.
From the left haft of figure 4.2, we have formula to calculate the R1:
V cc −V F 5−1.12
R 1= = =388 Ω
IF 0.01
Hence, we choose R1=470 Ω according to standard resistor in the market.
To calculate this value, we must examine value from sensor with each level of height
with each step = 1 mm.
42
Figure 4.514: Examine model schematic
43
Table 4.1: The result after measuring
DISTANC BLACK WHITE DIFF
E
We can see from the table, the result reached highest stability from the distance 11 to
14, hence we stop measuring at height of 15.
1000
800
600
400
200
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Black White
Figure 4.6: Chart of difference between black and white
44
The most difference between 2 background (Black and White) is at height at 12.
For better consideration, we examine the contrast when measure the ADC value within
each 2 mm from centerline.
Position Value
-20 44
-18 116
-16 325
-14 843
-12 902
-10 905
-8 902
-6 902
-4 920
-2 920
0 924
2 921
4 918
6 902
45
8 916
10 904
12 892
14 890
16 592
18 110
20 45
In the black line (-14 to 14), the ADC value is higher noticeably than the white line (-16 to -20 and 16
to 20).
Hence the distance from sensor to line is 12 mm is our choice.
4.1.3. Distance from the sensor to sensor:
46
Figure 17: Model alone
We have Lsmin is the projection area distance of sensor and 10.2 mm is the width of sensor according
to datasheet.
Lsmin =R+r −d
With:
R( 12−0.7 ) tan 15o : Radius of detector area
r = ( 12−0.7 ) tan 8o : Radius of emission area
d = 3.8 mm: Distance between emission and detector
Hence,
Lsmin =8.4 mm
The smallest distance is the distance that help avoid projection overlap between each sensor.
The projection area of sensor has distance Lsmin =8.4 mm while it’s width is 10.2 mm, thus, the
smallest distance is 10.2 +1 = 11.2 mm with 1 is safety coefficient. The distance between each sensor
must be larger than 11.2 mm. [1]
And we also want two sensors to be at the edge of the line which will play a role in detecting line for
straight movement. [2]
From figure 21, we see that at 14 mm from the center line, the analog signal is still considerable
contrast between black and white area. [3]
From [1], [2], [3], we decided the distance between each sensor is 14 mm
4.1.4. Sensor’s arrangement:
The switching distance is the distance from the sensor to the target surface at which the sensor’s
output changes state (ON/OFF).
Figure 23 shows that when we arrange sensors like in position 2, the switching distance is higher,
thus, the result is the line detection is much smoother. We decided to make sensors on our board
perpendicular to the line for higher switching distance.
The number of the line detection board is 5 sensors. 3 sensors will help detecting line and when our
robot does the curve, however, we will use two more sensors on two sides for higher efficiency when
making the curve or at junction.
47
Figure 19: Sensor arrangement
When testing sensors, we saw that the range of each sensor is difference although they are in the same
position with the same height. The result is that the value they gave is different and not like each
other. This can be an obstacle for coding and choosing thresholds.
Thus, we will make calibration to receive similar value from sensors at the same position.
From data from “Calibrating sensor” by Bill Earl, we have the equation for calibrating sensor is:
( xi −ximin )
y i= y min +
x imax −ximin
( y max − y min )
With:
x i :The current value∈the original range
x min , x max :The original minimum∧maximum values when∈the white∧black area
y min , y max :The desired minimum∧maximum values when∈the white∧black area
y i :Value after calibration
To determine x min , x maxand choosing y min , y max , we measure 5 sensors in black and white area.
48
Figure 20: Measure model
1 3843 866
2 3865 984
3 3945 1426
4 3771 1128
5 3987 1869
∑ x imin
1
y min = =3883
5
5
∑ ximax
1
y max = =1255
5
Thus, calibration equation for 5 sensors are:
( x i−866 ) × 2628
y 1=1255+
2977
( i
x −984 ) ×2628
y 2=1255+
2881
( x i−1426 ) × 2628
y 3=1255+
2519
( x i−1128 ) × 2628
y 4 =1255+
2643
( i
x −1869 ) × 2628
y 5=1255+
2118
49
4.1.6. Schematic and board simulation:
50
CHAPTER 5: CHOOSING THE CONTROL STRUCTURE
AND MCU
1. Choosing the control structure
Based on the load of algorithm programming and data processing, we believe that the
working ability of a MCU can totally handle all of the work. Therefore, we decided to choose
Centralized control Structure.
2. Choosing the MCU
There are several famous MCU in the market so-far, however, there 3 main
types that are famous and suitable for the college student:
2.1. Arduino
51
Easy to program using the Arduino Boards are not very durable or
IDE, which is intuitive and widely reliable for demanding or long-term
adopted. industrial use.
- Large Supportive Community: A - Poor Noise Immunity: Prone to
huge global community provides electrical disturbances and not
tutorials, forums, and suitable for harsh industrial
troubleshooting help. environments.
- Extensive Library Ecosystem: - Restricted Peripheral Options:
Thousands of open-source libraries Fewer built-in peripherals and
are available, enabling quick expansion options compared to
integration with sensors, modules, professional-grade microcontrollers,
and IoT functions. which limits complex IoT
applications.
2.2. STM32
Pros Cons
- Industrial-Grade Reliability: - Steep Learning Curve: Requires
Designed for robust operation with developers to have deep knowledge
strong noise immunity, making it of hardware architecture and low-
suitable for industrial environments. level programming.
52
- Powerful Processing: Based on - Limited HAL Library Quality:
ARM Cortex architecture, enabling Vendor-provided Hardware
high-speed computation and Abstraction Layer (HAL) libraries
efficient task handling. are often poorly structured and not
- High Flexibility & Optimization: developer-friendly.
Direct register-level configuration - No Built-In Wireless Connectivity:
allows fine performance tuning and Lacks integrated Wi-Fi or Bluetooth
efficient resource usage. modules, limiting IoT application
development without external
components.
2.3. ESP32
Pros Cons
- High Processing Capability: - Lower Robustness: Not as durable
Equipped with a strong CPU (up to or reliable as industrial-grade MCUs
… MHz), capable of handling like STM32, especially in harsh
multitasking and complex environments.
operations. - Moderate Processing Power:
- Robust Development Framework: While capable, its computational
Comes with ESP-IDF, a well- performance is generally lower than
maintained vendor-provided API STM32’s high-performance Cortex
framework, along with cores.
53
comprehensive documentation for - Learning Curve for Developers:
easier development. Although easier than pure register-
- Built-In Wireless Connectivity: level programming, developers still
Includes integrated Wi-Fi and need to become familiar with ESP-
Bluetooth modules, making it ideal IDF, FreeRTOS concepts, and Wi-
for wireless communication and IoT Fi/Bluetooth stack integration.
applications.
- Cost-Effective: Offers powerful
features at a low price point, making
it one of the most affordable MCUs
for IoT projects.
→ I think with our application, we use a simple line – follower robot, with many
IOT application, I think ESP32 is the most suitable for our application.
Coding Process
Pin assignment
54
Motor Direction Control
55
56
Encoder Reading and Interpreting with 4x Decoding
/* ---------- APIs ---------- */
esp_err_t encoderInit(void)
{
pcnt_unit_config_t unit_config =
{
//* Because this is a signed 16-bit counter
.high_limit = 32767, // upper limit
.low_limit = -32768 // lower limit
};
57
ESP_ERROR_CHECK(pcnt_new_unit(&unit_config, &pcnt_unit_motor_a)); // Initialize
counter for motor A
ESP_ERROR_CHECK(pcnt_new_unit(&unit_config, &pcnt_unit_motor_b)); // Initialize
counter for motor B
ESP_LOGI(TAG, "Created PCNT units: motorA=%p, motorB=%p", pcnt_unit_motor_a,
pcnt_unit_motor_b);
/*
* Use both Channel A and Channel B for 4× decoding:
* - Each encoder cycle has 11 pulses per motor rev (per channel).
* - With gearbox ratio 21.3:1 → ~11 × 21.3 = 234 cycles/output rev.
* - 4× decoding counts every edge (rise+fall of A and B):
* 234 × 4 ≈ 936 counts per output revolution.
* - Resolution = 360° / 936 ≈ 0.38° per count.
*
* → Therefore, we must use BOTH channels (A & B) to detect direction
* and to maximize resolution (4× decoding).
*/
58
pcnt_chan_config_t chan_b_motor_a_config =
{
.edge_gpio_num = ENCODER_MOTORA_PHASEB_PIN, // Channel B drive the
counter (Motor A)
.level_gpio_num = ENCODER_MOTORA_PHASEA_PIN // Channel A decides
direction (Motor A)
};
ESP_ERROR_CHECK(pcnt_new_channel(pcnt_unit_motor_a, &chan_b_motor_a_config,
&pcnt_chan_b_motor_a)); // Config channel from a unit
ESP_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_chan_b_motor_a,
PCNT_CHANNEL_EDGE_ACTION_INCREASE,
PCNT_CHANNEL_EDGE_ACTION_DECREASE));
ESP_ERROR_CHECK(pcnt_channel_set_level_action(pcnt_chan_b_motor_a,
PCNT_CHANNEL_LEVEL_ACTION_INVERSE,
PCNT_CHANNEL_LEVEL_ACTION_KEEP));
pcnt_chan_config_t chan_b_motor_b_config =
{
.edge_gpio_num = ENCODER_MOTORB_PHASEB_PIN, // Channel B drives the
counter (Motor B)
.level_gpio_num = ENCODER_MOTORB_PHASEA_PIN // Channel A decides
direction (Motor B)
};
ESP_ERROR_CHECK(pcnt_new_channel(pcnt_unit_motor_b,
&chan_b_motor_b_config, &pcnt_chan_b_motor_b));
59
ESP_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_chan_b_motor_b,
PCNT_CHANNEL_EDGE_ACTION_INCREASE,
PCNT_CHANNEL_EDGE_ACTION_DECREASE));
ESP_ERROR_CHECK(pcnt_channel_set_level_action(pcnt_chan_b_motor_b,
PCNT_CHANNEL_LEVEL_ACTION_INVERSE,
PCNT_CHANNEL_LEVEL_ACTION_KEEP));
int get_encoder_count_motor_a(void)
{
int count = 0;
pcnt_unit_get_count(pcnt_unit_motor_a, &count);
return count;
}
int get_encoder_count_motor_b(void)
{
int count = 0;
pcnt_unit_get_count(pcnt_unit_motor_b, &count);
return count;
}
60
CHAPTER 6: SYSTEM MODELLING
6.1. Robot Kinematic Modelling
Because of the working conditions of the robot: constant load, plain map, we decided to
use the kinematic model because using the dynamic model in this case is unnecessary.
From the principle diagram of the robot, the kinematic model can be denoted as
follows:
GR= d (m): the length from the driving shaft to the tracking point
ω : Rotation velocity of the robot
R (m): Radius of the driven wheels
G, R: Center point of the driving shaft and tracking point, respectively.
E: is the reference point that the robot needs to follow.
For the robot to track the line properly, an error model of the robot must be made to
design the line tracking model. The model in the figure below defines the error of the system.
We define the errors as follows:
e 1: is the error from the reference point E to the tracking point R in the horizontal
direction.
e 2: is the error from the reference point E to the tracking point R in the vertical
direction.
e 3: is the error from the GR axis to the line-tracking vector.
61
x˙G=v × cosθ
y˙G=v × sinθ
θ˙G=ω
[ ] [ ][ ]
x˙G cos θ 0
→ y˙G = sin θ 0 v
ω
θ˙G 0 1
{ x R=x G + dcosθ
y R = y G +dsinθ
The kinematic equation at the point R is:
{
x˙R = x˙G−d θ̇ sinθ
y˙R = y˙G +d θ̇ cosθ
θ˙R=θ̇
{
x˙E =v E cos θ E
y˙ E=v E sin θ E
θ˙E =ω E
[][ ][ ]
e1 cosθ sinθ 0 x E−x R
=
e 2 −sinθ cosθ 0 y E− y R
e3 0 0 1 θ E−θ R
{
e1=( x E−x R ) cosθ+ ( y E − y R ) sinθ
→ e =−( x −x ) sinθ+ ( y − y ) cosθ
2 E R E R
e3 =θE −θ R
( 1)
Take the derivative of the 2 side of equation (1):
{
ė1=( x˙E− x˙R ) cos θ−( x E −x R ) sin θ θ̇+ ( y˙E− y˙R ) sinθ+ cosθ ( y E − y R ) θ̇
ė 2=−( x˙E − x˙R ) sin θ−( x E −x R ) cos θ d θ̇+ ( y˙E − y˙R ) cosθ−sinθ( y E − y R ) θ̇
ė3=θ˙E −θ̇
{
e˙1=v E cos ( θ E −θ R )−v +e 2 ω
→ ė2=v E sin ( θ E −θ R )−(d +e 1)ω
e˙3 =ω E−ω
62
[ ][ ][ ][ ]
e˙1 v E cos e3 −1 e2
v
e˙2 = v E sin e3 + 0 −d−e 1
ω
e˙3 ωE 0 −1
The coordinate of e 3
{ v=v r cos e 3+ k 1 e1
ω=k 2 v r e 2 +ωr + k 3 sin e3
Therefore, it means that the system is stable, with the tracking error converging to 0 as t
→ ∞ because V̇ <0 with k i> 0.
We define the linear velocity and angular velocity of the left wheels and right wheels as
v l , v r ,ω l ,ω r , respectively.
Based on the law of the differential drive, we have a relationship between the above
parameters as follows:
v l +v r ωl +ω r
v= ω=
2 2
ωb ωb
v l=v − =r ω l ( rpm ) , v r =v + =r ωr ( rpm ) ,
2 2
vl v
ω l= ( rpm ) , ωr = r ( rpm )
r r
64
Using the Matlab System Identification Toolbox, we can find the Transfer function of
the above motors, with the input value is a PWM signal (0-255), the output signal is the
output shaft revolution per minute. The result is as follows:
Motor 1:
45.82
G ( s )=
s +38.93
- Motor 2:
37.16
G ( s )=
s +31.84
65
6.5.2. PID Controller Design
After having the transfer function of the two motors, we can use the PID tuner and
MATLAB Simulink to get the two PID controllers for the 2 motors. The result is as follows:
The performance criteria are stated as follows:
Rise time ≤ 0.1(s)
Settling time ≤ 0.2(s)
Overshoot ≤ 5(% )
e ss ≈ 0
The block diagram setup in MATLAB Simulink is as follows:
Using the PID tuner in addition to the trial and error method, we get the results as
follows:
(
Gs = K p +
Ki
s )( s+38.93
45.82
)
Hence, the characteristic equation:
2
s +38.93 s + K p 45.82 s +45.82 K i=0
2
↔ s + ( 38.93+ K p × 45.82 ) s+ 45.82 × K i=0
Besides, we have the criteria for the controller:
−δπ
√ 1−δ 2
%OS ≤ 5 % → e ≤0.05 → δ=0.6901
4
T s ≤0.2 → ≤ 0.2→ ωn ≥ 28.9813
δ ×ω n
3 2
1.76 δ −0.417 δ +1.039 δ+1
T r ≤0.1 → ≤0.1 → ωn ≥ 20.9687
ωn
66
Therefore, we will choose,
{ωδ=0.6901
≥ 28.9813
n
{
2 δ ω n=38.93+ K p × 45.82
ω 2n=45.82 × K i
↔
{
40=38.93+ K p × 45.82
839.9157=45.82× K i
↔
{
K p =0.02335
K i=18.3307
After we simulate with MATLAB, here is the response
As we can see from the graph, the response has Overshoot 5%.
Next, we move to the second motor, the second motor after compensated:
(
Gs = K p +
Ki
s )( s+31.84
37.16
)
Hence, the characteristic equation:
2
s +31.84 s+ K p 37.16 s+37.16 K i =0
2
↔ s + ( 31.84+ K p ×37.16 ) s +37.16 × K i=0
Besides, we have the criteria for the controller:
−δπ
√ 1−δ 2
%OS ≤ 5 % → e ≤0.05 → δ =0.6901
4
T s ≤0.2 → ≤ 0.2→ ωn ≥ 28.9813
δ ×ω n
3 2
1.76 δ −0.417 δ +1.039 δ+1
T r ≤0.1 → ≤0.1 → ωn ≥ 20.9687
ωn
Therefore, we will choose,
{ωδ=0.6901
≥ 28.9813
n
67
We have the standard form of Second-Order TF is:
2 2
s +2 δ ω n s+ ωn=0
Then we will synchronize the formula, we have:
{
2 δ ω n=31.84+ K p ×37.16
ω 2n=37.16 × K i
↔
{
40=31.84 + K p ×37.16
839.9157=37.16 × K i
↔
{
K p=0.2195
K i =22.6027
After we simulate with MATLAB, here is the response
Motor 2:
Parameter Value
Kp 0.66
68
KI 40
KD 0
Rise time 0.0428(s)
Settling time 0.0773(s)
Overshoot 0%
69
1kg-load case 2kg-load case
70
References:
1.
Iman Ghaffari Barzegar . Motor-Reducer Sizing Procedure for Robotic Application .
Retrieved September 26th, 2025, from https://www.politesi.polimi.it/retrieve/83894122-
77e9-41e2-8af0-514bed35156a/imanghaffariFinalThesis.pdf
2.
Raspberry Pi Foundation. (n.d.). Build a line-following robot with Python. Raspberry Pi
Projects. Retrieved September 12, 2025, from
https://projects.raspberrypi.org/en/projects/rpi-python-line-following/2
3.
Circuit Digest. (n.d.). Arduino Uno line follower robot. Circuit Digest. Retrieved
September 12, 2025, from https://circuitdigest.com/microcontroller-projects/arduino-uno-
line-follower-robot
4.
Instructables. (n.d.). Line following robot. Instructables. Retrieved September 12, 2025,
from https://www.instructables.com/Line-Following-Robot/
5.
AUMI. (n.d.). Magnetic line-based navigation solutions for AGV line detector sensor from
MLS SICK. AUMI. Retrieved September 12, 2025, from
https://aumi.com.vn/en/magnetic-line-based-navigation-solutions-for-agv-line-detector-
sensor-from-mls-sick/#:~:text=Built%2Din%2010%20markers%20(Makers,CANopen
%2C%20Modbus%20RTU%20RS%2D485
6.
Robu. (n.d.). IR sensor working. Robu. Retrieved September 12, 2025, from
https://robu.in/ir-sensor-working/
7.
NShopVN. (n.d.). Nguyên lý hoạt động của cảm biến cân nặng Loadcell: Cách sử dụng
cảm biến với Arduino để làm một cân điện tử đơn giản. NShopVN Blog. Retrieved
September 12, 2025, from https://nshopvn.com/blog/nguyen-ly-hoat-dong-cua-cam-bien-
can-nang-loadcell-cach-su-dung-cam-bien-voi-arduino-de-lam-mot-can-dien-tu-don-gian/
8. Mechatronics System Design through Project – A Case Study
9. Analysis of Line Sensor Configuration for the Advanced Line Follower Robot
10. A Hierarchical Control System for Autonomous Driving towards Urban Challenges
11. Nguyễn Hữu Cẩn, Lý thuyết Ô tô – máy kéo.
71