ert_main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * File: ert_main.c
  3. *
  4. * Code generated for Simulink model 'PMSM_Controller'.
  5. *
  6. * Model version : 1.885
  7. * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020
  8. * C/C++ source code generated on : Sat Jan 8 15:44:11 2022
  9. *
  10. * Target selection: ert.tlc
  11. * Embedded hardware selection: ARM Compatible->ARM Cortex-M
  12. * Code generation objectives:
  13. * 1. Execution efficiency
  14. * 2. RAM efficiency
  15. * Validation result: Not run
  16. */
  17. #include <stddef.h>
  18. #include <stdio.h> /* This ert_main.c example uses printf/fflush */
  19. #include "PMSM_Controller.h" /* Model's header file */
  20. #include "rtwtypes.h"
  21. static RT_MODEL rtM_;
  22. static RT_MODEL *const rtMPtr = &rtM_; /* Real-time model */
  23. static DW rtDW; /* Observable states */
  24. /* '<Root>/Vdc' */
  25. static int16_T rtU_Vdc;
  26. /* '<Root>/Ts' */
  27. static uint16_T rtU_Ts;
  28. /* '<Root>/adc_a' */
  29. static int16_T rtU_adc_a;
  30. /* '<Root>/adc_b' */
  31. static int16_T rtU_adc_b;
  32. /* '<Root>/rotor_angle' */
  33. static real_T rtU_rotor_angle;
  34. /* '<Root>/rotor_speed' */
  35. static int16_T rtU_rotor_speed;
  36. /* '<Root>/input_target' */
  37. static int16_T rtU_input_target;
  38. /* '<Root>/hall_a' */
  39. static uint8_T rtU_hall_a;
  40. /* '<Root>/hall_b' */
  41. static uint8_T rtU_hall_b;
  42. /* '<Root>/hall_c' */
  43. static uint8_T rtU_hall_c;
  44. /* '<Root>/hw_count' */
  45. static uint32_T rtU_hw_count;
  46. /* '<Root>/b_motor_enable' */
  47. static boolean_T rtU_b_motor_enable;
  48. /* '<Root>/b_cruise_enable' */
  49. static boolean_T rtU_b_cruise_enable;
  50. /* '<Root>/n_ctrl_type' */
  51. static uint8_T rtU_n_ctrl_type;
  52. /* '<Root>/pwm_counts' */
  53. static uint16_T rtY_pwm_counts[8];
  54. /* '<Root>/sector' */
  55. static uint8_T rtY_sector;
  56. /*
  57. * Associating rt_OneStep with a real-time clock or interrupt service routine
  58. * is what makes the generated code "real-time". The function rt_OneStep is
  59. * always associated with the base rate of the model. Subrates are managed
  60. * by the base rate from inside the generated code. Enabling/disabling
  61. * interrupts and floating point context switches are target specific. This
  62. * example code indicates where these should take place relative to executing
  63. * the generated code step function. Overrun behavior should be tailored to
  64. * your application needs. This example simply sets an error status in the
  65. * real-time model and returns from rt_OneStep.
  66. */
  67. void rt_OneStep(RT_MODEL *const rtM);
  68. void rt_OneStep(RT_MODEL *const rtM)
  69. {
  70. static boolean_T OverrunFlag = false;
  71. /* Disable interrupts here */
  72. /* Check for overrun */
  73. if (OverrunFlag) {
  74. rtmSetErrorStatus(rtM, "Overrun");
  75. return;
  76. }
  77. OverrunFlag = true;
  78. /* Save FPU context here (if necessary) */
  79. /* Re-enable timer or interrupt here */
  80. /* Set model inputs here */
  81. /* Step the model for base rate */
  82. PMSM_Controller_step(rtM, rtU_Vdc, rtU_Ts, rtU_adc_a, rtU_adc_b,
  83. rtU_rotor_angle, rtU_rotor_speed, rtU_input_target,
  84. rtY_pwm_counts, &rtY_sector);
  85. /* Get model outputs here */
  86. /* Indicate task complete */
  87. OverrunFlag = false;
  88. /* Disable interrupts here */
  89. /* Restore FPU context here (if necessary) */
  90. /* Enable interrupts here */
  91. }
  92. /*
  93. * The example "main" function illustrates what is required by your
  94. * application code to initialize, execute, and terminate the generated code.
  95. * Attaching rt_OneStep to a real-time clock is target specific. This example
  96. * illustrates how you do this relative to initializing the model.
  97. */
  98. int_T main(int_T argc, const char *argv[])
  99. {
  100. RT_MODEL *const rtM = rtMPtr;
  101. /* Unused arguments */
  102. (void)(argc);
  103. (void)(argv);
  104. /* Pack model data into RTM */
  105. rtM->dwork = &rtDW;
  106. /* Initialize model */
  107. PMSM_Controller_initialize(rtM);
  108. /* Attach rt_OneStep to a timer or interrupt service routine with
  109. * period 6.2E-6 seconds (the model's base sample time) here. The
  110. * call syntax for rt_OneStep is
  111. *
  112. * rt_OneStep(rtM);
  113. */
  114. printf("Warning: The simulation will run forever. "
  115. "Generated ERT main won't simulate model step behavior. "
  116. "To change this behavior select the 'MAT-file logging' option.\n");
  117. fflush((NULL));
  118. while (rtmGetErrorStatus(rtM) == (NULL)) {
  119. /* Perform other application tasks here */
  120. }
  121. /* Disable rt_OneStep() here */
  122. return 0;
  123. }
  124. /*
  125. * File trailer for generated code.
  126. *
  127. * [EOF]
  128. */