Joan Queraltó Torras
← Back to projects

Modelling · Electronics · Control

STM32 BLE
Self-Balancing Robot

The motivation behind this project is to further develop my engineering skills through the design and construction of a self-balancing robot. Although relatively simple in concept and admittedly not particularly useful, the project brings together several engineering disciplines, offering an opportunity to explore the challenges involved in taking an idea from initial concept through to physical implementation.

I began by developing a simplified mathematical model of the robot's dynamics to estimate its mechanical requirements. The model provided a quantitative basis for component selection and helped establish the constraints for the physical and electrical design.

With these requirements established, the project extends into electronics and mechanical design, embedded firmware development using Zephyr RTOS, and the exploration of different control strategies to achieve stable balancing.

Ultimately, the goal is not simply to build a self-balancing robot, but to use the project as a platform for continued learning, technical experimentation, and refining my approach to engineering design.

Project topic

01 / The model

From a falling body to a motor-selection envelope.

The model asks a specific engineering question: what motor can move the robot toward upright without it falling, slipping, or losing ground contact? The answer informs more than the motor choice. It allows us to establish the current and voltage that the subsequent electrical design must be designed around.

01 / Wheel dynamics

Wheel analysis

Wheel free-body diagram showing the ground-frame axes, motor torque, wheel radius, and horizontal floor force at the contact patch
Wheel free-body diagram. The rotational balance represents the combined action of both wheels.

We start by analysing the forces around the wheel. Motor torque acts in the positive clockwise direction. The tire pushes the ground backwards and, provided the wheel does not slip, the floor exerts an equal and opposite forward force Fx on the tire, driving the robot forward. Re-arranging for Tmotor, Newton's second law for rotation therefore gives:

Tmotor=Fxr+Jwαw Tmotor=Fxr +Jwαw
(1)

Equation (1) shows that the torque produced by the motor is split between generating the driving force Fx and accelerating the wheels rotationally.

Provided the no-slip condition remains true, we can tie wheel rotation to axle translation through αw = ax / r. Substituting this relationship and rearranging for Fx gives:

Fx=Tmotorr-Jwr2ax Fx=Tmotorr -Jwr2ax
(2)
02 / Body dynamics

Body analysis

Body free-body diagram showing angle theta from upward vertical, gravity at the body centre of mass, the opposing pseudo-force, motor reaction torque, and horizontal axle force
Body free-body diagram in the axle-following frame. The diagram's θ is θb in the equations; the pseudo-force vanishes when the axle has zero acceleration.

Focusing now on the body, the motors apply a reaction torque equal in magnitude and opposite in direction to the torque applied to the wheels. Using the same coordinate axes with the origin at the wheel axle, gravity acting through the body's centre of mass produces a positive clockwise moment, opposing the motor reaction torque.

Meanwhile, the wheels drive the axle forward and accelerate the robot linearly. The wheel force is transmitted to the body through the axle, so it produces no direct moment about that point. At first glance, this leaves only gravity and the motor reaction torque contributing to body rotation. However, the selected frame follows the accelerating axle and is therefore non-inertial. To account for the frame's acceleration, we introduce a pseudo-force −mbax at the body's centre of mass, acting opposite to the axle acceleration. Taking moments about the axle and using the geometry shown in the diagram gives:

Tmotor=mbghsin(θb)-mbhaxcos(θb)-Jbαb Tmotor =mbghsin(θb) -mbhaxcos(θb) -Jbαb
(4)

We now have two independent equations, Equations (2) and (4), relating the applied motor torque Tmotor to three unknown responses: the driving force Fx, axle acceleration ax, and body angular acceleration αb. Two equations are not enough to determine all three, so we need a third independent relationship that connects the wheel and body motion.

03 / Whole-robot dynamics

Horizontal and vertical force balance

To do this, we consider the robot as a whole and analyse the external forces acting in the horizontal and vertical directions.

The only external horizontal force is the driving force Fx at the wheel–ground contact. This force accelerates both the wheels and the body horizontally, so the horizontal force balance can be written as:

Fx=mwax+mbab,x Fx=mwax +mbab,x
(5)

In the vertical direction, the upward ground reaction N opposes the total weight of the wheels and body. With no other external vertical forces, and noting that the wheels have no vertical acceleration, the vertical force balance can be written as:

N-(mw+mb)g=mbab,y N-(mw+mb)g =mbab,y
(6)

To connect the body's rotation to the robot's motion, we examine the kinematics of the body's centre of mass in the stationary ground frame. Its horizontal and vertical acceleration components are denoted by ab,x and ab,y.

With the axle at horizontal position x and the body centre of mass a distance h from it, the body's horizontal position is x + h sin(θb). Differentiating this position twice with respect to time gives its horizontal acceleration:

ab,x=ax+hαbcos(θb)-hωb2sin(θb) ab,x =ax +hαbcos(θb) -hωb2sin(θb)
(7)

The axle remains at a fixed height while the wheels stay in contact with the ground. The body's vertical position relative to the axle is h cos(θb), so differentiating twice gives:

ab,y=-hαbsin(θb)-hωb2cos(θb) ab,y =-hαbsin(θb) -hωb2cos(θb)
(8)

Substituting the horizontal body acceleration from Equation (7) into Equation (5) and collecting terms gives the complete horizontal force balance. This provides the third independent relationship needed to close the system, linking the driving force Fx to the axle's horizontal acceleration ax and the body's angular acceleration αb.

Fx=(mb+mw)ax+mbhαbcos(θb)-mbhωb2sin(θb) Fx=(mb+mw)ax +mbhαbcos(θb) -mbhωb2sin(θb)
(9)

Similarly, substituting the vertical body acceleration from Equation (8) into Equation (6) gives the expanded vertical force balance, which allows us to determine the normal force N needed to evaluate the earlier no-slip constraint, Constraint 1, and the upcoming ground-contact constraint, Constraint 2.

N-(mw+mb)g=-mbhαbsin(θb)-mbhωb2cos(θb) N-(mw+mb)g =-mbhαbsin(θb) -mbhωb2cos(θb)
(10)
04 / Coupled dynamics

The coupled model

We can now combine the wheel torque balance in Equation (2), the body pitch balance in Equation (4), and the horizontal force balance in Equation (9). These provide three equations for Fx, ax, and αb. Eliminating the auxiliary contact force leaves two coupled equations for the two accelerations:

Tmotorr+mbhωb2sin(θb)=(mb+mw+Jwr2)ax+mbhcos(θb)αb Tmotorr +mbhωb2sin(θb) =(mb+mw+Jwr2)ax +mbhcos(θb)αb
(13)
Tmotor-mbghsin(θb)=-mbhcos(θb)ax-Jbαb Tmotor -mbghsin(θb) =-mbhcos(θb)ax -Jbαb
(14)

For a given motor torque and initial conditions for the body state variables θb and ωb, the equations above allow us to calculate the accelerations ax and αb. We then use αb to update the body's angular velocity and angle over a small time step. The updated state becomes the starting point for the next calculation, allowing us to iteratively simulate how the body's motion and the forces in the system evolve over time.

From equations to a capability map

Simulation and capability sweep

The coupled model, together with the motor torque–speed relationship, gives us a system of equations with which we can simulate the robot's behaviour across a range of stall torques and no-load speeds. These parameters are commonly listed for commercially available motors, making them useful for comparing potential candidates. For each wheel diameter, we can create a two-dimensional capability map and overlay real motors using their published specifications to assess their suitability for the robot.

To run the simulation, we first need to define the initial conditions and make provisional estimates of the robot's mass properties and geometry. The parameters used are:

Body mass
mb = 0.200 kg
Combined wheel mass
mw = 0.050 kg for both wheels
Total modelled mass
0.250 kg (250 g)
Axle-to-body-COM distance
h = 35.5 mm
Body pitch inertia
Jb = mbh2, using a point-mass estimate
Wheel inertia
Jw = ½mwr2, recalculated for each radius
Ground parameters
μ = 0.8; g = 9.81 m/s2
Initial state
90° from upright; zero initial linear and angular velocity; axle position starts at zero
Wheel diameters
90, 100, and 110 mm
Stall torque per motor
0.5–3.0 kgf·cm, in increments of 0.5 kgf·cm
No-load speed
150–400 rpm, in increments of 50 rpm
Integration
Semi-implicit Euler with a fixed 0.05 ms time step
Model assumptions and scope

The model assumes planar motion, equal motor torque, identical wheels, level rigid ground, rolling contact, and a rigid chassis-fixed assembly. Mass properties and friction are provisional. Internal motor and gearbox inertia, drivetrain loss, backlash, tire compliance, electrical motor dynamics, current limits, and thermal effects are omitted. A run also stops if gearbox-output speed reverses, outside the present motor model's operating range.

The 90° release assumes that the body can rotate freely about the axle. An upright crossing measures open-loop mechanical capability. Closed-loop balancing and physical self-righting from a floor-contact configuration require separate analysis and validation. The motor markers are coarse datasheet cross-references, not product-specific simulations.

With these parameters, we can simulate the robot's motion for each motor and wheel combination. A combination is considered successful only if the body reaches upright while satisfying our specified constraints throughout the manoeuvre. In particular, the wheels must remain within the available friction limit, as defined by Constraint 1, and maintain contact with the ground, as required by Constraint 2. Checking these conditions allows us to identify combinations with sufficient mechanical capability under the assumptions of the model.

Running all 108 combinations produces the following capability maps, one for each wheel diameter. Successful combinations are coloured by the time taken to reach upright: green indicates quicker crossings, through yellow to red for slower crossings. Grey cells identify unsuccessful combinations. The published specifications of real motors are overlaid to help identify suitable candidates.

Motor-wheel capability sweep for the self-balancing robot
Capability sweep, 108 simulated combinations. Each panel uses one wheel diameter. Coloured cells cross upright within the model constraints: green is quickest, through yellow to red for the slowest crossings. Grey cells show a fall or traction failure. Blue diamonds mark HP/HPCB candidates and purple circles mark MP candidates; the overlays do not change the generic simulation.

Choice rationale

Motor and wheel selection

Based on the robot's estimated size, the sweep covered wheel diameters of 90, 100, and 110 mm. The maps showed good results with 90 mm wheels and, with the motor manufacturer offering readily available wheel options around this size, the #1435 90 × 10 mm wheel pair was selected. These wheels mount directly onto the output shafts of all the motor candidates, simplifying the mechanical design and assembly for any of the motor choices and avoiding the need for extra hubs or adapters.

Among the motor candidates, both #5165 and #5137 mapped to successful crossings across all three wheel diameters. The #5165 was selected because its nearest map point sat further from the simulated fall region for the selected 90 mm wheel diameter.