35
Magnus Sethson [email protected] Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

Embed Size (px)

Citation preview

Page 1: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

Magnus [email protected]

Lecture 03TMHP51 Servomechanisms (HT2012)

NumericsModel algorithmModel refinement

1

1

Page 3: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Explicit Euler Method (Forward Euler)

3

x[(n+ 1)�T ] = x[n�T ] +�T x[n�T ]

The explicit Euler method is a general method for integrating functions on a discreet grid. In simulation techniques the grid is often one-dimensional time.

3

Page 4: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Implicit Euler Method (Backward Euler)

4

The implicit Euler method is also a general method for integrating functions on a discreet grid. The numerical stability is far better than the implicit, but it often comes with a need of solving non-linear functions numerically in every time step.

x[(n+ 1)�T ] = x[n�T ] +�T x[(n+ 1)�T ]

4

Page 5: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Explicit Trapetzoidal Method

5

The trapetzoidal method is a natural extension to the explicit Euler method. It has far better frequency performance. It also shares some characteristics with the transmission line modeling and is therefore a highly suitable numeric method for hydraulic systems simulation.

x[(n+ 1)�T ] = x[n�T ] +�T

2(x[(n+ 1)�T ] + x[n�T ])

5

Page 7: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected] 7

Non-linear Model

8>>><

>>>:

pLAp �Bpxp � FL = Mtxp

q = Apxp +14Vt�epL

q = Cqwxv

q1⇢ (Ps � pL)

xv : Input signal

xp : Output signal

FL : External disturbance

xp, xp, pL : Internal states

7

Page 9: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Solving using Explicit Euler Method

9

0

@xp[(n+ 1)�T ]vp[(n+ 1)�T ]pL[(n+ 1)�T ]

1

A =

0

@xp[n�T ]vp[n�T ]pL[n�T ]

1

A+�T

0

@xp[n�T ]vp[n�T ]pL[n�T ]

1

A

0

@xp

vp

pL

1

A =

0

BBB@

vp

pLAp�Bpvp�FL

Mt

4�e

Vt

⇣Cqwxv

q1⇢ (Ps � pL)�Apvp

1

CCCA

9

Page 10: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Sort equations, find iterative algorithm

10

q[(n+ 1)�T ] = Cqwxv[(n+ 1)�T ]

r1

(Ps � pL[n�T ])

pL[(n+ 1)�T ] =4�e

Vt(q[(n+ 1)�T ]�Apxp[n�T ])

pL[(n+ 1)�T ] = pL[n�T ] +�T pL[(n+ 1)�T ]

xp[(n+ 1)�T ] =1

Mt(pL[(n+ 1)�T ]Ap �Bpxp[n�T ]� FL[(n+ 1)�T ])

xp[(n+ 1)�T ] = xp[n�T ] +�T xp[(n+ 1)�T ]

xp[(n+ 1)�T ] = xp[n�T ] +�T xp[(n+ 1)�T ]

t

10

Page 11: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

From model, to algorithm, to code

11

void servo_system(unsigned int nsamp) { /* parameters and variables */ double pL=0; /* load pressure, initially zero, system state */ double Ps=70e5; /* supply pressure, 70 [bar] */ double rho=790; /* oil density, 790 [kg/m^3] */ double xv=0; /* spool displacement, (system input), initially zero*/ double w=8e-3; /* area gradient, [-] */ double Cq=0.67; /* flow coefficient, standard value, 0.67 [-] */ double dT=0; /* numerical integration time step size */ double T=0; /* time [s] */ double q=0; /* flow, [m^3/s] */ double Ap=0.001963495408; /* piston area, [m^2] (value from 50mm piston diameter) */ double vp=0; /* piston velocity, initially zero, system state */ double betae=1.5e9; /* bulk modulus of oil, [N/m^2] */ double Vt=0.001570796327; /* total cylinder volume, [m^3] (value from 50mm piston and 0.8m cylinder length) */ double Bp=5e4; /* system damping, [Ns/m] */ double Mt=500; /* system mass load, [kg] */ double xp=0; /* piston position within cylinder, [m] (main output), system state */ double FL=0; /* external load */ /* iteration variable */ unsigned long int n=0; /* iteration counter */ /* set up time step */ dT=0.2/nsamp; /* write first line for T=0 */ printf("\"Te%u\",\"XVe%u\",\"PLe%u\",\"VPe%u\",\"XPe%u\"\n",nsamp,nsamp,nsamp,nsamp,nsamp); printf("%.16f,%.16f,%.16f,%.16f,%.16f\n",T,xv,pL,vp,xp); /* iterate and write result for nsamp time steps */ for(n=1;n<nsamp;n++) { if(0.01<=T) xv=0.0001; /* on step T=0.01 the input is set to 0.1mm valve displacement */ if(0.08<=T) xv=0; /* on step T=0.08 the input is reset and valve is closed */ q=Cq*w*xv*sqrt((Ps-pL)/rho); pL=pL+(q-Ap*vp)*4*betae/Vt*dT; vp=vp+(pL*Ap-Bp*vp-FL)/Mt*dT; xp=xp+vp*dT; T=T+dT; /* increment time */ printf("%.16f,%.16f,%.16f,%.16f,%.16f\n",T,xv,pL,vp,xp); } }

Unreadable

11

Page 12: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

DEMO: Solving the model in plain C

12

#include <stdio.h>#include <math.h>

void servo_system(unsigned int nsamp) { /* parameters and variables */ double pL=0; /* load pressure, initially zero, system state */ double Ps=70e5; /* supply pressure, 70 [bar] */ double rho=790; /* oil density, 790 [kg/m^3] */ double xv=0; /* spool displacement, (system input), initially zero*/ double w=8e-3; /* area gradient, [-] */ double Cq=0.67; /* flow coefficient, standard value, 0.67 [-] */ double dT=0; /* numerical integration time step size */ double T=0; /* time [s] */ double q=0; /* flow, [m^3/s] */ double Ap=0.001963495408; /* piston area, [m^2] (value from 50mm piston diameter) */ double vp=0; /* piston velocity, initially zero, system state */ double betae=1.5e9; /* bulk modulus of oil, [N/m^2] */ double Vt=0.001570796327; /* total cylinder volume, [m^3] (value from 50mm piston and 0.8m cylinder length) */ double Bp=5e4; /* system damping, [Ns/m] */ double Mt=500; /* system mass load, [kg] */ double xp=0; /* piston position within cylinder, [m] (main output), system state */ double FL=0; /* external load */ /* iteration variable */ unsigned long int n=0; /* iteration counter */ /* set up time step */ dT=0.2/nsamp; /* write first line for T=0 */ printf("\"Te%u\",\"XVe%u\",\"PLe%u\",\"VPe%u\",\"XPe%u\"\n",nsamp,nsamp,nsamp,nsamp,nsamp); printf("%.16f,%.16f,%.16f,%.16f,%.16f\n",T,xv,pL,vp,xp); /* iterate and write result for nsamp time steps */ for(n=1;n<nsamp;n++) { if(0.01<=T) xv=0.0001; /* on step T=0.01 the input is set to 0.1mm valve displacement */ if(0.08<=T) xv=0; /* on step T=0.08 the input is reset and valve is closed */ q=Cq*w*xv*sqrt((Ps-pL)/rho); pL=pL+(q-Ap*vp)*4*betae/Vt*dT; vp=vp+(pL*Ap-Bp*vp-FL)/Mt*dT; xp=xp+vp*dT; T=T+dT; /* increment time */ printf("%.16f,%.16f,%.16f,%.16f,%.16f\n",T,xv,pL,vp,xp); } }

int main(int argc,char** argv) { unsigned int nsamp=2000; if(2==argc) sscanf(argv[1],"%u",&nsamp); servo_system(nsamp); return 0;}

Unreadable

gcc -Wall -pedantic -o servo_explicit_euler servo_explicit_euler.c -lm./servo_explicit_euler > servo_explicit_euler.csv

DE

MO

D

EM

O

DE

MO

12

Page 13: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Results Explicit Euler Method

13

Xv vs TimePL vs Time

Vp vs TimeXp vs Time

Variables

X v [m

m]

0

0.02

0.04

0.06

0.08

0.10

PL [bar]

−5

0

5

10

Vp [m

m/s]

−10

−5

0

5

10

15

20

25

30

35

Xp [m

m]

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

1.8

2.0

Time [s]0 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22

Simulation Example, Explicit Euler Method

13

Page 14: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Results, Step Comparison

14

ΔT=0.0167 (NaN)ΔT=0.0111 (NaN)ΔT=0.0091 (NaN)ΔT=0.0080ΔT=0.0040ΔT=0.0020ΔT=0.0010ΔT=0.0001

TimestepsPosi

tion

X p [m

m]

−1.0

−0.5

0

0.5

1.0

1.5

2.0

Time [s]0 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22

Simulation example, Euler's explicit method

14

Page 16: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Solving using Implicit Euler Method

16

0

@xp[(n+ 1)�T ]vp[(n+ 1)�T ]pL[(n+ 1)�T ]

1

A =

0

@xp[n�T ]vp[n�T ]pL[n�T ]

1

A+�T

0

@xp[(n+ 1)�T ]vp[(n+ 1)�T ]pL[(n+ 1)�T ]

1

A

0

@xp[(n+ 1)�T ]vp[(n+ 1)�T ]pL[(n+ 1)�T ]

1

A =

0

BBB@

vp[(n+ 1)�T ]

pL[(n+1)�T ]Ap�Bpvp[(n+1)�T ]�FL[(n+1)�T ]Mt

4�e

Vt

⇣Cqwxv[(n+ 1)�T ]

q1⇢ (Ps � pL[(n+ 1)�T ])�Apvp[(n+ 1)�T ]

1

CCCA

16

Page 17: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Solving using Implicit Euler Method (cont.)

17

0

@xp[(n+ 1)�T ]vp[(n+ 1)�T ]pL[(n+ 1)�T ]

1

A =

0

BBB@

vp[(n+ 1)�T ]

pL[(n+1)�T ]Ap�Bpvp[(n+1)�T ]�FL[(n+1)�T ]Mt

4�e

Vt

⇣Cqwxv[(n+ 1)�T ]

q1⇢ (Ps � pL[(n+ 1)�T ])�Apvp[(n+ 1)�T ]

1

CCCA

17

Page 18: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Newton-Raphson Method

18

The Newton-Raphson method for solving non-linear equations numerically is widely adopted in many applications. Even if originally defined in one dimension it is available in multi-dimensional versions also. It iterates and moves an initial guess towards a near-by root-locus.

One-dimensional

Multi-dimensional

Jij =@fi(x1...N )

@xj

xk+1 = xk � f(xk)

f

0(xk)

f(xk+1)� f(xk)

xk+1 � xk⇡ f

0(xk)Hint:

xk+1 = xk � J

�1f(xk)

18

Page 19: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

The Jacobian

19

f(xp[(n+ 1)�T ], vp[(n+ 1)�T ], pL[(n+ 1)�T ]) =0

@xp[(n+ 1)�T ]vp[(n+ 1)�T ]pL[(n+ 1)�T ]

1

A�

0

@xp[n�T ]vp[n�T ]pL[n�T ]

1

A�

�T

0

BBB@

vp[(n+ 1)�T ]

pL[(n+1)�T ]Ap�Bpvp[(n+1)�T ]�FL[(n+1)�T ]Mt

4�e

Vt

⇣Cqwxv[(n+ 1)�T ]

q1⇢ (Ps � pL[(n+ 1)�T ])�Apvp[(n+ 1)�T ]

1

CCCA

19

Page 20: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

The Jacobian (cont.)

20

J11 = 1

J12 = ��T

J13 = 0

J21 = 0 (No spring involved!)

J22 = 1 +

Bp

Mt�T

J23 = �Ap

Mt�T

J31 = 0

J32 = �4�eAp

Vt�T

J33 = 1 +

4�e

Vt

⇢Cqwxv[(n+ 1)�T ][k]

2

pPs � pL[(n+ 1)�T ][k]

�T

xk+1 � xk = J

�1f(xk)

The Newton-Raphson method is often solved like a linear set of equations, avoiding the full Jacobian inverse.

J(xk) dk+1 = J(xk)[xk+1 � xk] = �f(xk)

20

Page 21: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

From model, to algorithm, to code

21

void servo_system(unsigned int nsamp) { /* parameters and variables */ double pL=0; /* load pressure, initially zero, system state */ double Ps=70e5; /* supply pressure, 70 [bar] */ double rho=790; /* oil density, 790 [kg/m^3] */ double xv=0; /* spool displacement, (system input), initially zero*/ double w=8e-3; /* area gradient, [-] */ double Cq=0.67; /* flow coefficient, standard value, 0.67 [-] */ double dT=0; /* numerical integration time step size */ double T=0; /* time [s] */ double Ap=0.001963495408; /* piston area, [m^2] (value from 50mm piston diameter) */ double vp=0; /* piston velocity, initially zero, system state */ double betae=1.5e9; /* bulk modulus of oil, [N/m^2] */ double Vt=0.001570796327; /* total cylinder volume, [m^3] (value from 50mm piston and 0.8m cylinder length) */ double Bp=5e4; /* system damping, [Ns/m] */ double Mt=500; /* system mass load, [kg] */ double xp=0; /* piston position within cylinder, [m] (main output), system state */ double FL=0; /* external load */ /* GSL related structures */ gsl_matrix* J=NULL; gsl_matrix* A=NULL; gsl_vector* b=NULL; gsl_vector* x=NULL; gsl_vector* xk=NULL; int s=0; gsl_permutation* p=NULL; double xpk=0; double vpk=0; double pLk=0; /* iteration variable */ unsigned long int n=0; /* iteration counter */ unsigned long int k=0; /* numerical counter */ /* set up time step */ dT=0.2/nsamp;

/* set up GSL parts */ J=gsl_matrix_alloc(3,3); A=gsl_matrix_alloc(3,3); b=gsl_vector_alloc(3); x=gsl_vector_alloc(3); xk=gsl_vector_alloc(3); p=gsl_permutation_alloc(3); /* write first line for T=0 */ printf("\"Ti%u\",\"XVi%u\",\"PLi%u\",\"VPi%u\",\"XPi%u\"\n",nsamp,nsamp,nsamp,nsamp,nsamp); printf("%.16f,%.16f,%.16f,%.16f,%.16f\n",T,xv,pL,vp,xp); /* iterate and write result for nsamp time steps */ for(n=1;n<nsamp;n++) { if(T>=0.01) xv=0.0001; /* on step T=0.01 the input is set to 0.1mm valve displacement */ if(T>=0.08) xv=0; /* on step T=0.08 the input is reset and valve is closed */ /* implicit iteration loop */ k=0; xpk=xp; vpk=vp; pLk=pL; do { gsl_vector_set(xk, 0, xpk); gsl_vector_set(xk, 1, vpk); gsl_vector_set(xk, 2, pLk);

gsl_vector_set(b,0,(xpk-xp-dT*vpk)); gsl_vector_set(b,1,(vpk-vp-dT*(pLk*Ap-Bp*vpk-FL)/Mt)); gsl_vector_set(b,2,(pLk-pL-dT*4*betae/Vt*(Cq*w*xv*sqrt((Ps-pL)/rho)-Ap*vpk))); gsl_matrix_set(J,0,0,1.0); gsl_matrix_set(J,0,1,-dT); gsl_matrix_set(J,0,2,0.0); gsl_matrix_set(J,1,0,0.0); gsl_matrix_set(J,1,1,1+Bp/Mt*dT); gsl_matrix_set(J,1,2,-Ap/Mt*dT); gsl_matrix_set(J,2,0,1.0); gsl_matrix_set(J,2,1,-4*betae*Ap/Vt*dT); gsl_matrix_set(J,2,2,1.0+2*betae/Vt*rho*Cq*w*xv/sqrt(Ps-pLk)*dT); gsl_matrix_memcpy(A,J); gsl_blas_dgemv(CblasNoTrans, 1.0, J, xk, -1.0, b); gsl_linalg_LU_decomp(J,p,&s); gsl_linalg_LU_solve(J,p,b,x); gsl_linalg_LU_refine(A,J,p,b,x,xk); xpk=gsl_vector_get(x,0); vpk=gsl_vector_get(x,1); pLk=gsl_vector_get(x,2); k++; } while(k<20 && (abs(gsl_vector_get(x,0)/(xp+EPS))>RAC || abs(gsl_vector_get(x,1)/(vp+EPS))>RAC || abs(gsl_vector_get(x,2)/(pL+EPS))>RAC));

xp=xpk; vp=vpk; pL=pLk; T=T+dT; /* increment time */ printf("%.16f,%.16f,%.16f,%.16f,%.16f\n",T,xv,pL,vp,xp); } /* clean up GSL */ gsl_permutation_free(p); gsl_vector_free(xk); gsl_vector_free(x); gsl_vector_free(b); gsl_matrix_free(A); gsl_matrix_free(J);}

Unreadable

21

Page 22: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Results Implicit Euler Method

22

Xv vs TimePL vs Time

Vp vs TimeXp vs Time

Variables

X v [m

m]

0

0.02

0.04

0.06

0.08

0.10

PL [bar]

−5

0

5

10

Vp [m

m/s]

−10

−5

0

5

10

15

20

25

30

35

Xp [m

m]

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

1.8

2.0

Time [s]0 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22

Simulation Example, Implicit Euler Method

22

Page 24: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

From model, to algorithm, to code

24

void servo_system(unsigned int nsamp) { /* parameters and variables */ double pL=0; /* load pressure, initially zero, system state */ double Ps=70e5; /* supply pressure, 70 [bar] */ double rho=790; /* oil density, 790 [kg/m^3] */ double xv=0; /* spool displacement, (system input), initially zero*/ double w=8e-3; /* area gradient, [-] */ double Cq=0.67; /* flow coefficient, standard value, 0.67 [-] */ double dT=0; /* numerical integration time step size */ double T=0; /* time [s] */ double q=0; /* flow, [m^3/s] */ double Ap=0.001963495408; /* piston area, [m^2] (value from 50mm piston diameter) */ double vp=0; /* piston velocity, initially zero, system state */ double betae=1.5e9; /* bulk modulus of oil, [N/m^2] */ double Vt=0.001570796327; /* total cylinder volume, [m^3] (value from 50mm piston and 0.8m cylinder length) */ double Bp=5e4; /* system damping, [Ns/m] */ double Mt=500; /* system mass load, [kg] */ double xp=0; /* piston position within cylinder, [m] (main output), system state */ double FL=0; /* external load */ /* trapetzoid related storage */ double npL=0; double nvp=0; double nxp=0; double spL=0; double svp=0; double sxp=0; /* iteration variable */ unsigned long int n=0; /* iteration counter */ /* set up time step */ dT=0.2/nsamp; /* write first line for T=0 */ printf("\"Te%u\",\"XVe%u\",\"PLe%u\",\"VPe%u\",\"XPe%u\"\n",nsamp,nsamp,nsamp,nsamp,nsamp); printf("%.16f,%.16f,%.16f,%.16f,%.16f\n",T,xv,pL,vp,xp); /* iterate and write result for nsamp time steps */ for(n=1;n<nsamp;n++) { if(0.01<=T) xv=0.0001; /* on step T=0.01 the input is set to 0.1mm valve displacement */ if(0.08<=T) xv=0; /* on step T=0.08 the input is reset and valve is closed */ spL=npL; svp=nvp; sxp=nxp; q=Cq*w*xv*sqrt((Ps-pL)/rho); npL=(q-Ap*vp)*4*betae/Vt; pL=pL+0.5*(npL+spL)*dT; nvp=(pL*Ap-Bp*vp-FL)/Mt; vp=vp+0.5*(nvp+svp)*dT; nxp=vp; xp=xp+0.5*(nxp+sxp)*dT; T=T+dT; /* increment time */ printf("%.16f,%.16f,%.16f,%.16f,%.16f\n",T,xv,pL,vp,xp); } }

Unreadable

24

Page 25: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Results Explicit Trapetzoid Method

25

Xv vs TimePL vs Time

Vp vs TimeXp vs Time

Variables

X v [m

m]

0

0.02

0.04

0.06

0.08

0.10

PL [bar]

−5

0

5

10

Vp [m

m/s]

−10

−5

0

5

10

15

20

25

30

35

Xp [m

m]

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

1.8

2.0

Time [s]0 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22

Simulation Example, Explicit Trapetzoid Method

25

Page 26: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Results, Method Comparison

26

Xv vs TimePL,implicit vs TimePL,trapetz vs TimePL,explicit vs TimeXp,implicit vs TimeXp,trapetz vs TimeXp,explicit vs Time

Variables

X v [m

m]

0

0.02

0.04

0.06

0.08

0.10

PL [bar]

−5

0

5

10

Xp [m

m]

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

1.8

2.0

Time [s]0 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22

Simulation Example, Method Comparison (ΔT=20μs)

1.76

1.78

1.80

0.090 0.091 0.092

4.2

4.4

4.6

4.8

0.038 0.039 0.040

26

Page 28: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Add Physics!!!

28

• Internal leakage

• External springs

• Asymmetric cylinder

• End-of-Stroke conditions

• Laminar flow

• Valve dynamics

• Supply system dynamics

• Asymmetric valve orifice

• Separate meter-in and meter-out

• Piston friction

• Long lines between cylinder and valve

• Wave propagation in lines

28

Page 29: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Dynamic Mathematical Model Principles

29

Force Balance

Continuity

Orifice

p1A1 � P2A2 � FL �BLxp = Mtxp

qin

� qout

=dV

dt+

V

�e

dp

dt

q = Cqwxv

r2

(Pa � Pb)

29

Page 30: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Textual Simulation Computer Languages

30

C C++ Fortran 77

ModelicaPhyton

MatlabVHDL-AMS

Haskell

Haskell

30

Page 31: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

DEMO: Simple hydraulic servo system in plain C

31

/* An example of a simulation program for a very simple hydraulic servo system */

#include <stdlib.h>#include <stdio.h>#include <math.h>

#define PI 3.141592653589793

void warning(const char* msg,const char* fn,const int line) { fprintf(stderr, "WARNING! %s(%d) : %s\n",fn,line,msg);}

void cavitation(double p,const char* comp,const char* fn,const int line) { if(p<0.7e5) { fprintf(stderr, "WARNING! %s(%d) : CAVITATION in %s (p=%.16f)\n",fn,line,comp,p); }}

double orifice(double Cq,double w,double xv,double rho,double p1,double p2) { double q=0; if(xv<0) return 0; if(Cq<0) return 0; if(w<0) return 0; if(rho<0) return 0; if(p1<0) return 0; if(p2<0) return 0; cavitation(p1, "orifice p1",__FILE__,__LINE__); cavitation(p2, "orifice p2",__FILE__,__LINE__); if((p1-p2)>0) { q=+Cq*w*xv*sqrt(2/rho*(p1-p2)); } else { q=-Cq*w*xv*sqrt(2/rho*(p2-p1)); } return q;}

double area(double Dp,double Dr) { double A; if(Dp<0) warning("Negative diameter in area()",__FILE__,__LINE__); if(Dr<0) warning("Negative diameter in area()",__FILE__,__LINE__); A=PI*(Dp*Dp-Dr*Dr)*0.25; return A;}

double volume(double Dp,double D,double L) { double V=0; if(L<0) warning("Negative length in volume()",__FILE__,__LINE__); if(L>=0) V=area(Dp,D)*L; return V;}

double compression(double q,double V,double dV,double betae) { double dp=0; if(V<0) warning("Negative volume in compression()",__FILE__,__LINE__); if(V>0) dp=(q-dV)*betae/V; return dp;}

double piston(double p1,double p2,double Dp,double d1,double d2) { double Fc=0; double A1=0; double A2=0;

A1=area(Dp,d1); A2=area(Dp,d2); Fc=p1*A1-p2*A2; return Fc;}

/* trapetzoid numerical integration */void trapetz(double dx,double state[3],double dT) { state[2]=state[1]; state[1]=dx; state[0]+=0.5*(state[1]+state[2])*dT;}

double value(double state[3]) { return state[0];}

void initialize(double state[3],double x0) { state[0]=x0; state[1]=0; state[2]=0;}

double spring(double k,double L0,double x) { double Fs=0; if(k<0) warning("Negative spring coefficient in spring()",__FILE__,__LINE__); Fs=k*(x-L0); return Fs;}

void servo_system(unsigned int nsamp,double Tmax) { /* system states */ double p1[3]; /* positive cylinder chamber pressure */ double p2[3]; /* positive cylinder chamber pressure */ double vp[3]; /* piston velocity */ double xp[3]; /* piston position */ /* parmeters and variables */ double Ps=70e5; /* supply pressure, 70 [bar] */ double Pt=1e5; /* tank pressure, 70 [bar] */ double rho=790; /* oil density, 790 [kg/m^3] */ double xv=0; /* spool displacement, (system input), initially zero*/ double w=8e-3; /* area gradient, [-] */ double Cq=0.67; /* flow coefficient, standard value, 0.67 [-] */ double dT=0; /* numerical integration time step size */ double Dp=0.050; /* piston diameter [m] */ double Dr1=0.020; /* rod diameter in chamber 1 [m] */ double Dr2=0.020; /* rod diameter in chamber 2 [m] */ double V01=0.0001; /* dead volume chamber 1 [m^3] */ double V02=0.0001; /* dead volume chamber 2 [m^3] */ double V1=0; /* total volume in chamber 1 */ double V2=0; /* total volume in chamber 2 */ double L=0.8; /* cylinder length */ double Fc=0; /* total applied force on piston */ double FL=0; /* external load force */ double T=0; /* time [s] */ double q1=0; /* flow into chamber 1, [m^3/s] */ double q2=0; /* flow into chamber 2, [m^3/s] */ double betae=1.5e9; /* bulk modulus of oil, [N/m^2] */ double Bp=5e4; /* system damping, [Ns/m] */ double Mt=500; /* system mass load, [kg] */ unsigned long int n=0; /* iteration counter */ /* temporaries */ double dp1=0;

double dp2=0; double dV1=0; double dV2=0; initialize(p1, Pt); initialize(p2, Pt); initialize(vp, 0); initialize(xp, 0); dT=Tmax/nsamp; printf("\"T\",\"x_v\",\"p_1\",\"p_2\",\"v_p\",\"x_p\",\"q_1\",\"q_2\"\n"); n=0; do { T=n*dT; if(0.01<=T) xv=0.0001; /* open valve */ if(0.08<=T) xv=0; /* close valve */ if(xv>=0) { q1=orifice(Cq, w, xv, rho, Ps, value(p1)); q2=orifice(Cq, w, xv, rho, Pt, value(p2)); } else { q1=orifice(Cq, w, -xv, rho, Pt, value(p1)); q2=orifice(Cq, w, -xv, rho, Ps, value(p2)); } if(value(xp)<-0.5*L) { initialize(xp, -0.5*L); initialize(vp, 0); } if(value(xp)>+0.5*L) { initialize(xp, +0.5*L); initialize(vp, 0); } dV1=+value(vp)*area(Dp, Dr1); dV2=-value(vp)*area(Dp, Dr2); V1=volume(Dp, Dr1, 0.5*L+value(xp))+V01; V2=volume(Dp, Dr2, 0.5*L-value(xp))+V02; dp1=compression(q1, V1, dV1, betae); dp2=compression(q2, V2, dV2, betae); trapetz(dp1, p1, dT); trapetz(dp2, p2, dT); Fc=piston(value(p1), value(p2), Dp, Dr1, Dr2)-FL-Bp*value(vp); trapetz(Fc/Mt, vp, dT); trapetz(value(vp), xp, dT); printf("%.12f,%.12f,%.12f,%.12f,%.12f,%.12f,%.12f,%.12f\n",T,xv,value(p1),value(p2),value(vp),value(xp),q1,q2); n++; } while(n<nsamp);}

int main(int argc, const char * argv[]) { unsigned int nsamp=2000; double Tmax=0.2; if(2<=argc) sscanf(argv[1],"%u",&nsamp); if(3==argc) sscanf(argv[2],"%lg",&Tmax); servo_system(nsamp,Tmax); return 0;}

Unreadable

DE

MO

D

EM

O

DE

MO

31

Page 32: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

[email protected]

Simulation Programs

32

Dymola (Dassault Systems)HOPSAN NG (FluMeS, LiU)

Easy5 (MSC Software) Matlab/Simulink (Mathworks Inc)

Amesim (LMS Intl.)

Flowmaster (Flowmaster Inc.)

32

Page 35: TMHP51 Servomechanisms (HT2012) Lecture 03 - iei.liu.se · PDF fileMagnus Sethson @liu.se Lecture 03 TMHP51 Servomechanisms (HT2012) Numerics Model algorithm Model refinement 1 1

Magnus [email protected]

Next Lecture:08:15, 2012-11-08, P34

35

35