r/arduino • • Sep 01 '26

Machine Learning I want help building this project.

This is going to be my biggest project yet. I’ve done quite a bit of research, but I’m currently stuck and honestly don’t know how to proceed from here.

I’m also looking for someone who’s willing to work on this project with me. The idea is that we both build the same thing independently at home, share our progress and insights along the way, and help each other figure things out. If we’re successful, we could even take it a step further and try building a double pendulum balancing system.

Has anyone here worked on or built something similar before? I’d love to hear about your experience, resources, or projects.

Also, what tools, components, software, or other equipment would you recommend for building something like this?

884 Upvotes

42 comments sorted by

196

u/billgytes Sep 01 '26

I recommend reading this to understand the theory here. This is a classic control theory problem. You will need more than PID.

https://www.control.utoronto.ca/~broucke/ece1653s/Intro/ast_fur96.pdf

Implement a simulator first. As a bonus you can also use the exact C same code in your simulation if you factor out your control loop as a library and abstract the hardware a bit. Compile simulator code as x86 and then you can share it between your simulator and the real system, helps a lot with running experiments.

8

u/No_Alarm123 Sep 02 '26

Thank you very much

93

u/agate_ Sep 01 '26

The point of these projects isn't the machinery, it's the math. It is easy enough to build a machine like this, throw in someone else's code, and see it work, but the real value comes from working through the calculus to develop the control code yourself.

9

u/jaknil Sep 01 '26

Just curious: Would it actually work with ready made code? Or would it need modelling, extensive testing and tuning to work. Or is it a matter of having fast/strong enough motors and some friction adding mechanical dampening?

23

u/WantedBeen Sep 01 '26

it would need to be tuned unless you use identical materials

10

u/anally_ExpressUrself Sep 02 '26

It might depend on the robustness of the code too.

2

u/Much-Fudge-7374 Sep 02 '26

Even then. Friction is different for different stages. Controlled motion is easy, low following error and a stable but responsive system is hard.

3

u/D0hB0yz Sep 02 '26

They are self tuning or else they are nightmares. The adjustment when he taps the stick seems to indicate self tuning.

1

u/Much-Fudge-7374 Sep 02 '26

Friction is the enemy, especially static friction. The dampening comes from KD and filtering (low pass and notch filtering)

1

u/2dof Sep 04 '26

In control inverted pendulum you need I.plement couple of strategies - first is to start-up - rotatare up and then switch to  balancing control. IN mathworks (matlab) web as example is described quite universal and robust strategy ) with simulink example. 

2

u/Flater420 Sep 02 '26

Just because the maths is harder than the engineering does not mean that someone cannot enjoy building the machine.

1

u/Much-Fudge-7374 Sep 02 '26

PID tuning in practical world has little to do with math. Unless they are going to design the control loop from the ground up. In practice, You are looking at open loop response for gain crossover and phase cross over to determine your stability margins and qualify the mechanics, commanding a profile and tuning the gains around that.

28

u/ripred3 My other dev board is a Porsche Sep 01 '26 edited Sep 01 '26

The hardware is a good quality encoder mounted on a moveable screw-driven carriage. The screw is turned using a stepper motor connected to a stepper motor driver. The weight hangs freely off of the encoder shaft and all feedback is from that encoder. I have a couple of industrial encoders I picked up at a sale that are 4096 pulses-per-revolution and they include both digital gray-code outputs and two sine wave outputs that are 90 degrees out of phase. At resolutions like that (360° / 4096 = 0.08789° per pulse) you can treat the position of the encoder shaft - the upright set point, just like the accelerometer error on ground-based balancing platforms in your primary or secondary PID loop.

5

u/AarontheTinker Sep 02 '26

I came to ask if this could apply a PID loop! Your industrial encoders sound fun too. What are your current plans with them?

2

u/ripred3 My other dev board is a Porsche Sep 02 '26 edited Sep 03 '26

I came to ask if this could apply a PID loop!

yes, multiple PID's most likely

Your industrial encoders sound fun too. What are your current plans with them?

I have no idea heheh. I have a terrible habit of keeping "special" parts reserved for some project that makes the best use of them and taking years before something finally clicks 😉

2

u/nudelsalat3000 Sep 02 '26

When it's up you use the PID.

How do you get it up? Predefined loop or a formula for a amplifier?

2

u/ripred3 My other dev board is a Porsche Sep 03 '26 edited Sep 03 '26

The encoder’s digital A/B quadrature outputs can be decoded by interrupts or, preferably where available, a separate hardware quadrature counter. This maintains a signed encoder-count value from which the software obtains pendulum angle. Sampling that value at a fixed interval also provides an estimate of angular velocity. Integer counts are the natural representation for the raw encoder position; the controller can use integer, fixed-point, or floating-point calculations as appropriate for the processor.

After constructing the platform you would measure important limits such as: carriage travel, maximum usable acceleration and speed, backlash, motor torque, and the acceleration at which a stepper begins missing steps. The carriage also needs a known position, normally established using homing and limit switches.

Swing-up is usually handled by a function (controller) separate from the upright-balancing controller. Rather than relying only on a predetermined timing loop an energy-based swing-up controller uses the pendulum’s angle and angular velocity to determine whether energy should be added or removed. It commands carriage acceleration at the appropriate phase of each swing while also respecting the carriage’s position and travel limits.

The system switches to its upright controller only when the pendulum’s angle and angular velocity are inside a region that the balancing controller can recover from and when the carriage has enough available travel. If that capture attempt fails, it returns to swing-up rather than continuing blindly.

The upright controller then runs at a fixed sample rate and commands the carriage to accelerate underneath the pendulum’s center of mass. It should account for both the pendulum state and the carriage state - typically pendulum angle and angular velocity plus carriage position and velocity. This may be implemented using cascaded PID loops or full-state feedback; an angle-only PID can balance the pendulum while allowing the carriage to drift into the end of the rail.

5

u/Much-Fudge-7374 Sep 01 '26

It seems like you are asking a lot, not because you cannot do it, or because it is impossible. The best hardware I can think of for this is not an Arduino. It is not an ESP or a STM32. There are controllers/drives dedicated to servoing of BLDC motors and closed loop stepper control. The hardware design alone for the stage/system will be enough of a hurdle without designing drive and controller electronics from the ground up. The monetary investment will determine how good of a system you are to build. What drive system are you going to use direct drive linear motor or screw or belt drive? Encoder on motor more accurate control loop (plant doesn’t include the mechanics) but less accurate position feedback since the encoder is not on the load. You can use dual feedback where you use an encoder on the motor and one on the load - these will serve different purposes in your control loop ie. one will be for velocity loop/commutation if brushless and the other will be for position loop. You will need an encoder on the pendulum and have that hooked up on another axis. You may have to electronic gear or couple the reference position of the axis to the position of the linear stage or vice versa I am really not sure what the equations of motion for the system are without looking them up. You may have to employ gain scheduling for larger or smaller deviations from 0 degrees off vertical.

3

u/ivosaurus Sep 02 '26

I would start with balancing a ball in the middle of a tippy rail

2

u/2dof Sep 04 '26

Try find  inverted pendulum control in matlab/ mathworks web.  It is basic example In any proces control theory( a lot of theory in control books ). Also you should fing example code with application in  micropython esp32/ rp2040  project github.

I used to implement control solution sinulation)  during study for this type control with various type of controllers - from PID strategy to fuzzy / neural network solution.

It is very nice example for nonlinear control and you can learn a lot.

I recommend to take rp2040 ( or esp32) with micropython and just copy given algorithms from books.  (Give me couple of days and I will give you  authors ans titles) 

1

u/No_Alarm123 Sep 06 '26

Thank you. Ama proceed with this approach.

1

u/2dof 28d ago

Reffering to controll if inverted pendulum ( it it is on cart or on rotational axis ) there are two approaches:

  1. starting control form "Up" position

  2. starting control from "down" position" ( like on movie You linked)

second solution has always two strategies ( we swing up pendulum to up position, and then we switch to control position of ( cart) pendulum. First one often has only only position control (without swing-up ).

There is couple of stratgiee to swing-up - one of them (and popular ) is approach described by Åström and Furuta in lined ariticle above.

As a controller it can be PID control (tuned by Oprimal Control algoruthms ) or implementation of PID as Fuzzy controller or using nonlinear controllers.

In My opinion if You do not have experience in Control Algorithms - use always PI/PID control ( it is easy to tune).

If it is Your first approach - start from:

  1. read about physical hardware realization - Chose what is more feasible for You ( it can be like in video, it can be like in https://www.instructables.com/Inverted-Pendulum-Control-Theory-and-Dynamics/

    or like https://www.mdpi.com/2073-8994/13/8/1491

  2. when You choose construction - write mathematical model of Your system, most academics use Matlab ( expensive ) for modeling ( Scilab, Octave - free ) but nowadays Python has Control System library equivalent to Matlab and it very good tool for rapid prototyping.

( I also recommend Python, because if You choose any platform withh mocropython ( ESP32/ Rp2040) with Ulab package - You get very powerful tool). If You prefer programming in C ( arduino) - I still reccomend use Python for R&D

  1. Correctly written equations of motion are a base for for any Controller design.

Maretials to read abaout control of inverted pendulum:

https://ctms.engin.umich.edu/CTMS/index.php?example=InvertedPendulum&section=SystemModeling#1

https://ctms.engin.umich.edu/CTMS/index.php?example=InvertedPendulum&section=SystemModeling

https://www.mathworks.com/help/control/ug/control-of-an-inverted-pendulum-on-a-cart.html

https://fse.studenttheses.ub.rug.nl/17710/1/final.pdf

read abaout this projects.

https://www.mdpi.com/2073-8994/13/8/1491

https://github.com/funsho45/Inverted-Pendulum-Control#control-design-and-simulation

https://www.instructables.com/Inverted-Pendulum-Control-Theory-and-Dynamics/

I will try put also some books Titles - but i first have to "dig it out"

I assume that You have some experience in process control modeling

2

u/Creative_Sushi 27d ago

This is called Swing-Up Control of Pendulum and there is a walk-through example in MALTAB documentation. https://www.mathworks.com/help/mpc/ug/swing-up-control-of-a-pendulum-using-nonlinear-model-predictive-control.html

1

u/No_Alarm123 26d ago

This is a great resource. Thank u!

1

u/Suitable-Guitar4347 Sep 02 '26

The Exploratorium in San Francisco has one that at least for a while was totally analog- no microcontrollers- I’m not sure if it was changed for ease of repair. It’s right next to a manual one you can try and operate yourself to balance the stick. Check their site to see if they have any info on it, the engineers and developers there are very helpful if you email with a question.

1

u/razmoha Sep 02 '26

Selecting the appropriate oscillator is determined by several factors including the requirements for timing, power, and accuracy. Other factors include the operating conditions of the system, frequency, stability, size, and price. In an embedded system that requires accurate timing, selecting the proper oscillator is very important.

1

u/kwaaaaaaaaa Sep 02 '26

It would be helpful to mention your experience. This could be built mostly off the shelf stuff (linear rails, BLDC, rotary encoders) and some PID code. If you're approaching learning to write software for this, I would go with something more tried and true, a balancing robot. Much more resources and availability of guidance on hardware/electronics.

1

u/No_Pin4895 Sep 02 '26

Ive messed around with cartpole in simulation before and its honestly a lot easier to get running than the hardware side. The RL approach works but youll still need a solid control loop running underneath because real encoders have noise and motors dont respond instantly like they do in pybullet. If youre set on building physical id start with a simple brushed DC motor and a cheap optical encoder on a piece of extrusion, get PID balancing working first before you worry about steppers or BLDC. The double pendulum is a whole other beast though, youll need much faster loop times and better mechanics than most people realize

1

u/JSanctity Sep 02 '26

The one and only inverted pendulum control system. Thanks for sharing! Reminds me of my days in EE

1

u/nice_3D_print Sep 03 '26 edited Sep 03 '26

It’s unclear to me if you have already built the hardware or if you also need that. In case you have to build everything from scratch, follow the suggestions in the comments for the hardware side. You should aim at having very low friction if possible, because that’s a pain in the butt to deal with.

In terms of software. A basic model-based controller is really easy to implement in practice. Understanding the math instead… well, it’s a totally different beast. When the pendulum is nearly upright, you can use a simple PID, the code for it should be rather trivial. The issue is the tuning. Ideally, you’d want to use something called Linear-Quadratic-Regulator (LQR). The math behind it is far from trivial, but the practical part is quite easy: you just “plug your model specs in” and get the PID constants out. In these modern times, do a mix of online research and ask AI chatbots to help you putting the information together. You should’ve enough to start. You can write the upright controller with a simple logic: if the pendulum angle isn’t far away from the upright position, run the controller, otherwise switch the motor off. After you checked it works, move to the swing up phase. Again, the math behind the control is not easy, but the end result is. You could use an energy-based lyapunov control function (this will probably sound like nonsense, but again, this is rather high level math…). Look this up online and ask ChatGPT or similar to fill the gaps. All of this should be “easily” achievable with just an arduino. The issue is understanding where the math plugs in, but again, using online resources plus some AI agent should help getting there. The control loop now is: if the pendulum isn’t close to the upright position, use the swing up control law; otherwise, the stabilising PID.

The issue is if you want to use Reinforcement Learning and/or other advanced techniques. In that case, I think it will be better to have a PC doing the heavy lifting and communicating with the arduino over serial. If you insist going in this direction, my best recommendation would be to do some transfer learning. Basically, you create a simulator, train a controller there (for example, using DQN or other algorithms) and then use it on the real system. The controller will not work well at first, so you train it even more but this time on the real system. If you go this route, good luck because it’s a very ambitious project!

1

u/CauliflowerTop2464 Sep 04 '26

Wasnt something like the solution to reusable rockets?

1

u/solitude042 Sep 06 '26

Have you built a balance bot yet? It's a similar challenge with a much simpler hardware list. Not trying to sway you away from your chosen project, just offering a simpler alternative. 

1

u/nnmax_ Sep 01 '26

It's the classic cartpole problem isn't it?

I'm not an expert in hardware side but I do have quite some experience in ML side. You can train a very small using reinforcement learning within minutes and use that model for balancing the pole. It'd save you lot of time in software part.

And another thing is cartpole problem is the very first example you'd find in reinforcement learning as well. It's incredibly easy to understand and implement by yourself in colab. Then all you need to do is focus on integration of hardware i/o with the model.

1

u/GabrielCRadu Sep 01 '26

I think you could actually train a neuronal network if you give it enough data and punish it for failing, but it would theoretically require you to manually start each new simulation perfectly upright

3

u/ViolentlyVia Sep 01 '26

if I'm not mistaken I have actually seen a project that did just that

1

u/nice_3D_print Sep 03 '26

Absolutely! If you had a decent enough estimation of the electromechanical parameters, you could pre-train a network in simulation with reinforcement learning, then deploy on the real system and let it run a bit more to fine tune and adapt. I wouldn’t recommend it as a first DIY project though xD

-1

u/CryingOverVideoGames Sep 01 '26

A double pendulum is a chaotic system so that would be impossible to build

7

u/wigitty Sep 01 '26

Nope, they're more difficult to balance, but definitely doable. I think I've even seen triple setups. Look it up on youtube.

3

u/daney098 Sep 02 '26

Yeah, and they were so badass that they included every possible stable configuration of the 3 linkages. It's crazy seeing how it arrives at some configurations

0

u/Realistic_Account787 Sep 02 '26

It looks complete already, you don't need anymore help.