Mechatronics Exercises

Workspace Navigation


Group 15: Pyry Weckman

The goal is to build a self balancing cube that can stand on its corner using three reaction wheels controlled by a gyroscope and a PID control loop. Planned size is 210x210x210mm with 180mm reaction wheels. The wheels are driven by Nidec 24H BLDC motors with built in encoders, powered by a 14.4V Li-Ion battery pack (4S1P 18650 cells) or an external 12-32V DC voltage source.

Current situation

The cube is completed in terms of mechanical and electrical design. However the motors are causing a lot of headache and I'm not confident if the special models I received are even suitable for this application (see the "Motors and challenges" section). The cube tries very hard to balance, but the motors are simply not acting as they should.



The baisc concept is to accelerate (and deaccelerate) a weighted disk (the flywheel) to balance the device in the target angle. Change in angular momentum of the flywheel causes torque around its axis of rotation, which "twists" the device in the wanted direction. By causing the right amount of torque at the right time, the cube can therefore be balanced on its edge. By adding one of such flywheels on each axis (x, y, z), we can control every rotational axis of the cube, and therefore the cube can be balanced on its vertex. The required torque is determined by having an accelerometer and a gyroscope fixed inside the cube, and using the gyroscope output angle as the input for a PID control loop, that spins the motors. 

There are a few examples of such devices being made in the past, most notably "the Cubli", made around 10 years ago as a research project in Zürich's research university ETH Zürich. The Cubli served as the original inspiration for the project. 

List of parts used

PartPiecesPrice áLink
Nidec 24H BLDC motor36.08€
MPU6050 gyroscope module13.97€
ESP32 module13.45€
5V3A step-down converter11.42€
12V3A step-down converter11.84€
6mm to 4mm shaft coupling30.40€
4mm x 100mm shaft20.98€
4mm shaft flange31.10€
4x12x4mm bearing30.42€
M3x10 machine screw89--
M3 nuts89--
M8 bolts27--
M8 nuts27--
Total price
36.64€+22.51€ shipping = 59.15€

Mechanical design

The general idea with the mechanical design was to make the device as easy and straight forwards to assemble and disassemble. Only one size of screws and nuts are used, and the design is modular, so that each face of the cube can be removed individually. Originally I was planning on using threaded inserts, but through some testing found out that printed countersinks for M3 nuts worked very well and were even easier to design/assemble than threaded inserts.

Overall size of the cube is 210x210x210mm, which was chosen based on the maximum size possible with my own 3D printer. Initially I planned on 180mm flywheels, but after deciding on using M8 bolts for weights I had to shrink the flywheels to 160mm to avoid the bolts hitting each other. With proper machined flywheels it would be possible to use 180mm flywheels, which would decrease the required weight of the flywheel as the larger diameter causes larger inertia.

I designed the cube to be as 3D-printable as possible, and all mechanical parts that are not listed in the table above were designed in Autodesk Inventor and then 3D printed on my Anycubic Mega S. All parts were printed using PLA at 0.25mm layer height. The parts were designed so that none of them require any supports or modification after printing. The 3D printed parts consist of:

Part nameAmount neededPurposeRemarks
210mm face top3Face of the cube, for top halfPrint with outside face against build plate. Could also be cut from plexiglass etc
210mm face bottom3Face of the cube, for bottom halfPrint with outside face against build plate. Could also be cut from plexiglass etc
Corner brace8Holds faces together at corners3-way symmetrical, print however you want
Midframe1Holds the electronicsPrint "upside down"
Flywheel3FlywheelCould also be milled from metal if you calclulate the right mass
Face-motor arm12Attaches the motor bracket to the face of the cubePrint laying on its side to avoid supports.
Motor bracket3Attaches motor to the armsPrints either way

Finished CAD design.

Flywheels were designed with 24 holes each. 24 was chosen due to it having a proportionally very large number of divisors. This allows adding weights in multiples two, three, four, six, eight, or twelve, good adjustability of the inertia.

The three sides with motors on them were built to be only connected from the corners, so for example replacing a motor only requires taking apart one "module". The arms connecting the motor to the face were made curved and printed with low infill to give them some springyness. The idea was that by isolating the motors from the rest of the cube, it should help reduce vibration from the motors making its way to the gyroscope module.

The motors shipped with 6mm shafts, that were connected to 4mm shafts using flexible shaft couplers. The flywheels then screwed onto the shaft with a 4mm shaft flange coupler, and finally the other end of the shaft was supported by a 4mm bearing press fit onto the face of the cube. This mounting solution proved to work very well. The flexible coupler allows some throw in terms of how centered the motor is to the face of the cube, and also helps absorb some of the vibration. Overall the flywheels turned out to be surprisingly stable, and the flywheels did not need any special balancing outside of distributing the bolts evenly. If balancing had been necessary, it could've been done by adding washers to the bolts.

Corner brackets used for attaching the faces to each other.

All of the electronics were mounted on the "top half" of the cube, on a triangular mounting piece that I designed to be full of holes for flexible mounting options. The electrical portion was designed to be fully modular too. The active components (5V voltage regulator, 12V voltage regulator, ESP32, and gyroscope) are all mounted in sockets and can be replaced without any soldering. The voltage regulation PCB (left) is likewise connected to the logic PCB (right) with an unpluggable connector.

Electrical design

Motors and challenges

The most important part of the device are the three brushless DC motors driving the flywheels. For this purpose three Nidec 24H motors were chosen. This model was chosen due to it being popular with other similar balancing devices, resulting in a good amount of documentation and positive experiences. The Nidec 24H motors have built in drive circuitry and encoders, so they only require 12V power, speed, direction and enable, and brake signals, making them theoretically very easy to drive compared to other BLDC motors which require a separate ESC and encoder for this purpose.

However unfortunately the 24H model seems to only be available from third party sellers on eBay and Aliexpress, as Nidec only sells them directly for industrial use. As a result of this, the motors I received appear to be some special version of the motor, with a significantly different pinout and drive logic. The "standard" version has PWM speed control with eight pins, good documentation, and 12-24V voltage tolerance. The version I got has 100-25000Hz PFW (Pulse Frequency Modulation) speed control with twelve pins, zero documentation, and unknown voltage tolerance. This caused significant issues with getting the motors to work, and consumed a significant amount of my time to resolve. Eventually I did get the speed control working, but I feel like the motors still behave a bit "odd", and it appears the motors might have their own built in PID control fighting against my own PID system, this causes significant issues with trying to get the motors running smoothly. If you are planning on making a device using Nidec 24H motors like many others have, for your own sanity please make sure you get the "standard" 8 pin PWM version. Do not buy the 12 pin version.

Dc 12-24v Japan Nidec 24h Brushless Servo Motor Built-in Drive 100 Line  Encoder Pwm Speed Regulation - Dc Motor - AliExpress

Left: Standard 8 pin PWM model of the 24H. Buy this.                                  Right: Non-standard 12 pin PFM (even though it says PWM) vesion. Do not buy this. You will not find any more documentation for it than what is written in that image. 

Interestingly it appears that some sellers on Aliexpress are selling just the PWM motor control boards individually for very cheap (about 2€ each), so I suppose I could try swapping the PCBs of my motors to avoid having to spend money on entirely new motors. However I only discovered this once it was already too late to order them. 

Rest of the sytem

The cube is driven by an ESP32 microcontroller. The ESP32 was chosen due to the amount of individually variable frequency PWM output channels (required due to the motors being PFM controlled), which an ESP8266 or any Arduino does not have enough of. The ESP32 communicates with the MPU6050 gyroscope via I2C, and additionally the ESP32 can be connected to via Bluetooth for adjusting the balancing parameters on the fly without having to touch the device. The motors are driven by 12V, while everything else functions at 5V, so two voltage regulators were used. The voltage regulators can handle up to 32V input, so the cube can be powered by for example 4S Li-Ion batteries via the DC in connector (white in the picture above and below). Even with the max speed of the motors limited in software, they still pull over 20W each when accelerating, so use an appropriate power supply.

Each motor is connected to the logic PCB with two connectors, one for Speed/Direction/Enable/Brake (red connectors on the left), and one for 12V/Ground (black connectors on the right). Between the connectors is the gyroscope module, which is placed so that the gyroscope chip is right in the middle of the main (tip to tip) axis of the cube.

Software design

While the initial goal was to write the software essentially from scratch as a learning experience, due to time constraints I had to resort to heavily basing the code on an existing similar open-source project, so major credit to the author of that software. His project used PWM controlled motors, so I had to modify the program to work with frequency control instead of pulse width control. I also added some features to make tuning the PID-parameters and setting the balancing points over Bluetooth easier, and did some refactoring and a lot of commenting of the code to make myself understand it better, as it was very lacking in documentation.

Current software capabilities include:

  • Retain all calibration values in EEPROM between power cycles
  • Automatic calibration of the gyroscope on startup (make sure the cube is stationary and on a flat surface when you power it on)
  • Guided calibration routine via Bluetooth
    • Type "cs" for "calibration start
    • Raise the cube on its edge/vertex at the angle where it's balanced
    • Type "cn" for "calibrate next"
    • Program saves the target angle
    • Repeat for other edges/vertex
  • Cube will automatically detect when it's near a balancing point and automatically start balancing
  • Cube will automatically detect when it's about to fall over and stops the motors
  • The PID loop can be tuned live on the fly over Bluetooth
    • "p+", "p-", "i+", "i-", "d+", "d-" to increase/decrease the P, I, and D values incrementally
    • "p 100" to set P to 100, "i 50" to set I to 50, and so on

Code can be found on my Github, at PyryW/Gyrocube

3D files (Autodesk Inventor) are here:

210mm face top.ipt210mm face.iptAssembly1.iamCorner brace.iptFace-Motor arm.iptMidframe.iptMotor Bracket.iptNidec 25H.iptTest Assy.iamWheel assy.iam170mm reaction wheel.ipt

  • No labels
  File Modified
File 170mm reaction wheel.ipt Apr 27, 2023 by Pyry Weckman
File 210mm face.ipt Apr 27, 2023 by Pyry Weckman
File 210mm face top.ipt Apr 27, 2023 by Pyry Weckman
File Assembly1.iam Apr 27, 2023 by Pyry Weckman
File Corner brace.ipt Apr 27, 2023 by Pyry Weckman
File Face-Motor arm.ipt Apr 27, 2023 by Pyry Weckman
PNG File image2023-4-25_2-14-37.png Apr 24, 2023 by Pyry Weckman
PNG File image2023-4-25_2-14-43.png Apr 24, 2023 by Pyry Weckman
PNG File image2023-4-25_2-44-31.png Apr 24, 2023 by Pyry Weckman
PNG File image2023-4-25_2-44-35.png Apr 24, 2023 by Pyry Weckman
PNG File image2023-4-25_2-45-0.png Apr 24, 2023 by Pyry Weckman
PNG File image2023-4-25_2-4-7.png Apr 24, 2023 by Pyry Weckman
PNG File image2023-4-25_2-5-0.png Apr 24, 2023 by Pyry Weckman
PNG File image2023-4-25_2-5-23.png Apr 24, 2023 by Pyry Weckman
File Midframe.ipt Apr 27, 2023 by Pyry Weckman
File Motor Bracket.ipt Apr 27, 2023 by Pyry Weckman
File Nidec 25H.ipt Apr 27, 2023 by Pyry Weckman
Multimedia File PXL_20230425_145712812.TS- Apr 25, 2023 by Pyry Weckman
Multimedia File PXL_20230425_145832196.TS.mp4 Apr 25, 2023 by Pyry Weckman
PNG File Screenshot_20230215_042657.png Feb 15, 2023 by Pyry Weckman
File Test Assy.iam Apr 27, 2023 by Pyry Weckman
File Wheel assy.iam Apr 27, 2023 by Pyry Weckman