Abstract
This paper presents balancing, velocity and motion control of a self-balancing vehicle. A cascade controller is implemented for both balancing control and angular velocity control. This controller is tested in simulations using a proposed mathematical model of the system. Motion control is achieved based on the kinematics of the robot. Control hardware is designed and integrated to implement the proposed controllers. Pitch is kept under 1° from the equilibrium position with no external disturbances. The linear cascade control is able to handle slight changes in the system dynamics, such as in the centre of mass and the slope on an inclined surface.
1. Introduction
Over the past years, self-balancing systems have attracted increased attention, since engineers and researchers are now able to test control algorithms on the inherent non-linear behaviour of such systems. Previous studies deploying basic Proportional Integral Derivative (PID) controllers have been tested [1–3]. However, the addition of motion control of a supporting moving platform requires a more sophisticated control scheme, such as a cascade PID.
The application of a cascade controller strategy simplifies the controller design for self-balancing systems. One of various advantages is that one controller can correct the disturbances in the secondary process before they affect the primary variable [4]. Nevertheless, one disadvantage is that there is an additional controller that has to be tuned.
Golem Krang, the humanoid self-balancing robot, uses a cascade PID for vertical and motion control, reporting an error of less than
A comparison between two methods is presented by [8]. The first method is a cascade of a PI controller and a mathematical model of the robot. The second method is a cascade of two PID controllers. The first method proves to be more responsive to user commands and easier to tune. On the other hand, type-2 fuzzy cascade controls have been proven to be more effective in the presence of uncertainties [9].
Other applications of cascade control include autonomous vehicles [10], study and control of the payload variation of Wheeled Inverted Pendulum (WIP) systems [11, 12], and path tracking of differential mobile robots.
Cascade control has been implemented with fuzzy controllers in Ball and Beam Systems [13] with computer simulation, as well as in real-world experiments. An optimized type-2 fuzzy cascade controller is applied based on Particle Swarm Optimization.
Another cascade controller design strategy is presented in [14]. This study shows the application of the cascade control architecture to solve the path-tracking problem of mobile robots that inherits large amounts of uncertainties.
2. Robot Design
Fig. 1 shows the robot used in the experimental tests. The main components of the robot are:
BeagleBone Black, a low-cost computer with an ARM processor
Inertial Measurement Unit ADIS16365
Two 12 V brushed DC motors with a 50:1 metal gearbox and incremental encoders
Two MOSFET H-bridge motor drivers
12 V battery.
In order to measure the pitch angle, an inertial sensor ADIS16365 unit (accelerometer and gyroscope) was used. In order to measure the robot position, odometry data were obtained from incremental encoders attached to the DC motors of the wheels.

Self-balancing vehicle used in the experiments
3. System Modelling
3.1. Motor
The electric circuit and free-body diagram for a Direct Current (DC) motor is shown in Fig. 2. In a DC motor, the torque is proportional to the armature current:
where Kt is a constant factor and i is the armature current. The back electromotive force (BEMF) e is proportional to the angular velocity of the rotor:

Motor electrical circuit
Assuming there are no electromagnetic losses, mechanical power is equivalent to the electrical power dissipated by the BEMF:
Since Kt equals Ke, K will be used to represent both constants. From Newton's second law and Kirchhoff's voltage law, the following equations are derived:
where J is the moment of inertia of the rotor, τa is the torque applied to the shaft of the motor, R is the electrical resistance of the motor, and V is the voltage applied.
Assuming that the electrical time constant
3.2. Wheels
Assuming there is acceptable friction between wheels and the contact surface, a free body diagram of the forces and torques applied to the wheels can be derived (see Fig. 3). Since a gearbox with a gear ratio of 50:1 is attached between the motor shaft and the wheel, the torque applied to the wheels is assumed to be 50 times the output torque from the motor:

Free body diagram of the wheel
From Newton's second law, two new equations are obtained:
where H is the force applied to the wheel by the chassis, and Hf is the friction force applied to the wheel by the surface contact. IW is the moment of inertia of the wheel, and r is the wheel radius.
3.3. Inverted pendulum
The robot chassis is modelled as an inverted pendulum, whose base is attached to the wheels. A two-dimensional free body diagram of the robot chassis is depicted in Fig. 4, where mC is the chassis mass and l is the length from the base of the pendulum to its centre of mass.

Inverted pendulum free body diagram
Adding forces in the horizontal direction, the following equation is obtained:
The moments about the centre of mass in the pendulum are summarised as follows:
It is possible to linearize both (11) and (12) by making the following approximations:
Substituting these approximations into (11) and (12), the following equations are obtained:

Cascade control for the self-balancing vehicle
3.4. State space model
In order to test the controllers proposed in section 5, a state space model of the self-balancing vehicle was used to simulate the response of the system. Since the equations are linear, they can be written in the standard matrix form, as follows:
where the pitch angle φ and the position x are the outputs of the system, and the voltage applied to the motors V is the input u.
Element values for matrices A and B were obtained by substituting i from (8) into (6), and then substituting τa from (6) into (10). Then, Hf from (10) was substituted into (9), and, finally, force H from (9) into (13).
The resulting matrices for the state space model are:
where W1 and W2 become:
Estimated values attributed to the robot used in experimental tests were obtained by measuring the weights and dimensions of the chassis, wheels, and motors of the robot. The obtained values are:
4. Cascade Control
A cascade control is designed to increase the control performance when the system can be considered as two processes in series. The increase in performance is a consequence of the fact that the disturbances in the secondary process are not measured until they have affected the output of the primary process [16]. In a self-balancing vehicle, the linear velocity is strongly affected by the pitch angle. Therefore, the linear velocity could be considered as the primary process and the pitch angle as the secondary process. In Fig. 5, the cascade control of the self-balancing vehicle is illustrated.
Odometry was used to locate the self-balancing vehicle in a Cartesian plane based on the angular velocity of the wheels. The required variables to obtain the linear and angular velocities are shown in Fig. 6. l is the length of the vehicle, as measured from the end of one wheel to the end of the other. v and ω are the linear and angular velocities of the vehicle, respectively.

Motion equation variables
Assuming there is no slip between the wheels and the ground, the values for v and ω can be obtained from the angular velocities of the wheels (
5. Control Design
The vehicle has to balance on its own. In other words, the vehicle has to keep from falling while moving on the ground. In order for the controller to keep balance, the pitch angle (the angle between the chassis and the vertical axis) needs to be reduced to zero. In order to maintain linear velocity control, the pitch controller must be able to change the set point to a value close to zero to keep balance, a positive value to move forward, and a negative value to move backward.
The controllers proposed for both the pitch angle control and the linear velocity control are PID. An extra controller was implemented for the angular velocity. Since the required action to change the orientation of the self-balancing vehicle has no impact on the actions of the main controllers (pitch angle and linear velocity), this extra controller can be added without introducing a significant disturbance.
The addition of this controller achieves the final configuration of the controllers, as shown in Fig. 7, where vr is the linear velocity reference and ωr is the angular velocity reference. The outputs of the self-balancing vehicle are the pitch angle φ, angular velocity of the left wheel

Diagram of the proposed controllers
6. Simulation
In order to adjust the gains of the pitch angle and linear velocity controllers, a simulation of the system was executed in Matlab©. To accomplish this task, the model obtained in section 3.4 was used. Additionally, the behaviour of the controller was considered. The variables were sampled every 10 ms.
Fig. 8 shows the response of the pitch angle and the position using only a pitch angle controller. There is no cascade control in this simulation. The initial conditions for the state variables are [x ẋ φ φ̇]T = [0 0 1.15° 0]T. It can be seen that the pitch angle reached 0° after 0.5 seconds. Nevertheless, the position drifts from the zero reference. This behaviour was expected, since there is no control over the position or the linear velocity.

Pitch angle (left) and position (right) response in simulation with only pitch angle control
Fig. 9 shows the response of the vehicle when the cascade control system is implemented in a second simulation. In this simulation, the linear velocity reference vr was set to 0. The simulation shows that the pitch angle can be controlled as close as possible to

Pitch angle (left) and position (right) response in simulation with pitch angle and velocity control
In Fig. 10, the response from another simulation is shown. Again, cascade control was implemented, but the linear velocity reference vr was set to

Pitch angle (top left), position (top right), linear velocity (bottom left) and input motor voltage (bottom right) response with pitch angle and velocity control
7. Motion Control
In mobile robotics, it is crucial to understand the behaviour of a given mechanical configuration in order to create control software for a particular piece of hardware [18].
Once the control is able to balance the mobile robot by controlling both the linear velocity and the angular velocity, it is possible to move the robot on a Cartesian plane. The kinematics of the self-balancing vehicle and those of a differential-drive robot are very similar.
7.1. Kinematics
The motion equations for the self-balancing vehicle are as follows:
where v is the linear velocity of the vehicle and ω is the angular velocity (both parameters were already obtained in 18). Variables
In order to integrate equation 19, a modified Euler method was used [19]. This method is given as follows:
From (20), a discrete equation is obtained in order to determine the approximate position of the vehicle:
where h is the sampling interval of the variables’ acquisition using the information from the encoders located in the wheels. Considering the modified Euler method, vector velocity
As stated in section 6, interval h is equal to
7.2. Control design
The purpose of this control is to drive the robot to a desired position. This desired position is called reference position or reference posture (see Fig. 11). In order to obtain an error between the actual position and the reference position, the following equation is derived:

Motion equation variables
Error ex can be derived from a vector projection of unit vector u on a vector from the actual position towards the reference position. In a similar way, ey is obtained from a vector projection of unit vector u on a vector from an orthogonal vector to a vector from the actual position towards the reference position (see Fig. 11).
The controller can be derived from observing error vectors in Fig. 11. The objective of this controller is to reduce the error vector
where
8. Experiments and Results
In order to test the performance of the different controllers proposed in this work, several experiments were carried out. The main variables that define the state of the vehicle were plotted to show the behaviour of the controller.
Four experiments are described in the next subsections. In the first experiment, pitch and velocity control was implemented; at a given time, some disturbances were applied to the system. In the second experiment, pitch, velocity, and motion control was implemented, and a change in the centre of mass of the robot was introduced. In the third experiment, the robot was placed on an inclined surface while implementing pitch, velocity, and motion control. In the last experiment, the robot was placed in a flat surface with no inclination; the robot position set-point given to the motion control was changed at three given times.
8.1. Pitch angle and linear velocity control
In a first experiment, the self-balancing vehicle is controlled only by the cascade control system, using the controller described in Fig. 7. In other words, a controller is present for the linear velocity and for the angular velocity only. The set-points for each controlled variable were set to
Pitch angle and position data were recorded for 20 seconds. Then, the collected data were plotted in Figures 12 and 13. Since angular velocity is not strongly affected by disturbances in the system, data related to this variable were not plotted.
In this experiment, the two disturbances D1 and D2 were introduced to the vehicle at
When the vehicle reaches a stable state, the pitch angle varies from
From the plotted data, when the vehicle reaches a stable state, the position varies from

Pitch angle response with only pitch angle and linear velocity control

Position response with only pitch angle and linear velocity control (no control on position)
8.2. Position control
A second experiment was carried out implementing position control defined by the control law from (26). Displacement along x is considered. In this experiment, xr was set to
Fig. 14 shows how the pitch angle of equilibrium changes when the centre of mass changes. The cascade control system compensates the change of the position of the centre of mass by changing the pitch angle of equilibrium. Before E1 occurs, the mean pitch angle of equilibrium is approximately
Fig. 15 shows how the position of the vehicle is affected by the changes in the centre of mass. Once the centre of mass is changed, the position drifts away from the reference. Then, after the angle of equilibrium is reached, the error between the vehicle position and the reference is reduced. Approximately five seconds after each event, the position has reached a steady state.

Pitch angle response with position control

Position response with position control
8.3. Position control on an inclined surface
Another experiment was carried out implementing position control with the vehicle positioned on an inclined surface. In a similar way as in the previous experiment, xr was set to 0cm. The vehicle successfully balanced itself while keeping its position. Data were recorded for 10 seconds in order to obtain the plots presented in this section.
The experiment was divided into three parts:
A. On a surface with a −3° inclination.
B. On a surface with a 10° inclination.
C. On a surface with a 20° inclination.
Fig. 16 shows the plot of a pitch angle versus the time. The data collected from the experiment revealed that the pitch angle of equilibrium augments as the slope increases. During the first part of the experiment, the pitch angle of equilibrium is approximately
Fig. 17 shows the plot of the voltage applied to the motors. The mean voltage for each part of the experiment was recorded. The mean value of the voltage during the first part of the experiment is
Fig. 18 shows the plot of the position of the vehicle. The obtained data show that the amplitude of the position increases when the slope of the surface is increased. During the first part, the position varies from

Pitch angle on an inclined surface

Action response on an inclined surface

Position response on an inclined surface
8.4. Motion control
The motion control law proposed in section 7.2 was tested with a different experiment. Three reference positions, also called target positions, were set while recording the response of the vehicle to achieve these targets on a flat surface with no inclination. The following targets were defined:
T1. At time
T2. At time
T3. At time
Before T1, the target position is already set to
Fig. 19 shows the path that the vehicle follows to reach the desired targets. When the vehicle goes to T1, it moves in the forward direction. The vehicle goes forwards again to reach T2. Finally, when trying to reach T3, the vehicle moves backwards. This behaviour occurs because of the initial orientation of the vehicle, which tries to take the shortest path; notice that the vehicle can always move in two directions (forwards or backwards). However, it will only move in the direction that is best oriented towards the target position.
In Fig.20, the actual position variables
Fig. 21 shows the variations in the pitch angle when the vehicle tries to balance while going from one target to another. Finally, in Fig. 22, the voltage applied to the motors is plotted. It can be seen that, most of the time, the voltage is kept in a range not greater than

Position response with motion control

Position response with motion control

Pitch angle response with motion control

Input voltage applied to motors with motion control
9. Conclusions
The cascade controller proposed in this work is capable of successfully keeping the self-balancing vehicle from falling. The pitch angle can be kept under
The addition of control for the linear velocity using motor encoders leads to a more stable system compared to one that does not implement these sensors.
Despite the fact that the proposed controller is based on a linear model, it can handle slight changes in the dynamics of the system. Two of the changes tested here have been the change of the centre of mass and the change in the slope of an inclined surface.
The motion control implemented in this work was able to drive the vehicle with an error of approximately
