#include <stdio.h> int main() { X rtX; /* Block signals and states (default storage) */ DW rtDW; /* External inputs (root inport signals with default storage) */ ExtU rtU; /* External outputs (root outports fed by signals with default storage) */ ExtY rtY; /* Real-time model */ static RT_MODEL rtM_; RT_MODEL *const rtM = &rtM_; extern real_T rt_powd_snf(real_T u0, real_T u1); /* private model entry point functions */ extern void CA_derivatives(void); static real_T rtGetInf(void); static real32_T rtGetInfF(void); static real_T rtGetMinusInf(void); static real32_T rtGetMinusInfF(void); static real_T rtGetNaN(void); static real32_T rtGetNaNF(void); extern real_T rtInf; extern real_T rtMinusInf; extern real_T rtNaN; extern real32_T rtInfF; extern real32_T rtMinusInfF; extern real32_T rtNaNF; static void rt_InitInfAndNaN(size_t realSize); static boolean_T rtIsInf(real_T value); static boolean_T rtIsInfF(real32_T value); static boolean_T rtIsNaN(real_T value); static boolean_T rtIsNaNF(real32_T value); typedef struct { struct { uint32_T wordH; uint32_T wordL; } words; } BigEndianIEEEDouble; typedef struct { struct { uint32_T wordL; uint32_T wordH; } words; } LittleEndianIEEEDouble; typedef struct { union { real32_T wordLreal; uint32_T wordLuint; } wordL; } IEEESingle; real_T rtInf; real_T rtMinusInf; real_T rtNaN; real32_T rtInfF; real32_T rtMinusInfF; real32_T rtNaNF; static real_T rtGetInf(void) { size_t bitsPerReal = sizeof(real_T) * (NumBitsPerChar); real_T inf = 0.0; if (bitsPerReal == 32U) { inf = rtGetInfF(); } else { union { LittleEndianIEEEDouble bitVal; real_T fltVal; } tmpVal; tmpVal.bitVal.words.wordH = 0x7FF00000U; tmpVal.bitVal.words.wordL = 0x00000000U; inf = tmpVal.fltVal; } return inf; } static real32_T rtGetInfF(void) { IEEESingle infF; infF.wordL.wordLuint = 0x7F800000U; return infF.wordL.wordLreal; } static real_T rtGetMinusInf(void) { size_t bitsPerReal = sizeof(real_T) * (NumBitsPerChar); real_T minf = 0.0; if (bitsPerReal == 32U) { minf = rtGetMinusInfF(); } else { union { LittleEndianIEEEDouble bitVal; real_T fltVal; } tmpVal; tmpVal.bitVal.words.wordH = 0xFFF00000U; tmpVal.bitVal.words.wordL = 0x00000000U; minf = tmpVal.fltVal; } return minf; } static real32_T rtGetMinusInfF(void) { IEEESingle minfF; minfF.wordL.wordLuint = 0xFF800000U; return minfF.wordL.wordLreal; } static real_T rtGetNaN(void) { size_t bitsPerReal = sizeof(real_T) * (NumBitsPerChar); real_T nan = 0.0; if (bitsPerReal == 32U) { nan = rtGetNaNF(); } else { union { LittleEndianIEEEDouble bitVal; real_T fltVal; } tmpVal; tmpVal.bitVal.words.wordH = 0xFFF80000U; tmpVal.bitVal.words.wordL = 0x00000000U; nan = tmpVal.fltVal; } return nan; } /* * Initialize rtNaNF needed by the generated code. * NaN is initialized as non-signaling. Assumes IEEE. */ static real32_T rtGetNaNF(void) { IEEESingle nanF = { { 0.0F } }; nanF.wordL.wordLuint = 0xFFC00000U; return nanF.wordL.wordLreal; } /* * Initialize the rtInf, rtMinusInf, and rtNaN needed by the * generated code. NaN is initialized as non-signaling. Assumes IEEE. */ static void rt_InitInfAndNaN(size_t realSize) { (void) (realSize); rtNaN = rtGetNaN(); rtNaNF = rtGetNaNF(); rtInf = rtGetInf(); rtInfF = rtGetInfF(); rtMinusInf = rtGetMinusInf(); rtMinusInfF = rtGetMinusInfF(); } /* Test if value is infinite */ static boolean_T rtIsInf(real_T value) { return (boolean_T)((value==rtInf || value==rtMinusInf) ? 1U : 0U); } /* Test if single-precision value is infinite */ static boolean_T rtIsInfF(real32_T value) { return (boolean_T)(((value)==rtInfF || (value)==rtMinusInfF) ? 1U : 0U); } /* Test if value is not a number */ static boolean_T rtIsNaN(real_T value) { boolean_T result = (boolean_T) 0; size_t bitsPerReal = sizeof(real_T) * (NumBitsPerChar); if (bitsPerReal == 32U) { result = rtIsNaNF((real32_T)value); } else { union { LittleEndianIEEEDouble bitVal; real_T fltVal; } tmpVal; tmpVal.fltVal = value; result = (boolean_T)((tmpVal.bitVal.words.wordH & 0x7FF00000) == 0x7FF00000 && ( (tmpVal.bitVal.words.wordH & 0x000FFFFF) != 0 || (tmpVal.bitVal.words.wordL != 0) )); } return result; } /* Test if single-precision value is not a number */ static boolean_T rtIsNaNF(real32_T value) { IEEESingle tmp; tmp.wordL.wordLreal = value; return (boolean_T)( (tmp.wordL.wordLuint & 0x7F800000) == 0x7F800000 && (tmp.wordL.wordLuint & 0x007FFFFF) != 0 ); } /* * This function updates continuous states using the ODE3 fixed-step * solver algorithm */ static void rt_ertODEUpdateContinuousStates(RTWSolverInfo *si ) { /* Solver Matrices */ static const real_T rt_ODE3_A[3] = { 1.0/2.0, 3.0/4.0, 1.0 }; static const real_T rt_ODE3_B[3][3] = { { 1.0/2.0, 0.0, 0.0 }, { 0.0, 3.0/4.0, 0.0 }, { 2.0/9.0, 1.0/3.0, 4.0/9.0 } }; time_T t = rtsiGetT(si); time_T tnew = rtsiGetSolverStopTime(si); time_T h = rtsiGetStepSize(si); real_T *x = rtsiGetContStates(si); ODE3_IntgData *id = (ODE3_IntgData *)rtsiGetSolverData(si); real_T *y = id->y; real_T *f0 = id->f[0]; real_T *f1 = id->f[1]; real_T *f2 = id->f[2]; real_T hB[3]; int_T i; int_T nXc = 3; rtsiSetSimTimeStep(si,MINOR_TIME_STEP); (void) memcpy(y, x, (uint_T)nXc*sizeof(real_T)); rtsiSetdX(si, f0); CA_derivatives(); hB[0] = h * rt_ODE3_B[0][0]; for (i = 0; i < nXc; i++) { x[i] = y[i] + (f0[i]*hB[0]); } rtsiSetT(si, t + h*rt_ODE3_A[0]); rtsiSetdX(si, f1); CA_step(); CA_derivatives(); for (i = 0; i <= 1; i++) { hB[i] = h * rt_ODE3_B[1][i]; } for (i = 0; i < nXc; i++) { x[i] = y[i] + (f0[i]*hB[0] + f1[i]*hB[1]); } rtsiSetT(si, t + h*rt_ODE3_A[1]); rtsiSetdX(si, f2); CA_step(); CA_derivatives(); for (i = 0; i <= 2; i++) { hB[i] = h * rt_ODE3_B[2][i]; } for (i = 0; i < nXc; i++) { x[i] = y[i] + (f0[i]*hB[0] + f1[i]*hB[1] + f2[i]*hB[2]); } rtsiSetT(si, tnew); rtsiSetSimTimeStep(si,MAJOR_TIME_STEP); } real_T rt_powd_snf(real_T u0, real_T u1) { real_T y; if (rtIsNaN(u0) || rtIsNaN(u1)) { y = (rtNaN); } else { real_T tmp; real_T tmp_0; tmp = fabs(u0); tmp_0 = fabs(u1); if (rtIsInf(u1)) { if (tmp == 1.0) { y = 1.0; } else if (tmp > 1.0) { if (u1 > 0.0) { y = (rtInf); } else { y = 0.0; } } else if (u1 > 0.0) { y = 0.0; } else { y = (rtInf); } } else if (tmp_0 == 0.0) { y = 1.0; } else if (tmp_0 == 1.0) { if (u1 > 0.0) { y = u0; } else { y = 1.0 / u0; } } else if (u1 == 2.0) { y = u0 * u0; } else if ((u1 == 0.5) && (u0 >= 0.0)) { y = sqrt(u0); } else if ((u0 < 0.0) && (u1 > floor(u1))) { y = (rtNaN); } else { y = pow(u0, u1); } } return y; } /* Model step function */ void CA_step(void) { real_T Vnet; real_T rtb_Dist; if (rtmIsMajorTimeStep(rtM)) { /* set solver stop time */ rtsiSetSolverStopTime(&rtM->solverInfo,((rtM->Timing.clockTick0+1)* rtM->Timing.stepSize0)); } /* end MajorTimeStep */ if (rtmIsMinorTimeStep(rtM)) { rtM->Timing.t[0] = rtsiGetT(&rtM->solverInfo); } if (rtmIsMajorTimeStep(rtM)) { real_T rtb_Velocity; rtb_Dist = sqrt((rtU.Distance[0] * rtU.Distance[0] + rtU.Distance[1] * rtU.Distance[1]) + rtU.Distance[2] * rtU.Distance[2]); Vnet = (rtU.Distance[3] + rtU.Distance[4]) + rtU.Distance[5]; rtb_Velocity = sqrt((rtU.Distance[3] * rtU.Distance[3] + rtU.Distance[4] * rtU.Distance[4]) + rtU.Distance[5] * rtU.Distance[5]) * (Vnet / fabs(Vnet)); Vnet = fmax(fmin((25.0 - rtb_Dist) * 0.2, 0.7), 0.0) + fmax(fmin((0.0 - rtb_Velocity) * 0.2, 0.7), 0.0); if (rtb_Dist == 0.0) { Vnet = 0.0; rtb_Dist = 0.2; rtb_Velocity = 0.0; } else { rtb_Dist = fmax(fmin((rtb_Dist - 25.0) * 0.2, 0.3) + fmin(0.5 * rtb_Velocity, 0.3), 0.0); rtb_Velocity = Vnet; } if (sqrt((rtU.V3x1[0] * rtU.V3x1[0] + rtU.V3x1[1] * rtU.V3x1[1]) + rt_powd_snf(rtU.V3x1[2], 3.0)) > 10.0) { rtb_Velocity = fmax(Vnet, 0.4); rtb_Dist = 0.0; } rtY.Torque = 1352.04 * rtb_Dist; rtY.Brake = -655.34 * rtb_Velocity; rtY.Gear = 1.0; } rtY.Steer = 10.0 * rtX.TransferFcn1_CSTATE * 54.0; rtb_Dist = (rtU.DistToLane - 3.0) * 0.5 + rtU.HA; rtDW.FilterCoefficient = (0.01 * rtb_Dist - rtX.Filter_CSTATE) * 100.0; Vnet = (0.25 * rtb_Dist + rtX.Integrator_CSTATE) + rtDW.FilterCoefficient; if (Vnet < 0.5) { if (Vnet > -0.5) { rtDW.SteerOut = 0.0; } else { rtDW.SteerOut = Vnet; } } else { rtDW.SteerOut = Vnet; } rtDW.IntegralGain = 0.0 * rtb_Dist; if (rtmIsMajorTimeStep(rtM)) { rt_ertODEUpdateContinuousStates(&rtM->solverInfo); ++rtM->Timing.clockTick0; rtM->Timing.t[0] = rtsiGetSolverStopTime(&rtM->solverInfo); { rtM->Timing.clockTick1++; } } /* end MajorTimeStep */ } void CA_derivatives(void) { XDot *_rtXdot; _rtXdot = ((XDot *) rtM->derivs); /* Derivatives for TransferFcn: '<S1>/Transfer Fcn1' */ _rtXdot->TransferFcn1_CSTATE = 0.0; _rtXdot->TransferFcn1_CSTATE += -10.0 * rtX.TransferFcn1_CSTATE; _rtXdot->TransferFcn1_CSTATE += rtDW.SteerOut; _rtXdot->Integrator_CSTATE = rtDW.IntegralGain; _rtXdot->Filter_CSTATE = rtDW.FilterCoefficient; } /* Model initialize function */ void CA_initialize(void) { /* Registration code */ /* initialize non-finites */ rt_InitInfAndNaN(sizeof(real_T)); { /* Setup solver object */ rtsiSetSimTimeStepPtr(&rtM->solverInfo, &rtM->Timing.simTimeStep); rtsiSetTPtr(&rtM->solverInfo, &rtmGetTPtr(rtM)); rtsiSetStepSizePtr(&rtM->solverInfo, &rtM->Timing.stepSize0); rtsiSetdXPtr(&rtM->solverInfo, &rtM->derivs); rtsiSetContStatesPtr(&rtM->solverInfo, (real_T **) &rtM->contStates); rtsiSetNumContStatesPtr(&rtM->solverInfo, &rtM->Sizes.numContStates); rtsiSetNumPeriodicContStatesPtr(&rtM->solverInfo, &rtM->Sizes.numPeriodicContStates); rtsiSetPeriodicContStateIndicesPtr(&rtM->solverInfo, &rtM->periodicContStateIndices); rtsiSetPeriodicContStateRangesPtr(&rtM->solverInfo, &rtM->periodicContStateRanges); rtsiSetErrorStatusPtr(&rtM->solverInfo, (&rtmGetErrorStatus(rtM))); rtsiSetRTModelPtr(&rtM->solverInfo, rtM); } rtsiSetSimTimeStep(&rtM->solverInfo, MAJOR_TIME_STEP); rtM->intgData.y = rtM->odeY; rtM->intgData.f[0] = rtM->odeF[0]; rtM->intgData.f[1] = rtM->odeF[1]; rtM->intgData.f[2] = rtM->odeF[2]; rtM->contStates = ((X *) &rtX); rtsiSetSolverData(&rtM->solverInfo, (void *)&rtM->intgData); rtsiSetSolverName(&rtM->solverInfo,"ode3"); rtmSetTPtr(rtM, &rtM->Timing.tArray[0]); rtM->Timing.stepSize0 = 0.2; /* InitializeConditions for TransferFcn: '<S1>/Transfer Fcn1' */ rtX.TransferFcn1_CSTATE = 0.0; /* InitializeConditions for Integrator: '<S41>/Integrator' */ rtX.Integrator_CSTATE = 0.0; /* InitializeConditions for Integrator: '<S36>/Filter' */ rtX.Filter_CSTATE = 0.0; } }
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else
is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}