Help implementing LQR on rotary inverted Pendulum

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 2K views
Pipsqueakalchemist
Messages
141
Reaction score
20
TL;DR
Simulink modelling of rotary inverted pendulum for LQR (Linear-Quadratic Regulator) implementation on hardware
I'm trying to work on a rotary inverted pendulum project. I have an image of it below where most of the stuff is 3D printed.
My electronics is NEMA17 stepper, ESP32 devkit v1, TMC2208 v1.2, and AS5600 encoder for pendulum sensing. I was planning to use the accelstepper library to keep track of step count for rotor position sensing.

1787708363110.webp


I derived the dynamics and developed my LQR controller to spit out rotor acceleration, u = q1ddot(rad/s^2), so my state space was x = [q1 q2 q1dot q2dot] where q1/q1dot are rotor angle/velocity and q2/q2dot are pendulum angle/velocity.
So since my control was rotor acceleration then xdot = [q1dot q2dot u f(q1,q2,q1dot,q2dot,u)] where f = q2ddot is function I found from rearranging the dynamics equation I derived.

1787708473391.webp


Then I linearized xdot using Jacobian matrix about equilibrium point xbar = [0 0 0 0] to obtain my A and B matrix to obtain gains for LQR. I created a Simulink model that essentially feeds in the full states into the controller block which contains the LQR controller + energy based swing up controller. Then obtain rotor acceleration u = q1ddot then I used inverse dynamics to convert it to torque and apply it to the rotary plant. The issue is that this simulink model doesn't represent my hardware that well.

1787708559034.webp


In order to improve my simulink model I'm going to import my CAD model. I wanted to ask what good experiment should I run on my rotary hardware and log it on Serial monitor and send that data to simulink to improve my model. Also I'm not 100% sure how I would model the nema17 stepper. If it was DC motor I could use the DC motor equations. In Simulink I can only input into the revolute joint either torque or angle, so I could either perform inverse dynamics like before and get torque, or I could double integrate the acceleration and input position. I'm not sure which model is more realistic way to model nema17. And if I did do the double integration method, I think it would be
speed = prev_speed + accel x period
angle = prev_angle + speed x period

1787708969160.webp


I would appreciate any help on this, I understand the theory and can get things working in simulation but I'm having issues applying it to hardware, so I'd appreciate any advice from people who have. Thank you
 
Physics news on Phys.org
Paul Colby said:
What is the intended behavior of your hardware? For example, is it to balance? Cancel tipping motion?
Hello, the purpose is to balance the pendulum, I'm correctly trying to develop a better model of the system in simulink. I'm trying to run some step response experiments and try to model the nema17 motor as a 2nd order system. Then try to run some additional experiments and try to use mat labs parameter estimation to approximate the hardware parameters such as inertia or pendulum friction
 
Typically people use feedback to solve such problems. This doesn’t require an accurate model of motion. All that’s needed is a measurement of the error and a response function.
 
thing is I'm trying to implement not just a PID control but an LQR controller. I want to develop the best model I can to get the best performance and get my simulation to behave as much like my hardware. I feel like getting a good model is also good practice to develop my overall skills as a control engineer
 
If your sw is updating the step motor position frequently (not necessarily moving it, if that's not needed) and the step size is small, I think you can use an analog DC motor as a good low frequency model. Otherwise you'll have to enter the world of digital control with z-transforms and such. Operation near the sampling frequency is a real PITA, especially for analysis; usually very non-linear.