An Automatic Self-Tuning Control System Design for an Inverted Pendulum

A control problem of an inverted pendulum in the presence of parametric uncertainty has been investigated in this paper. In particular, synthesis and implementation of an automatic self–tuning regulator for a real inverted pendulum have been given. The main cores of the control system are a swing–up control method and a stabilisation regulator. The first one is based on the energy of an inverted pendulum, whereas the second one uses the linear–quadratic regulator (LQR). Because not all of the inverted pendulum parameter values are exactly known an automatic self–tuning mechanism for designed control system has been proposed. It bases on a devised procedure for identifying parameters. The entire derived control system enables effective a pendulum swing–up and its stabilisation at an upper position. The performance of the proposed control system has been validated by simulation in Matlab/Simulink environment with the use of the inverted pendulum model as well as through experimental works using the constructed inverted pendulum on a cart.


I. INTRODUCTION
An inverted pendulum (IP) is one of the widespread benchmarks of a non-linear, unstable and under-actuated (more degrees of freedom than the number of control inputs) mechanical dynamic systems. From the construction point of view, two main structures of an IP can be distinguished, i.e., linear and rotational. Moreover, each of them may consist of multiple arms connected in such a way as to allow rotational motion between them, e.g., [1]- [4]. In general, solving a problem of an IP control is one of the basic issues in control engineering and robotics. Therefore, it is often used as an application, e.g., for the design of control and/or estimation purposes. The balancing robots and vehicles, e.g., [5]- [10], humanoid robots, e.g., [11], [12] and oscillators synchronisation, e.g., [13], [14] can be pointed out as the most common applications. It is worth adding that there are several types of ready-to-use, commercial, demonstrationeducational assets available on the market that enable the study of inverted pendulum behaviour in general. As examples of these applications the Pendulum and Cart Control System from Inteco [15], the Ball with Pendulum The associate editor coordinating the review of this manuscript and approving it for publication was Guangdeng Zong .
Suspension from Feedback Instruments [16] or the Rotary Inverted Pendulum from Quanser [17] can be indicated.
The main aim of an IP control is stabilisation at a given equilibrium point and usually bringing a pendulum to the neighbourhood of this point. Due to the non-linear dynamics of an IP, it is well-known that this system holds more than one equilibrium point. There are two physical equilibrium points among which the only one stable (a bottom position) is in the case of an IP. In connection with the aftermentioned control goal, an upper equilibrium point (an upper position) is considered primarily. Hence, to solve a control task, an appropriate control system is necessary. The common feature of most of these systems is the need to have a utility mathematical model at the synthesis stage. This model is usually based on a cognitive model that predicts the real behaviour of the object. There are three main approaches to an IP modelling, i.e., via Newton's laws of motion, Euler-Lagrange equation or Kane's method [18]. The first methodology has been used in the manuscript. Note that the model is only the end of an accurate representation of reality. Hence, uncertainty modelling is necessary. A typical approach is to stand out structural and parametric uncertainty. The structural uncertainty usually includes model simplifications, whereas parametric uncertainty holds inaccurate knowledge of the values of given parameters. In the paper, since a structure of the cognitive white-box model of an IP is considered as a good representation of reality, it is assumed that structural uncertainty may be neglected. In turn, parametric uncertainty has been modelled using one of the popular ways. Clearly, a set-membership approach has been applied [19]. In this approach, the uncertainty is described as an additive bounded error where just only bounds are known. The bounded models of uncertainty need less a priori information about the system and they are less demanding than, e.g., probabilistic models.
In general, it is possible to distinguish two main control strategies of an IP. Structurally, the first approach is based on a single regulator, which is able to realise both the swingup and stabilisation phases. The different kinds of control systems of this type can be found in literature, e.g., using sliding mode control method [1], [20]- [22], by considering the passivity properties of an IP [23] or using methods from the family of computational intelligence. In the latter group, it is worth pointing out swarm algorithms, e.g., [1], fuzzy control strategy also with stochastic sensor faults, e.g., [24]- [26] and artificial neural networks, e.g., [27]. Whereas the second approach consists of separate controllers ensuring a pendulum swing-up and its stabilisation at the upper position, respectively. The whole control system is completed by a proper switching condition between regulators. One of the most widespread approaches to the swing-up control method (a swing-up mechanism) based on the energy balance of an IP [28]. In turn, a stabilisation problem can be solved using a feedback control system. Two main methodologies can be pointed out in this issue. The first one is based on various configurations of PID controllers [29]- [33]. The latter one utilises state feedback where state feedback gains are designed using i.a. pole placement method [34] or optimisation tools leading to, e.g., the linear-quadratic regulator (LQR) [30], [32], [34], [35]. In the further part of the paper, a control system consisting of the LQR and swing-up mechanism is considered.
As it has been mentioned above, it is assumed that certain parameters are not exactly known. Hence, a designed control system has to be able to deal with it. On the background of control theory, various techniques for such operation can be identified. At the same time, it seems that the most popular are robust control systems or control systems with adjustment of parameter values. The main difference between them is that the former are designed for the worst-case scenario and the latter are able to adapt to current circumstances. This paper focuses on the latter ones, which are known as adaptive control systems. In general, four types of adaptive control systems can be distinguished, that is, gain scheduling, selftuning regulators, model-reference adaptive control and dual control [36]. It is worth adding that different kinds of adaptive control have been utilised for an IP control purposes, e.g., adaptive sliding mode controller has been designed in [1], adaptive control using reference model can be found in [37] and self-tuning LQR has been proposed in [38].
The aim of this work is to provide an alternative selftuning LQR method. Clearly, an adaptation of the control system by adjusting the LQR gains bases on improving the IP model which is devised in the paper. Whereas, the approach to adjust the LQR gains using modified weighted matrices can be found in [38]. The proposed approach is based on a devised procedure for identifying parameters. More specifically, for one of the IP parameters (mass placed at the end of the arm), only value bounds are available. This mass is found by means of identification and used for correcting the IP model parameters. As a consequence, the LQR gains are adjusted to the new circumstances. The entire control system also includes proposed swing-up mechanism and switching condition. Therefore, the influence of parametric uncertainty on these elements is also discussed. As it has been mentioned in the second paragraph, for control system synthesis purposes, a proper IP mathematical model has been prepared. This model is a linear state-space model and is based on a derived, via Newton's laws of motion, cognitive model. The performance of the proposed control system has been validated in simulation and experimental way. In other words, the cognitive model of IP and the entire control system have been implemented in Matlab/Simulink environment. Moreover, the devised solution has been implemented in a real (constructed) IP. Hence, in the paper the abovementioned ready-to-use device have not been used. The main motivation for such approach has been the fact that, when purchasing such equipment, the user receives a ready-to-run workstation together with software and documentation, thus it makes that it is impossible using the acquired engineering knowledge to construct a plant on one's own and involves a certain financial outlay. Moreover, despite the fact that the software provided usually allows for the implementation of a control system other than that delivered by the producer, e.g., [39], it usually requires some adaptation, in terms of programming tools, code design, etc., to the standards used by the producer. In addition, it is usually licensed software, which typically involves additional implementation costs. And although numerical analyses carried out in this paper are based on license software (Matlab/Simulink), they could also be carried out using open source environments, e.g., Python. Nevertheless, the presented work does not aim at replacing the specified commercial solutions, but may constitute their extension (in the sense of using the proposed algorithms) or alternative, if the user is interested in independent construction of the plant. Moreover, it is worth noting that for simulation and experimental research it has been necessary to propose an approach to the issues of measuring devices and actuators. Summarised, the main contributions of this paper are as follows: 1) an adaptation of the control system by adjusting the LQR gains bases on improving the IP model which allows to take into account parametric uncertainty is proposed, VOLUME 8, 2020 2) an appropriate IP model parameters identification procedure for the control system adaptation purposes is devised, 3) the IP workstation has been constructed for experimental research of the proposed control system. The paper is organised as follows. The problem formulation and main assumptions are presented in section II. Section III includes the derivation of the IP mathematical models. Next, the synthesis of an automatic self-tuning control system is given in section IV. The simulation and experimental results are described in section V. The paper is concluded in section VI.

II. PROBLEM STATEMENT
The IP is taken under consideration as depicted in Fig. 1. It consists of several elements. First, the 'cart' constitutes the basis of the IP. It is mounted on the second element, namely the gantry. In particular, the gantry is composed of two 'guide shafts'. The third element is the 'arm' which is mounted on the 'cart' at a mounting point called the joint. The 'arm' consists of two elements, i.e., the 'rod' and the 'mass'. The 'mass' is mounted on the unlinked end of the 'rod'. The IP is fastened on a supporting element, so-called the 'base'.
Taking R n to denote the n-dimensional vector space over a real number field R and notably a co-domain of realvalued vector functions such as: T → R n , where T is an open set in R, with usual addition and scalar multiplication satisfies a suitable set of axioms and given symbolically by + and ·, respectively and × as a Cartesian product. Moreover, consider Z + to signify a positive part of an integer field, a set n x , n u , n z , n y ⊂ Z + and the quadruple (x, u, z, y), for which ∀t ∈ T the following holds: the dynamics (M IP ) of the IP yields: where: X x ×X u ×X z ×X y ⊂ R n x ×R n u ×R n z ×R n y denotes a region in the state, control and disturbance inputs and outputs spaces combined, respectively; X v ∼ = X x represents the velocity space. Additionally, for clarity of presentation, one must consider (X , Y , Z ) ≡ R 3 to span a three dimensional Euclidean space in which the IP operates. In general, the goal of this work is to construct an appropriate control system of the IP. Hence, the control objectives of the IP are the IP swing-up and its stabilisation at the upper position. As it has been mentioned above, the proposed control system is composed of two main cores, i.e., a swingup mechanism and a stabilisation regulator, which are completed by a switching condition. Moreover, in order to cope with parametric uncertainty, the self-tuning method is used. Hence, the automatic self-tuning controller (M ASTC ) yields: where: X y m ≡ X y and X u * ≡ X u (with respect to assumption 6). The control aim is achieved by closing a loop using measuring information: and actuation by: As a result in obtaining a M IPCS given by: where • is a function composition operator. The described setup is illustrated in Fig. 2. For the control system design purposes, the following assumptions are formulated.

Assumption 1: The IP is composed of interconnected rigid body elements.
It causes that the geometry of the IP elements does not distort during operation under considered mass.
Assumption 2: Movement is considered in two dimensions.
The movement is constrained to the (X , Y ) plane by the IP geometry which disallows the rotation around the X and Y axes. In other words, the connection restricts the movement of the cart only to the linear motion in X direction and the arm only to the rotary motion in respect to the Z -axis, respectively.
Assumption 3: Friction effects are considered only between the cart and the guide shafts.
By assumption, the other components of friction such as effects in the IP joint can be neglected. Moreover, the friction force is modelled as linearly dependent on the velocity of the cart.
Assumption 4: The IP parameters are not exactly known.
It is considered that the mass placed at the end of the arm is not perfectly known. However, by using a set-membership approach to uncertainty modelling, the value bounds are available. Hence: where m cd , m cg denote the lower and upper bound of the mass, respectively. Assumption 5: The disturbance input (the uncontrolled external force) is considered to be external force applied to the mass. Assumption 6: X y m ≡ X y and X u * ≡ X u . By assumption, the measurement errors are negligible and the control input is delivered by dedicated actuator system.

III. MODELLING AND IDENTIFICATION
As it has been mentioned above, for control system synthesis purposes, the IP model is necessary. In this work, this model is derived using the IP cognitive model M IP (see Fig. 2). Therefore, in this section firstly M IP is devised.
As it can be noticed in Fig. 1, the movement of the cart is characterised by linear displacement s(t) and velocityṡ(t). In turn, the rotary motion of the arm is determined by angular displacement θ(t) and angular velocityθ(t). In order to set the IP in motion an external input force F(t) is applied to the cart. This force constitutes the control input u(t), thus ∀t ∈ T: u(t) ∈ X u . Moreover, by assumption 5 the surrounding environment may affect the IP through uncontrolled external force ∀t ∈ T: Z (t) ∈ X z . Additionally, according to assumption 3 the friction between the cart and the guide shafts is marked by T (t). Utilising this characterisation allows one to define the overall IP state (position and velocity) and control and disturbance inputs vectors as: where:( ·) denotes the derivative with respect to t and ∀t ∈ T: The main parameters of the IP are marked in Figs. 1 and 3. These are: m w , m p , m c -masses of: the cart, rod and mass (static load), respectively; l, l p , l c -distances from the beginning of the arm to centres of gravity of: the arm, rod and mass, respectively. Moreover, the following quantities are shown in Fig. 3: F x (t) and F y (t) are the interaction forces between the particular elements of the IP in the X -axis and Y -axis, respectively; g stands for the gravitational acceleration and x g (t), y g (t) denotes coordinates of the centre of gravity of the arm.

A. COGNITIVE MODEL OF IP
In order to derive a cognitive mathematical model of the IP, the movement of its two main parts, i.e., the cart and the arm is considered separately (with respect to assumption 1), as it is shown in Fig 3. By applying Newton's second law to the linear displacement of the cart (see Fig. 3a) yields: where( ·) denotes the second derivative with respect to t. Again, by applying Newton's second law to describe the movement of the arm (see Fig. 3b) in the X and Y axes (which justifies assumption 2), the following equations can be written: where: m r is the mass of the arm; x g (t), y g (t) are the time varying coordinates of the centre of gravity of the arm, defined as: In turn, by applying Newton's second law to the angular displacement of the arm (see Fig. 3b) holds: (14) where I denotes the moment of inertia for the arm (relative to the axis of rotation passing through the centre of gravity of the arm) given by: According to assumption 3, friction force between the cart and the guide shafts can be written as follows: where k T is the friction coefficient between the cart and the guide shafts.
Combining (12) with (10), then inserting the obtained result and (16) into (9), the following holds: (17) Similarly, combining (12) with (10) and (13) with (11), then inserting the obtained result into (14) reads: The equations (17) and (18) constitute the cognitive mathematical model of the IP. As it can be noticed, this model is in the differential-algebraic equations (DAE) format. It is well-known that the DAE problem is numerically or algorithmically more demanding then, e.g., ordinary differential equations (ODE) problem. Therefore, by appropriate substitutions, the DAE IP model is transformed into the following ODE format: Finally, M IP is obtained by rewriting (19) and (20) in the state-space form by using (8).

1) MODEL PARAMETERS IDENTIFICATION
There are several parameters in the derived IP model (19) and (20). In this paper, they are divided into two groups. The first group includes masses, i.e., m w , m r , m p , m c and distances, i.e., l, l p , l c . Their values can be directly obtained by measurements. Typically, measurements are burdened by measurement errors, however, according to assumption 4 the uncertainty in m c is dominant. Therefore, other uncertainties can be neglected. The second group consists of the friction coefficient k T . In order to identify its value, the following experiment, using the constructed IP (with the dedicated actuators system), has been carried out. For experiment purposes, the arm has been detached, thus the interaction forces (see Fig. 3a) had no influence on its results. The cart velocityṡ(t) controller has been implemented, based on a PI algorithm, and a constant reference velocity has been set. Once the system has been in the steady-state, the values of velocityṡ(t) and motor current i(t) have been registered. Next, using (26) and (28) (see section III-C), the gathered motor current values have been converted into external input force F(t) applying to the cart. The obtained results are shown in Fig 4. It is worth noting that the only forces acting on the cart during this experiment are T (t) and F(t) (see Fig. 3a). Because the cart velocity is constant, the value of obtained F(t) is equal to T (t). Taking the average value of trajectories (red lines in Fig. 4), using (16), the friction coefficient k T can be calculated as follows: It is worth adding that the above-described procedure is not perfect in the identification theory, but it is sufficient for further considerations.

B. MODEL OF IP FOR CONTROL DESIGN PURPOSES
As it has been mentioned above, in order to stabilise the IP at the upper position (upper equilibrium point) S 0 the LQR is used. Hence, the necessity of deriving the linear state-space model has appeared. Therefore, the Taylor series expansion (neglecting the second and higher-order terms) has been used to obtain the linear approximation of (19) and (20) around S 0 . This approximation can be written as follows: where: 0, 0, 0, 0, 0, 0, 0).
Finally, the linear state-space model of IP (M SS IP ) for control design purposes is obtained by rewriting (22) and (23) by using (8) as: where: It is worth adding that the denominator (m w I + m r l 2 + m r I ), from its nature, is always positive.

C. MODELLING OF ACTUATOR SYSTEM
According to Fig. 2, the signal u * (t) generated by the automatic self-tuning controller (M ASTC ) is realised by the dedicated actuator system (M A ). The block diagram of M A is shown in Fig. 5. This system is composed of: a DC motor, gearbox, PI controller, motor current sensor and scaling factor.

DC motor
In order to derive the DC motor model, the following assumptions are made: the DC motor shaft is inert and a lack of friction forces associated with its movement, the losses in the magnetic circuit can be neglected, the back EMF is not taken under consideration (due to zero motor angular velocity in the neighbourhood of the upper position of the IP). Hence, the considered model of the DC motor is as follows: where: U z (t), M N (t), R, L, k m denote supply voltage, torque, winding resistance, winding inductance and torque constant, respectively. In order to identify the model (25) and (26) parameters, the following experiments have been made.
Identification of R and L Assuming zero initial conditions of (25), by using the Laplace transform, the transfer function of (25) yields: where: k = 1 R is the DC gain; τ = L R denotes the time constant.
The values of k and τ have been determined in the following identification experiment using the constructed IP. The step change of U z (t) has been applied to the DC motor and the motor current i(t) has been registered. The experimental results are shown in Fig. 6. As it can be noticed in Fig. 6 the trajectory of U z (t) is not perfect, but it is sufficient for this experiment purposes. The values of k and τ have been identified based on the step responsei(t). Finally, the values of R and L can be determined.
Identification of k m The k m value has been determined in the following identification experiment way using the constructed IP. By using the spring dynamometer, when the motor shaft has been stopped, for several values of i(t) the values of F(t) have been measured. Next, the obtained results of F(t) have been converted into torque M N (t) values by: where r p is the pulley radius. Then, the data (Fig. 7) have been used to estimate the k m value by utilising least squares method to fit (26) model.

Gearbox
The second element of the actuator system is the gearbox (see Fig. 5). The main task of this element is to convert M N (t) into F(t) according to (28).

PI controller
In order to control the DC motor, the PI controller is used. The transfer function of this controller can be written as follows: where K P , K I are the coefficients for the proportional and integral terms, respectively. This regulator generates signal U z (t) based on continuously calculated error value e i (t) as the difference between the reference value i ref (t) and the measurements i m (t) of i(t). The tuning of the PI controller has been performed experimentally with the use of the constructed IP.
Motor current sensor The feedback information for the PI controller (i m (t)) is provided by the Hall effect motor current sensor. It is worth adding that the dynamics of this sensor can be neglected, therefore, its transfer function can be approximated by a static element with a gain equal to 1.

Scaling factor
The reference value (i ref (t)) for the PI controller results from an appropriate scaling of the signal generated by the controller (u * (t)). In order to realise u * (t) of satisfactory quality, the DC gain of the actuator system should be equal to 1. Hence, it can be written: where: K u * −I ref is the scaling factor; K I ref −F denotes the gain in the track i ref (t) -F(t) given by: The gain can be determined by: whereas the gain K I−F in the track i(t) -F(t) can be written as: Combining (30) -(33), the scaling factor yields: Hence, M A has been now completed.

D. MODELLING OF MEASURING DEVICES
According to Fig. 2, the measurements to the automatic self-tuning controller (M ASTC ) are delivered by measuring devices (M MD ). The block diagram of M MD is shown in Fig. 8. In this work, two optical rotary encoders are used to deliver the measurements for the control system purposes. One of them is coupled with the DC motor shaft, whereas the second one is mounted to joint. The motor shaft position is related to the cart position s(t) by pulley radius r p . It should be noted that only two of state variables, i.e., s(t) and θ(t) can be measured directly using encoders. Thus, in order to obtain values of other variables -ṡ(t) andθ(t), a derivative approximation algorithm (DAA) is used. It is worth adding that the dynamics of the measuring devices in comparison with the dynamics of the IP is negligible. Additionally, it is assumed that the DC gain of the measuring devices is equal to 1. Therefore, the M MD model can be approximated by a static element with a gain equal to 1 during synthesise the control system. Moreover, according to assumption 4 the measurement errors can be neglected. Hence, assumption 6 is justified and M MD has been now completed.

IV. SYNTHESIS OF THE CONTROL SYSTEM
In general, the proposed control system of the IP (M IPCS ) (see Fig. 2), in accordance with (6), still needs to be designed, based on the models M IP , M A and M MD , M ASTC . The automatic self-tuning controller (M ASTC ) includes the swing-up mechanism and the stabilisation regulator, which are completed by the switching condition. Moreover, in order to cope with parametric uncertainty (with respect to assumption 4), the self-tuning method has to be designed.

A. SWING-UP MECHANISM
For swinging up a pendulum, a mechanism based on the energy balance of the IP is used. Therefore, the swing-up control law can be written as [28]: (35) where: u SU (t) denotes the external input force which is being applied to the cart during swing-up phase, hence during this phase u * (t) ≡ u SU (t); sat F SU [·] stands for the linear function with symmetric saturates F SU ; F SU is the limit value of F(t); k SU signifies the swing-up mechanism parameter; sgn(·) is the signum function; E(t), E 0 denote the energy and energy at the upper position of the IP, respectively given by: The value of u SU (t) is based on the difference between the current and reference energy value, which is multiplied by k SU . Moreover, (35) contains the condition that the force applied to the cart is changed so that its value always increases the energy of the IP. Additionally, the maximum value of u SU (t) is constrained by F SU , to take into account the limited length of the guide shafts.
In order to determine the values of k SU and F SU , taking into account m c = 0.1 kg, a simulations series in Matlab/Simulink environment as well as an experiments on the constructed IP have been performed. The final results are as follows:

B. LQR DESIGN
In order to stabilise the IP at the upper position, a regulator based on the state feedback is designed. According to (24) the whole state vector is available. Therefore, the stabilisation control law can be written as [40]: where: u FB (t) denotes the external input force which is being applied to the cart during stabilisation phase, hence during this phase u * (t) ≡ u FB (t); K ∈ R 1×4 is the matrix of state feedback gains. The u FB (t) ensures the stability of internal dynamics of the IP. Thus, by choosing the values of feedback gains the desired pole placement of closed-loop system is achieved. Hence, the internal dynamics is asymptotically stable, which can be written as: where · 2 denote the Euclidean norm. It is well-known that pole placement is a viable design technique only for a system that is controllable. It is easy to show that the pair (A, B) of (24) is controllable. As it has been mentioned above, in this work the state feedback gains are designed by solving a linear-quadratic optimisation task. This approach -LQR is one of the most common in optimal control. In the approach, for the considered linear state-space model of IP (M SS IP ) the minimised objective function yields: where: J (·) is the objective function; Q ∈ R 4×4 , R ∈ R denote diagonal semi-positive and positive definite matrices, respectively. The solution to the LQR problem is given by (40) with: where P ∈ R 4×4 is a positive definite, symmetric matrix that satisfies the following algebraic Riccati equation [40]: The values of particular elements of Q and R determine the IP behaviour and control quality. In order to determine the values of Q and R and as a consequence K, taking into account the parameter values from the table 1, a simulations series have been performed in Matlab/Simulink environment (using the lqr command). The final results are as follows: Only one of the two control laws presented above, i.e., u SU (t) or u FB (t) is proper for the given IP state. In this work, the choice between u SU (t) and u FB (t) is based only on the value of the IP current angular displacement (θ(t)). Hence, the switching condition can be written as: where θ S denotes the threshold value, which has been determined in simulation and experimental way as:

D. AUTOMATIC SELF-TUNING CONTROL SYSTEM
According to assumption 4, the only bounds on the mass placed at the end of the arm are known. Hence, to adapt to the control system, improving the IP model (M SS IP ) is used. Three main stages of this mechanism can be distinguished: the mass identification, IP parameter estimation and regulators self-tuning.

1) MASS IDENTIFICATION
It is assumed that m c satisfies (which justifies assumption 4): The identification of the current value of m c is based on a result of an experiment performed on the constructed IP automatically each time system is powered. Nevertheless, before experiments, the identification procedure had been implemented in Matlab/Simulink environment. This procedure is as follows. For the IP at the bottom position the constant force F(t) has been applied to the cart and θ(t) has been registered. This force causes the cart to accelerate and the arm to swing. Maximum value of the first fluctuation -θ M is varied for different values of m c which is illustrated in Fig. 9. The trajectories of θ(t) for the three exemplary values of m c with marked θ M are shown in Fig. 9. Moreover, the program implemented in the Matlab/Simulink environment allows to generate trajectories from Fig. 9 is available under [41].
By performing a series of similar simulations (the loop with a step of 0.01 kg), enough data have been gathered to obtain the trajectory of θ M which is shown by a blue line in Fig. 10. This allows determining the value of m c based on known value of θ M . The program implemented in the Matlab/Simulink environment allows to generate trajectories from Fig. 10 is available under [41].
The identification procedure presented above has been repeated in the constructed IP. The trajectory of θ M is presented in Fig. 10 by a yellow line. As it can be noticed, the trajectory is only slightly different from that obtained  from the simulation. This confirms the good quality of the devised model of the IP. It should be added that the nonsmooth shape of the yellow trajectory is due to that during the experiments fewer different m c values have been used (represented by the red circles) and linear interpolation has been performed between them. Moreover, this identification procedure is sufficiently accurate for regulator self-tuning purposes.

2) IP PARAMETER ESTIMATION
For value of m c determined in identification way, the values of the other IP parameters can be found. There are three parameters depend on m c , i.e., m r , l and I . The first one can be calculated as follows: By applying a balance of the torque of the gravity forces acting on the centre of gravity of the rod and the mass and assuming that the rod has a constant cross-sectional area along its whole length and the mass is always placed at its end, i.e., l c = 2 l p (see Fig. 3b), the second parameter can be calculated as: In turn, the value of I can be estimated using (15).

3) REGULATORS SELF-TUNING
It is obvious that the change of m c and as a consequence m r , l and I has an impact on the IP dynamics. Therefore, the values of particular elements of A and B have to be updated. It should be added that the values of Q and R are constant in  Fig. 11. The program implemented in the Matlab/Simulink environment allows to generate trajectories from Fig. 11 is available under [41]. Moreover, according to (36) and (37) the new values of energies are calculated which are used in the swing-up control law. Nevertheless, the values of k SU and F SU remain unchanged. Hence, the switching condition is also unchanged.

V. SIMULATION AND EXPERIMENTAL RESULTS
The M IPCS system with the set of parameters which is shown in table 1 has been implemented and validated in Matlab/Simulink environment. Clearly, the IP cognitive model (19) and (  subsection III-C. It should be noted that PI controller has been also implemented with the K P and K I values adjusted (see table 1). Furthermore, the measuring devices (M MD ) according to Fig. 8 have been also implemented. Finally, in order to complete M IPCS , the automatic self-tuning controller (M ASTC ) in accordance with the considerations set out in section IV has been implemented in Matlab/Simulink environment.   s(t) and θ(t) have been registered (see Fig. 12). As it can be noticed, the force applied to the cart causes its linear displacement, which also involves angular displacement of the arm. After about 10 seconds the IP has been stabilised at the upper position (S 0 ). This naturally results in F(t) reaching zero. The program implemented in the Matlab/Simulink environment, containing detailed simulation conditions, allows to generate trajectories from Fig. 12 is available under [41].
The next simulation experiment shows the performance of the stabilisation regulator in the presence of disturbances (see Fig. 13). For simulation purposes, zero initial conditions have been assumed. During the simulation, as a result of the action Z (t) (external force applied to the mass), the IP has been perturbed from the upper position. The Z (t) trajectory as well as the registered trajectories of F(t), s(t) and θ(t) are shown in Fig. 13, whereas an associated program implemented in the Matlab/Simulink environment is available under [41]. As it can be noticed, the LQR rejects the disturbances and consequently stabilises the IP at the upper position. Thus, taking into account the results presented in Figs. 12 and 13, it can be claimed that the stabilisation performance is satisfactory.
In turn, the performance of the entire control system has been validated by the following simulation experiment, the results of which are shown in Fig. 14 and an associated program implemented in the Matlab/Simulink environment is available under [41]. Starting the IP from the bottom position the swing-up mechanism has been activated. Thus, at the first moment of the simulation, the given value of F(t), i.e., F SU has been applied to the cart. The cart accelerates and swings the arm. The angular acceleration (θ(t)) of the arm or the sign of cos θ(t) is changed successively, which according to (35) causes the control input sign to change and the energy to increase further. At about t = 4 s, the arm reaches the appropriate angular position (θ(t)) to meet the switching condition (48). The control law changes, i.e., from u SU (t) to u FB (t) and the LQR stabilises the IP at the upper position. Hence, the performance of the designed control system is satisfactory. The devised control system has been implemented in the constructed IP. The following main components have been used to build the IP. The two hardened guide shafts with a length of 1 m and a diameter of 0.016 m constitute the gantry. The arm has been composed of the aluminium rod with length l c , width 0.025 m and thickness 0.003 m and the varied mass (m c ). The cart frame has been made of 0.008 m thick plywood. The entire mechanical structure has been complemented by brackets, bearings, limit switches, etc. Documentation of the main elements of mechanical construction is available under [42]. As the measuring devices two encoders of types OMRON E6B2-C (for θ(t) measurements) and LPD3806 (for s(t) measurements) have been used. The KAG M48x25/l-type motor has been selected as the DC motor. The STM32F302R8 microcontroller has been used as the computing unit. The power supply is composed of two transformers with secondary voltages of 9 V (for the logic devices) and 26 V (for the DC motor). The whole electrical/ electronic structure has been complemented by rectifiers, voltage stabilisers, H-bridge, a sensor of motor current type ACS712, etc. Documentation of the electronic circuits design is available under [43]. The three-dimensional drawing of  constructed IP is available under [42], whereas a photo of the constructed IP is presented in Fig. 15.
The control laws (35) and (40) in discrete time with the switching condition (48) and the automatic self-tuning mechanism have been implemented in the microcontroller. The main program for the STM32F302R8 microcontroller is available under [44]. As it has been mentioned in section IV-D.3, the trajectories of the state feedback gains (see Fig. 11) have been saved in the microcontroller memory in order to update the LQR gains depending on the identified value of m c . The trajectories of s(t) and θ(t) obtained during the example work experiment are shown in Fig. 16. As it can be noticed, after about 3 seconds the IP has been stabilised at the upper position. Naturally, the main advantage of the designed control system is its adaptation to an unknown value of m c . Concluding, a video of this experiment, illustrating the operation of the devised control system in the presence of the unknown m c , is available under [45].

VI. CONCLUSIONS
In this paper, the control problem of the classical inverted pendulum on the cart in the presence of parametric uncertainty has been investigated. The designed control system includes the stabilisation regulator (LQR), the swing-up control method, the switching condition and the automatic self-tuning mechanism. This mechanism is based on the devised procedure for identifying the IP parameters. Moreover, the IP modelling and identification issues and the approach to take into account the actuator system and measuring devices have been widely discussed. The performance of the proposed control system has been successfully demonstrated in the simulation way as well as in the experimental setting on the constructed IP. Hence, in general, extensive knowledge about the IP control problem has been aggregated in this paper. It can be found interesting and useful for the relevant community, both in research and engineering applications.