Self-Balancing Robot: Fredrik Ihrfelt William Marin
Self-Balancing Robot: Fredrik Ihrfelt William Marin
Self-balancing robot
FREDRIK IHRFELT
WILLIAM MARIN
Keywords
Mechatronics, self-balancing, robot, package delivery,
remote controlled
Referat
Tvåhjulig själv-balanserande robot
Denna rapport strävar efter att undersöka möjligheterna
av att använda en själv-balanserande robot för paketleve-
ranser. Rörelsen av en tvåhjulig själv-balanserande robot
liknar den mänskliga rörelsen mer än ett traditionellt fyr-
hjuligt fordon. Målet med rapporten är att bygga en själv-
balanserande robot för att undersöka hur långt från dess
centeraxel en vikt kan placeras, samt undersöka vilken re-
sponstid som uppnås med en Wireless Fidelity (WiFi)-länk
och hur en WiFi-länk jämför med en Bluetooth-länk. Ba-
lans uppnåddes genom att använda en Proportional-Integral-
Derivative (PID) regulator med input från ett gyroskop och
en accelerometer. Stegmotorer användes för att manövrera
roboten. När roboten hade konstruerats utfördes tester för
att undersöka hur långt från centrumaxeln en vikt kun-
de placeras. Ett test utfördes för att undersöka respons-
tiden för en WiFi-länk med avseende på avståndet mel-
lan operatör och robot, samt att undersöka den maximala
räckvidden och jämföra den mot Bluetooth. Resultaten vi-
sade att en vikt på ett kilogram kunde placeras fem centi-
meter från centeraxeln, att responstiden var ungefär 10-20
millisekunder för avstånd upp till 35 meter. En WiFi-länk
har en längre räckvidd än Bluetooth och kortare responstid.
Nyckelord
Mekatronik, själv-balanserande, robot, paketleverans,
fjärrstyrd
Acknowledgements
We would like to thank Nihad Subasic, supervisor and examiner, for giving us
the right knowledge and support that made this project possible. Seshagopalan
Thorapalli among other students have helped us a lot with the project.
Contents
1 Introduction 1
1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Theory 5
2.1 Inverted pendulum principle . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Control Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 PID controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Weight placed off center . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5 Gyroscope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 Accelerometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.7 Complementary filter . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8 Bipolar stepper motors . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.9 Stepper motor driver . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.10 WiFi communication . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Robot construction 13
3.1 Mechanical components . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.1.1 Baseplates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.1.2 Wheels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.1.3 Assembly of the demonstrator . . . . . . . . . . . . . . . . . . 15
3.2 Electrical Components . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2.1 Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2.2 Sensor for angular data . . . . . . . . . . . . . . . . . . . . . 15
3.2.3 Stepper motors and driver . . . . . . . . . . . . . . . . . . . . 16
3.2.4 Battery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2.5 Remote control and receiver . . . . . . . . . . . . . . . . . . . 17
3.3 Steering of the robot . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4.1 Robot Software . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4.2 Remote Software . . . . . . . . . . . . . . . . . . . . . . . . . 20
4 Results 21
4.1 Off mass center test . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.1.1 Normal weight offset . . . . . . . . . . . . . . . . . . . . . . . 22
4.1.2 Maximum weight offset . . . . . . . . . . . . . . . . . . . . . 24
4.2 Remote latency test . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.2.1 WiFi Latency . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.2.2 WiFi Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5 Discussion 27
5.1 The stepper motors impact . . . . . . . . . . . . . . . . . . . . . . . 27
5.2 The ability to balance with off center weights . . . . . . . . . . . . . 27
5.3 Is it a good idea to control a robot with WiFi? . . . . . . . . . . . . 28
6 Conclusion 29
Bibliography 33
Appendices 36
E Code E:1
E.1 Robot code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E:1
E.2 Remote code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E:9
E.2.1 Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . E:9
E.2.2 Reciever . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E:14
List of Figures
4.1 A CAD render of the weight mounted on the robot. Made in Solid Edge
[6]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.2 The graph of the response when the weight was placed one centimeter
from center and a φc value of 0,0015. Made in Mathlab [31]. . . . . . . . 23
4.3 The graph of the response when the weight placed one centimeter from
center and a φc value of 0,0060. Made in Mathlab [31]. . . . . . . . . . . 23
4.4 The graph of the response when the weight was placed five centimeters
from center and a φc value of 0,0115. Made in Mathlab [31]. . . . . . . . 24
4.5 The principle of the remote latency test. Made in Illustrator [8]. . . . . 25
4.6 The graph from the latency. Made in Mathlab [31]. . . . . . . . . . . . . 25
4.7 An illustration of the signal radius and area. Made in Photoshop [32]. . 26
A.1 A model of the forces with the weight placed off center. Made in Illus-
trator [8]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A:1
D.1 The circuit diagram for the robot. Made in Fritzing [38]. . . . . . . . . . D:1
D.2 The circuit diagram for the remote. Made in Fritzing. . . . . . . . . . . D:2
List of Tables
C.1 The key specifications for Japan servo KH56JM2 851 stepper motor. . . C:1
C.2 The key specifications for DRV8825 stepper motor driver. . . . . . . . . C:1
Acronyms
3D Three Dimensional.
Introduction
In this chapter the background, purpose of the thesis, scope and method used will
be presented.
1.1 Background
Growing e-commerce drives the need for faster and more flexible package deliveries,
leading to the increase of package delivering robots. These types of robots are
expected to revolutionize the last mile delivery systems since they offer a cheaper
and more efficient way of delivery [1]. Amazon has tested a six wheeled autonomous
delivery robot for last mile deliveries in urban environments, one of the problems
with their delivery robot was that it had to share the sidewalk space with pedestrians
[2]. One way to combat the problem is to utilize a two wheeled self-balancing robot
since it has a smaller footprint and is more maneuverable due to it having a zero
turn radius. This type of robot also has the advantage of being more stable in
inclines since they lean into the incline [3].
1
CHAPTER 1. INTRODUCTION
The principle of a two wheeled self-balancing robot is similar to that of the human
body, which is an inverted pendulum balancing the upper body around the ankles
[4]. In order to maintain balance a gyroscope and accelerometer, in the form of
an Inertial Measurement Unit (IMU) can be used to deviate the angle of the robot
relative the vertical axis. This angle is then used to increase or decrease the speed
of the motors with the goal of maintaining a constant angle [5].
1.2 Purpose
The purpose of this thesis is to evaluate the possibility of using two wheeled self-
balancing robots for package deliveries, by testing their ability to maintain balance
with an off-centered weight on top and evaluating the viability of remote control over
WiFi. In order to fulfill the purpose of this thesis, the following research questions
have been defined:
• How far off-center can a one kilogram weight be added while maintaining
balance of the robot?
• What is the response time when controlling the robot with a WiFi connection?
A package with uneven weight distribution and desired robot response is shown in
figure 1.2.
Figure 1.2. A CAD render of a package with uneven weight placed on the robot
and desired robot response. Made in Solid Edge [6].
1.3 Scope
The focus of the thesis is to build a robot and implement a code that enables the
robot to balance and to be controlled over a wireless connection. The robot should
also be able to an off centered weight without loosing balance. The budget for the
components amounts to 1 000 Swedish Krona (SEK) and the time constraint for
the thesis is five months.
2
1.4. METHOD
1.4 Method
First research was conducted to understand the system and the electronics. Then
a PID controller was designed that would enable balancing of the robot. The robot
and a remote controller was designed as a Computer Aided Design (CAD) model
in Solid Edge. The physical robot and remote controller was built with the CAD
model as a guideline. Experiments with the physical robot and remote controller
was then conducted in order to determine the robots ability to balance with an off
center weight, as well as determine the the performance of WiFi communication
between the robot and the remote controller.
3
Chapter 2
Theory
In this chapter the theory that was needed to achieve the purpose of this thesis is
presented.
5
CHAPTER 2. THEORY
Z t
d
u(t) = KP e(t) + KI e(τ )d + KD e(t) (2.1)
0 dt
6
2.4. WEIGHT PLACED OFF CENTER
Figure 2.2. A model of the forces with the weight placed off center. Made in
Illustrator [8].
The system was modeled as a pendulum and a weight connected to the pendulum.
The pendulum was approximated as the base plates, threaded rods and the electrical
components except the motors [14]. To simplify the system the mass of the beam
connecting the weight to the robot was neglected and only the angles and torque
in the x- and y-axis were calculated. It was also assumed that the wheels will not
loose contact with the ground and there will be no slip between the wheels and the
7
CHAPTER 2. THEORY
ground [15].
Equation 2.2 describing the relationship between the angle for the balance point
and the length from the pendulums center axis to the weight was then derived. See
Appendix A
mw lwc
φ = arctan ( ) (2.2)
mpend l + mw lw
Where φ is the lean angle, mw is the mass of the weight, mpend is the mass of the
pendulum, l is the length from the center of the wheel to the center of mass for
the pendulum, lw is the length from the center of the wheel to center of the weight
perpendicular to the pendulum and lwc is the length from the top of the pendulum
to the center of the weight.
Equation 2.3 could then be derived in order to calculate the theoretical settling
time from the balance point angle, code frequency and the constant φc .
φ
Tst = f (2.3)
φc
Where Tst is the theoretical settling time, φ is the lean angle and f is the frequency
of the code.
2.5 Gyroscope
A gyroscope is a sensor that measures angular velocity in degrees per second (◦ /s).
It can be used to calculate an angle by integrating the angular velocity over time.
The problem with using a gyroscope to calculate an angle is that the integration
of the angular velocity leads to the value drifting over time, not returning to zero
when the system is returned to its starting position [16].
2.6 Accelerometer
An accelerometer is a sensor that measures linear acceleration in m/s2 . It can be
used to calculate an angle by determining the position of the gravity vector. The
problem with using an accelerometer to calculate an angle is that it is sensitive to
external forces, for example vibrations [16].
8
2.7. COMPLEMENTARY FILTER
Where φ is the calculated angle of the robot, gyroData is the data read from the
gyroscope and accData is the data read from the accelerometer. cgyro and cacc are
constants that can be changed to tune the filter and they must add up to one.
The data from the gyroscope is integrated with the calculated angle and then
combined with the data from the accelerometer [16].
9
CHAPTER 2. THEORY
The motor used rotates 1,8◦ per step and microstepping was used in order to reduce
the angle traveled per step. By applying the same current to both coils at the same
time the rotor locks into place between the two coils resulting in a half step, see
figure 2.5. To further reduce the step length the size of the current in the two coils
can be changed independently, resulting in the rotor locking into place in multiple
positions between the coils. This method was be used in order to achieve eighth
steps, resulting in a smoother motion [17].
Figure 2.5. Half stepping of the stepper motor. Made in Illustrator [8].
10
2.9. STEPPER MOTOR DRIVER
11
Chapter 3
Robot construction
The following chapter lists and describes the software, the chosen pre-fabricated
components as well as the components that were constructed and manufactured.
The robot and the hand controller were both constructed with Solid Edge [6]. A
render of the CAD-model is displayed in figure 3.1. A complete list of all components
used is found in Appendix B.
13
CHAPTER 3. ROBOT CONSTRUCTION
3.1.1 Baseplates
Four different base plates were designed to create layers for the robot. Two base
plates were designed to clamp the two motors in place. Another base plate was
designated as a frame for mounting the Arduino, WiFi module and stepper motor
drivers. This base plate also increased stability for the robot. The top base plate
was designed to mount the IMU, a counter balance weight used to raise the center of
gravity and a mount enabling the mounting of the sliding weight used for the tests.
All base plates were Three Dimensional (3D) printed in Polylactic Acid (PLA).
3.1.2 Wheels
Wheels were designed consisting of two components, rim and tire. Weight was
intended to be low to reduce the moment of inertia and thereby improve acceleration.
The two rims were laser cut in Polymethyl Methacrylate (PMMA). This material
was chosen for the wheels because of the high yield strength [21]. The tires were
cut from an inner tube of a bicycle tire and subsequently glued to the rims. This
material provided high friction between the wheels and the maneuvering base.
14
3.2. ELECTRICAL COMPONENTS
Figure 3.2. The electrical components used. Made in Solid Edge [6].
15
CHAPTER 3. ROBOT CONSTRUCTION
3.2.4 Battery
A standard 12V 2200mAh Lithium Polymer (LiPo) battery was used to power the
robot. LiPo batteries have the advantage of being lightweight relative their capacity
[26].
16
3.2. ELECTRICAL COMPONENTS
Figure 3.4. The remote control attached to the 5V powerbank. Made in Solid Edge
[6].
17
CHAPTER 3. ROBOT CONSTRUCTION
18
3.4. SOFTWARE
3.4 Software
In this section the software developed to balance the robot and to steer the robot
is described.
• Data is read from the IMU and apply a complementary filter and low pass
filter to reduce noise and gyroscope drift before the angle is calculated
• A condition checks if the the lean angle is withing 0, 5◦ > φ > −0, 5◦ in order
to reduce the effect of measurement errors and high frequency shaking
– If true the motor speed is set to zero and break from the loop
– If false continue the loop
• A condition to check if the angle set point is zero and the motor speed > 0 in
order to find the robots balance point even with an off centered weight
19
CHAPTER 3. ROBOT CONSTRUCTION
Figure 3.6. Figure showing flow chart for the Arduino code. Made in Lucidchart
[30].
20
Chapter 4
Results
This chapter contains the results from the tests performed with the robot. The
values of the robots mass and dimensions are shown in table 4.1 where mcart is the
mass of the wheels and motors [14], mpend is the mass of the pendulum, l is the
length to from the center of the wheels the center of gravity, lw is the length from
the center of the wheels to the center of the weight perpendicular to the pendulum.
The PID controller tuning parameters KP , KI and KD are shown in table 4.2.
KP 12
KI 0,5
KD 22
21
CHAPTER 4. RESULTS
In order to evaluate the robots ability to balance with a weight placed off its center
axis an experiment was conducted. In the experiment a one kilogram weight was
placed a fixed distance from the center vertical axis of the robot. The angular
data was plotted and the distance the robot travelled due to adding the weight was
measured. Figure 4.1 shows the weight mounted on the robot.
Figure 4.1. A CAD render of the weight mounted on the robot. Made in Solid Edge
[6].
To test how the value off the constant φc would affect the settling time and oscillation
a test was performed where the distance, lwc , that the weight was placed from the
center axis of the robot was one centimeter. The graphs from two tests with different
values of φc are shown in figure 4.2 and 4.3. The theoretical settling time Tst , real
settling time Tsr , oscillations ∆φ, distance travelled d and overshoot O are shown
in table 4.3. The test showed that a smaller φc -value of 0,0015 led to an overshoot
of 42,9%, oscillations of 0,65◦ , a real settling time of 3,75 seconds and a distance
travelled from when the weight was added of 46 centimeters. When φc was increased
to 0,0060 the oscillations increased to 1,1◦ , the overshoot increased to 100%, the
real settling time was reduced to 2,6 seconds and the distance travelled to ten
centimeters.
22
4.1. OFF MASS CENTER TEST
Figure 4.2. The graph of the response when the weight was placed one centimeter
from center and a φc value of 0,0015. Made in Mathlab [31].
Figure 4.3. The graph of the response when the weight placed one centimeter from
center and a φc value of 0,0060. Made in Mathlab [31].
23
CHAPTER 4. RESULTS
Figure 4.4. The graph of the response when the weight was placed five centimeters
from center and a φc value of 0,0115. Made in Mathlab [31].
24
4.2. REMOTE LATENCY TEST
Figure 4.5. The principle of the remote latency test. Made in Illustrator [8].
Figure 4.6. The graph from the latency. Made in Mathlab [31].
25
CHAPTER 4. RESULTS
Figure 4.7. An illustration of the signal radius and area. Made in Photoshop [32].
Table 4.5. Table showing the WiFi range with a stable, unstable and both stable
and unstable connection.
Connection type Radius [m] Area [m2 ]
Stable 0-35 4000
Unstable 35-50 4000
Stable, Unstable 0-50 8000
26
Chapter 5
Discussion
This chapter provides a discussion of the results obtained from the tests performed
with the robot and remote controller.
27
CHAPTER 5. DISCUSSION
settling time slightly. The robot’s ability to maintain balance after a disturbance
was also affected by the increase of φc , when using a higher value the robot’s stability
was reduced and a small disturbance led to large oscillations. From the testing it
was determined that the highest usable value of φc was 0,0115.
When comparing the theoretical settling time,Tst , to the real settling time, Tsr ,
it was evident that the theoretical settling time was shorter than the actual. The dif-
ference between the theoretical and real settling time increased as φc was increased,
which is probably caused by the larger overshoot and oscillations when using a
larger φc . If a PID controller was used where speed of the robot was the input and
φc was the output the real settling would probably be closer to the theoretical.
Remote control of the robot was affected by the off centered weight, when the
weight was placed over four centimeters from the center axis the robot would fail to
maintain balance when a full forward command was given to the robot. This was
probably related to the motors not being able to rotate fast enough to maintain
balance.
28
Chapter 6
Conclusion
How far off-center can a one kilogram weight be added while maintaining balance of
the robot?
A one kilogram weight can be placed five centimeters from the center axis of the
robot while maintaining balance.
What is the response time when controlling the robot with a WiFi connection?
For distances under 35 meters the average response time was 10-20 milliseconds,
when the range was over 35 meters spikes increased the average response while
the robot was moving, when the robot did not move the response time was 20-30
milliseconds. At a distance of 50 meters the response time became greater than
5000 milliseconds.
The main difference between WiFi and Bluetooth is that WiFi typically have a
larger effective range of around 35 meters while Bluetooth has a effective range of
around ten meters. The response time is typically shorter when using WiFi.
29
Chapter 7
7.1 Recommendations
A external antenna is recommended for the robot and the controller, since it would
give the robot a longer and more stable range. By adding a motor operated sliding
counterweight it could be used to balance the robot when an off center weight is
added. This would enable the robot to stand vertically even when loaded. A sun-
shield for the display is also recommended, since the screen was hard to read in
daylight conditions.
31
Bibliography
[2] ZDNet. ”Amazon delivery robots are officially on the streets of California”. Date
accessed: 2020-03-20. [Online]. Available: https://www.zdnet.com/article/
amazon-delivery-robots-are-officially-on-the-streets-of-california/
[3] Daniel Jones and Karl Stol. ”Modelling and Stability Control of Two-
Wheeled Robots in Low-Traction Environments”, Autralasian conference on
robotics and automation, 2010. Date accessed: 2020-02-10. [Online]. Available:
https://www.researchgate.net/publication/267941440_Modelling_
and_Stability_Control_of_Two-Wheeled_Robots_in_Low-Traction_
Environments
[5] Bruno Bosco, André Enllnefjärd, Victor Ellqvist Nordenmark, Alexander Hem-
berg, Henrik Olsson, Jonas Tegelberg ”Utveckling av en självbalanserande
tvåhjulig robot”, KTH, Tech Rep., 2012. Date accessed: 2020-01-15. [On-
line]. Available: http://www.diva-portal.org/smash/get/diva2:550532/
FULLTEXT01.pdf
[7] Rui Zhang, Gang Xiong, Changjian Cheng, Xiuqin Shang, Yonghong Ma,
Zichen Lu. ”Control system design for two-wheel self-balanced robot based on
the stepper motor”, IEEE, 2008. Date accessed: 2020-01-25. [Online]. DOI:
https://doi.org/10.1109/SOLI.2013.6611417
[9] Torkel Glad and Lennart Ljung, Reglerteknik. Poland: Dimograf, 2014, pp. 9-14.
33
BIBLIOGRAPHY
[10] Torkel Glad and Lennart Ljung, Reglerteknik. Poland: Dimograf, 2014, pp.
63-64.
[11] Torkel Glad and Lennart Ljung, Reglerteknik. Poland: Dimograf, 2014, p. 20.
[12] Control tutorials for MATLAB and SIMULINK. ”Introduction: PID Controller
Design”. Date accessed: 2020-02-10. [Online]. Available: http://ctms.engin.
umich.edu/CTMS/index.php?example=Introduction§ion=ControlPID
[13] Albin Cassirer, Viktor Eriksson, Mikael Gnospelius, Anna Kramarz and Jo-
han Landerholm, ”Timing Grey: En självbalanserande robot med ultraljud och
egenutvecklad hastighetsmätare”, KTH, Tech Rep., 2012. Date accessed: 2020-
01-25. [Online]. Available: http://www.diva-portal.org/smash/get/diva2:
550518/FULLTEXT01.pdf
[15] F. Grasser, A. D’Arrigo, S. Colombi and A.C. Rufer, ”JOE: a mobile, inverted
pendulum”, IEEE, 2002. Date accessed: 2020-02-15. [Online]. DOI: https://
doi.org/10.1109/41.982254
[17] DroneBot Workshop. ”Stepper Motors with Arduino - Getting Started with
Stepper Motors”. Date accessed: 2020-01-20. [Online]. Available: https://
dronebotworkshop.com/stepper-motors-with-arduino/
[18] Texas instruments. ”DRV8825 stepper motor controller IC”. Date ac-
cessed: 2020-01-05. [Online]. Available: https://www.electrokit.com/
produkt/stegmotordrivare-drv8825/
[19] CircuitDigest. ”Getting Started with ESP8266 WiFi Transciever (Part 1)”.
Date accessed: 2020-04-10. [Online]. Available: https://circuitdigest.com/
microcontroller-projects/getting-started-with-esp8266-modul
[21] MatWeb. ”Material data for PMMA”. Date accessed: 2020-03-20. [On-
line]. Available. http://www.matweb.com/search/datasheet.aspx?bassnum=
O1303&ckck=1
34
BIBLIOGRAPHY
[22] Arduino, Arduino Uno Rev3. Date accessed: 2020-01-15. [Online]. Available:
https://store.arduino.cc/arduino-uno-rev3
[25] Nema23 stepper motor. Date accessed: 2020-02-15. [Online]. Available: https:
//joy-it.net/en/products/NEMA23-02
[29] Brokking. ”Your Arduino Balancing Robot”. Date accessed: 2020-01-20. [On-
line]. Available: http://www.brokking.net/yabr_main.html
[33] Dan Nguyen and Kayan Phuong, ”SB-bot”, KTH, Tech Rep., 2016. Date ac-
cessed: 2020-01-25. [Online]. Available: http://www.diva-portal.org/smash/
get/diva2:957115/FULLTEXT01.pdf
[34] Bluair. ”Bluetooth Range: 100m, 1km or 10km?”. Date accessed: 2020-05-08.
[Online]. Available: http://www.bluair.pl/bluetooth-range
35
BIBLIOGRAPHY
Barcelona, 2017, pp. 148-151. Date accessed: 2020-05-08. [Online]. DOI: https:
//doi.org/10.1109/TSP.2017.8075956
[36] Lifewire. ”Bluetooth vs. Wi-Fi: What’s the Difference?”. Date ac-
cessed: 2020-05-08. [Online]. Available: https://www.lifewire.com/
bluetooth-vs-wi-fi-4088218
36
Appendix A
The system was modelled as an pendulum and a weight attached to the pendulum
to simplify the equations. The model is shown in figure A.1.
Figure A.1. A model of the forces with the weight placed off center. Made in
Illustrator [8].
A:1
APPENDIX A. WEIGHT OFF CENTER MODEL
A.1 Pendulum
The length along the horizontal axis from the origin to the center of mass for the
pendulum was derived:
xcgpend = l sin φ (A.1)
Then the torque acting on the origin due to the pendulums mass was derived:
A.2 Weight
The length along the horizontal axis from the origin to the center of the weight was
derived:
xcgweight = lw sin φ − lwc cos φ (A.3)
Then the torque acting on the origin due to the weights mass was derived:
A:2
Appendix B
Component table
B:1
APPENDIX B. COMPONENT TABLE
Hardware
1 Arduino Uno Rev3 40 228
2 Steppermotors Indonesia 859 500
2 Motor drivers DRV8825 3 158
1 Balance sensor MPU-6050 6 49
1 ESP8266 ESP12-E 30 49
1 Battery 12V 185 200
B:2
Appendix C
Table C.1. The key specifications for Japan servo KH56JM2 851 stepper motor.
Number of phases 2
Step angle [◦ /step] 1,8
Voltage [V] 3,51
Current [A/phase] 1,3
Inductance [mH/phase] 5,6
Holding torque [kgf·cm] 5,0
Max speed at 12 V [rpm] 253
Table C.2. The key specifications for DRV8825 stepper motor driver.
C:1
Appendix D
Circuit diagram
Figure D.1. The circuit diagram for the robot. Made in Fritzing [38].
D:1
APPENDIX D. CIRCUIT DIAGRAM
Figure D.2. The circuit diagram for the remote. Made in Fritzing.
D:2
Appendix E
Code
E:1
APPENDIX E. CODE
E:2
E.1. ROBOT CODE
TIMER2_COMPA_vect
80 TCCR2A = 0;
81 TCCR2B = 0;
82 TIMSK2 |= (1 << OCIE2A ) ;
83 TCCR2B |= (1 << CS21 ) ;
84 OCR2A = 39;
85 TCCR2A |= (1 << WGM21 ) ;
86
87 // Starting the MPU6050
88 Wire . beginTransmission ( gyro_address ) ;
89 Wire . write (0 x6B ) ;
90 Wire . write (0 x00 ) ;
91 Wire . endTransmission () ;
92
93 // Setting the scale of the gyro to +/ - 250 degrees per second
94 Wire . beginTransmission ( gyro_address ) ;
95 Wire . write (0 x1B ) ;
96 Wire . write (0 x00 ) ;
97 Wire . endTransmission () ;
98
99 // Setting the scale of the accelerometer to +/ - 4 g .
100 Wire . beginTransmission ( gyro_address ) ;
101 Wire . write (0 x1C ) ;
102 Wire . write (0 x08 ) ;
103 Wire . endTransmission () ;
104
105 // Setting a Low Pass Filter on MPU6050 to 43 Hz
106 Wire . beginTransmission ( gyro_address ) ;
107 Wire . write (0 x1A ) ;
108 Wire . write (0 x03 ) ;
109 Wire . endTransmission () ;
110
111 // Defining outputs
112 pinMode (2 , OUTPUT ) ;
113 pinMode (3 , OUTPUT ) ;
114 pinMode (4 , OUTPUT ) ;
115 pinMode (5 , OUTPUT ) ;
116 pinMode (13 , OUTPUT ) ;
117
118 // Calibrating the MPU6050 by reading the gyro offset 500 times and
calculating the mean value
119 for ( receive_counter = 0; receive_counter < 500; receive_counter ++) {
120 Wire . beginTransmission ( gyro_address ) ;
121 Wire . write (0 x43 ) ;
122 Wire . endTransmission () ;
123 Wire . requestFrom ( gyro_address , 4) ;
124 g y r o _ y a w _ c a l i b r a t i o n _ v a l u e += Wire . read () < <8| Wire . read () ;
125 g y r o _ p i t c h _ c a l i b r a t i o n _ v a l u e += Wire . read () < <8| Wire . read () ;
126 delayMicroseconds (3700) ;
127 }
128 g y r o _ p i t c h _ c a l i b r a t i o n _ v a l u e /= 500;
129 g y r o _ y a w _ c a l i b r a t i o n _ v a l u e /= 500;
130
131 // Creating loop timer to achieve 250 Hz frequency
E:3
APPENDIX E. CODE
E:4
E.1. ROBOT CODE
E:5
APPENDIX E. CODE
235
236 // If a left rotation command is given , increase speed on the right
motor and decrease speed on the left motor
237 if ( rotation > 1) {
238 pid_output_left -= desired_rotation ;
239 pid_output_right += desired_rotation ;
240 }
241
242 // If a forward command is given , increase the PID angle set point
243 if ( spd > 1) {
244 if ( pid_setpoint > -2.5) pid_setpoint -= 0.1;
245 if ( pid_output > desired_speed * -1) pid_setpoint -= 0.005;
246 }
247
248 // If a backward command is given , decrease the PID angle set point
249 if ( spd < 1) {
250 if ( pid_setpoint < 2.5) pid_setpoint += 0.1;
251 if ( pid_output < desired_speed ) pid_setpoint += 0.005;
252 }
253
254 // If no steering command is given , set the PID angle set point to
zero
255 if ( spd == 1 && rotation == 1) {
256 if ( pid_setpoint > 0.5) pid_setpoint -=0.05;
257 else if ( pid_setpoint < -0.5) pid_setpoint +=0.05;
258 else pid_setpoint = 0;
259 }
260
261 // Change the PID angle setpoint to compensate for off centered
weight
262 if ( pid_setpoint == 0) {
263 if ( pid_output < 0) s e l f _ b a l a n c e _ pi d _ s e t p o i n t += 0.0115;
264 if ( pid_output > 0) s e l f _ b a l a n c e _ pi d _ s e t p o i n t -= 0.0115;
265 }
266
267
268 // --- CALCULATING THE STEPPER MOTOR PULSE - - -//
269 // Linearize the stepper motors non - linear behavior
270 if ( pid_output_left > 0) pid_output_left = 405 - (1/( pid_output_left
+ 9) ) * 5500;
271 else if ( pid_output_left < 0) pid_output_left = -405 - (1/(
pid_output_left - 9) ) * 5500;
272
273 if ( pid_output_right > 0) pid_output_right = 405 - (1/(
pid_output_right + 9) ) * 5500;
274 else if ( pid_output_right < 0) pid_output_right = -405 - (1/(
pid_output_right - 9) ) * 5500;
275
276 // Calculate the pulse time for the stepper motors
277 if ( pid_output_left > 0) left_motor = 400 - pid_output_left ;
278 else if ( pid_output_left < 0) left_motor = -400 - pid_output_left ;
279 else left_motor = 0;
280
281 if ( pid_output_right > 0) right_motor = 400 - pid_output_right ;
E:6
E.1. ROBOT CODE
E:7
APPENDIX E. CODE
335
336 // Stop reading when ’>’ is sent
337 if ( x == endMarker ) {
338 readInProgress = false ;
339 newDataFromEsp = true ;
340 inputBuffer [ bytesRecvd ] = 0;
341 parseData () ;
342 }
343
344 // Read the string between ’<’ and ’>’
345 if ( readInProgress ) {
346 inputBuffer [ bytesRecvd ] = x ;
347 bytesRecvd ++;
348 if ( bytesRecvd == buffSize ) {
349 bytesRecvd = buffSize - 1;
350 }
351 }
352
353 // Start reading when ’<’ is sent
354 if ( x == startMarker ) {
355 bytesRecvd = 0;
356 readInProgress = true ;
357 }
358 }
359 }
360
361 // --- FUNCTION TO SPLIT THE READ SERIAL DATA - - -//
362 void parseData ()
363
364 // Using strtoIndx as an index
365 char * strtokIndx ;
366
367 // Read the first part of the string and store as the speed command
368 strtokIndx = strtok ( inputBuffer , " ," ) ;
369 spd = atof ( strtokIndx ) ;
370
371 // Read the second part of the string and store as the rotation
command
372 strtokIndx = strtok ( NULL , " ," ) ;
373 rotation = atof ( strtokIndx ) ;
374
375 }
E:8
E.2. REMOTE CODE
E:9
APPENDIX E. CODE
49 float spdp = 0;
50 float rotp = 0;
51 float num = 0;
52 float voltage = 0;
53 float voltage2 = 0;
54 float RssI = 0;
55
56 # define SCREEN_WIDTH 128 // OLED display width , in pixels
57 # define SCREEN_HEIGHT 64 // OLED display height , in pixels
58 // Declaration for SSD1306 display connected using software SPI
59 # define OLED_MOSI 12
60 # define OLED_CLK 14
61 # define OLED_DC 0
62 # define OLED_CS 2
63 # define OLED_RESET 13
64
65 Adafruit_SSD1306 display ( SCREEN_WIDTH , SCREEN_HEIGHT , OLED_MOSI ,
66 OLED_CLK , OLED_DC , OLED_RESET , OLED_CS ) ;
67 Adafruit_ADS1115 ads (0 x48 ) ;
68
69 void setup () {
70 Serial . begin (115200) ;
71 if (! display . begin ( SSD1306_SWITCHCAPVCC ) ) {
72 Serial . println ( F ( " SSD1306 allocation failed " ) ) ;
73 for (;;) ; // Don ’t proceed , loop forever
74 }
75
76 ads . begin () ; // Starts ADC
77
78 // Explicitly set the ESP8266 to be a WiFi - client
79 WiFi . mode ( WIFI_STA ) ;
80 WiFi . begin ( ssid , password ) ;
81
82 // Loops until connection with server is established
83 while ( WiFi . status () != WL_CONNECTED ) {
84 int16_t i ;
85 display . clearDisplay () ;
86 display . setTextSize (1) ; // Normal 1:1 pixel scale
87 display . setTextColor ( SSD1306_WHITE ) ; // Draw white text
88 display . setCursor (10 , 10) ; // Start at top - left corner
89 display . print ( F ( " Connecting " ) ) ; // Prints status to screen
90 display . display () ;
91 delay (600) ;
92 display . print ( F ( " . " ) ) ;
93 display . display () ;
94 delay (300) ;
95 display . print ( F ( " . " ) ) ;
96 display . display () ;
97 delay (300) ;
98 }
99 }
100
101 void loop () {
102 int16_t adc0 ; // Sixteen bit integer as a result
E:10
E.2. REMOTE CODE
E:11
APPENDIX E. CODE
E:12
E.2. REMOTE CODE
206
207 display . clearDisplay () ; // Clears Display
208 display . setTextSize (1) ; // Normal 1:1 pixel scale
209 display . setTextColor ( SSD1306_WHITE ) ; // Draw white text
210 display . setCursor (0 , 0) ; // Start at top - left corner
211 display . print ( F ( " SPD " ) ) ;
212 display . print ( spd , 0) ;
213 display . print ( F ( " % " ) ) ;
214 display . setCursor (64 , 0) ;
215 display . print ( F ( " ROT " ) ) ;
216
217 // Prints R if joystick x - axis position is Right
218 if ( rot > 100) {
219 rot = rot - 100;
220 display . print ( F ( " R " ) ) ;
221 display . print ( rot , 0) ;
222 display . println ( F ( " % " ) ) ;
223 }
224
225 // Prints L if joystick x - axis position is Right
226 else if ( rot < 100) {
227 rot = 100 - rot ;
228 display . print ( F ( " L " ) ) ;
229 display . print ( rot , 0) ;
230 display . println ( F ( " % " ) ) ;
231 }
232
233 // Prints Zero if joystick x - axis is 0
234 else {
235 display . print ( " 0 " ) ;
236 display . println ( F ( " % " ) ) ;
237 }
238
239 refreshMean = refreshMean + refreshTime ; // Creates a 20 - loop
refresh mean
240 // Saves the maximum response time
241 if ( refreshTime >= refreshMax ) {
242 refreshMax = refreshTime ;
243 }
244
245 // Saves refresh times to display float
246 if ( i == 20) {
247 refreshMeanDisp = refreshMean / i ;
248 refreshMean = 0;
249 refreshMaxDisp = refreshMax ;
250 refreshMax = 0;
251 i = 0;
252 }
253 i = i + 1;
254
255 // Prints response time
256 display . setCursor (0 , 10) ;
257 display . print ( F ( " refreshTime " ) ) ;
258 display . print ( refreshMeanDisp , 0) ;
E:13
APPENDIX E. CODE
E.2.2 Reciever
This section contains the code on the ESP8266 in the robot.
1 /*
2 Remote Transmitter Code
3 Date : 2020 -05 -20
4 Written by : Fredrik Ihrfelt and William Marin
5 Examinor : Nihad Subasic
6 TRITA - nr : 2020:36
7 Course : MF133X
8
9 Bachelor ’s thesis at KTH in mechatronics
10
11 The code :
12 - Reads request from client
13 - Processes request and sends to Arduino
14 - Measure battery voltage
15 - Sends battery voltage to remote controller
16 */
17 # include < ESP8266WiFi .h >
18 # include < ESP8266WebServer .h >
19
20 const char * ssid = " robotwifi " ;
21 const char * password = " kthkex20 " ;
22 const int analogInPin = A0 ; // ESP8266 Analog Pin ADC0 = A0
23
24 int sensorValue = 0; // value read from the pot
E:14
E.2. REMOTE CODE
25 int sendToEsp = 0;
26 float voltage = 12;
27 float spdA = 1;
28 float rotA = 1;
29
30 ESP8266WebServer server (80) ; // Opens the 80 port
31
32 // This handle is triggerd every 10 ms
33 void handleSentVar () {
34 if ( server . hasArg ( " sensor_reading " ) ) { // this is the variable sent
from the client
35
36 float spd = server . arg ( " sensor_reading " ) . toInt () ;
37 float rot = server . arg ( " sensor_reading2 " ) . toInt () ;
38 float state = server . arg ( " sensor_reading3 " ) . toInt () ;
39
40 spd = spd / 100;
41 rot = rot / 100;
42
43
44 // Sends data to Arduino if diffrent from previous or start value
45 if ( spd >= 0) {
46 if ( spd <= 2) {
47 if ( rot >= 0) {
48 if ( rot <= 2) {
49 if ( spd != spdA ) {
50 Serial . print ( " <" ) ;
51 Serial . print ( spd ) ;
52 Serial . print ( " ," ) ;
53 Serial . print ( rot ) ;
54 Serial . println ( " >" ) ;
55 }
56
57 else if ( rot != rotA ) {
58 Serial . print ( " <" ) ;
59 Serial . print ( spd ) ;
60 Serial . print ( " ," ) ;
61 Serial . print ( rot ) ;
62 Serial . println ( " >" ) ;
63 }
64
65 spdA = spd ;
66 rotA = rot ;
67
68 // Reads battery voltage , scale and convert to int
69 sensorValue = analogRead ( analogInPin ) ;
70 voltage = ( sensorValue * 1800000) / (1023) ;
71 round ( voltage ) ;
72 sendToEsp = voltage ;
73
74 server . send ( sendToEsp , " " , " " ) ; // Sends data to
transmitter
75 }
76 }
E:15
APPENDIX E. CODE
77 }
78 }
79 }
80 }
81
82
83
84 void setup () {
85 Serial . begin (115200) ;
86 delay (50) ;
87 WiFi . softAP ( ssid , password ) ;
88 IPAddress myIP = WiFi . softAPIP () ;
89 server . on ( " / data / " , HTTP_GET , handleSentVar ) ;
90 server . begin () ;
91 }
92
93 void loop () {
94 server . handleClient () ;
95 delay (10) ;
96 }
E:16
TRITA ITM-EX 2020:36
www.kth.se