1 | /* |
2 | * File: spring1.c |
3 | * |
4 | * Code generated for Simulink model 'spring1'. |
5 | * |
6 | * Model version : 1.23 |
7 | * Simulink Coder version : 8.10 (R2016a) 10-Feb-2016 |
8 | * C/C++ source code generated on : Sun Jun 05 15:43:23 2016 |
9 | * |
10 | * Target selection: ert.tlc |
11 | * Embedded hardware selection: ARM Compatible->ARM Cortex |
12 | * Emulation hardware selection: |
13 | * Differs from embedded hardware (MATLAB Host) |
14 | * Code generation objectives: |
15 | * 1. Execution efficiency |
16 | * 2. RAM efficiency |
17 | * Validation result: Not run |
18 | */ |
19 | |
20 | #include "spring1.h" |
21 | |
22 | /* Private macros used by the generated code to access rtModel */ |
23 | #ifndef rtmIsMajorTimeStep |
24 | # define rtmIsMajorTimeStep(rtm) (((rtm)->Timing.simTimeStep) == MAJOR_TIME_STEP) |
25 | #endif |
26 | |
27 | #ifndef rtmIsMinorTimeStep |
28 | # define rtmIsMinorTimeStep(rtm) (((rtm)->Timing.simTimeStep) == MINOR_TIME_STEP) |
29 | #endif |
30 | |
31 | #ifndef rtmGetTPtr |
32 | # define rtmGetTPtr(rtm) ((rtm)->Timing.t) |
33 | #endif |
34 | |
35 | #ifndef rtmSetTPtr |
36 | # define rtmSetTPtr(rtm, val) ((rtm)->Timing.t = (val)) |
37 | #endif |
38 | |
39 | /* Continuous states */ |
40 | X rtX; |
41 | |
42 | /* Block signals and states (auto storage) */ |
43 | DW rtDW; |
44 | |
45 | /* Real-time model */ |
46 | RT_MODEL rtM_; |
47 | RT_MODEL *const rtM = &rtM_; |
48 | |
49 | /* private model entry point functions */ |
50 | extern void spring1_derivatives(void); |
51 | |
52 | /* |
53 | * This function updates continuous states using the ODE3 fixed-step |
54 | * solver algorithm |
55 | */ |
56 | static void rt_ertODEUpdateContinuousStates(RTWSolverInfo *si ) |
57 | { |
58 | /* Solver Matrices */ |
59 | static const real_T rt_ODE3_A[3] = { |
60 | 1.0/2.0, 3.0/4.0, 1.0 |
61 | }; |
62 | |
63 | static const real_T rt_ODE3_B[3][3] = { |
64 | { 1.0/2.0, 0.0, 0.0 }, |
65 | |
66 | { 0.0, 3.0/4.0, 0.0 }, |
67 | |
68 | { 2.0/9.0, 1.0/3.0, 4.0/9.0 } |
69 | }; |
70 | |
71 | time_T t = rtsiGetT(si); |
72 | time_T tnew = rtsiGetSolverStopTime(si); |
73 | time_T h = rtsiGetStepSize(si); |
74 | real_T *x = rtsiGetContStates(si); |
75 | ODE3_IntgData *id = (ODE3_IntgData *)rtsiGetSolverData(si); |
76 | real_T *y = id->y; |
77 | real_T *f0 = id->f[0]; |
78 | real_T *f1 = id->f[1]; |
79 | real_T *f2 = id->f[2]; |
80 | real_T hB[3]; |
81 | int_T i; |
82 | int_T nXc = 2; |
83 | rtsiSetSimTimeStep(si,MINOR_TIME_STEP); |
84 | |
85 | /* Save the state values at time t in y, we'll use x as ynew. */ |
86 | (void) memcpy(y, x, |
87 | (uint_T)nXc*sizeof(real_T)); |
88 | |
89 | /* Assumes that rtsiSetT and ModelOutputs are up-to-date */ |
90 | /* f0 = f(t,y) */ |
91 | rtsiSetdX(si, f0); |
92 | spring1_derivatives(); |
93 | |
94 | /* f(:,2) = feval(odefile, t + hA(1), y + f*hB(:,1), args(:)(*)); */ |
95 | hB[0] = h * rt_ODE3_B[0][0]; |
96 | for (i = 0; i < nXc; i++) { |
97 | x[i] = y[i] + (f0[i]*hB[0]); |
98 | } |
99 | |
100 | rtsiSetT(si, t + h*rt_ODE3_A[0]); |
101 | rtsiSetdX(si, f1); |
102 | spring1_step(); |
103 | spring1_derivatives(); |
104 | |
105 | /* f(:,3) = feval(odefile, t + hA(2), y + f*hB(:,2), args(:)(*)); */ |
106 | for (i = 0; i <= 1; i++) { |
107 | hB[i] = h * rt_ODE3_B[1][i]; |
108 | } |
109 | |
110 | for (i = 0; i < nXc; i++) { |
111 | x[i] = y[i] + (f0[i]*hB[0] + f1[i]*hB[1]); |
112 | } |
113 | |
114 | rtsiSetT(si, t + h*rt_ODE3_A[1]); |
115 | rtsiSetdX(si, f2); |
116 | spring1_step(); |
117 | spring1_derivatives(); |
118 | |
119 | /* tnew = t + hA(3); |
120 | ynew = y + f*hB(:,3); */ |
121 | for (i = 0; i <= 2; i++) { |
122 | hB[i] = h * rt_ODE3_B[2][i]; |
123 | } |
124 | |
125 | for (i = 0; i < nXc; i++) { |
126 | x[i] = y[i] + (f0[i]*hB[0] + f1[i]*hB[1] + f2[i]*hB[2]); |
127 | } |
128 | |
129 | rtsiSetT(si, tnew); |
130 | rtsiSetSimTimeStep(si,MAJOR_TIME_STEP); |
131 | } |
132 | |
133 | /* Model step function */ |
134 | void spring1_step(void) |
135 | { |
136 | if (rtmIsMajorTimeStep(rtM)) { |
137 | /* set solver stop time */ |
138 | rtsiSetSolverStopTime(&rtM->solverInfo,((rtM->Timing.clockTick0+1)* |
139 | rtM->Timing.stepSize0)); |
140 | } /* end MajorTimeStep */ |
141 | |
142 | /* Update absolute time of base rate at minor time step */ |
143 | if (rtmIsMinorTimeStep(rtM)) { |
144 | rtM->Timing.t[0] = rtsiGetT(&rtM->solverInfo); |
145 | } |
146 | |
147 | /* Integrator: '<Root>/Integrate y''' */ |
148 | rtDW.Integratey = rtX.Integratey_CSTATE_a; |
149 | |
150 | /* Sum: '<Root>/Sum' incorporates: |
151 | * Gain: '<Root>/Gain y' |
152 | * Integrator: '<Root>/Integrate y'' |
153 | * Sin: '<Root>/Sine Wave' |
154 | */ |
155 | rtDW.Sum = (sin(rtM->Timing.t[0] + 1.5707963267948966) - 4.0 * |
156 | rtX.Integratey_CSTATE) - rtDW.Integratey; |
157 | if (rtmIsMajorTimeStep(rtM)) { |
158 | rt_ertODEUpdateContinuousStates(&rtM->solverInfo); |
159 | |
160 | /* Update absolute time for base rate */ |
161 | /* The "clockTick0" counts the number of times the code of this task has |
162 | * been executed. The absolute time is the multiplication of "clockTick0" |
163 | * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not |
164 | * overflow during the application lifespan selected. |
165 | */ |
166 | ++rtM->Timing.clockTick0; |
167 | rtM->Timing.t[0] = rtsiGetSolverStopTime(&rtM->solverInfo); |
168 | |
169 | { |
170 | /* Update absolute timer for sample time: [0.4s, 0.0s] */ |
171 | /* The "clockTick1" counts the number of times the code of this task has |
172 | * been executed. The resolution of this integer timer is 0.4, which is the step size |
173 | * of the task. Size of "clockTick1" ensures timer will not overflow during the |
174 | * application lifespan selected. |
175 | */ |
176 | rtM->Timing.clockTick1++; |
177 | } |
178 | } /* end MajorTimeStep */ |
179 | } |
180 | |
181 | /* Derivatives for root system: '<Root>' */ |
182 | void spring1_derivatives(void) |
183 | { |
184 | XDot *_rtXdot; |
185 | _rtXdot = ((XDot *) rtM->ModelData.derivs); |
186 | |
187 | /* Derivatives for Integrator: '<Root>/Integrate y'' */ |
188 | _rtXdot->Integratey_CSTATE = rtDW.Integratey; |
189 | |
190 | /* Derivatives for Integrator: '<Root>/Integrate y''' */ |
191 | _rtXdot->Integratey_CSTATE_a = rtDW.Sum; |
192 | } |
193 | |
194 | /* Model initialize function */ |
195 | void spring1_initialize(void) |
196 | { |
197 | /* Registration code */ |
198 | { |
199 | /* Setup solver object */ |
200 | rtsiSetSimTimeStepPtr(&rtM->solverInfo, &rtM->Timing.simTimeStep); |
201 | rtsiSetTPtr(&rtM->solverInfo, &rtmGetTPtr(rtM)); |
202 | rtsiSetStepSizePtr(&rtM->solverInfo, &rtM->Timing.stepSize0); |
203 | rtsiSetdXPtr(&rtM->solverInfo, &rtM->ModelData.derivs); |
204 | rtsiSetContStatesPtr(&rtM->solverInfo, (real_T **) |
205 | &rtM->ModelData.contStates); |
206 | rtsiSetNumContStatesPtr(&rtM->solverInfo, &rtM->Sizes.numContStates); |
207 | rtsiSetNumPeriodicContStatesPtr(&rtM->solverInfo, |
208 | &rtM->Sizes.numPeriodicContStates); |
209 | rtsiSetPeriodicContStateIndicesPtr(&rtM->solverInfo, |
210 | &rtM->ModelData.periodicContStateIndices); |
211 | rtsiSetPeriodicContStateRangesPtr(&rtM->solverInfo, |
212 | &rtM->ModelData.periodicContStateRanges); |
213 | rtsiSetErrorStatusPtr(&rtM->solverInfo, (&rtmGetErrorStatus(rtM))); |
214 | rtsiSetRTModelPtr(&rtM->solverInfo, rtM); |
215 | } |
216 | |
217 | rtsiSetSimTimeStep(&rtM->solverInfo, MAJOR_TIME_STEP); |
218 | rtM->ModelData.intgData.y = rtM->ModelData.odeY; |
219 | rtM->ModelData.intgData.f[0] = rtM->ModelData.odeF[0]; |
220 | rtM->ModelData.intgData.f[1] = rtM->ModelData.odeF[1]; |
221 | rtM->ModelData.intgData.f[2] = rtM->ModelData.odeF[2]; |
222 | rtM->ModelData.contStates = ((X *) &rtX); |
223 | rtsiSetSolverData(&rtM->solverInfo, (void *)&rtM->ModelData.intgData); |
224 | rtsiSetSolverName(&rtM->solverInfo,"ode3"); |
225 | rtmSetTPtr(rtM, &rtM->Timing.tArray[0]); |
226 | rtM->Timing.stepSize0 = 0.4; |
227 | |
228 | /* InitializeConditions for Integrator: '<Root>/Integrate y'' */ |
229 | rtX.Integratey_CSTATE = 1.0; |
230 | |
231 | /* InitializeConditions for Integrator: '<Root>/Integrate y''' */ |
232 | rtX.Integratey_CSTATE_a = 0.0; |
233 | } |
234 | |
235 | /* |
236 | * File trailer for generated code. |
237 | * |
238 | * [EOF] |
239 | */ |
240 | |