ROS stack with an efficient real-time NMPC for the Crazyflie 2.1
The advances in computer processor technology have enabled the application of nonlinear model predictive control (NMPC) to agile systems, such as quadrotors. These systems are characterized by their underactuation, nonlinearities, bounded inputs, and time-delays. Classical control solutions fall short in overcoming these difficulties and fully exploiting the capabilities offered by such platforms. This paper presents the design and implementation of an efficient position controller for quadrotors based on real-time NMPC with time-delay compensation and bounds enforcement on the actuators. To deal with the limited computational resources onboard, an offboard control architecture is proposed. It is implemented using the high-performance software package acados, which solves optimal control problems and implements a real-time iteration (RTI) variant of a sequential quadratic programming (SQP) scheme with Gauss-Newton Hessian approximation. The quadratic subproblems (QP) in the SQP scheme are solved with HPIPM, an interior-point method solver, built on top of the linear algebra library BLASFEO, finely tuned for multiple CPU architectures. Solution times are further reduced by reformulating the QPs using the efficient partial condensing algorithm implemented in HPIPM. We demonstrate the capabilities of our architecture using the Crazyflie 2.1 nano-quadrotor.READ FULL TEXT VIEW PDF
Iterative linear quadradic regulator(iLQR) has become a benchmark method...
This paper focuses on the trajectory tracking control problem for an
This paper describes autonomous racing of RC race cars based on mathemat...
The theoretical unification of Nonlinear Model Predictive Control (NMPC)...
Differential games offer a powerful theoretical framework for formulatin...
Flying robots such as the quadrotor could provide an efficient approach ...
We introduce the Control Toolbox (CT), an open-source C++ library for
ROS stack with an efficient real-time NMPC for the Crazyflie 2.1
Nonlinear model predictive control (NMPC) is an optimization-based control method that relies on the online solution of nonlinear nonconvex programs to compute an optimal feedback policy. In particular, domains characterized by sufficiently long sampling times, such as the chemical processing and oil refineries, have seen successful industrial applications of NMPC since the 80s. As the computation times associated with the solution of the underlying optimization problems can be rather long, the employment of NMPC has only recently been extended to applications where shorter sampling times are required.
One important precondition for its employment in the context of agile systems is the availability of approximate schemes that enable real-time feasibility. Particularly successful schemes trade control performance for speed and typically hinge on implementations of: Newton-type method [deng2020, barroscarlos2020], augmented Lagrangian method [englert2020, sathya2020], and continuation/generalized minimal residual (C/GMRES) method [guo2020, katayama2020]. Solution times can be further reduced by building efficient condensing approaches and structure-exploiting solvers.
Quadrotors are well-known examples of agile systems. They are characterized by their underactuation, nonlinearities, bounded inputs, and, in some cases, communication time-delays. The development of their maneuvering capability poses some challenges that cover dynamics modeling, state estimation, trajectory generation, and control. The latter, in particular, must be able to exploit the system nonlinear dynamics to generate complex motions. However, the presence of communication time-delay is known to highly degrade the control performance of the system in question. This issue has been extensively studied in the literature over the years, but given the growing number of applications requiring collective interaction, most efforts are in the field of multi-agent control[kartal2020, alvarez2019, huang2019]. Although these strategies are encouraging, the resulting feedback control laws are not optimal.
This paper proposes an efficient position control architecture based on real-time NMPC with time-delay compensation for quadrotors. The architecture consists of predicting the state over the delay time interval, granting a nominally delay-free model in the NMPC formulation, which takes into account the input bounds. The NMPC algorithm is built upon the real-time iteration (RTI) scheme with Gauss-Newton Hessian approximation, and it is implemented using the high-performance software package acados [verschueren2019]. We use the automatic differentiation and modeling framework CasADi [andersson2019] to provide sensitivity calculations. The quadratic program (QP) solver is a Riccati-based interior-point method, HPIPM [frison2020], which relies on the hardware-tailored linear algebra library BLASFEO [frison2018]
. We show that by using a Hessian condensing algorithm particularly well-suited for a structure-exploiting QP solver, a considerable speed-up can be achieved with respect to a state-of-the-art NMPC solver. To experimentally validate the architecture, we use the Crazyflie 2.1 nano-quadrotor, an open-source off-the-shelf platform for research and education in robotics (see Fig.1 for an illustration). The main contributions of our work are:
Provide an experimental validation of our architecture.
Grant an efficient software implementation that needs to cope with resource-constrained hardware.
This paper is outlined as follows. Section II provides the details of the proposed architecture. Section III shows the comparative results of the simulations. Sections IV and V present the experimental closed-loop results and the discussion of our strategy, respectively. In Section VI the conclusions are summarized.
Consider a body-fixed frame located at the center of mass (CoM) of a quadrotor and aligned with a North-West-Up inertial frame . Then consider a quadrotor with position expressed in , attitude , linear velocity expressed in and angular rate
. Its nonlinear dynamics are given by the first order ordinary differential equations adapted from[luis2016]:
with state . Here, , while the constants and
represent the gravitational acceleration and the mass of the quadrotor, respectively. The inertia moment of the vehicle is denoted by. The quaternion rotation matrix from to is expressed by . The total external forces and moments applied to the CoM of the quadrotor and expressed in are defined as:
where is the thrust coefficient, is the drag coefficient, and is half of the distance between motors. Finally, assuming it is possible to change the rotational speed of the propeller instantaneously, we define their set as the control input of the system:
In our aerial system, because of the radio communication latency, we have delays both in receiving measurements () and sending control inputs (). Likewise, since we intend to use NMPC, the potentially high computational burden () associated with its solution becomes an element that must also be taken into account to minimize the error in the state prediction.
One-way latency is inherently difficult to measure due to the synchronization clocks dilemma, an issue that is usually solved by measuring the round-trip time (RTT). In this context, the sum of network latencies must be considered and can be deliberately placed where it is most convenient, as long as the RTT is not changed. Following these considerations, we propose a state predictor based on the RTT as a delay compensator. The prediction is computed by performing forward iterations of system (1), starting from the current measured state and over the RTT, through an explicit Runge Kutta 4th order (ERK4) integrator. Due to the independent nature of this operation, perfect delay compensation can be achieved by adjusting the integration step to be equal to the RTT. Thus, in this paper, it is assumed that there is a fixed RTT, defined by , to be compensated. Although this prediction is simple (and inaccurate), it will prove surprisingly effective in experimentation.
Employing direct multiple shooting [bock1984] to discretize the underlying continuous-time optimal control problem and assuming a linear least squares objective, the NMPC problem considered in this paper is defined as the following constrained nonlinear program (NLP):
where and denote the state and input trajectories of the discrete-time system whose dynamics are described by . The functions in the stage and terminal least squares terms are denoted by and respectively. Variables and denote the time-varying references. The weight matrices are denoted by . The inputs bounds are . Finally, and represent the horizon length and the estimated state of the system at the current time instant , respectively.
When using NMPC to control a system, at each sampling time, an instance of NLP (2) needs to be solved within the available computation time, where the value of the current estimate of the system is set to . To this end, several numerical approaches have been investigated in the literature [Diehl2009c]. Among others, two main classes of approaches stand out: sequential quadratic programming (SQP) and nonlinear interior-point (IP) methods.
Due to the non-negligible computational burden associated with the solution of (2), it is often necessary to use approximate feedback policies to achieve real-time feasibility. A rather widespread approach, the so-called real-time iteration (RTI) [Diehl2005], consists of using an SQP-based numerical strategy that relies on the solution of a limited number of QP subproblems. In this way, the computation times can be drastically reduced. Although only an approximate solution for (2) is obtained, the parametric manifold that describes the optimal solution can be tracked, leading to a feedback policy that, under suitable assumptions [Diehl2007b], achieves bounded suboptimality and preserves attractivity of equilibria. In this work, we will be using the implementation of the RTI method through the high-performance software package acados.
Among other methods to approximate the Hessian of the Lagrangian, a particularly successful one is the Generalized Gauss-Newton (GGN) method, which is applicable when the objective function is of least squares type [bock1984], as in (2). After applying it, the resulting QP has a sparse banded structure [kouzoupis2015] and can be rewritten as:
where with state and control deviations . To match the QP formulation used in acados, we define: , , , for , and . The matrices used in the linearized dynamics and the upper and lower bounds of the polyhedral constraints are defined as
where the superscript refers to the linearization points at the previous QP iteration. Finally, we have that the Hessian and the gradient terms for are defined as
with and denoting the blocks in related to states and inputs respectively, and
One way to address the QP (3) could be to exploit its banded structure and apply a sparse QP solver to it. Another way could be to first reduce, or condense, the variable space of the QP by eliminating all state deviations through the continuity constraint of (3) and then apply a dense QP solver. Differently, in this paper, we employ a recently proposed approach, in-between the sparse and the condensed one, called partial condensing. In particular, we use a Hessian condensing algorithm where a state component is retained as an optimization variable at each stage of the partially condensed QP [frison2016]
. The algorithm finds the optimal level of sparsity for the QP solver at hand, trading horizon length for input vector size. Thus, partial condensing is employed as a preparation step before the call toHPIPM, the Riccati-based interior-point method solver we use, which has been tailored to exploit the particular structure of the obtained QP.
In this section, we will first assess the performance of two optimal controllers in simulation. In particular, a linear-quadratic regulator (LQR) and an NMPC will be considered. Such comparison is valuable since the LQR can perform an optimal control law within short execution times and with low computational burden, while the NMPC deals with constraints and predictions. The values of the parameters appearing in the model are listed in Table I and correspond to the parameters of the actual Crazyflie, obtained partly from the study in [luis2016] and partly via system identification. Accounting for numerous computation time uncertainties, in this work, a sampling time of ms is set for both controllers. Next, we will investigate the effect of the time-delay on the control performance of the nano-quadrotor system, considering different RTTs.
|9.8066 m/s||gravitational acceleration|
|0.33 kg||total mass|
|0.0325 m||arm length|
|inertia moment around x|
|inertia moment around y|
|inertia moment around z|
The first controller considered is the LQR. During the LQR control design, the nonlinear dynamics (1) are linearized around the hovering steady state and input () and also discretized using as sampling time, producing:
Due to the uncontrollable linearized quaternion dynamics, the local linear model (4) is also not controllable. To address this issue, we project the dynamics onto a controllable subspace as done in [yang2012, zanelli2018], using the fact that the first component of the quaternion vector can be eliminated thanks to the relation . Thus, we obtain the 12-state model needed to project the LQR static gain. A discrete-time LQR controller can be designed by finding the infinite horizon solution associated with the discrete-time algebraic Riccati equation
which yields the static feedback law , where
The positive-definite matrices and are defined so that the stability of the closed-loop system is guaranteed.
The second controller is the NMPC. When designing an NMPC, choosing the horizon length has profound implications for computational burden and tracking performance. For the former, the longer the horizon, the higher the computational burden. As for the latter, in principle, a long prediction horizon tends to improve the overall performance of the controller. In order to select this parameter and achieve a trade-off between performance and computational burden, NLP (2) has been implemented in acados considering , discretizing the dynamics (1) using an ERK4 integration scheme. Likewise, we compare the condensing approach with the state-of-the-art solver qpOASES [ferrau2014] against the partial condensing approach with HPIPM, in relation to the set of horizons considered. The weight matrices are the same in all simulations. The functions in the stage and terminal least squares terms are defined as
We regulate for so that the geodesic arc can be locally approximated by a straight line.
According to Fig. 2, horizon provides a higher tracking performance. Nevertheless, when considering a long horizon, the available computational power may not be sufficient to deliver a solution within the runtime requirements of an aerial system. In Fig. 3, the average runtimes per SQP-iteration are reported. As qpOASES is a solver based on active-set method, it requires condensing to be computationally efficient. In line with the observations found in the literature that condensing is effective for short to medium horizon lengths, we note that qpOASES is competitive for horizons up to approximately when compared to HPIPM. The break-even point moves higher on the scale for longer horizons, mainly due to efficient software implementations that cover: a) Hessian condensing procedure tailored for partial condensing, b) structure-exploiting QP solver based on novel Riccati recursion, c) hardware-tailored linear algebra library.
Furthermore, when dealing with a nominal NMPC formulation, as in this paper, the model uncertainties and their propagation also contribute to the decision of the horizon length. The longer the prediction, the more the modeling errors accumulate, and the tracking performance can no longer be guaranteed. Uncertainty can be added to the system model to account for the noise associated with the onboard inertial measurement unit (IMU) and to simulate the variability in the repetition of the task. However, this has not been included in the scope of this paper.
Therefore, we can conclude that horizon offers a reasonable trade-off between deviation from the reference trajectory and computational burden.
This section is dedicated to compare and discuss the simulation results for LQR and NMPC. Initially, the LQR was tuned so that its control policy was locally equivalent to the one of the NMPC. However, as also experienced in the work of [zanelli2018], the simulations showed the need to detune the controller so that the performance, specifically for the position in in our case, was acceptable. Thus, the following weight matrices are used:
For the NMPC, the weight matrices are
while the input bounds on the rotational speed of the propellers are , krpm. The simulation scenario consists of producing steep changes in the position reference. The closed-loop tracking performance of both controllers is shown in Fig. 4. We notice that the NMPC controller outperforms the LQR, considering that absence of overshoots and faster response to changes in the references can be achieved. This may be due to the fact that nonlinearity and unmodeled constraints can degrade the performance of the LQR controller for large reference changes.
For the purposes of this analysis, we assume that for some nonnegative integer . However, we emphasize that the same analysis can be done considering any value of , as mentioned in Section II-B. The results of the simulations for the step response of proposed NMPC without considering the time-delay compensation are shown in Fig. 5. We observe that the greater the time-delay associated with the RTT, the greater the degradation on the NMPC performance. This degradation is observed through the increase of overshoots and oscillations in the system’s response, indicating that the proposed NMPC is not able to control the system properly. Unlike in simulation, in a real-world context, where model uncertainties come into view, the overshoots and oscillations will tend to be even greater. Besides, other simulations have shown that the system becomes unstable for greater RTTs. That is why delay compensation is of particular importance in our case.
The proposed architecture is experimentally validated on the Crazyflie 2.1 nano-quadrotor, developed by the Swedish company Bitcraze111https://www.bitcraze.io. The system can be controlled through the onboard long-range radio receiver and its correspondent transmitter, the Crazyradio PA, or even through the Bluetooth LE (low-energy) connection. It has an open-source firmware based on FreeRTOS that allows the user to log sensor data at a maximum frequency of 100 Hz. The radio communication uses a protocol developed by Bitcraze, the Crazy RealTime Protocol (CRTP), which limits the size of the packages sent from the Crazyflie to the Crazyradio in up to 30 bytes (1-byte header, 29-bytes data payload).
The motion capture system consists of ten Raptor-E cameras and the Cortex software, both from Motion Analysis. It provides the 3D global position of an IR-reflective marker placed on top of the Crazyflie at a frequency of 100 Hz. The entire setup has an estimated RTT of ms to be compensated.
Our control architecture hinges upon a ROS Kinetic framework and runs at 66.67 Hz. The CRTP is used in combination with our crazyflie_nmpc stack [github2020], based on the Crazyflie ROS interface [honig2017], to stream in runtime custom packages containing the required data to reconstruct the part of the measurement vector that depends on the IMU data. The logging subsystem is mapped into ROS messages and, thus, it is possible to recover the current Euler angles and angular velocities of the nano-quadrotor. Likewise, the cortex_ros bridge streams the 3D global position of the Crazyflie, which is then passed through a second-order, discrete-time Butterworth filter to estimate the linear velocities.
Through the use of acados Python interface and CasADi automatic differentiation and modeling language, we export a plain C-code that corresponds to NLP (2). The exported code is linked against the precompiled acados core library and then wrapped by our NMPC ROS node, written in C++. The QP subproblems arising in the SQP algorithm in acados are solved using the high-performance HPIPM solver, which implements an interior-point method. It is written entirely in C and is built on top of the high-performance linear algebra package BLASFEO. We use the X64_INTEL_HASWELL target in BLASFEO, which explicitly employs the powerful vector instructions provided by this computer architecture. In particular, the high-performance routines provided by BLASFEO outperform other state-of-the-art dense linear algebra libraries in the case of matrices of moderate size, as in the current application. Regarding the condensing approach, we use the partial condensing routines provided by HPIPM.
the desired roll and pitch angles, in degrees
the yaw rate , in degrees per second
the base PWM signal
It is important to clarify that is a 16-bit integer number ranging from 0 to 65535 and stands for the base value of pulse width modulation (PWM) applied to all motors to maintain altitude. Despite the name thrust in the official firmware222https://github.com/bitcraze/crazyflie-firmware, does not directly represent the net force applied along the body-fixed axis, but is related to it. In principle, once the NMPC solution is obtained, the first element of the predicted control trajectory should be sent to the Crazyflie. However, there is an incompatibility between and . To translate the control inputs , in krpm, to the base PWM signal , an integer, we need to establish some mappings. To that end, we use the following relations adapted from [luis2016]:
Similarly, at each new NMPC solution, we reconstruct the remaining input commands , , using part of the state solution at stage , as denoted in Fig. 6. This approach, however, entails some distortions onto the onboard controllers (attitude and rate controllers) since the NMPC does not take them into account in its formulation. This effect will be described in the next subsection.
How the onboard controllers (PIDs) use the setpoints of the offboard controller (NMPC) in our architecture is not entirely conventional and, thereby, deserves some considerations. Two main points presented in Fig. 6 must be addressed. First, the reference signals that the PID loops track do not fully correspond to the control inputs considered in the NMPC formulation. Instead, part of the state solution is used in conjunction with the control inputs to reconstruct the actual input commands passed as a setpoint to the Crazyflie. Second, a part of the reconstructed input commands is sent as a setpoint to the outer loop (attitude controller), and the other part is sent to the inner loop (rate controller).
Furthermore, as the NMPC model does not include the PID loops, it does not truly represent the real system, even in the case of perfect knowledge of the physical parameters. As a consequence, the optimal feedback policy is distorted in the real system by the PIDs. Note that intuitively reducing the NMPC discretization time mitigates the distortion. Another solution is to implement a linear interpolation onboard, considering the frequency of each loop. In this case, given a new input command from the NMPC, data points are constructed and passed as reference signals to the PIDs.
In every experiment, the Crazyflie takes off and remains hovering at . This is done to avoid the unmodeled ground effect during the tracking phase. When the nano-quadrotor reaches the steady-state for hovering, the user triggers the tracking. The nano-quadrotor is then asked to hold the last point of the reference trajectory until the user triggers the landing command.
To validate the effectiveness of our control architecture, we ran two experiments. For each experiment, we generate a reference trajectory on a base computer and pass it to our NMPC ROS node every ms. When generating the trajectories, we explicitly address the feasibility issue in the design process, creating two references: one feasible and one infeasible. In addressing this issue, we prove through experiments that the performance of the proposed NMPC is not degraded even when the nano-quadrotor attempts to track an infeasible trajectory, which could, in principle, make it deviate significantly or even crash. The reference trajectories considered for each experiment are:
For the first experiment, the reference is a dynamically feasible smooth step on that drives the nano-quadrotor from the hover position to the final one, defined as . To generate it, we implemented NLP (2) using CasADi with a time horizon of s and shooting nodes, discretizing the dynamics (1) using an ERK4 integration method. The resulting NLP is solved using the interior-point solver IPOPT [wachter2006]. The closed-loop trajectory and input commands for this experiment are shown in Fig. (a)a.
For the second experiment, the reference is a cartesian helical trajectory, generated using MATLAB. We imposed a radius of m, initial height of m, height increments of m per , and a duration of s, divided into intervals. The closed-loop trajectory and input commands for this experiment are presented in Fig. (b)b.
In Fig. 7, we note that the PID distortion on the NMPC performance is relatively small. As expected, the most challenging setpoints to be tracked are the positions in which, given a change in the motion, the Crazyflie has to pitch/roll in the opposite direction quickly. These are the setpoints where the distortion has the greatest influence on the system, causing the small overshoots in position.
To assess the computational complexity of our algorithm, we gather the average and maximum solution times of the tailored RTI scheme using acados. The results were obtained on an Intel Core i5-8250U @3.4 GHz running Ubuntu. We had ms and ms where the computational peak was during take-off. Throughout the tracking phase, the computational workload offered a reasonable safety margin within the sampling time. These results show the efficiency of the proposed scheme.
In this paper, we presented the design and implementation of a novel position controller based on nonlinear model predictive control for quadrotors. The control architecture incorporates a predictor as a delay compensator for granting a delay-free model in the NMPC formulation, which in turn enforces bounds on the actuators. To validate our architecture, we implemented it on the Crazyflie 2.1 nano-quadrotor. The experiments demonstrate that the efficient RTI-based scheme, exploiting the full nonlinear model, achieves a high-accuracy tracking performance and is fast enough for real-time deployment.