An example of importing Embedded Coder code into the mbed IDE. Currently doesn't connect IO to PWM, ADC, and Encoder, instead provides random inputs and measures execution time.
Dependencies: mbed-dsp mbed Nucleo_pmsmfoc
main.cpp@0:70d27fec6d71, 2014-10-11 (annotated)
- Committer:
- paulcox
- Date:
- Sat Oct 11 08:39:22 2014 +0000
- Revision:
- 0:70d27fec6d71
- Child:
- 1:83374f658183
functioning pmsm algorithm with simulated currents and profiling
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
paulcox | 0:70d27fec6d71 | 1 | #include "mbed.h" |
paulcox | 0:70d27fec6d71 | 2 | #include "rtwdemo_pmsmfoc.h" /* Model's header file */ |
paulcox | 0:70d27fec6d71 | 3 | #include "rtwtypes.h" |
paulcox | 0:70d27fec6d71 | 4 | |
paulcox | 0:70d27fec6d71 | 5 | Timer timer1; |
paulcox | 0:70d27fec6d71 | 6 | |
paulcox | 0:70d27fec6d71 | 7 | /* '<Root>/pwm_compare' */ |
paulcox | 0:70d27fec6d71 | 8 | static uint16_T pwm_compare[3]; |
paulcox | 0:70d27fec6d71 | 9 | |
paulcox | 0:70d27fec6d71 | 10 | /* '<Root>/sensors' */ |
paulcox | 0:70d27fec6d71 | 11 | static SENSORS_STRUCT sensors = { |
paulcox | 0:70d27fec6d71 | 12 | { |
paulcox | 0:70d27fec6d71 | 13 | 0U, 0U } |
paulcox | 0:70d27fec6d71 | 14 | , /* adc_phase_currents */ |
paulcox | 0:70d27fec6d71 | 15 | 0U, /* encoder_valid */ |
paulcox | 0:70d27fec6d71 | 16 | 0U /* encoder_counter */ |
paulcox | 0:70d27fec6d71 | 17 | } ; |
paulcox | 0:70d27fec6d71 | 18 | |
paulcox | 0:70d27fec6d71 | 19 | |
paulcox | 0:70d27fec6d71 | 20 | void rt_OneStep(void); |
paulcox | 0:70d27fec6d71 | 21 | void rt_OneStep(void) |
paulcox | 0:70d27fec6d71 | 22 | { |
paulcox | 0:70d27fec6d71 | 23 | static boolean_T OverrunFlag = false; |
paulcox | 0:70d27fec6d71 | 24 | |
paulcox | 0:70d27fec6d71 | 25 | /* '<Root>/motor_on' */ |
paulcox | 0:70d27fec6d71 | 26 | static uint16_T motor_on = 1U; |
paulcox | 0:70d27fec6d71 | 27 | |
paulcox | 0:70d27fec6d71 | 28 | /* '<Root>/command_type' */ |
paulcox | 0:70d27fec6d71 | 29 | static EnumCommandType command_type = Velocity; |
paulcox | 0:70d27fec6d71 | 30 | |
paulcox | 0:70d27fec6d71 | 31 | /* '<Root>/command_value' */ |
paulcox | 0:70d27fec6d71 | 32 | static real32_T current_request = 0.0F; |
paulcox | 0:70d27fec6d71 | 33 | |
paulcox | 0:70d27fec6d71 | 34 | /* '<Root>/error' */ |
paulcox | 0:70d27fec6d71 | 35 | static EnumErrorType error; |
paulcox | 0:70d27fec6d71 | 36 | |
paulcox | 0:70d27fec6d71 | 37 | /* Disable interrupts here */ |
paulcox | 0:70d27fec6d71 | 38 | |
paulcox | 0:70d27fec6d71 | 39 | /* Check for overrun */ |
paulcox | 0:70d27fec6d71 | 40 | if (OverrunFlag) { |
paulcox | 0:70d27fec6d71 | 41 | printf("------------------OVERRUN!!\r\n"); |
paulcox | 0:70d27fec6d71 | 42 | return; |
paulcox | 0:70d27fec6d71 | 43 | } |
paulcox | 0:70d27fec6d71 | 44 | |
paulcox | 0:70d27fec6d71 | 45 | OverrunFlag = true; |
paulcox | 0:70d27fec6d71 | 46 | |
paulcox | 0:70d27fec6d71 | 47 | /* Save FPU context here (if necessary) */ |
paulcox | 0:70d27fec6d71 | 48 | /* Re-enable timer or interrupt here */ |
paulcox | 0:70d27fec6d71 | 49 | /* Set model inputs here */ |
paulcox | 0:70d27fec6d71 | 50 | |
paulcox | 0:70d27fec6d71 | 51 | timer1.start(); |
paulcox | 0:70d27fec6d71 | 52 | |
paulcox | 0:70d27fec6d71 | 53 | /* Step the model for base rate */ |
paulcox | 0:70d27fec6d71 | 54 | error = Controller(motor_on, command_type, current_request, &sensors, |
paulcox | 0:70d27fec6d71 | 55 | pwm_compare); |
paulcox | 0:70d27fec6d71 | 56 | |
paulcox | 0:70d27fec6d71 | 57 | timer1.stop(); |
paulcox | 0:70d27fec6d71 | 58 | |
paulcox | 0:70d27fec6d71 | 59 | printf("exec us: %d\r\n",timer1.read_us()); |
paulcox | 0:70d27fec6d71 | 60 | timer1.reset(); |
paulcox | 0:70d27fec6d71 | 61 | |
paulcox | 0:70d27fec6d71 | 62 | /* Get model outputs here */ |
paulcox | 0:70d27fec6d71 | 63 | |
paulcox | 0:70d27fec6d71 | 64 | /* Indicate task complete */ |
paulcox | 0:70d27fec6d71 | 65 | OverrunFlag = false; |
paulcox | 0:70d27fec6d71 | 66 | |
paulcox | 0:70d27fec6d71 | 67 | /* Disable interrupts here */ |
paulcox | 0:70d27fec6d71 | 68 | /* Restore FPU context here (if necessary) */ |
paulcox | 0:70d27fec6d71 | 69 | /* Enable interrupts here */ |
paulcox | 0:70d27fec6d71 | 70 | } |
paulcox | 0:70d27fec6d71 | 71 | |
paulcox | 0:70d27fec6d71 | 72 | |
paulcox | 0:70d27fec6d71 | 73 | Serial pc(SERIAL_TX, SERIAL_RX); |
paulcox | 0:70d27fec6d71 | 74 | |
paulcox | 0:70d27fec6d71 | 75 | DigitalOut myled(LED1); |
paulcox | 0:70d27fec6d71 | 76 | |
paulcox | 0:70d27fec6d71 | 77 | int main() { |
paulcox | 0:70d27fec6d71 | 78 | |
paulcox | 0:70d27fec6d71 | 79 | /* Initialize model */ |
paulcox | 0:70d27fec6d71 | 80 | Controller_Init(); |
paulcox | 0:70d27fec6d71 | 81 | srand(time(NULL)); |
paulcox | 0:70d27fec6d71 | 82 | int i = 1; |
paulcox | 0:70d27fec6d71 | 83 | pc.printf("PMSM Init done.\n"); |
paulcox | 0:70d27fec6d71 | 84 | |
paulcox | 0:70d27fec6d71 | 85 | while(1) { |
paulcox | 0:70d27fec6d71 | 86 | wait(0.5); |
paulcox | 0:70d27fec6d71 | 87 | sensors.adc_phase_currents[0] = rand()%MAX_uint16_T; |
paulcox | 0:70d27fec6d71 | 88 | sensors.adc_phase_currents[1] = rand()%MAX_uint16_T; |
paulcox | 0:70d27fec6d71 | 89 | printf("%d %d\r\n",sensors.adc_phase_currents[0],sensors.adc_phase_currents[1]); |
paulcox | 0:70d27fec6d71 | 90 | sensors.encoder_valid = 1; |
paulcox | 0:70d27fec6d71 | 91 | sensors.encoder_counter = i*10; |
paulcox | 0:70d27fec6d71 | 92 | |
paulcox | 0:70d27fec6d71 | 93 | rt_OneStep(); |
paulcox | 0:70d27fec6d71 | 94 | |
paulcox | 0:70d27fec6d71 | 95 | pc.printf("%d: pwm0: %d\r\n", i++, pwm_compare[0]); |
paulcox | 0:70d27fec6d71 | 96 | myled = !myled; |
paulcox | 0:70d27fec6d71 | 97 | } |
paulcox | 0:70d27fec6d71 | 98 | } |
paulcox | 0:70d27fec6d71 | 99 |