SlideShare a Scribd company logo
1 of 31
Download to read offline
REPORT
SELF BALANCING ROBOT
May 3, 2016
Teru Komal Kumar, 12EE35018
Guide : Prof. Siddhartha Sen
Indian Institute of Technology, Kharagpur
Department of Electrical Engineering
1
Electrical Engineering Indian Institute of Technology, Kharagpur
Contents
1 Introduction 3
2 Nomenclature of Symbols used 4
3 System overview 5
3.0.0.1 Mechanical system . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.0.0.2 Electronic System . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1 Processor Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Sensor Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.1 Inertial Measurement Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2.1.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2.1.2 Calibration Process . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2.2 Incremental Quadrature encoder . . . . . . . . . . . . . . . . . . . . . . . 8
3.2.2.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Actuator Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.3.0.2 DC Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.3.0.3 Motor Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 Communication Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4.0.4 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Power circuit design 11
5 Dynamic Modelling 12
5.1 DC Motor Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.2 Model of the robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.2.1 Equations for the wheels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.2.2 Equations for the chassis . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.3 Approximate values of system parameters . . . . . . . . . . . . . . . . . . . . . . 17
6 Control System Design 18
6.1 PID Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.2 State Space Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
7 MATLAB Code 27
References 31
Page 2 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
1 INTRODUCTION
In this report, we discuss the process of building the self balancing robot from the scratch.
This robot uses the model of inverted pendulum as its reference model. The cart in this case
is made up of two wheeled differential drive. The robot that is built is inherently unstable
and without the action of an external controller, it would fall down. To stabilise it, the motors
need to driven in the appropriate direction.
The report is majorly divided into three sections. In the first section, we look at the system
overview i.e., different modules involved and their functioning. In the second section, we
worked on finding the parameters related to motor dynamics and the robot dynamics that
would help us in getting the state space equations. In the next section, we have discussed
about two control methods that would help us in stabilising the system namely the PID
control and the state space control.
Figure 1: Model of the robot developed for this project
Page 3 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
2 NOMENCLATURE OF SYMBOLS USED
τm Motor Torque
τa Applied Torque
Va Applied Voltage
Ve Back emf
R Armature resistance
IR Rotor Inertia
Km Torque Constant (N.m/A)
Ke Back emf Constant (V.s/rad)
i Armature current
Mw Mass of the wheel
Mp Mass of the chasis
Ip Robot Chasis Inertia
CL,CR Torque exerted by motors on the wheels
Hf Frictional force between the wheel and the ground
HL,HR,PL,PR Reaction forces between wheels and chasis
Kp Proportional Gain
Kd Differential Gain
Ki Integral Gain
Page 4 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
3 SYSTEM OVERVIEW
3.0.0.1 Mechanical system The basic structure of the robot is built from wood. The
wooden structure has two plates where the components like the motors, sensors and com-
munication module can be mounted.These plates can be moved up and down which would
help us in manipulating the centre of gravity of the bot. Two wheels of radii of about 5 cm are
used for the ’cart’ part of the system.
3.0.0.2 Electronic System The system contains 4 different modules which include Proces-
sor module, Sensor module, Actuator module and Communication module.
Figure 2: System Overview
3.1 Processor Module
Owing to our experience in working with Atmega processor as part of our coursework, we
have chosen Arduino Mega 2560 microcontroller as the processor for the robot. Some of its
desirable qualities include
1. It has 4 serial ports and hence can be used to communicate with multiple devices
simultaneously.
2. It has a onboard TTL-to-USB serial chip that allows for ease in communication with
the PC.
Page 5 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 3: Arduino MEGA 2560
3. It has 15 PWM pins which are helpful in controlling the actuators.
4. It also has a onboard FTDI chip that smoothens the process of burning the code on to
the processor.
Microarchitecture AVR
Clock frequency 16 MHz
Flash Memory 256 KB (8 KB bootloader)
SRAM 8 KB
Microprocessor ATmega2560
Word Size 8 bit
Operating Voltage 5V
EEPROM 4 KB
3.2 Sensor Module
In the system, we need two sensing elements. One is for determining the orientation of the
robot and another for sensing the angular position of the motor which effectively gives us the
linear displacement.
Page 6 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
3.2.1 Inertial Measurement Unit
For the orientation feedback of the robot, a 9 DOF (Degrees of Freedom) IMU was used. This
has 9 DOF through the combination of a Gyroscope, an Accelerometer and a Magnetometer.
The outputs of all sensors are processed by an on-board ATmega328 and transmitted over a
serial interface. It operates in different modes configured by the firmware uploaded in the
on-board Atmega 128. When placed over the bot, the IMU gives us the yaw, pitch and roll
(called as YPR from here on) of the system. The sensor encodes the values of YPR as double
precision floating point format and sends it through the serial output. The Rx and Tx pins are
connected to one of the serial ports of Arduino Mega and thus the data is transmitted.
3.2.1.1 Testing The IMU was tested and calibrated. For testing purpose, the IMU was
connected to the PC through a FTDI USB2Serial converter chip. A graphical interface was de-
veloped using software called Processing to visualize the results. Shown below is a snapshot
of the said visualization.
From the above tests, the geometric conventions of the IMU were found to be as below:
• X axis pointing forward (towards the short edge with the connector holes)
• Y axis pointing to the right
• Z axis pointing down.
• Positive yaw : clockwise
• Positive roll : right wing down
• Positive pitch : nose up
3.2.1.2 Calibration Process The precision and responsiveness of IMU can be improved a
lot by calibrating the sensors. If not calibrated there might be effects like
• Drifts in yaw when you apply roll to the board.
• Pointing up does not really result in an up altitude.
All three component sensors namely, gyroscope, accelerometer and magnetometer were
calibrated individually. The accelerometer was calibrated by moving the sensor slowly about
all the three axes and noting the extreme values of the sensor readings along them (this
accounts for the acceleration due to gravity). Gyroscope was calibrated by holding the
Page 7 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 4: IMU Testing in Processing software
sensor still for about 10 seconds. Magnetometer was calibrated by noting the maximum and
minimum values of the earth s magnetic field along each axis. The values obtained above,
referred as offset values, were incorporated in the firmware of the on-board processor of the
IMU.
3.2.2 Incremental Quadrature encoder
A 900 ppr incremental optical encoder was used for DC motor position feedback. It gives
two outputs (Channel A and Channel B) with phase difference depending on the direction of
the rotation. Direction of rotation can be identified by checking if a channel’s waveform is
leading or lagging the other. The Figure 5 demonstrates the waveforms.
Page 8 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 5: Quadrature signals of motor encoders
3.2.2.1 Testing The motor position was decoded from the quadrature signals using inter-
rupts triggered at rising edge of the two signals. The interrupt service routine was written
in assembly language for optimized execution. After successful position feedback from the
motors, P controller was implemented for position control of the motor with voltage as the
control input. The voltage was varied using pulse width modulation (PWM) at a frequency of
490 Hz. The result was imported in MATLAB and plotted as shown below.
Figure 6: Response of P controller on the motor position
Page 9 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
3.3 Actuator Module
The actuator module consists of two brushed DC motors and a motor driver IC.
3.3.0.2 DC Motor Two 12V, 170 RPM gear (1:30) motors with position encoders (with
specification as mentioned in the previous section) were used to drive the system. Each
motor has a stall torque of 15 kg/cm @ 12 V and 7.5 kg/cm @ 6 V. The stall current required is
1.8A @ 12V and 0.8 A @ 6 V. The motors are specified to run smoothly from 4V to 12 V.
Note: The above specifications are according to the datasheet provided by the vendor.
3.3.0.3 Motor Driver Motor driver board based on L298 was used. It can provide current
up to 2A. It can operate with PWM frequency as high as 20 kHz.
3.4 Communication Module
All the on-system communications are done through various Rx and Tx pins connected
across different modules. Wireless communication is used to control the motion of the robot
remotely and to track the motion of the robot in real time on our Personal Computer. For
this purpose, we have chosen to use Zigbee wireless protocol. It is mainly useful for short-
range low-rate wireless transfer. Its low power consumption limits transmission distances
to 10 to 100 meters. ZigBee has a defined rate of 250 kbit/s, best suited for intermittent data
transmissions from a sensor or input device.
Using this, the real time sensor data and the robot position could be sent to our Personal
computer and this data could be processed by some application like Labview or MATLAB
and plot the robot s movement. For this, we need a pair of Zigbee modules. One is connected
on board to the Arduino and the other is connected to our PC and they both are coupled.
Through this module, we could also control the movement our robot by giving the appropriate
commands through the keyboard. Thus the wireless module serves a dual purpose.
3.4.0.4 Testing Each module was connected to two different PCs and they were configured
with appropriate source and destination addresses using a software called XCTU. One of the
modules was connected to the Arduino communicating through the USART protocol. This
module then communicates with the other Zigbee connected to the PC.
Page 10 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
4 POWER CIRCUIT DESIGN
The current and power ratings of the various components of the system have been tabulated
below.
Components Voltage Ratings (volts) current rating (mA)
Arduino Mega 5 200
IMU Razor 3.3 10
Encoder 5 20
DC Motor 12 2*1800
Motor Driver 5 110
Zigbee 5 50
Total Current Requirement 3990
Total Power Requirement 45.133 W
As can be seen from the above table, the current requirements and the power requirements
are around 4 Amps and 45 Watts. A Li Po battery rated at 18.5 V, 2000mAH, 20C is used as
the power source for the entire system. Drawing the required current of 4 A will drain the
supply in about half an hour which is acceptable for our application. We use 7812 IC (voltage
regulators) to step down the 18.5 voltage to the required 12V. This reduces the efficiency of the
system but increases the stability in the supply voltage. Appropriate power distribution circuit
was developed with LEDs, diodes, decoupling capacitors and ON/OFF switches. Shown below
is the schematic of the same.
Figure 7: Power circuit schematic in Proteus
All the modules connect to the central processor, in this case, the Arduino MEGA. A shield
specific for this application has been developed for an easy-to-plug system. Shown below is
the labelled picture of the completed shield.
Page 11 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
5 DYNAMIC MODELLING
To develop a robust control system, we need to know the dynamics of the system. So, now we
derive the linear model of the DC motor and the equations of motion for the robot which
would help us in writing the state space equations of the system.
5.1 DC Motor Model
We are using two DC motors to drive the robot. The input to these motors is the voltage
applied which we are controlling in our system by varying the PWM output from the Arduino.
From this model, we get the relation between voltage applied and the controlling torque
required to balance the system.
In the DC motor, the armature current is developed as we connect a voltage source across
it. The torque produced τm is directly proportional to the this current i.
τm = kmi (1)
Also, as the motor rotates, a back emf Vem f is generated. This can be approximated to be
directly proportional to shaft velocity ω
Ve = keω (2)
We can ignore motor inductance for the purpose of modelling. Now, applying Kirchoff’s law
to the motor circuit, we have
Va −Ri −Ve = 0 (3)
The sum of all torques produced on the shaft is linearly related to the moment of inertia of
the rotor Ir i.e.,
τm −τa = IR
dω
dt
(4)
Substituting the value of Ve from Eq 2 in Eq 3 and rearranging the terms, we get
i =
−Keω
R
+
Va
R
(5)
Substituting the value of τm from Eq 1 in Eq 4 and rearranging the terms, we get
dω
dt
=
Kmi
IR
+
−τa
IR
(6)
Page 12 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Substituting the value of i from Eq 5 in Eq 6 and rearranging the terms, we get
dω
dt
=
−KmKeω
IRR
+
KmVa
IRR
+
−τa
IR
(7)
The motor dynamics can be represented with a state space model, it is a system of first
order differential equations with parameters position θ, and velocity ω, that represents its
operation. The inputs to the motor is the applied voltage and applied torque.
˙θ
˙ω
=
0 1
0 −KmKe
IR R
θ
ω
+
0 0
Km
IR R
−1
IR
Va
τa
(8)
y = 1 0
θ
ω
+ 0 0
Va
τa
(9)
5.2 Model of the robot
We are equating our robot to a inverted pendulum. The two wheels act as the cart and the
robot chassis acts as the inverted pendulum. We first analyse the wheel dynamics and then
analyse the body dynamics which will eventually get us the state equations that govern the
motion of the system.
5.2.1 Equations for the wheels
We are writing the equations of motion with respect to two wheels. The different forces
acting on one of the wheels could be shown in the below figure. Since, both the wheels are
analogous, we could write for one of them and get the other easily.
From the Figure 8, the forces acting on the right wheel could be seen. Equating the forces
in the horizontal x direction, we get
Mw ¨x = Hf − HR (10)
Page 13 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 8: Free body diagram of the right wheel
Sum of moments around the centre of wheels is given by
Iw ¨ωw = CR − Hf r (11)
We know CR is nothing but the τm which we derived in the previous section Eq 4.By substitut-
ing Eq 7 in Eq 4 and rearranging the terms, we have
CR =
−KmKeω
R
+
KmVa
R
(12)
Substituting the value of CR in Eq 11, we get
Iw ¨ωw =
−KmKeω
R
+
KmVa
R
− Hf (13)
Rearranging the above equation to get Hf as the subject
Hf =
−KmKeω
Rr
+
KmVa
Rr
−
−Iw ¨ωw
r
(14)
Substituting this value of Hf in Eq 10, we get
Mw ¨x =
−KmKeω
Rr
+
KmVa
Rr
−
−Iw ¨ωw
r
− HR (15)
We can similarly find this equation for the left wheel which is
Mw ¨x =
−KmKeω
Rr
+
KmVa
Rr
−
−Iw ¨ωw
r
− HL (16)
Page 14 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Since the centre of the wheel is moving in a straight line, we can approximate the angular
rotation into linear motion by simple transformations,
r ¨θ = ¨x (17)
r ˙θ = ˙x (18)
Using this transformations and rewriting the equations 15 and 16, we get the following
equations for the left and the right wheel respectively
Mw ¨x =
−KmKe x
Rr2
+
KmVa
Rr
−
−Iw ¨x
r2
− HL (19)
Mw ¨x =
−KmKe x
Rr2
+
KmVa
Rr
−
−Iw ¨x
r2
− HR (20)
Adding the above two equations and rearranging the terms, we get
2 Mw +
Iw
r2
¨x =
−2KmKe x
Rr2
+
2KmVa
Rr
−(HL + HR) (21)
5.2.2 Equations for the chassis
Figure 9: Free body diagram of the chasis
We are approximating the chassis to be the inverted pendulum in the above figure which
Page 15 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
shows us all the forces acting on it
By summing all the forces in the horizontal direction, we get
(HL + HR) = Mp ¨x + Mpl ¨θcos(θ)− Mpl ˙θ2 sin(θ) (22)
Now, summing the forces in the direction perpendicular to the pendulum, we have
(HL + HR)cos(θ)+(PL +PR)sin(θ)− Mp g sin(θ)− Mpl ¨θ = Mp ¨x cos(θ) (23)
Now, summing the moments around the centre of mass of pendulum, we get
−(HL + HR)I cos(θ)−(PL +PR)l sin(θ)−(CL +CR) = I ¨θ (24)
From Eq 12, we have
(CL +CR) = 2
−KmKeω
R
+2
KmVa
R
(25)
Substituting the value of (CL +CR) in Eq 24, we get
−(HL + HR)l cos(θ)−(PL +PR)l sin(θ) = I ¨θ +2
−KmKeω
R
+2
KmVa
R
(26)
Multiplying the Eq 23 by -l and equating its RHS with the RHS of Eq 26, we get
−Mp gl sin(θ)− Mpl2 ¨θ − Mpl ¨x cos(θ) = I ¨θ +2
−KmKeω
R
+2
KmVa
R
(27)
Substituting the value of (HL + HR) from Eq 22 in Eq 21 gives
2 Mw +
Iw
r2
¨x =
−2KmKe x
Rr2
+
2KmVa
Rr
− Mp ¨x − Mpl ¨θcos(θ)+ Mpl ˙θ2 sin(θ) (28)
The above 2 equations can be linearised by assuming θ = π + φ where φ is the small angular
deviation from the vertical axis. With this approximation, we have cos(θ) = -1, sin(θ) = −φ
and ¨θ = 0
Using the above approximations in the equations 27 and 28 and rearranging the terms,
we have
¨φ =
Mpl ¨x
Ip + Mpl2
+
−2KmKe
˙(x)
Rr Ip + Mpl2
+
−2KmVa
Rr Ip + Mpl2
+
Mp glφ
Ip + Mpl2
(29)
¨x =
2KmVa
Rr 2Mw + 2Iw
r2 + Mp
−
2KmKe ˙x
Rr2 2Mw + 2Iw
r2 + Mp
+
Mpl ¨φ
2Mw + 2Iw
r2 + Mp
(30)
Page 16 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
After substituting the above two equations in each other and some manipulation of terms,
we get the state space equations given by







˙x
¨x
˙φ
¨φ







=







0 1 0 0
0
2KmKe (Mp lr−Ip −Mp l2
)
Rr2α
M2
p gl2
α 0
0 0 0 1
0
2KmKe (rβ−Mp l)
Rr2α
M2
p glβ
α
0














x
˙x
φ
˙φ







+







0
2Km(Ip +Mp l2
−Mp lr)
Rrα
0
2Km(Mp l−rβ)
Rrα







Va
(31)
where α and β are given by
α = Ipβ+2Mpl2
Mw +
Iw
r2
(32)
β = 2Mw +
2Iw
r2
+ Mp (33)
x
φ
=
1 0 0 0
0 0 1 0







x
˙x
φ
˙φ







(34)
Thus we found the matrices A, B, C for the state space model of our system based on the
system parameters. We can find the transfer function for the input and output using the
equation below and the resulting transfer function is shown in the next section.
Y (s) = C (sI − A)−1
BU (s) (35)
5.3 Approximate values of system parameters
Here, we are approximately finding the values of system parameters to get a rough idea by
simulating the resulting transfer functions.
1. The mass of the wheel has been given in the buyer’s manual and the weight of the robot
is approximated to 4 kg.
2. Radius of the wheel and the length of the bot have been measured with the help of a
ruler
Page 17 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
3. The wheel is assumed to be a solid cylinder and its moment o inertia is thus found from
its mass and radius.
4. The moment of inertia of the robot is measured by approximating it to a rod with the
axis of rotation being perpendicular to it through one of the ends which gives us Ml2
3
5. The armature resistance is assumed to be 1 ohm.
6. The Km and Ke values are approximated to be 0.01 N.m/Amp and 0.01 V/rad/sec
respectively
Mp 4 kg
Ip 0.12 kg.m2
Mw 0.123 kg
Iw 0.00173 kg.m2
r 0.05 m
l 0.3 m
R 1 ohm
Km 0.01 N.m/Amp
Ke 0.01 V/rad/sec
6 CONTROL SYSTEM DESIGN
In this section we describe the detailed analysis of the dynamic model of the system formu-
lated in the previous section. An appropriate control system for the system is suggested by
various techniques. Hardware implementation of the same has also been commented on by
the end of the section.
The transfer function of the pendulum’s angular position, φ, to the input voltage, Va, can
be obtained from Eq 35. Solving it we get the transfer function as follows.
T (s) =
φ(S)
V (s)
=
αzS +γz
s3 −αp s2 −βp s +γp
(36)
Page 18 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
where,
αz =
2Km(Mpl)−2r Mw −2Iw
r − Mpr
Rr(Ipβ+2Mpl2(Mw + Iw
r2 ))
γz =
−4K 2
mKe(Ip + Mpl2
− Mplr)2
+4K 2
mKe(Mplr − Ip − Mpl2
)(Mpl −rβ)
R2r3(β)
αp =
2KmKe(Mplr − Ip − Mpl2
)
Rr2(Ipβ+2Mpl2(Mw + Iw
r2 )
βp =
Mp gl(Mp −β)
β
γp =
2KmKe Mp gl(Mplrβ− Ipβ− Mpl2
β+ Mpl2
−rlβ)
Rr2(β)2
β = 2Mw +
2Iw
r2
+ Mp
The open loop impulse response and step response of the system are shown in Figure 10
and Figure 11 respectively.
Figure 10: Open loop impulse response
Page 19 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 11: Open loop step response
6.1 PID Control
The control system problem is to find an appropriate compensator which places the closed-
loop poles at desired locations to achieve required transient response. Figure 12 shows the
structure of the compensated system with compensator transfer function given by C(s).
Figure 12: System Structure
The generic model of the PID compensator is given by Eq 37.
Page 20 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
C(s) =
Kd s2
+Kp s +Ki
s
(37)
Tuning the three gains, Kd, Kp and Ki, we can place the closed-loop poles at the desired
locations. Further analysis in terms of root-locus of the system can yield a detailed insight
into the system behaviour.
The root-locus of the plant without any compensator, plotted in MATLAB (with sample
system parameters for the purpose of qualitative analysis), is shown in Figure 13. The poles
of T (s) are located at 5.5651, -5.6041 and -0.1428. One zero is present at origin. The same can
be seen in the root-locus plot.
Figure 13: Root locus of the plant with varying gain
As can be seen, one branch (coloured blue) is always on the right side of the imaginary
axis. This establishes that for any value of the gain K chosen, the system can not be stabilized
without a compensator. To solve this, we add a pole (integrator) at origin to cancel the zero at
that location. This generates two poles branching out to the right-side of the s-plane as shown
in Figure 14. From the properties of root-locus we know that we can pull the poles to the
left-half plane by addition of zeroes. We observe that addition of one zero does not pull the
poles enough to the left-side to meet acceptable transient behaviour. Thus we add two zeroes
Page 21 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
at −3 and −4. The resulting root-locus is shown in Figure 15. Choosing the gain at appropriate
closed loop pole-zero location on the root-locus will result in a fully compensated system
meeting the transient response requirements. The system impulse response of pendulum
angular position with gain at 20 is shown in Figure 16. The poles for the final compensated
closed loop transfer function are as follows.
p1 = −84.0439+0.0000i
p2 = 0.0000+0.0000i
p3 = −3.5235+0.7157i
p4 = −3.5235−0.7157i
All poles are located on the left side of the imaginary axis indicating the stability of the
system. The response of the cart’s position is shown in Figure 17.
Figure 14: Root locus of the plant with integrator
This method allows for control of the pendulum position but provides no means for the
control of the cart position, as evidenced by Figure 17. The state-space method described in
the next section caters to this need.
Page 22 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 15: Root locus of the plant with PID compensator
Note that in the current hardware implementation, these gains were tuned using the
Ziegler-Nichols PID tuning algorithm. This is because of the unavailability of the value of
various system parameters.
6.2 State Space Control
The state-space control technique can be applied to Multi-Input, Multi-Output (MIMO) sys-
tems. Our system, when considering cart position control, is a SIMO (Single-Input, Multiple-
Output) system.
The system dynamics can be represented by
˙X = AX +BU
Y = C X
where matrices A, B and C are given Eq 31 and Eq 34. X is the state vector with four states
as cart position: x, cart velocity: ˙x, pendulum’s angular position from the vertical: φ and
Page 23 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 16: Root locus of the plant with integrator
pendulum’s angular velocity: ˙φ. The outputs of the system are cart position: x and pendulum
angular position from the vertical: φ
The state-space control uses full state feedback. The schematic is shown in Figure 18.
The state-space equations of the above system configuration is given as follows.
˙X = AX +BU = AX +B(−K X +r) = (A −BK )X +Br
˙X = (A −BK )X +Br
Y = C X (38)
where K = [k1k2 ···k3]
The feedback gain vector K can be easily found by representing the system in Phase-
Variable form. The characteristic equation of the system is given by
sn
+ an−1sn−1
+···+ a1s + a0 = 0
The state-space representation of the open-loop system in this form is given by Eq 39.
Page 24 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 17: Root locus of the plant with integrator
Figure 18: State feedback control loop structure
A =












0 1 0 ... 0
0 0 0 ... 0
. . . . .
. . . . .
. . . . .
−a0 −a1 −a2 ... −an−1












B =












0
0
.
.
.
1












C = c1 c2 c3 ... cn (39)
Page 25 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
The closed loop form is given by Eq 40.
A =












0 1 0 ... 0
0 0 0 ... 0
. . . . .
. . . . .
. . . . .
−(a0 +k1) −(a1 +k2) −(a2 +k3) ... −(an−1 +kn)












(40)
The characteristic equation of the closed loop system is given by Eq 41.
sn
+(an−1 +kn)sn−1
+(an−2 +kn−1)sn−2
+···+(a1 +k2)s +(a0 +k1) = 0 (41)
Let the desired characteristic equation be as follows,
sn
+dn−1sn−1
+dn−2sn−2
+···+d1s +d0 = 0
In the above two equations, by equating the coefficients to the desired coefficients we get
the values of Ki .
Ki+1 = di − ai
After placing the poles at the following locations, the system step response is shown in
Figure 19. The input is the step input for target cart position.
p1 = −8.4910+7.9283i
p2 = −8.4910−7.9283i
p3 = −4.7592+0.8309i
p4 = −4.7592−0.8309i
Page 26 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
Figure 19: Response of Cart’s position and Pendulum’s angular position under state space control
7 MATLAB CODE
The MATLAB code used for obtaining the above simulation results is shown from here on for
the reference of the reader.
clear all;
%%%%%%% System Parameter Values %%%%%%%%
Mp = 4;
Ip = 0.12;
Mw = 0.123;
Iw = 0.00173;
r = 0.05;
l = 0.3;
R = 1;
Km = 0.01;
Ke = 0.01;
g = 9.8;
b = 2*Mw + 2*Iw/(r^2) + Mp;
Page 27 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
a = (Ip*b + 2*Mp*l^2 * (Mw + Iw/(r^2)));
A = [0 1 0 0;
0 2*Km*Ke*(Mp*l*r−Ip−Mp*(l^2))/R*(r^2)*a (Mp^2*g*l^2)/a 0;
0 0 0 1;
0 2*Km*Ke*(r*b − Mp*l)/R*(r^2)*a (Mp^2*g*l*b)/a 0];
B = [ 0;
2*Km*(Ip + Mp*(l^2) − Mp*l*r)/R*r*a;
0;
2*Km*(Mp*l − r*b)/R*r*a];
C = [1 0 0 0;
0 0 1 0];
D = [0;
0];
s = tf('s');
states = {'x' 'x_dot' 'phi' 'phi_dot'}
inputs = {'u'};
outputs = {'x'; 'phi'};
sys_ss = ss(A, B, C, D, 'statename', states, 'inputname', inputs, 'outputname', outputs)
sys_tf = tf(sys_ss)
%%%%%%%% Impulse response %%%%%%%
% t=0:0.01:1;
% impulse(sys_tf,t);
% title('Open−Loop Impulse Response')
%
% [zeros_1 poles_1] = zpkdata(P_pend, 'v');
% [zeros_2 poles_2] = zpkdata(P_pend, 'v');
%%%%%%%% Step response %%%%%%%
% t = 0:0.05:10;
% u = ones(size(t));
% [y, t] = lsim(sys_tf, u, t);
%
% plot(t, y)
% title('Open−Loop Step Response')
% axis([0 3 0 50])
Page 28 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
% legend('x', 'phi')
%
% step_info = lsiminfo(y, t);
% cart_info = step_info(1)
% pend_info = step_info(2)
%%%%%%%%% PID Control %%%%%%%%
% Kp = 100;
% Ki = 1;
% Kd = 0;
%
% C = pid(Kp, Ki, Kd);
% T = feedback(P_pend, C);
%
% T2 = feedback(1,P_pend*C)*P_cart;
%
% t=0:0.01:10;
% impulse(T,t)
% title('Response of Pendulum Position to an Impulse Disturbance under PID Control: Kp =
% t = 0:0.01:5;
% impulse(T2, t);
% title('Response of Cart Position to an Impulse Disturbance under PID Control: Kp = 100
%%%%%%% Root locus analysis %%%%%%%
% rlocus(P_pend)
% title('Root locus of the plant with vaying gain')
% C_int = 1/s;
% rlocus(C_int * P_pend)
% title('Root locus of the plant with integrator')
%
% % zeros = zero(C_int * P_pend)
% % poles = pole(C_int * P_pend)
%
% z = [−3 −4];
% p = 0;
% k = 1;
% C_pid = zpk(z, p, k);
% rlocus(C_pid*P_pend)
% title('Root locus of the plant with PID compensator')
Page 29 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
% % %
% [k, target_pole] = rlocfind(C_pid*P_pend)
%
% k = 35;
% T = feedback(P_pend, k*C_pid);
% figure(1)
% impulse(T)
% title('Impulse response of the plant with PID compensator')
%
% T3 = feedback(1,P_pend*C_pid)*P_cart;
% figure(2)
% impulse(T3)
% title('Impulse response of the cart position with PID compensator')
% axis([0 10 −200 200])
%%%%%%% State−space control %%%%%%%
% poles = eig(A)
P = [−8.4910 + 7.9283i −8.4910 − 7.9283i −4.7592 + 0.8309i −4.7592 − 0.8309i];
K = place(A, B, P)
Ac = [(A − B*K)];
Bc = [B];
Cc = [C];
Dc = [D];
p = eig(Ac)
states = {'x' 'x_dot' 'phi' 'phi_dot'};
inputs = {'r'};
outputs = {'x' 'phi'};
sys_cl = ss(Ac, Bc, Cc, Dc, 'statename', states, 'inputName', inputs, 'outputName', outp
t = 0:0.01:5;
r = 0.2*ones(size(t));
[y, t, x] = lsim(sys_cl, r, t);
[AX, H1, H2] = plotyy(t, y(:,1), t, y(:, 2), 'plot');
set(get(AX(1), 'Ylabel'), 'String', 'Cart Position (m)');
set(get(AX(2), 'Ylabel'), 'String', 'Pendulum angle (radians)');
title('Step Response with State Space Control')
Page 30 of 31
Electrical Engineering Indian Institute of Technology, Kharagpur
REFERENCES
1. The Control of an Inverted Pendulum,[manual] Available at:<https://engineering.
purdue.edu/AAE/Academics/Courses/aae364L/2011/Fall_2011/lab3/manual>
2. Self-balancing two-wheeled robot,[report] Available at:<http://sebastiannilsson.
com/wp-content/uploads/2013/05/Self-balancing-two-wheeled-robot-report.
pdf>
3. Control Systems Engineering - Norman S. Nise
4. Exploring XBees and XCTU,[manual] Available at:<https://learn.sparkfun.com/
tutorials/exploring-xbees-and-xctu>
5. 9 DOF razor IMU calibration manual,[manual] Available at:<https://github.com/
ptrbrtz/razor-9dof-ahrs/wiki/Tutorial>
6. Arduino tuitorials on www.arduino.cc
Page 31 of 31

More Related Content

What's hot

Stabilized controller of a two wheels robot
Stabilized controller of a two wheels robotStabilized controller of a two wheels robot
Stabilized controller of a two wheels robotjournalBEEI
 
Self-Balancing Two-Wheeler
Self-Balancing Two-WheelerSelf-Balancing Two-Wheeler
Self-Balancing Two-Wheelervivatechijri
 
MAE501 Independent Study
MAE501 Independent StudyMAE501 Independent Study
MAE501 Independent Studyhrishishah
 
The stabilization of forced inverted pendulum via fuzzy controller
The stabilization of forced inverted pendulum via fuzzy controllerThe stabilization of forced inverted pendulum via fuzzy controller
The stabilization of forced inverted pendulum via fuzzy controllereSAT Journals
 
IMPROVING POWER FACTORS BY USING LANDSMAN CONVERTER IN PMBLDC MOTOR
IMPROVING POWER FACTORS BY USING LANDSMAN CONVERTER IN PMBLDC MOTORIMPROVING POWER FACTORS BY USING LANDSMAN CONVERTER IN PMBLDC MOTOR
IMPROVING POWER FACTORS BY USING LANDSMAN CONVERTER IN PMBLDC MOTORvivatechijri
 
Design and fabrication of delta robot.pptx ignite
Design and fabrication of delta robot.pptx igniteDesign and fabrication of delta robot.pptx ignite
Design and fabrication of delta robot.pptx igniteAbhishekKash2
 
Mathematical model analysis and control algorithms design based on state feed...
Mathematical model analysis and control algorithms design based on state feed...Mathematical model analysis and control algorithms design based on state feed...
Mathematical model analysis and control algorithms design based on state feed...hunypink
 
Gesture control wheel chair cum stretcher
Gesture control wheel chair cum stretcherGesture control wheel chair cum stretcher
Gesture control wheel chair cum stretcherIRJET Journal
 
Dual Angle Comparison for Wheel Chair Vehicle using Fuzzy Logic
Dual Angle Comparison for Wheel Chair Vehicle using Fuzzy LogicDual Angle Comparison for Wheel Chair Vehicle using Fuzzy Logic
Dual Angle Comparison for Wheel Chair Vehicle using Fuzzy LogicIRJET Journal
 
Nov163265 research paper
Nov163265 research paperNov163265 research paper
Nov163265 research paperARJUN MAURYA
 
Development of a two link robotic manipulator
Development of a two link robotic manipulatorDevelopment of a two link robotic manipulator
Development of a two link robotic manipulatorNajeem Olawale Adelakun
 
Asts file
Asts file Asts file
Asts file ssmb45
 
Development of a quadruped mobile robot and its movement system using geometr...
Development of a quadruped mobile robot and its movement system using geometr...Development of a quadruped mobile robot and its movement system using geometr...
Development of a quadruped mobile robot and its movement system using geometr...journalBEEI
 
Chong-A-dual-mode-EMG-controlled-robotic-orthosis
Chong-A-dual-mode-EMG-controlled-robotic-orthosisChong-A-dual-mode-EMG-controlled-robotic-orthosis
Chong-A-dual-mode-EMG-controlled-robotic-orthosisAlhasan Alabd
 

What's hot (20)

Self Balancing Robot
Self Balancing RobotSelf Balancing Robot
Self Balancing Robot
 
invertedpendulum
invertedpenduluminvertedpendulum
invertedpendulum
 
Stabilized controller of a two wheels robot
Stabilized controller of a two wheels robotStabilized controller of a two wheels robot
Stabilized controller of a two wheels robot
 
Self-Balancing Two-Wheeler
Self-Balancing Two-WheelerSelf-Balancing Two-Wheeler
Self-Balancing Two-Wheeler
 
final project
final projectfinal project
final project
 
about my Robotic design
about my Robotic designabout my Robotic design
about my Robotic design
 
MAE501 Independent Study
MAE501 Independent StudyMAE501 Independent Study
MAE501 Independent Study
 
The stabilization of forced inverted pendulum via fuzzy controller
The stabilization of forced inverted pendulum via fuzzy controllerThe stabilization of forced inverted pendulum via fuzzy controller
The stabilization of forced inverted pendulum via fuzzy controller
 
IMPROVING POWER FACTORS BY USING LANDSMAN CONVERTER IN PMBLDC MOTOR
IMPROVING POWER FACTORS BY USING LANDSMAN CONVERTER IN PMBLDC MOTORIMPROVING POWER FACTORS BY USING LANDSMAN CONVERTER IN PMBLDC MOTOR
IMPROVING POWER FACTORS BY USING LANDSMAN CONVERTER IN PMBLDC MOTOR
 
Design and fabrication of delta robot.pptx ignite
Design and fabrication of delta robot.pptx igniteDesign and fabrication of delta robot.pptx ignite
Design and fabrication of delta robot.pptx ignite
 
Mathematical model analysis and control algorithms design based on state feed...
Mathematical model analysis and control algorithms design based on state feed...Mathematical model analysis and control algorithms design based on state feed...
Mathematical model analysis and control algorithms design based on state feed...
 
Gesture control wheel chair cum stretcher
Gesture control wheel chair cum stretcherGesture control wheel chair cum stretcher
Gesture control wheel chair cum stretcher
 
V5I2-IJERTV5IS020384
V5I2-IJERTV5IS020384V5I2-IJERTV5IS020384
V5I2-IJERTV5IS020384
 
Robot tradeind
Robot tradeindRobot tradeind
Robot tradeind
 
Dual Angle Comparison for Wheel Chair Vehicle using Fuzzy Logic
Dual Angle Comparison for Wheel Chair Vehicle using Fuzzy LogicDual Angle Comparison for Wheel Chair Vehicle using Fuzzy Logic
Dual Angle Comparison for Wheel Chair Vehicle using Fuzzy Logic
 
Nov163265 research paper
Nov163265 research paperNov163265 research paper
Nov163265 research paper
 
Development of a two link robotic manipulator
Development of a two link robotic manipulatorDevelopment of a two link robotic manipulator
Development of a two link robotic manipulator
 
Asts file
Asts file Asts file
Asts file
 
Development of a quadruped mobile robot and its movement system using geometr...
Development of a quadruped mobile robot and its movement system using geometr...Development of a quadruped mobile robot and its movement system using geometr...
Development of a quadruped mobile robot and its movement system using geometr...
 
Chong-A-dual-mode-EMG-controlled-robotic-orthosis
Chong-A-dual-mode-EMG-controlled-robotic-orthosisChong-A-dual-mode-EMG-controlled-robotic-orthosis
Chong-A-dual-mode-EMG-controlled-robotic-orthosis
 

Similar to report_komal

ROBOCON 2015 Documentation
ROBOCON 2015 DocumentationROBOCON 2015 Documentation
ROBOCON 2015 DocumentationVamsi Krishna
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
Identification and Real Time Control of a DC Motor
Identification and Real Time Control of a DC MotorIdentification and Real Time Control of a DC Motor
Identification and Real Time Control of a DC MotorIOSR Journals
 
Computer Numerical Control_2018.pdf
Computer Numerical Control_2018.pdfComputer Numerical Control_2018.pdf
Computer Numerical Control_2018.pdfadminpeo
 
Small_Size_League_-_RoboCup_2016_-_TDP_KN2C
Small_Size_League_-_RoboCup_2016_-_TDP_KN2CSmall_Size_League_-_RoboCup_2016_-_TDP_KN2C
Small_Size_League_-_RoboCup_2016_-_TDP_KN2CMorvarid Mohammadian
 
Small_Size_League_-_RoboCup_2016_-_TDP_KN2C
Small_Size_League_-_RoboCup_2016_-_TDP_KN2CSmall_Size_League_-_RoboCup_2016_-_TDP_KN2C
Small_Size_League_-_RoboCup_2016_-_TDP_KN2CArmin Agharazi
 
D0255033039
D0255033039D0255033039
D0255033039theijes
 
Autonomous Terrain Mapping Using COTS Hardware
Autonomous Terrain Mapping Using COTS HardwareAutonomous Terrain Mapping Using COTS Hardware
Autonomous Terrain Mapping Using COTS HardwareJames Anderson
 
Report pid controller dc motor
Report pid controller dc motorReport pid controller dc motor
Report pid controller dc motorchea kimsairng
 
Modeling of DC Motor and Choosing the Best Gains for PID Controller
Modeling of DC Motor and Choosing the Best Gains for PID ControllerModeling of DC Motor and Choosing the Best Gains for PID Controller
Modeling of DC Motor and Choosing the Best Gains for PID Controllerijtsrd
 
Comparison of different controllers for the improvement of Dynamic response o...
Comparison of different controllers for the improvement of Dynamic response o...Comparison of different controllers for the improvement of Dynamic response o...
Comparison of different controllers for the improvement of Dynamic response o...IJERA Editor
 
Simulation Design of DC Motor Control System Based on MC9S12D64 MCU
Simulation Design of DC Motor Control System Based on MC9S12D64 MCUSimulation Design of DC Motor Control System Based on MC9S12D64 MCU
Simulation Design of DC Motor Control System Based on MC9S12D64 MCUIJERA Editor
 
Direct Torque Control of a Bldc Motor Based on Computing Technique
Direct Torque Control of a Bldc Motor Based on Computing TechniqueDirect Torque Control of a Bldc Motor Based on Computing Technique
Direct Torque Control of a Bldc Motor Based on Computing TechniqueIOSR Journals
 
Gesture Controlled Chair
Gesture Controlled ChairGesture Controlled Chair
Gesture Controlled Chairtheijes
 
Power System Stabilizer (PSS) for generator
Power System Stabilizer (PSS) for generatorPower System Stabilizer (PSS) for generator
Power System Stabilizer (PSS) for generatorKARAN TRIPATHI
 

Similar to report_komal (20)

ROBOCON 2015 Documentation
ROBOCON 2015 DocumentationROBOCON 2015 Documentation
ROBOCON 2015 Documentation
 
An4556
An4556An4556
An4556
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)
 
Pid
PidPid
Pid
 
Identification and Real Time Control of a DC Motor
Identification and Real Time Control of a DC MotorIdentification and Real Time Control of a DC Motor
Identification and Real Time Control of a DC Motor
 
Computer Numerical Control_2018.pdf
Computer Numerical Control_2018.pdfComputer Numerical Control_2018.pdf
Computer Numerical Control_2018.pdf
 
Small_Size_League_-_RoboCup_2016_-_TDP_KN2C
Small_Size_League_-_RoboCup_2016_-_TDP_KN2CSmall_Size_League_-_RoboCup_2016_-_TDP_KN2C
Small_Size_League_-_RoboCup_2016_-_TDP_KN2C
 
Small_Size_League_-_RoboCup_2016_-_TDP_KN2C
Small_Size_League_-_RoboCup_2016_-_TDP_KN2CSmall_Size_League_-_RoboCup_2016_-_TDP_KN2C
Small_Size_League_-_RoboCup_2016_-_TDP_KN2C
 
D0255033039
D0255033039D0255033039
D0255033039
 
Autonomous Terrain Mapping Using COTS Hardware
Autonomous Terrain Mapping Using COTS HardwareAutonomous Terrain Mapping Using COTS Hardware
Autonomous Terrain Mapping Using COTS Hardware
 
Report pid controller dc motor
Report pid controller dc motorReport pid controller dc motor
Report pid controller dc motor
 
Modeling of DC Motor and Choosing the Best Gains for PID Controller
Modeling of DC Motor and Choosing the Best Gains for PID ControllerModeling of DC Motor and Choosing the Best Gains for PID Controller
Modeling of DC Motor and Choosing the Best Gains for PID Controller
 
Comparison of different controllers for the improvement of Dynamic response o...
Comparison of different controllers for the improvement of Dynamic response o...Comparison of different controllers for the improvement of Dynamic response o...
Comparison of different controllers for the improvement of Dynamic response o...
 
FYP template.pptx
FYP template.pptxFYP template.pptx
FYP template.pptx
 
Simulation Design of DC Motor Control System Based on MC9S12D64 MCU
Simulation Design of DC Motor Control System Based on MC9S12D64 MCUSimulation Design of DC Motor Control System Based on MC9S12D64 MCU
Simulation Design of DC Motor Control System Based on MC9S12D64 MCU
 
An 706
An 706An 706
An 706
 
Direct Torque Control of a Bldc Motor Based on Computing Technique
Direct Torque Control of a Bldc Motor Based on Computing TechniqueDirect Torque Control of a Bldc Motor Based on Computing Technique
Direct Torque Control of a Bldc Motor Based on Computing Technique
 
Gesture Controlled Chair
Gesture Controlled ChairGesture Controlled Chair
Gesture Controlled Chair
 
Power System Stabilizer (PSS) for generator
Power System Stabilizer (PSS) for generatorPower System Stabilizer (PSS) for generator
Power System Stabilizer (PSS) for generator
 
Ecas bus
Ecas busEcas bus
Ecas bus
 

report_komal

  • 1. REPORT SELF BALANCING ROBOT May 3, 2016 Teru Komal Kumar, 12EE35018 Guide : Prof. Siddhartha Sen Indian Institute of Technology, Kharagpur Department of Electrical Engineering 1
  • 2. Electrical Engineering Indian Institute of Technology, Kharagpur Contents 1 Introduction 3 2 Nomenclature of Symbols used 4 3 System overview 5 3.0.0.1 Mechanical system . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.0.0.2 Electronic System . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.1 Processor Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.2 Sensor Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.2.1 Inertial Measurement Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.2.1.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.2.1.2 Calibration Process . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.2.2 Incremental Quadrature encoder . . . . . . . . . . . . . . . . . . . . . . . 8 3.2.2.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.3 Actuator Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.3.0.2 DC Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.3.0.3 Motor Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.4 Communication Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.4.0.4 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4 Power circuit design 11 5 Dynamic Modelling 12 5.1 DC Motor Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 5.2 Model of the robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 5.2.1 Equations for the wheels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 5.2.2 Equations for the chassis . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.3 Approximate values of system parameters . . . . . . . . . . . . . . . . . . . . . . 17 6 Control System Design 18 6.1 PID Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 6.2 State Space Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 7 MATLAB Code 27 References 31 Page 2 of 31
  • 3. Electrical Engineering Indian Institute of Technology, Kharagpur 1 INTRODUCTION In this report, we discuss the process of building the self balancing robot from the scratch. This robot uses the model of inverted pendulum as its reference model. The cart in this case is made up of two wheeled differential drive. The robot that is built is inherently unstable and without the action of an external controller, it would fall down. To stabilise it, the motors need to driven in the appropriate direction. The report is majorly divided into three sections. In the first section, we look at the system overview i.e., different modules involved and their functioning. In the second section, we worked on finding the parameters related to motor dynamics and the robot dynamics that would help us in getting the state space equations. In the next section, we have discussed about two control methods that would help us in stabilising the system namely the PID control and the state space control. Figure 1: Model of the robot developed for this project Page 3 of 31
  • 4. Electrical Engineering Indian Institute of Technology, Kharagpur 2 NOMENCLATURE OF SYMBOLS USED τm Motor Torque τa Applied Torque Va Applied Voltage Ve Back emf R Armature resistance IR Rotor Inertia Km Torque Constant (N.m/A) Ke Back emf Constant (V.s/rad) i Armature current Mw Mass of the wheel Mp Mass of the chasis Ip Robot Chasis Inertia CL,CR Torque exerted by motors on the wheels Hf Frictional force between the wheel and the ground HL,HR,PL,PR Reaction forces between wheels and chasis Kp Proportional Gain Kd Differential Gain Ki Integral Gain Page 4 of 31
  • 5. Electrical Engineering Indian Institute of Technology, Kharagpur 3 SYSTEM OVERVIEW 3.0.0.1 Mechanical system The basic structure of the robot is built from wood. The wooden structure has two plates where the components like the motors, sensors and com- munication module can be mounted.These plates can be moved up and down which would help us in manipulating the centre of gravity of the bot. Two wheels of radii of about 5 cm are used for the ’cart’ part of the system. 3.0.0.2 Electronic System The system contains 4 different modules which include Proces- sor module, Sensor module, Actuator module and Communication module. Figure 2: System Overview 3.1 Processor Module Owing to our experience in working with Atmega processor as part of our coursework, we have chosen Arduino Mega 2560 microcontroller as the processor for the robot. Some of its desirable qualities include 1. It has 4 serial ports and hence can be used to communicate with multiple devices simultaneously. 2. It has a onboard TTL-to-USB serial chip that allows for ease in communication with the PC. Page 5 of 31
  • 6. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 3: Arduino MEGA 2560 3. It has 15 PWM pins which are helpful in controlling the actuators. 4. It also has a onboard FTDI chip that smoothens the process of burning the code on to the processor. Microarchitecture AVR Clock frequency 16 MHz Flash Memory 256 KB (8 KB bootloader) SRAM 8 KB Microprocessor ATmega2560 Word Size 8 bit Operating Voltage 5V EEPROM 4 KB 3.2 Sensor Module In the system, we need two sensing elements. One is for determining the orientation of the robot and another for sensing the angular position of the motor which effectively gives us the linear displacement. Page 6 of 31
  • 7. Electrical Engineering Indian Institute of Technology, Kharagpur 3.2.1 Inertial Measurement Unit For the orientation feedback of the robot, a 9 DOF (Degrees of Freedom) IMU was used. This has 9 DOF through the combination of a Gyroscope, an Accelerometer and a Magnetometer. The outputs of all sensors are processed by an on-board ATmega328 and transmitted over a serial interface. It operates in different modes configured by the firmware uploaded in the on-board Atmega 128. When placed over the bot, the IMU gives us the yaw, pitch and roll (called as YPR from here on) of the system. The sensor encodes the values of YPR as double precision floating point format and sends it through the serial output. The Rx and Tx pins are connected to one of the serial ports of Arduino Mega and thus the data is transmitted. 3.2.1.1 Testing The IMU was tested and calibrated. For testing purpose, the IMU was connected to the PC through a FTDI USB2Serial converter chip. A graphical interface was de- veloped using software called Processing to visualize the results. Shown below is a snapshot of the said visualization. From the above tests, the geometric conventions of the IMU were found to be as below: • X axis pointing forward (towards the short edge with the connector holes) • Y axis pointing to the right • Z axis pointing down. • Positive yaw : clockwise • Positive roll : right wing down • Positive pitch : nose up 3.2.1.2 Calibration Process The precision and responsiveness of IMU can be improved a lot by calibrating the sensors. If not calibrated there might be effects like • Drifts in yaw when you apply roll to the board. • Pointing up does not really result in an up altitude. All three component sensors namely, gyroscope, accelerometer and magnetometer were calibrated individually. The accelerometer was calibrated by moving the sensor slowly about all the three axes and noting the extreme values of the sensor readings along them (this accounts for the acceleration due to gravity). Gyroscope was calibrated by holding the Page 7 of 31
  • 8. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 4: IMU Testing in Processing software sensor still for about 10 seconds. Magnetometer was calibrated by noting the maximum and minimum values of the earth s magnetic field along each axis. The values obtained above, referred as offset values, were incorporated in the firmware of the on-board processor of the IMU. 3.2.2 Incremental Quadrature encoder A 900 ppr incremental optical encoder was used for DC motor position feedback. It gives two outputs (Channel A and Channel B) with phase difference depending on the direction of the rotation. Direction of rotation can be identified by checking if a channel’s waveform is leading or lagging the other. The Figure 5 demonstrates the waveforms. Page 8 of 31
  • 9. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 5: Quadrature signals of motor encoders 3.2.2.1 Testing The motor position was decoded from the quadrature signals using inter- rupts triggered at rising edge of the two signals. The interrupt service routine was written in assembly language for optimized execution. After successful position feedback from the motors, P controller was implemented for position control of the motor with voltage as the control input. The voltage was varied using pulse width modulation (PWM) at a frequency of 490 Hz. The result was imported in MATLAB and plotted as shown below. Figure 6: Response of P controller on the motor position Page 9 of 31
  • 10. Electrical Engineering Indian Institute of Technology, Kharagpur 3.3 Actuator Module The actuator module consists of two brushed DC motors and a motor driver IC. 3.3.0.2 DC Motor Two 12V, 170 RPM gear (1:30) motors with position encoders (with specification as mentioned in the previous section) were used to drive the system. Each motor has a stall torque of 15 kg/cm @ 12 V and 7.5 kg/cm @ 6 V. The stall current required is 1.8A @ 12V and 0.8 A @ 6 V. The motors are specified to run smoothly from 4V to 12 V. Note: The above specifications are according to the datasheet provided by the vendor. 3.3.0.3 Motor Driver Motor driver board based on L298 was used. It can provide current up to 2A. It can operate with PWM frequency as high as 20 kHz. 3.4 Communication Module All the on-system communications are done through various Rx and Tx pins connected across different modules. Wireless communication is used to control the motion of the robot remotely and to track the motion of the robot in real time on our Personal Computer. For this purpose, we have chosen to use Zigbee wireless protocol. It is mainly useful for short- range low-rate wireless transfer. Its low power consumption limits transmission distances to 10 to 100 meters. ZigBee has a defined rate of 250 kbit/s, best suited for intermittent data transmissions from a sensor or input device. Using this, the real time sensor data and the robot position could be sent to our Personal computer and this data could be processed by some application like Labview or MATLAB and plot the robot s movement. For this, we need a pair of Zigbee modules. One is connected on board to the Arduino and the other is connected to our PC and they both are coupled. Through this module, we could also control the movement our robot by giving the appropriate commands through the keyboard. Thus the wireless module serves a dual purpose. 3.4.0.4 Testing Each module was connected to two different PCs and they were configured with appropriate source and destination addresses using a software called XCTU. One of the modules was connected to the Arduino communicating through the USART protocol. This module then communicates with the other Zigbee connected to the PC. Page 10 of 31
  • 11. Electrical Engineering Indian Institute of Technology, Kharagpur 4 POWER CIRCUIT DESIGN The current and power ratings of the various components of the system have been tabulated below. Components Voltage Ratings (volts) current rating (mA) Arduino Mega 5 200 IMU Razor 3.3 10 Encoder 5 20 DC Motor 12 2*1800 Motor Driver 5 110 Zigbee 5 50 Total Current Requirement 3990 Total Power Requirement 45.133 W As can be seen from the above table, the current requirements and the power requirements are around 4 Amps and 45 Watts. A Li Po battery rated at 18.5 V, 2000mAH, 20C is used as the power source for the entire system. Drawing the required current of 4 A will drain the supply in about half an hour which is acceptable for our application. We use 7812 IC (voltage regulators) to step down the 18.5 voltage to the required 12V. This reduces the efficiency of the system but increases the stability in the supply voltage. Appropriate power distribution circuit was developed with LEDs, diodes, decoupling capacitors and ON/OFF switches. Shown below is the schematic of the same. Figure 7: Power circuit schematic in Proteus All the modules connect to the central processor, in this case, the Arduino MEGA. A shield specific for this application has been developed for an easy-to-plug system. Shown below is the labelled picture of the completed shield. Page 11 of 31
  • 12. Electrical Engineering Indian Institute of Technology, Kharagpur 5 DYNAMIC MODELLING To develop a robust control system, we need to know the dynamics of the system. So, now we derive the linear model of the DC motor and the equations of motion for the robot which would help us in writing the state space equations of the system. 5.1 DC Motor Model We are using two DC motors to drive the robot. The input to these motors is the voltage applied which we are controlling in our system by varying the PWM output from the Arduino. From this model, we get the relation between voltage applied and the controlling torque required to balance the system. In the DC motor, the armature current is developed as we connect a voltage source across it. The torque produced τm is directly proportional to the this current i. τm = kmi (1) Also, as the motor rotates, a back emf Vem f is generated. This can be approximated to be directly proportional to shaft velocity ω Ve = keω (2) We can ignore motor inductance for the purpose of modelling. Now, applying Kirchoff’s law to the motor circuit, we have Va −Ri −Ve = 0 (3) The sum of all torques produced on the shaft is linearly related to the moment of inertia of the rotor Ir i.e., τm −τa = IR dω dt (4) Substituting the value of Ve from Eq 2 in Eq 3 and rearranging the terms, we get i = −Keω R + Va R (5) Substituting the value of τm from Eq 1 in Eq 4 and rearranging the terms, we get dω dt = Kmi IR + −τa IR (6) Page 12 of 31
  • 13. Electrical Engineering Indian Institute of Technology, Kharagpur Substituting the value of i from Eq 5 in Eq 6 and rearranging the terms, we get dω dt = −KmKeω IRR + KmVa IRR + −τa IR (7) The motor dynamics can be represented with a state space model, it is a system of first order differential equations with parameters position θ, and velocity ω, that represents its operation. The inputs to the motor is the applied voltage and applied torque. ˙θ ˙ω = 0 1 0 −KmKe IR R θ ω + 0 0 Km IR R −1 IR Va τa (8) y = 1 0 θ ω + 0 0 Va τa (9) 5.2 Model of the robot We are equating our robot to a inverted pendulum. The two wheels act as the cart and the robot chassis acts as the inverted pendulum. We first analyse the wheel dynamics and then analyse the body dynamics which will eventually get us the state equations that govern the motion of the system. 5.2.1 Equations for the wheels We are writing the equations of motion with respect to two wheels. The different forces acting on one of the wheels could be shown in the below figure. Since, both the wheels are analogous, we could write for one of them and get the other easily. From the Figure 8, the forces acting on the right wheel could be seen. Equating the forces in the horizontal x direction, we get Mw ¨x = Hf − HR (10) Page 13 of 31
  • 14. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 8: Free body diagram of the right wheel Sum of moments around the centre of wheels is given by Iw ¨ωw = CR − Hf r (11) We know CR is nothing but the τm which we derived in the previous section Eq 4.By substitut- ing Eq 7 in Eq 4 and rearranging the terms, we have CR = −KmKeω R + KmVa R (12) Substituting the value of CR in Eq 11, we get Iw ¨ωw = −KmKeω R + KmVa R − Hf (13) Rearranging the above equation to get Hf as the subject Hf = −KmKeω Rr + KmVa Rr − −Iw ¨ωw r (14) Substituting this value of Hf in Eq 10, we get Mw ¨x = −KmKeω Rr + KmVa Rr − −Iw ¨ωw r − HR (15) We can similarly find this equation for the left wheel which is Mw ¨x = −KmKeω Rr + KmVa Rr − −Iw ¨ωw r − HL (16) Page 14 of 31
  • 15. Electrical Engineering Indian Institute of Technology, Kharagpur Since the centre of the wheel is moving in a straight line, we can approximate the angular rotation into linear motion by simple transformations, r ¨θ = ¨x (17) r ˙θ = ˙x (18) Using this transformations and rewriting the equations 15 and 16, we get the following equations for the left and the right wheel respectively Mw ¨x = −KmKe x Rr2 + KmVa Rr − −Iw ¨x r2 − HL (19) Mw ¨x = −KmKe x Rr2 + KmVa Rr − −Iw ¨x r2 − HR (20) Adding the above two equations and rearranging the terms, we get 2 Mw + Iw r2 ¨x = −2KmKe x Rr2 + 2KmVa Rr −(HL + HR) (21) 5.2.2 Equations for the chassis Figure 9: Free body diagram of the chasis We are approximating the chassis to be the inverted pendulum in the above figure which Page 15 of 31
  • 16. Electrical Engineering Indian Institute of Technology, Kharagpur shows us all the forces acting on it By summing all the forces in the horizontal direction, we get (HL + HR) = Mp ¨x + Mpl ¨θcos(θ)− Mpl ˙θ2 sin(θ) (22) Now, summing the forces in the direction perpendicular to the pendulum, we have (HL + HR)cos(θ)+(PL +PR)sin(θ)− Mp g sin(θ)− Mpl ¨θ = Mp ¨x cos(θ) (23) Now, summing the moments around the centre of mass of pendulum, we get −(HL + HR)I cos(θ)−(PL +PR)l sin(θ)−(CL +CR) = I ¨θ (24) From Eq 12, we have (CL +CR) = 2 −KmKeω R +2 KmVa R (25) Substituting the value of (CL +CR) in Eq 24, we get −(HL + HR)l cos(θ)−(PL +PR)l sin(θ) = I ¨θ +2 −KmKeω R +2 KmVa R (26) Multiplying the Eq 23 by -l and equating its RHS with the RHS of Eq 26, we get −Mp gl sin(θ)− Mpl2 ¨θ − Mpl ¨x cos(θ) = I ¨θ +2 −KmKeω R +2 KmVa R (27) Substituting the value of (HL + HR) from Eq 22 in Eq 21 gives 2 Mw + Iw r2 ¨x = −2KmKe x Rr2 + 2KmVa Rr − Mp ¨x − Mpl ¨θcos(θ)+ Mpl ˙θ2 sin(θ) (28) The above 2 equations can be linearised by assuming θ = π + φ where φ is the small angular deviation from the vertical axis. With this approximation, we have cos(θ) = -1, sin(θ) = −φ and ¨θ = 0 Using the above approximations in the equations 27 and 28 and rearranging the terms, we have ¨φ = Mpl ¨x Ip + Mpl2 + −2KmKe ˙(x) Rr Ip + Mpl2 + −2KmVa Rr Ip + Mpl2 + Mp glφ Ip + Mpl2 (29) ¨x = 2KmVa Rr 2Mw + 2Iw r2 + Mp − 2KmKe ˙x Rr2 2Mw + 2Iw r2 + Mp + Mpl ¨φ 2Mw + 2Iw r2 + Mp (30) Page 16 of 31
  • 17. Electrical Engineering Indian Institute of Technology, Kharagpur After substituting the above two equations in each other and some manipulation of terms, we get the state space equations given by        ˙x ¨x ˙φ ¨φ        =        0 1 0 0 0 2KmKe (Mp lr−Ip −Mp l2 ) Rr2α M2 p gl2 α 0 0 0 0 1 0 2KmKe (rβ−Mp l) Rr2α M2 p glβ α 0               x ˙x φ ˙φ        +        0 2Km(Ip +Mp l2 −Mp lr) Rrα 0 2Km(Mp l−rβ) Rrα        Va (31) where α and β are given by α = Ipβ+2Mpl2 Mw + Iw r2 (32) β = 2Mw + 2Iw r2 + Mp (33) x φ = 1 0 0 0 0 0 1 0        x ˙x φ ˙φ        (34) Thus we found the matrices A, B, C for the state space model of our system based on the system parameters. We can find the transfer function for the input and output using the equation below and the resulting transfer function is shown in the next section. Y (s) = C (sI − A)−1 BU (s) (35) 5.3 Approximate values of system parameters Here, we are approximately finding the values of system parameters to get a rough idea by simulating the resulting transfer functions. 1. The mass of the wheel has been given in the buyer’s manual and the weight of the robot is approximated to 4 kg. 2. Radius of the wheel and the length of the bot have been measured with the help of a ruler Page 17 of 31
  • 18. Electrical Engineering Indian Institute of Technology, Kharagpur 3. The wheel is assumed to be a solid cylinder and its moment o inertia is thus found from its mass and radius. 4. The moment of inertia of the robot is measured by approximating it to a rod with the axis of rotation being perpendicular to it through one of the ends which gives us Ml2 3 5. The armature resistance is assumed to be 1 ohm. 6. The Km and Ke values are approximated to be 0.01 N.m/Amp and 0.01 V/rad/sec respectively Mp 4 kg Ip 0.12 kg.m2 Mw 0.123 kg Iw 0.00173 kg.m2 r 0.05 m l 0.3 m R 1 ohm Km 0.01 N.m/Amp Ke 0.01 V/rad/sec 6 CONTROL SYSTEM DESIGN In this section we describe the detailed analysis of the dynamic model of the system formu- lated in the previous section. An appropriate control system for the system is suggested by various techniques. Hardware implementation of the same has also been commented on by the end of the section. The transfer function of the pendulum’s angular position, φ, to the input voltage, Va, can be obtained from Eq 35. Solving it we get the transfer function as follows. T (s) = φ(S) V (s) = αzS +γz s3 −αp s2 −βp s +γp (36) Page 18 of 31
  • 19. Electrical Engineering Indian Institute of Technology, Kharagpur where, αz = 2Km(Mpl)−2r Mw −2Iw r − Mpr Rr(Ipβ+2Mpl2(Mw + Iw r2 )) γz = −4K 2 mKe(Ip + Mpl2 − Mplr)2 +4K 2 mKe(Mplr − Ip − Mpl2 )(Mpl −rβ) R2r3(β) αp = 2KmKe(Mplr − Ip − Mpl2 ) Rr2(Ipβ+2Mpl2(Mw + Iw r2 ) βp = Mp gl(Mp −β) β γp = 2KmKe Mp gl(Mplrβ− Ipβ− Mpl2 β+ Mpl2 −rlβ) Rr2(β)2 β = 2Mw + 2Iw r2 + Mp The open loop impulse response and step response of the system are shown in Figure 10 and Figure 11 respectively. Figure 10: Open loop impulse response Page 19 of 31
  • 20. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 11: Open loop step response 6.1 PID Control The control system problem is to find an appropriate compensator which places the closed- loop poles at desired locations to achieve required transient response. Figure 12 shows the structure of the compensated system with compensator transfer function given by C(s). Figure 12: System Structure The generic model of the PID compensator is given by Eq 37. Page 20 of 31
  • 21. Electrical Engineering Indian Institute of Technology, Kharagpur C(s) = Kd s2 +Kp s +Ki s (37) Tuning the three gains, Kd, Kp and Ki, we can place the closed-loop poles at the desired locations. Further analysis in terms of root-locus of the system can yield a detailed insight into the system behaviour. The root-locus of the plant without any compensator, plotted in MATLAB (with sample system parameters for the purpose of qualitative analysis), is shown in Figure 13. The poles of T (s) are located at 5.5651, -5.6041 and -0.1428. One zero is present at origin. The same can be seen in the root-locus plot. Figure 13: Root locus of the plant with varying gain As can be seen, one branch (coloured blue) is always on the right side of the imaginary axis. This establishes that for any value of the gain K chosen, the system can not be stabilized without a compensator. To solve this, we add a pole (integrator) at origin to cancel the zero at that location. This generates two poles branching out to the right-side of the s-plane as shown in Figure 14. From the properties of root-locus we know that we can pull the poles to the left-half plane by addition of zeroes. We observe that addition of one zero does not pull the poles enough to the left-side to meet acceptable transient behaviour. Thus we add two zeroes Page 21 of 31
  • 22. Electrical Engineering Indian Institute of Technology, Kharagpur at −3 and −4. The resulting root-locus is shown in Figure 15. Choosing the gain at appropriate closed loop pole-zero location on the root-locus will result in a fully compensated system meeting the transient response requirements. The system impulse response of pendulum angular position with gain at 20 is shown in Figure 16. The poles for the final compensated closed loop transfer function are as follows. p1 = −84.0439+0.0000i p2 = 0.0000+0.0000i p3 = −3.5235+0.7157i p4 = −3.5235−0.7157i All poles are located on the left side of the imaginary axis indicating the stability of the system. The response of the cart’s position is shown in Figure 17. Figure 14: Root locus of the plant with integrator This method allows for control of the pendulum position but provides no means for the control of the cart position, as evidenced by Figure 17. The state-space method described in the next section caters to this need. Page 22 of 31
  • 23. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 15: Root locus of the plant with PID compensator Note that in the current hardware implementation, these gains were tuned using the Ziegler-Nichols PID tuning algorithm. This is because of the unavailability of the value of various system parameters. 6.2 State Space Control The state-space control technique can be applied to Multi-Input, Multi-Output (MIMO) sys- tems. Our system, when considering cart position control, is a SIMO (Single-Input, Multiple- Output) system. The system dynamics can be represented by ˙X = AX +BU Y = C X where matrices A, B and C are given Eq 31 and Eq 34. X is the state vector with four states as cart position: x, cart velocity: ˙x, pendulum’s angular position from the vertical: φ and Page 23 of 31
  • 24. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 16: Root locus of the plant with integrator pendulum’s angular velocity: ˙φ. The outputs of the system are cart position: x and pendulum angular position from the vertical: φ The state-space control uses full state feedback. The schematic is shown in Figure 18. The state-space equations of the above system configuration is given as follows. ˙X = AX +BU = AX +B(−K X +r) = (A −BK )X +Br ˙X = (A −BK )X +Br Y = C X (38) where K = [k1k2 ···k3] The feedback gain vector K can be easily found by representing the system in Phase- Variable form. The characteristic equation of the system is given by sn + an−1sn−1 +···+ a1s + a0 = 0 The state-space representation of the open-loop system in this form is given by Eq 39. Page 24 of 31
  • 25. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 17: Root locus of the plant with integrator Figure 18: State feedback control loop structure A =             0 1 0 ... 0 0 0 0 ... 0 . . . . . . . . . . . . . . . −a0 −a1 −a2 ... −an−1             B =             0 0 . . . 1             C = c1 c2 c3 ... cn (39) Page 25 of 31
  • 26. Electrical Engineering Indian Institute of Technology, Kharagpur The closed loop form is given by Eq 40. A =             0 1 0 ... 0 0 0 0 ... 0 . . . . . . . . . . . . . . . −(a0 +k1) −(a1 +k2) −(a2 +k3) ... −(an−1 +kn)             (40) The characteristic equation of the closed loop system is given by Eq 41. sn +(an−1 +kn)sn−1 +(an−2 +kn−1)sn−2 +···+(a1 +k2)s +(a0 +k1) = 0 (41) Let the desired characteristic equation be as follows, sn +dn−1sn−1 +dn−2sn−2 +···+d1s +d0 = 0 In the above two equations, by equating the coefficients to the desired coefficients we get the values of Ki . Ki+1 = di − ai After placing the poles at the following locations, the system step response is shown in Figure 19. The input is the step input for target cart position. p1 = −8.4910+7.9283i p2 = −8.4910−7.9283i p3 = −4.7592+0.8309i p4 = −4.7592−0.8309i Page 26 of 31
  • 27. Electrical Engineering Indian Institute of Technology, Kharagpur Figure 19: Response of Cart’s position and Pendulum’s angular position under state space control 7 MATLAB CODE The MATLAB code used for obtaining the above simulation results is shown from here on for the reference of the reader. clear all; %%%%%%% System Parameter Values %%%%%%%% Mp = 4; Ip = 0.12; Mw = 0.123; Iw = 0.00173; r = 0.05; l = 0.3; R = 1; Km = 0.01; Ke = 0.01; g = 9.8; b = 2*Mw + 2*Iw/(r^2) + Mp; Page 27 of 31
  • 28. Electrical Engineering Indian Institute of Technology, Kharagpur a = (Ip*b + 2*Mp*l^2 * (Mw + Iw/(r^2))); A = [0 1 0 0; 0 2*Km*Ke*(Mp*l*r−Ip−Mp*(l^2))/R*(r^2)*a (Mp^2*g*l^2)/a 0; 0 0 0 1; 0 2*Km*Ke*(r*b − Mp*l)/R*(r^2)*a (Mp^2*g*l*b)/a 0]; B = [ 0; 2*Km*(Ip + Mp*(l^2) − Mp*l*r)/R*r*a; 0; 2*Km*(Mp*l − r*b)/R*r*a]; C = [1 0 0 0; 0 0 1 0]; D = [0; 0]; s = tf('s'); states = {'x' 'x_dot' 'phi' 'phi_dot'} inputs = {'u'}; outputs = {'x'; 'phi'}; sys_ss = ss(A, B, C, D, 'statename', states, 'inputname', inputs, 'outputname', outputs) sys_tf = tf(sys_ss) %%%%%%%% Impulse response %%%%%%% % t=0:0.01:1; % impulse(sys_tf,t); % title('Open−Loop Impulse Response') % % [zeros_1 poles_1] = zpkdata(P_pend, 'v'); % [zeros_2 poles_2] = zpkdata(P_pend, 'v'); %%%%%%%% Step response %%%%%%% % t = 0:0.05:10; % u = ones(size(t)); % [y, t] = lsim(sys_tf, u, t); % % plot(t, y) % title('Open−Loop Step Response') % axis([0 3 0 50]) Page 28 of 31
  • 29. Electrical Engineering Indian Institute of Technology, Kharagpur % legend('x', 'phi') % % step_info = lsiminfo(y, t); % cart_info = step_info(1) % pend_info = step_info(2) %%%%%%%%% PID Control %%%%%%%% % Kp = 100; % Ki = 1; % Kd = 0; % % C = pid(Kp, Ki, Kd); % T = feedback(P_pend, C); % % T2 = feedback(1,P_pend*C)*P_cart; % % t=0:0.01:10; % impulse(T,t) % title('Response of Pendulum Position to an Impulse Disturbance under PID Control: Kp = % t = 0:0.01:5; % impulse(T2, t); % title('Response of Cart Position to an Impulse Disturbance under PID Control: Kp = 100 %%%%%%% Root locus analysis %%%%%%% % rlocus(P_pend) % title('Root locus of the plant with vaying gain') % C_int = 1/s; % rlocus(C_int * P_pend) % title('Root locus of the plant with integrator') % % % zeros = zero(C_int * P_pend) % % poles = pole(C_int * P_pend) % % z = [−3 −4]; % p = 0; % k = 1; % C_pid = zpk(z, p, k); % rlocus(C_pid*P_pend) % title('Root locus of the plant with PID compensator') Page 29 of 31
  • 30. Electrical Engineering Indian Institute of Technology, Kharagpur % % % % [k, target_pole] = rlocfind(C_pid*P_pend) % % k = 35; % T = feedback(P_pend, k*C_pid); % figure(1) % impulse(T) % title('Impulse response of the plant with PID compensator') % % T3 = feedback(1,P_pend*C_pid)*P_cart; % figure(2) % impulse(T3) % title('Impulse response of the cart position with PID compensator') % axis([0 10 −200 200]) %%%%%%% State−space control %%%%%%% % poles = eig(A) P = [−8.4910 + 7.9283i −8.4910 − 7.9283i −4.7592 + 0.8309i −4.7592 − 0.8309i]; K = place(A, B, P) Ac = [(A − B*K)]; Bc = [B]; Cc = [C]; Dc = [D]; p = eig(Ac) states = {'x' 'x_dot' 'phi' 'phi_dot'}; inputs = {'r'}; outputs = {'x' 'phi'}; sys_cl = ss(Ac, Bc, Cc, Dc, 'statename', states, 'inputName', inputs, 'outputName', outp t = 0:0.01:5; r = 0.2*ones(size(t)); [y, t, x] = lsim(sys_cl, r, t); [AX, H1, H2] = plotyy(t, y(:,1), t, y(:, 2), 'plot'); set(get(AX(1), 'Ylabel'), 'String', 'Cart Position (m)'); set(get(AX(2), 'Ylabel'), 'String', 'Pendulum angle (radians)'); title('Step Response with State Space Control') Page 30 of 31
  • 31. Electrical Engineering Indian Institute of Technology, Kharagpur REFERENCES 1. The Control of an Inverted Pendulum,[manual] Available at:<https://engineering. purdue.edu/AAE/Academics/Courses/aae364L/2011/Fall_2011/lab3/manual> 2. Self-balancing two-wheeled robot,[report] Available at:<http://sebastiannilsson. com/wp-content/uploads/2013/05/Self-balancing-two-wheeled-robot-report. pdf> 3. Control Systems Engineering - Norman S. Nise 4. Exploring XBees and XCTU,[manual] Available at:<https://learn.sparkfun.com/ tutorials/exploring-xbees-and-xctu> 5. 9 DOF razor IMU calibration manual,[manual] Available at:<https://github.com/ ptrbrtz/razor-9dof-ahrs/wiki/Tutorial> 6. Arduino tuitorials on www.arduino.cc Page 31 of 31