1/*
2 * File: spring4.c
3 *
4 * Code generated for Simulink model 'spring4'.
5 *
6 * Model version : 1.5
7 * Simulink Coder version : 8.10 (R2016a) 10-Feb-2016
8 * C/C++ source code generated on : Sun Jun 05 15:18:00 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 "spring4.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 */
40X rtX;
41
42/* Block signals and states (auto storage) */
43DW rtDW;
44
45/* Real-time model */
46RT_MODEL rtM_;
47RT_MODEL *const rtM = &rtM_;
48
49/* private model entry point functions */
50extern void spring4_derivatives(void);
51
52/*
53 * This function updates continuous states using the ODE3 fixed-step
54 * solver algorithm
55 */
56static 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 spring4_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 spring4_step();
103 spring4_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 spring4_step();
117 spring4_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 */
134void spring4_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 /* Sum: '<Root>/Sum' incorporates:
148 * Gain: '<Root>/Gain'
149 * Integrator: '<Root>/Integrator'
150 * Sin: '<Root>/Sine Wave'
151 */
152 rtDW.Sum[0] = 0.0 * rtX.Integrator_CSTATE[0] + rtX.Integrator_CSTATE[1];
153 rtDW.Sum[1] = (-4.0 * rtX.Integrator_CSTATE[0] + -rtX.Integrator_CSTATE[1]) +
154 sin(rtM->Timing.t[0] + 1.5707963267948966);
155 if (rtmIsMajorTimeStep(rtM)) {
156 rt_ertODEUpdateContinuousStates(&rtM->solverInfo);
157
158 /* Update absolute time for base rate */
159 /* The "clockTick0" counts the number of times the code of this task has
160 * been executed. The absolute time is the multiplication of "clockTick0"
161 * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
162 * overflow during the application lifespan selected.
163 */
164 ++rtM->Timing.clockTick0;
165 rtM->Timing.t[0] = rtsiGetSolverStopTime(&rtM->solverInfo);
166
167 {
168 /* Update absolute timer for sample time: [0.4s, 0.0s] */
169 /* The "clockTick1" counts the number of times the code of this task has
170 * been executed. The resolution of this integer timer is 0.4, which is the step size
171 * of the task. Size of "clockTick1" ensures timer will not overflow during the
172 * application lifespan selected.
173 */
174 rtM->Timing.clockTick1++;
175 }
176 } /* end MajorTimeStep */
177}
178
179/* Derivatives for root system: '<Root>' */
180void spring4_derivatives(void)
181{
182 XDot *_rtXdot;
183 _rtXdot = ((XDot *) rtM->ModelData.derivs);
184
185 /* Derivatives for Integrator: '<Root>/Integrator' */
186 _rtXdot->Integrator_CSTATE[0] = rtDW.Sum[0];
187 _rtXdot->Integrator_CSTATE[1] = rtDW.Sum[1];
188}
189
190/* Model initialize function */
191void spring4_initialize(void)
192{
193 /* Registration code */
194 {
195 /* Setup solver object */
196 rtsiSetSimTimeStepPtr(&rtM->solverInfo, &rtM->Timing.simTimeStep);
197 rtsiSetTPtr(&rtM->solverInfo, &rtmGetTPtr(rtM));
198 rtsiSetStepSizePtr(&rtM->solverInfo, &rtM->Timing.stepSize0);
199 rtsiSetdXPtr(&rtM->solverInfo, &rtM->ModelData.derivs);
200 rtsiSetContStatesPtr(&rtM->solverInfo, (real_T **)
201 &rtM->ModelData.contStates);
202 rtsiSetNumContStatesPtr(&rtM->solverInfo, &rtM->Sizes.numContStates);
203 rtsiSetNumPeriodicContStatesPtr(&rtM->solverInfo,
204 &rtM->Sizes.numPeriodicContStates);
205 rtsiSetPeriodicContStateIndicesPtr(&rtM->solverInfo,
206 &rtM->ModelData.periodicContStateIndices);
207 rtsiSetPeriodicContStateRangesPtr(&rtM->solverInfo,
208 &rtM->ModelData.periodicContStateRanges);
209 rtsiSetErrorStatusPtr(&rtM->solverInfo, (&rtmGetErrorStatus(rtM)));
210 rtsiSetRTModelPtr(&rtM->solverInfo, rtM);
211 }
212
213 rtsiSetSimTimeStep(&rtM->solverInfo, MAJOR_TIME_STEP);
214 rtM->ModelData.intgData.y = rtM->ModelData.odeY;
215 rtM->ModelData.intgData.f[0] = rtM->ModelData.odeF[0];
216 rtM->ModelData.intgData.f[1] = rtM->ModelData.odeF[1];
217 rtM->ModelData.intgData.f[2] = rtM->ModelData.odeF[2];
218 rtM->ModelData.contStates = ((X *) &rtX);
219 rtsiSetSolverData(&rtM->solverInfo, (void *)&rtM->ModelData.intgData);
220 rtsiSetSolverName(&rtM->solverInfo,"ode3");
221 rtmSetTPtr(rtM, &rtM->Timing.tArray[0]);
222 rtM->Timing.stepSize0 = 0.4;
223
224 /* InitializeConditions for Integrator: '<Root>/Integrator' */
225 rtX.Integrator_CSTATE[0] = 1.0;
226 rtX.Integrator_CSTATE[1] = 0.0;
227}
228
229/*
230 * File trailer for generated code.
231 *
232 * [EOF]
233 */
234