Search code examples
pythongekkompc

Is it possible to use this gekko MPC method online on a real robot?


I want to do some trajectory tracking on a real robot, and I want to use MPC, so I found this gekko. I can make to simulations of my robot and stuff, but is it possible to use it real-time, as I see it take some time to calculate the optimal solution, and I am not quite sure coding wise how to do it, if you just take the first imput and give it to the real system and then update the state values with the sensor reading, and then use the m.solve() function again, and so on.

Thanks in advance


Solution

  • Yes, Gekko is certainly possible to do real-time control but you'll need to pay attention to a few things:

    • Cycle time: the application has to read the sensors, calculate the update to the model (estimation), optimize the future move plan (control), and implement the move plan within the specified cycle time. If you have a cycle time faster than 10 Hz, you may want to look at an LQR controller or other method that is faster.
    • Model form: a nonlinear model will take longer to calculate or may not converge. A linear model is typically used in many industries because it is fast and reliable.
    • Horizon length: you need a sufficiently long horizon length to anticipate future constraints that affect current moves
    • Horizon resolution: the first step of the controller needs to be consistent with the cycle time. Future prediction steps can be of any step size.
    • Local solve: if you are implementing the MPC on an embedded hardware like a Raspberry Pi, the local solve may be slower than a remote solve. You can use the remote solve to the public server (remote=True), set up your own local server in Windows or Linux (remote=True server='http://10.0.0.10'), or else solve locally on the embedded hardware (remote=False). You set the remote and server when you create the gekko object (m=GEKKO(remote=False)).

    There is a lot of real-time MPC code and an application with the Temperature Control Lab (TCLab) on the course for Machine Learning and Dynamic Optimization. Below is MPC in action.

    TCLab MPC

    Here is the device:

    TCLab