1 ////////////////////////////////////////////////////////////////////////////
2 // Model wheel.cc - SIMLIB continuous model example
3 //
4
5 #include "simlib.h"
6
7 Constant g(9.81); // gravity acceleration
8
9 ////////////////////////////////////////////////////////////////////////////
10 // Car and wheel motion
11 //
12 // x'' = -g - F / M
13 // F = D * (x'-y') + K * (x-y-l0)
14 // y'' = -g + F / m
15 //
16 // parameters:
17 // l0 - length of spring
18 // k - spring stiffnes
19 // D - dissipation coefficient
20 // m - wheel mass
21 // M - car mass
22 // x - car position
23 // y - wheel position
24 //
25 const double r0 = 0.2; // wheel radius
26 const double l0 = 0.3; // spring length
27
28 class System : public aContiBlock { // new continuous block
29 public:
30 Integrator y, y1; // wheel
31 Integrator x, x1; // car
32 Status F; // force
33 Lim l;
System(double M,double m,double D,double K)34 System(double M, double m, double D, double K): // parameters
35 x1( -g - F/M ), // car body
36 x( x1, r0+l0 ),
37
38 y1( -g + (F + l*1e6)/m ), // wheel
39 y( y1, r0 ),
40
41 F( D*(x1-y1) + K*(x-y-l0) ), // spring & shock absorber
42
43 l(r0-y,0,r0) // wheel move limit
44
45 {}
Value()46 double Value() { return x.Value(); } // output = position
47 };
48
49 ////////////////////////////////////////////////////////////////////////////
50 System k1(1000, 20, 5000, 1e5); // instance of wheel system
51
52 ////////////////////////////////////////////////////////////////////////////
Sample()53 void Sample() { // output from model (form suitable for GNUplot)
54 Print("%g %g %g %g %g\n", Time, k1.x.Value(), k1.x1.Value(),
55 k1.y.Value(), k1.y1.Value() );
56 }
57 Sampler S(Sample,0.01);
58
59 ////////////////////////////////////////////////////////////////////////////
main()60 int main() { // experiment control ...
61 SetOutput("wheel.dat");
62 _Print("# WHEEL - Car and wheel motion model\n");
63 Init(0,5); // initialization
64 SetStep(1e-6,1);
65 Run(); // simulation run
66 }
67
68 //
69