Design of a Dynamic Simulator for a Biped Robot

Simulation is a virtual representation of a dynamic system. For the case of mechanical systems, the simulator is used to calculate the reaction forces between its base and the ground and other constraints. The intermittent nature of these forces and the mathematical inequalities that they must satisfy lead to models described by hybrid algebraic differential equations. In this paper, a simulator was developed for a seven degrees of freedom planar biped robot, which was modeled using the Euler-Lagrange formulation. This model allowed the design and implementation of a control strategy for balance management, and the monitoring of articular reference paths are tested in the simulator before proceeding to implementation on the actual prototype.


Introduction
Thanks to the evolution of computers during the last decades, simulation has become a tool indispensable for synthesizing control laws in mechanical systems. This tool allows to verify the behavior of a control law on a virtual model of the mechanical system known as a simulation model. It is important to highlight that this model allows to validate the hypotheses made during the controller synthesis phase. For this reason, the behavior described by the simulation model should be as close as possible to the actual behavior of the mechanical system. Bipedal robots are mechanical systems that are difficult to model and simulate due to impacts and intermittent contact between the foot and the ground. From the modeling point of view, the dynamics of a bipedal robot cannot be described in the same way as a classic mechanical system like a manipulator robot. Biped modeling requires the use of hybrid algebraic differential equations [1][2][3].
Bipedal robots have been studied from different approaches, like chaotic nonlinear systems [4][5][6][7]. A biped robot is modeled considering the effects of the impulses generated at the end of each step as a result of the impact of the free leg with the ground and the exchange of legs to start a new cycle; in these works, the closed-loop stability is tested via Poincare maps [8,9]. With regard to the generation of gait trajectories, studies have been developed such as the trajectories obtained from human gait [10,11].
The simulation model of a bipedal robot includes the calculation of the vector of generalized accelerations and the calculation of the vector of reaction forces between the foot and the ground. These forces, unlike generalized accelerations, must satisfy unilateral constraints described by mathematical inequalities: (i) normal reaction forces must be nonnegative. That means they prevent the foot from going through the ground but cannot prevent it from losing contact; (ii) the magnitude of the tangential components of the reaction forces must be less than or equal to the product between the coefficient of friction and the normal force. The simulation model of a biped is then constituted by a set of differential equations where a part of the unknowns must satisfy certain mathematical inequalities.
The main difficulty in simulating a bipedal robot comes from calculating the reaction forces between the foot and the ground. To calculate these forces, two types of methods have been proposed: the constraint methods [12] and the penalty methods [13]. For the first case, the reaction forces are determined by solving a special type of optimization problem known as a complementarity problem [14]. In the second case, models of springs and dampers are used in order to minimize the interpenetration of the bodies in contact. Both cases are based on the hypothesis of a perfectly rigid floor and nondeformable feet.
In the case of the constraint methods, there are two approaches. One called the acceleration-force approach and the other called the velocity-impulse approach. In the first case, the complementarity problem is formulated by expressing the accelerations of the contact points of the foot as a function of the reaction forces generated by the ground. For the second case, the direct dynamic model of the system is discretized using the Euler method. This makes it possible to transform the system of differential equations that governs the dynamics of the robot into a system of difference equations. Here, the complementarity problem is formulated by writing the velocities of the contact points in the instant t k+1 as a function of the product between the reaction forces in t k and the discretization period ðh ≜ t k+1 − t k Þ.
The velocity-impulse approach, also known as timestepping, was proposed in [15]. However, the most widespread formulation was presented in [16]. In the latter, the mathematical treatment is simpler; instead of differential inclusions, the problem is described using difference equations. In [16], the discretization of the dynamics of the system, it is carried out using Euler's semi-implicit method. In [17], discretization is done using Euler's implicit method. This modification allows to increase computational efficiency when simulating mechanical systems involving springs and dampers. As in the accelerationforce approach, in the time-stepping approach, the friction cone is approached by a polyhedron in order to get a linear problem of complementarity. In the time-stepping approach, the same complementarity formulation is employed to describe contacts and impacts regardless of whether the friction is static or dynamic. In this approach, the discretization of the dynamic model means that the simulations must be carried out with first-order fixedstep methods such as explicit and semi-implicit Euler. Nonadaptive, fixed-step simulation ensures that the accumulation of impact phenomenon cannot take place. First-order fixed-step methods, however, may require prohibitively small step sizes in order to ensure the numerical stability of the solution obtained. This, of course, may imply long simulation times. On the other hand, to compensate this deficiency, the linear problem can be solved with a low computational algorithm known as Lemke's algorithm [18]. Current technologies for robotics simulation had their origin in the video games industry [19]. In this type of application, the computational cost plays a very important role; the avatars do not need to calculate the zero moment point (ZMP), to walk without falling. However, for bipedal robots, the simulation tools must calculate the reaction forces between the ground and the robot's feet, in order to avoid the interpenetration of the feet with the ground and to evaluate the sliding conditions. These conditions make it necessary to include physical motors that respect the laws of physics, in each simulation step at a low computational cost. Under this premise, different simulation engines have been developed, such as ODE (Open Dynamics Engine), PhysX, Bullet, and MuJoCo. The most popular robotics simulation tools, [19] are Gazebo, V-Rep, Webots, and OpenHRP. Table 1 groups the different characteristics of the simulation tools.
Features such as open-source, multiplatform, customization, and documentation are desirable in all simulators. However, these are not found in all simulation tools; for these reasons, it was decided to program a simulator that allows evaluating the dynamics of gait in a biped robot. The rest of the paper is structured as follows: Section 2 shows the dynamic model of the robot and the constraints; Section 3 shows the discrete model by means of Euler's method; Section 4 calculates the control law and the reference trajectories, followed by its numerical results in Section 5 and concluding the paper in Section 6.

Mathematical Model of the Robot
The robot's dynamic model is given by where q ∈ ℝ 9 is the vector of generalized coordinates. This vector contains three types of coordinates: (i) ðx 0 , y 0 Þ denoting the Cartesian position of the reference frame hx 0 , y 0 i in the reference frame hx g , y g i, (ii) q 0 denoting the orientation of the reference frame hx 0 , y 0 i with respect to the reference frame hx g , y g i, and (iii) ðq 1 ,⋯,q 6 Þ denoting the six joint positions depicted in Figure 1. This model was reported in [10].
Vectors _ q ∈ ℝ 9 and € q ∈ ℝ 9 are, respectively, the first and second derivatives of q with respect to time, see the following: A ∈ ℝ 9×9 is the robot's inertia matrix. H ∈ ℝ 9 is the vector of centrifugal, gravitational, and Coriolis forces. Matrix B ∈ ℝ 9×6 contains only ones and zeros. The first three rows of B are zero, indicating that Γ has no direct influence on the acceleration of the first three generalized coordinates. The following 6 rows of B form an identity matrix, indicating that Γ i ði = 1,⋯,6Þ directly affects € q i , Γ ∈ ℝ 6 is the vector of torques applied to the joints of the robot. This leads to B fulfilling the following property: J r ∈ ℝ 3×9 is the Jacobian matrix relating the linear and angular velocities of the robot's right foot in the reference 2 Modelling and Simulation in Engineering In Equation (4), ð g p˙r x , g p˙r y Þ are the linear velocities of the reference frame hx 3 , y 3 i with respect to hx g , y g i, whereas that _ θ r is the angular velocity of the reference frame hx 3 , y 3 i with respect to hx g , y g i. The matrix J l ∈ ℝ 3×9 is used to express the linear and angular velocities of the robot's left foot to the ground ( Figure 2).
In Equation (5), ð g p˙l x , g p˙l y Þ are the linear velocities of the reference frame hx 6 , y 6 i with respect to hx g , y g i, whereas that _ θ l is the angular velocity of the reference frame hx 6 , y 6 i with respect to hx g , y g i.
The unknowns of the previous model are € q ∈ ℝ 9 , F r ∈ ℝ 3 , and F l ∈ ℝ 3 . That is, there are 9 equations and 15 variables. The required additional equations can be obtained by using complementarity conditions related to normal and tangential foot reaction forces [20].

Complementarity Conditions
(1) Geometric Constraints. In order to prevent the foot from penetrating the ground during the simulation, the height of the vertices of the feet must be nonnegative (see Figure 2). To obtain these heights, the direct geometric model is used. This model calculates the Cartesian position of the vertices of the feet with respect to the absolute referent frame used on generalized positions defined by Equation (2).
The vector φ i ∈ ℝ 2 ði = 1 ⋯ 4Þ (Equation (6)) represents the Cartesian position of the vertex i along the axes x and y from the inertial reference frame. Hereinafter, the component along the axes x and y of φ i will be denoted as φ ni and φ ti , respectively. The vector with the heights of the vertices v 1 to v 4 will be denoted Φ n Equation (7). In order to ensure that the distances between the vertices of    Modelling and Simulation in Engineering the foot and the ground are nonnegative, Φ n must satisfy the following inequality: The equation above implies that all the 4 components of the vector Φ n ðqÞ must be nonnegative.
(2) Inequality Constraints (i) Normal reaction forces are positive when there is contact between a foot and the ground. Then, the reaction forces prevent interpenetration between the foot and the ground but cannot stick the foot to the ground (ii) Tangential reaction forces must be maintained in the friction cone to avoid slipping (μ is the Coulomb friction coefficient) The scalars f ni ∈ ℝ y f ti ∈ ℝ are the normal and tangential components of the reaction force between the contact point i and the ground. The values f ni y f ti are the components of F n y F t , as follows: (3) Equality Constraints (i) The two corners of the stance foot must remain in contact with the ground and do not move with respect to the global reference frame. These two constraints correspond to the equations: Vector H stan ∈ ℝ 2 contains the height of the two corners of the foot in contact with the ground, and J l _ q were the three-element vector with the linear and the angular velocity of the foot defined in Equation (5).
Vector P swn ∈ ℝ 12 contains the Cartesian position of the four corners of the swing foot, and P d swing is the desired path (ii) The gait must be symmetrical with respect to the right and left legs. Hence, the final configuration of the right leg must be equal to the initial configuration of the left leg. In the case of the robot of Figure 1, this constraint implies are complementary quantities. This means that if the height is greater than zero, the normal reaction force is zero. In the same way, if the reaction force is positive, so the height is zero. Hence, the dot product between the vectors F n and Φ n must be zero. This condition and nonnegativity conditions on F n and Φ n can be written compactly using the ⊥ symbol that indicates perpendicularity between vectors: Φ n is derived twice with respect to time to make its dependency on F n explicit. However, the condition Φ n ≥ 0 is equivalent to € Φ n ≥ 0 only for the contact points that satisfies φ ni = 0 and _ φ ni = 0. To obtain the equivalent of Equation (14) but involving € Φ n rather than Φ n , we define N c as the set of vertices of the foot satisfying the two aforementioned conditions, and € Φ ðcÞ n as the vector with the normal acceleration of the elements of N c . Similarly, F ðcÞ n is the vector with the normal reaction forces of N c . The previous considerations lead to Equation (15).
Next, the complementarity equations that arise from Coulomb's friction law are deduced.
Consider N s ⊆ N c , the set of vertices of the foot in contact with the ground and with nonzero relative speed with respect to it. For the contact point i, with i ∈ N s , the complementarity condition between the tangential velocity and reaction force is [21] μf Equation (16) ensures that (i) the tangential reaction force will be inside the friction cone  The solution of the complementarity problem gðxÞ ≥ 0, x ≥ 0, and gðxÞ T x = 0 is equivalent to solve Equation (17) [22,23]: where g i ðxÞ and x i are the elements of the vectors g(x) and x, respectively. The Fischer-Burmeister functional Equation (17) expresses the complementarity conditions Equations (15) and (16) as follows: The unknowns in Equation (18) are F r and F l . In summary, the robot's dynamic behavior is described by the differential equation (1) subject to the algebraic constraint Equation (18) like 2.2. Impact Model. Newton's law of restitution will be used to model the impacts. The application of this law leads to discontinuities in the normal speeds of the contact points and consequently in the vector _ q. To explain these discontinuities, the reaction forces must be assumed as Dirac distributions (the impulse forces generate impulse accelerations that when integrated produce discontinuous velocities). Around the instant of impact t = t I , the reaction force on the contact point i is assumed to equal to p ni δðt − t I Þ, being p ni the magnitude of the impulse and δðt − t I Þ the Dirac distribution. The normal speed of the contact i just after impact denoted _ φ + ni and the magnitude of the impulse p ni are subject to the following complementarity constraint (Equation (20)) [24]: where N I is the set of the vertices of the foot that impact the ground in the instant t I . For the formulation of the impact model, it was assumed that the robot runs on completely inelastic soil. Consequently, the coefficient of restitution is assumed to equal to zero.

Modelling and Simulation in Engineering
The tangential components of the reaction forces are also considered impulsive. The magnitude of these impulses, denoted p ti , is related to tangential velocities _ φ + t i , through q + v depends in turn on p ðcÞ n y de p ðcÞ t . To obtain this relationship, the direct dynamic model is integrated during an infinitesimal duration time interval around the moment of impact [25]. When making such integration is obtained like Being in this case, _ q + v is the unknown of the problem. If in the instant t = t I at least one of the vertices of the foot satisfies the conditions φ i = 0 and _ φ i < 0, then a collision with the ground will have occurred. In such a case, the new generalized velocities q v + are given by Equation (22).
The planar biped robot is described by Equations (19) and (22). These equations comprise different complementary formulations: one for impacts and the other two are applied depending on whether or not there is relative movement between the bodies in contact. The main drawback of this approach is the possible appearance of shock accumulation phenomena [26]. That is the occurrence of a succession of collisions whose separation interval progressively tends to zero. In the case of simulations carried out with numerical methods of variable size, this leads to the blocking of the simulation.

Discrete-Time Model Based on Euler's Symplectic Method
The time-stepping approach is based on discretized models of a dynamic system. This means that the differential equation that describes the direct dynamic model must be transformed into a difference equation. For this, it is necessary to approximate the derivatives of the vectors q and _ q using a first-order difference: q ðkÞ y _ q ðk+1Þ represents the values of vectors q and _ q in the instant t k : The constant h is defined as h ≜ t k+1 − t k . In Euler's symplectic method, _ q ðk+1Þ is calculated using q ðk+1Þ instead of q ðkÞ . In the time-stepping approach, the Signor-ini condition is replaced by the following complementary condition: N v denotes the set of vertices of the foot that satisfy the condition φ ni = 0. The condition Equation (24) is interpreted as follows: if φ ni is equal to zero, the normal reaction force calculated at the instant t k must produce a positive or zero speed at the instant t k+1 in order to avoid penetration of the foot into the ground. Equation (24) is valid for both, the calculation of contact and impact forces. Thus, the fundamental difference between the acceleration-force approach and the time-stepping approach is that in the latter, there is no explicit application of an impact model. Another difference between the acceleration-force approach and the time-stepping approach is that the same complementary formulation is used regardless of whether or not there is relative movement between the bodies in contact. The condition of complementarity between the tangential force in the instant t k and the tangential velocity in the instant t k+1 is The vectors F ðkÞ n y F ðkÞ t comprises the reaction forces of the points contacting with the ground. The contact forces of the vertices that do not belong to the set N v are equal to zero. If the calculation of the reaction forces is formulated as an algebraic equation and combined with the discretized direct dynamic model, we obtain The model described by Equation (26) shows the enormous simplification introduced by the discrete-time approach. The same model allows to represent robot behavior regardless of the presence or not impact and the type of contact between the vertices of the foot and the ground.

Control of the Biped Robot
In this section, a CTC for joint trajectory tracking is proposed. The reference articular trajectories were obtained by optimizing a performance index. 8 Modelling and Simulation in Engineering 4.1. Nonlinear Control for the Single Support Phase. When the robot is resting on the left foot, the model Equation (1) is The model Equation (27) has unknowns that are € q ∈ ℝ 9 and F l ∈ ℝ 3 . That is, there are 9 equations and 12 variables. For the purpose of the control system, the model will be considered as subject to immobility constraints on the left foot By deriving Equation (28) once: By deriving Equation (29): Combining Equations (27) and (30), a new model relating € q and F l as a function of q, _ q, and Γ is obtained in Rewriting Equation (30), we obtained In Equation (33), q u is the vector containing the unactuated coordinates and q a is the actuated coordinate vector.
Matrix J lu ðqÞ ∈ ℝ 3×3 is composed by the first three columns of J l ðqÞ and J la ðqÞ ∈ ℝ 3×6 by the last six columns. Solving € q u of Equation (32): Equation (34) indicates that the contact between the left foot and the ground leads to the acceleration of the coordinates not being directly driven by the acceleration of the coordinates actuated. We defined the following variables: Substituting € q = M€ q a + N in the model Equation (27) AM€ q a + AN Equation (36) is multiplied by M T ; then If € q a is replaced by the desired acceleration € q d a , and € q d a is calculated by Equation (38), then Equation (37) becomes a classical CTC [27] € q d a = € q r + k ν q r v − _ q a ð Þ+ k p q r − q a ð Þ: ð38Þ q r a ∈ ℝ 6 , _ q r a ∈ ℝ 6 , and € q r a ∈ ℝ 6 being the joint reference motion. By replacing € q d a in Equation (37), we obtain the following equations which allow us to calculate the joint torques required to the joint coordinates follow their respective reference values in 4.2. Optimal Reference Trajectories. The optimization approaches for the generation of gait patterns in bipedal robot permits find the articular trajectories that minimize a performance index, such as the torques exerted by the actuators or the mechanical energy expenditure [28]. The criterion used in this article was proposed in [29,30]. It consists of minimizing Equation (40), which is related to the energy losses in the actuators of the robot 9 Modelling and Simulation in Engineering where Γðq, q v , € qÞ represents the torque vector exerted by the actuators as a function of generalized positions q, velocities q v , and accelerations € q. The numerical value of Γðq, q v , € qÞ is calculated by using the inverse dynamic model of the robot. To transform problem defined by Equation (40) into a parametric optimization problem, the angular positions must be defined in terms of a finite set of parameters. In this way, minimizing J no longer requires finding a function of time qðtÞ, but a vector of parameters, to find numerically. In [31], the author shows that spline interpolation as particularly simple, computationally efficient. In this approach, third-order polynomials will be used to parameterize q a ðtÞ like Being a i ∈ ℝ 6 ði = 0,⋯,3Þ, the vectors that contain the coefficients of the 6 polynomials. The coefficient vectors a i can be defined in terms of the initial and final positions and angular velocities By solving the previous system, we obtained where I is an identity matrix of 6 × 6 and 0 a zero matrix of 6 × 6. Of this way, instead of directly searching for 24 coefficients, the vectors q a ð0Þ ∈ ℝ 6 , q a ðTÞ ∈ ℝ 6 , _ q b ð0 Þ ∈ ℝ 6 , and _ q a ðTÞ ∈ ℝ 6 are sought.

Zero Moment Point.
The zero moment point (ZMP) [32] is used for the control of humanoid robots. The ZMP specifies the point where the reaction forces generated by the foot contact with the ground produce no moment in the plane of contact; it is assumed that the surface of the foot is flat and has a coefficient of friction sufficient to prevent slippage. The zero moment point (ZMP) can be calculated by where p x is the zero moment point, m z the angular momentum on axis z, and f y the reaction force of the ground exerted on foot. To maintain the balance of robot, the condition described by Equation (45) must be satisfied.
If jp x j remains inside of the polygon support, then the robot remains in balance; otherwise, it falls. When the robot is in single support, this condition is jp x j < 0:5l f , where l f = 0:2 m is the foot length. The control law, Equation (39), the parameters like velocity, and joint acceleration measurements are necessary. To estimate the velocity and acceleration, first, the interpolation between each joint position is performed by using thirdorder polynomials. Then, the resulting sequence of polynomials is derived for the first time and second time to estimate the speed and acceleration, respectively. The joint velocities are shown in Figure 4. Hence, values of the desired joint coordinates q r ðtÞ, _ q r ðtÞ and € q r ðtÞ are calculated off-line. The temporal evolution of the ZMP is shown in Figure 5. The ZMP fulfill the condition jp x j < 0:5l f , and the control law ensures robot balance.

Numeric Results
The tangential force is within the limits of the cone of friction for friction coefficient μ = 0:75, like Figure 6. Then, the robot does not slip.

Conclusions
The simulation and control of mechanical systems subject to constraints are presented. The robot modeled is a planar biped with only six actuators and underactuated during the single support phases. The dynamic model is discretized directly from the system using Euler's method. This allows transforming the differential equations governing the dynamics of robot in a system of difference equations. In this approach, the conditions of complementarity are obtained by writing the contact point speeds in the instant t k+1 depending on the product between reaction forces at time t k and period discretization h.
In the simulator developed the phenomenon of shock accumulation described in the introduction, it is presented when the two vertices of the same foot do not impact the ground simultaneously. In the case of the time-stepping approach, the discretization of the dynamic model implies that simulations must be performed at a fixed step. This fact ensures that the shock accumulation phenomenon may not occur. However, time-stepping methods may require step sizes prohibitively small in order to ensure the numerical stability of the solution obtained.
The trajectories were initially validated by simulation. This is crucial for the study of movement, rapid prototyping, controller design, and validation in a virtual environment before execution on a real robot. In the case of dynamic simulation, it offers visual information in the virtual world and the behavior of the robot in the real world, being the most efficient way to validate the trajectories of a gait cycle for a bipedal robot. The main difficulty in testing the dynamic simulator is that a complete mathematical model of the bipedal robot is needed, which limits its application to robots with many degrees of freedom.
In the future, I plan to prove the dynamic simulator with other biped robots with the mathematical model that was known and compare the results with commercial dynamic simulators.

Data Availability
Data of the mathematical model of the robot and the trajectories are available; please write an email to the corresponding author, Diego Bravo.

Conflicts of Interest
The authors declare that they have no conflicts of interest.