paul cox / Mbed 2 deprecated Nucleo_pmsmfoc

Dependencies:   mbed-dsp mbed Nucleo_pmsmfoc

Dependents:   Nucleo_pmsmfoc

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtwdemo_pmsmfoc.h"           /* Model's header file */
00003 #include "rtwtypes.h"
00004 #include "math_helper.h"
00005 
00006 Timer timer1;
00007 
00008   /* '<Root>/pwm_compare' */
00009   static uint16_T pwm_compare[3];
00010 
00011   /* '<Root>/sensors' */
00012   static SENSORS_STRUCT sensors = {
00013     {
00014       0U, 0U }
00015     ,                                  /* adc_phase_currents */
00016     0U,                                /* encoder_valid */
00017     0U                                 /* encoder_counter */
00018   } ;
00019   
00020   
00021 void rt_OneStep(void);
00022 void rt_OneStep(void)
00023 {
00024   static boolean_T OverrunFlag = false;
00025 
00026   /* '<Root>/motor_on' */
00027   static uint16_T motor_on = 1U;
00028 
00029   /* '<Root>/command_type' */
00030   static EnumCommandType command_type = Velocity;
00031 
00032   /* '<Root>/command_value' */
00033   static real32_T current_request = 0.0F;
00034 
00035   /* '<Root>/error' */
00036   static EnumErrorType error;
00037 
00038   /* Disable interrupts here */
00039 
00040   /* Check for overrun */
00041   if (OverrunFlag) {
00042     printf("------------------OVERRUN!!\r\n");
00043     return;
00044   }
00045 
00046   OverrunFlag = true;
00047 
00048   /* Save FPU context here (if necessary) */
00049   /* Re-enable timer or interrupt here */
00050   /* Set model inputs here */
00051 
00052   timer1.start();
00053 
00054   /* Step the model for base rate */
00055   error = Controller(motor_on, command_type, current_request, &sensors,
00056                      pwm_compare);
00057 
00058   timer1.stop();
00059   
00060   printf("exec us: %d err: %d\r\n",timer1.read_us(),error);
00061   timer1.reset();
00062   
00063   /* Get model outputs here */
00064 
00065   /* Indicate task complete */
00066   OverrunFlag = false;
00067 
00068   /* Disable interrupts here */
00069   /* Restore FPU context here (if necessary) */
00070   /* Enable interrupts here */
00071 }
00072 
00073 
00074 Serial pc(SERIAL_TX, SERIAL_RX);
00075  
00076 DigitalOut myled(LED1);
00077  
00078 int main() {
00079   
00080     /* Initialize model */
00081   Controller_Init();
00082   srand(time(NULL));
00083   int i = 1;
00084   pc.printf("PMSM Init done.\n");
00085   
00086   while(1) { 
00087       wait(0.5);
00088       sensors.adc_phase_currents[0] = rand()%MAX_uint16_T;
00089       sensors.adc_phase_currents[1] = rand()%MAX_uint16_T;
00090       printf("%d %d\r\n",sensors.adc_phase_currents[0],sensors.adc_phase_currents[1]);
00091       sensors.encoder_valid = 1;
00092       sensors.encoder_counter = i*20;
00093 
00094       rt_OneStep();
00095       
00096       pc.printf("%d: pwm0: %d vel: %f mode: %d\r\n", i++, pwm_compare[0], velocity_measured, controller_mode);
00097       myled = !myled;
00098       
00099       printf("arm: %f\r\n",arm_sin_f32(3.14159F/3));
00100   }
00101 }
00102