Rest-to-Rest control of a 2-DoF Robot using NMPC Controller implemented using CasADi in Matlab
Sample Video of the motion generated by the controller (angles visulization):
- Rest-to-Rest-control-of-a-2-DoF-Robot
- A) Goal of the Project
- B) Modeling
- C) Open-loop Optimal Control
- D) Model Predictive Control
An often occurring control task in industry is to compute optimal input trajectories for robots to reach a given end position while consuming minimal energy, or to reach the end position in minimal time. In this project we want to tackle these challenges by controlling a two-degree-of-freedom (2-DoF) robot shown in Figure 1. The dynamic model of the robot is given by a vector-valued second order differential equation.
The coefficient matrices are given by:
1) Obtaining model (1) from physical laws and formulating the model in q1-q2-coordinates and not in x-y-coordinates:
The dynamic model of a 2-DOF robotic arm is a set of mathematical equations describing the relationship between its motion, the forces acting on it, and the torques applied to its joints. It's essentially a way to predict how the arm will behave under different conditions. There are different ways to derive the dynamic model, but the most common methods involve:
Lagrangian dynamics: This approach uses the principle of Lagrange's equations to relate the kinetic and potential energies of the system to the applied torques. It results in a set of second-order, nonlinear differential equations.
Euler-Lagrange equations: This is a simplified version of the Lagrangian approach and leads to the same set of differential equations.
The dynamic model is often formulated in joint space (q1, q2) rather than Cartesian space (x, y) because it simplifies the representation of the system dynamics. In joint space, the dynamics are typically described by a set of coupled second-order differential equations, making it more straightforward to analyze and control the robot's behavior.
Additionally, the joint space naturally reflects the actuator inputs (joint torques or forces), making it more suitable for control purposes. The transformation between joint space and Cartesian space can be achieved through the robot's forward and inverse kinematics.
2) Model (1) is given as an implicit system of second-order differential equations. Reformulating it as an explicit first-order system in state space form $\dot{x} = f(x,u)$ :
To reformulate the implicit second-order differential equation system
First we define the state vector
We notice that the equation involves the two joint angles
The modified forms will be as follows:
The
The new equation structure: $$eqn=BY+CX+G==U$$ where the
Next we solve symbolically the equation in terms of solve(eqn, Y)
which results the value of
So in short, the appropriate state vector
1) Formulating different OCPs that drive the robot from the initial position $q = ({-5}, {-4})^\top$ rad, $\dot{q} = (0, 0)^\top$ rad/s to the upper position $q = (\frac{\pi}{2}, 0)^\top$rad, $\dot{q} = (0, 0)^\top$ rad/s in $3$ s. Thereby, the input constraints $u \in [{-1000}, {1000}]$ Nm and the state constraints $\dot{q} \in [-\frac{3}{2}\pi, \frac{3}{2}\pi]$ rad/s should be satisfied for all times.
- OCP1: Quadratic Cost Function of inputs and state error:
The optimal control problem (OCP) can be formulated as follows:
The Q and R are cost matrices which are:
A terminal constraint could be added also as:
- OCP2: Quadratic Cost Function of inputs only:
The optimal control problem (OCP) can be formulated as follows:
and all other constraints and values stays the same.
- OCP3: Minimum Time
OCP4: Minimum Time + quadratic input cost
Sampling time of 0.05, 0.1, 0.5 were used along with 3 different integrators: rk4, heun and euler forward. The results from using different was not much different but sampling time affected more the outputs.
eulerf | heun | rk4 | |
---|---|---|---|
Calculation Time [s] | 0.337 | 0.796 | 1.72 |
Num of Iterations | 49 | 42 | 43 |
Table 1: Different Integrators using a sampling time of 0.1 sec
Figure 3: Simulation Graphs using different integrators
The results are very similar but we can notice the difference in the calcualation time where euler forward method is much faster the rk4.
Sampling Times [s] | 0.05 | 0.1 | 0.5 |
---|---|---|---|
Calculation Time [s] | 12.6 | 1.74 | 0.163 |
Num of Iterations | 56 | 43 | 16 |
Table2: Different sampling times using rk4
We can notice that the graphs for sampling times of 0.05 and 0.1 are very similar. Also, both are better than the 0.5 sampling time. But the catch is in the time needed to calculate the optimal solution as a sampling time of 0.05 is 12.6 sec which is x7 longer than a sampling time of 0.1 . Thus, maybe the resolution is much better with 0.05 but not needed in our case.
Figure 4: Simulation Graphs using different sampling time
The OCP used is OCP3 formulated in part a). The min time found is: 2.3631 sec
Figure 5: Min Time Graphs
We can notice that the output is similar to bang bang control which makes sense so that we can find the minimum time to reach the optimal solution.
The compromised OCP is the OCP4 found also in part a). Below some tests were done to find a compromise between both costs:
Compromise | 1000T+J | 100000T+J | 10000000T + J |
---|---|---|---|
Min Time [s] | 7.2553 | 3.5432 | 2.3656 |
Calculation Time [s] | 30.43 | 46.83 | 50.50 |
Num of Iterations | 92 | 144 | 157 |
Figure num | 6 | 7 | 8 |
Table3: Compromise between time and input cost
Figure 6: 1000T+J
Figure 7: 100000T+J
Figure 8: 10000000T+J
a. Friction: Friction in the joints can cause unpredictable resistance to the robot's motion like Viscous friction torque and Static friction torque.
-
Viscous friction torque: The frictional torque
$-F_v$ can be modeled as a function of joint velocity$\dot{q}$ :$B(q)\ddot{q} + C(q, \dot{q})\dot{q} + g(q) = u - F_v \dot{q}$ -
Static friction torque: The frictional torque
$-F_c$ can be modeled as a function of joint velocity$\dot{q}$ :$B(q)\ddot{q} + C(q, \dot{q})\dot{q} + g(q) = u - F_c sgn(\dot{q})$
b. External Forces: Forces applied by the environment or objects can perturb the robot's motion.
-
Mathematical Formulation: External forces acting on the robot can be represented as a vector function of joint velocity
$J^T(q) h_e$ :$B(q)\ddot{q} + C(q, \dot{q})\dot{q} + g(q) = u - J^T(q) h_e$
c. Sensor Noise: Noise in sensor measurements can introduce uncertainty in the feedback signals.
-
Mathematical Formulation: Sensor measurements
$y$ may be corrupted by additive noise$n$ :$y_{\text{noisy}} = y_{\text{true}} + n$ .
d. Modeling Errors: Simplifications or inaccuracies in the mathematical model used for control can lead to discrepancies between the model and the actual system.
-
Mathematical Formulation: Model parameters or dynamics may deviate from the true system dynamics:
$\text{Model}(q, \dot{q}, u) \neq \text{Actual}(q, \dot{q}, u)$ .
Model Predictive Control (MPC) is a control strategy used in dynamical systems to optimize future behavior based on a predictive model. It operates by repeatedly solving an optimization problem over a finite time horizon, considering the current state of the system and predicting its future evolution. MPC takes into account system dynamics, constraints on inputs and states, and possibly disturbances or uncertainties to compute optimal control actions. By iteratively updating the control policy, MPC can adapt to changes in the system and external conditions, making it suitable for a wide range of applications including robotics, process control, and automotive systems.
For the MPC controller, we use the same OCP as before but this time we input only the first optimal input to the system:
3) Assume incorrect parameters in the model, such as $b1 = 180\ kg\ m^2/rad$ and $b2 = 45 \ kg\ m^2/rad$ . When/how would this happen in practice? Simulate the open loop and the closed loop system. Plot your results and describe the differences.
One common scenario is when the physical properties of the system are not accurately known or change over time due to wear and tear. For example, if the inertia of the robot's joints is incorrectly estimated, it can lead to discrepancies between the predicted and actual behavior of the system. Below is a graph comparing between the original and mismatched models. Only the Joint graph is shown for clearer results. The OCP is solved assuming we have correct parameters and then applied on both the original or mismatched systems. The difference is clearly shown as there is no feedback.
Figure 9: Comparing between the original and mismatched dynamics behaviour using optimal input
Figure 10: Comparison between Open and Closed Loop when model is mismatched
4) Now assume an additive Gaussian measurement error; choose a meaningful value for the variance. Implement this situation and simulate your closed-loop system for the fixed end-time problem. What do you observe? Plot your results.
Variance is implemented using the below formula and is added to the initial condition of the input X variable at the start of the MPC loop so that it represent error in the measurement:
Figure 11: MPC with Gaussian Noise
Analyzing the stability properties of the MPC controller involves examining how well it performs under various conditions, including disturbances, noise, and model inaccuracies. From Figures 11 and 12, it's evident that the MPC model demonstrates good performance, effectively driving the system towards the optimal values despite potential challenges. Moreover, the length of the horizon in MPC significantly influences the controller's performance and computational requirements. A shorter horizon length leads to faster computation times and more localized control actions, focusing on immediate future states. However, it may limit the controller's ability to anticipate future disturbances or changes in system dynamics. On the other hand, a longer horizon length increases computational complexity but enhances predictive capability, allowing the controller to consider a broader range of future states.
In this project, we have added an obstacle object near the robot as a constraint. This addition is crucial for ensuring that the robot avoids collisions with the obstacle while performing its tasks. The obstacle is represented by a fixed position in the workspace, and the robot must maintain a safe distance from it during its motion.
To incorporate the obstacle avoidance constraint in the Optimal Control Problem (OCP) formulation, we have introduced a new constraint that calculates the distance between the robot and the obstacle. The robot's position is determined based on its joint angles, and the distance to the obstacle is computed accordingly. The constraint ensures that the robot maintains a minimum distance from the obstacle at all times.
The modeling section has been updated to include the obstacle avoidance mechanism. The robot's dynamics now account for the presence of the obstacle, and the control strategy is designed to ensure that the robot avoids collisions while reaching its target position. The obstacle avoidance is implemented by calculating a repulsive force that pushes the robot away from the obstacle when it gets too close. This force is integrated into the robot's control inputs to ensure safe and efficient motion.