X-CUBE-SPN11 for X-NUCLEO-IHM11M1: 6Step_Lib.c Source File

X-CUBE-SPN11 for X-NUCLEO-IHM11M1

6Step_Lib.c
Go to the documentation of this file.
1 /**
2  ******************************************************************************
3  * @file 6Step_Lib.c
4  * @author System lab - Automation and Motion control team
5  * @version V1.0.0
6  * @date 06-July-2015
7  * @brief This file provides the set of functions for Motor Control library
8  ******************************************************************************
9  * @attention
10  *
11  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  * 3. Neither the name of STMicroelectronics nor the names of its contributors
21  * may be used to endorse or promote products derived from this software
22  * without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  ******************************************************************************
36  */
37 
38 /*! ****************************************************************************
39 ================================================================================
40  ###### Main functions for 6-Step algorithm ######
41 ================================================================================
42 The main function are the following:
43 
44 1) MC_SixStep_TABLE(...) -> Set the peripherals (TIMx, GPIO etc.) for each step
45 2) MC_SixStep_ARR_step() -> Generate the ARR value for Low Frequency TIM during start-up
46 3) MC_SixStep_INIT() -> Init the main variables for motor driving from MC_SixStep_param.h
47 4) MC_SixStep_RESET() -> Reset all variables used for 6Step control algorithm
48 5) MC_SixStep_Ramp_Motor_calc() -> Calculate the acceleration profile step by step for motor during start-up
49 6) MC_SixStep_NEXT_step()-> Generate the next step number according with the direction (CW or CCW)
50 7) MC_Task_Speed() -> Speed Loop with PI regulator
51 8) MC_Set_Speed(...) -> Set the new motor speed value
52 9) MC_StartMotor() -> Start the Motor
53 10)MC_StopMotor() -> Stop the Motor
54 *******************************************************************************/
55 
56 /* Includes ------------------------------------------------------------------*/
57 #include "6Step_Lib.h"
58 
59 #include <string.h>
60 
61 /** @addtogroup MIDDLEWARES MIDDLEWARES
62  * @brief Middlewares Layer
63  * @{
64  */
65 
66 
67 /** @addtogroup MC_6-STEP_LIB MC_6-STEP_LIB
68  * @brief Motor Control driver
69  * @{
70  */
71 
72 /* Data struct ---------------------------------------------------------------*/
73 SIXSTEP_Base_InitTypeDef SIXSTEP_parameters; /*!< Main SixStep structure*/
74 SIXSTEP_PI_PARAM_InitTypeDef_t PI_parameters; /*!< SixStep PI regulator structure*/
75 
76 /* Variables -----------------------------------------------------------------*/
77 uint16_t Rotor_poles_pairs; /*!< Number of pole pairs of the motor */
78 uint32_t mech_accel_hz = 0; /*!< Hz -- Mechanical acceleration rate */
79 uint32_t constant_k = 0; /*!< 1/3*mech_accel_hz */
80 uint32_t Time_vector_tmp = 0; /*!< Startup variable */
81 uint32_t Time_vector_prev_tmp = 0 ; /*!< Startup variable */
82 uint32_t T_single_step = 0; /*!< Startup variable */
83 uint32_t T_single_step_first_value = 0; /*!< Startup variable */
84 int32_t delta = 0; /*!< Startup variable */
85 uint16_t index_array = 1; /*!< Speed filter variable */
86 int16_t speed_tmp_array[FILTER_DEEP]; /*!< Speed filter variable */
87 uint16_t speed_tmp_buffer[FILTER_DEEP]; /*!< Potentiometer filter variable */
88 uint16_t HFBuffer[HFBUFFERSIZE]; /*!< Buffer for Potentiometer Value Filtering at the High-Frequency ADC conversion */
89 uint16_t HFBufferIndex = 0; /*!< High-Frequency Buffer Index */
90 uint8_t array_completed = FALSE; /*!< Speed filter variable */
91 uint8_t buffer_completed = FALSE; /*!< Potentiometer filter variable */
92 uint8_t UART_FLAG_RECEIVE = FALSE; /*!< UART commmunication flag */
93 uint32_t ARR_LF = 0; /*!< Autoreload LF TIM variable */
94 int32_t Mech_Speed_RPM = 0; /*!< Mechanical motor speed */
95 int32_t El_Speed_Hz = 0; /*!< Electrical motor speed */
96 uint16_t index_adc_chn = 0; /*!< Index of ADC channel selector for measuring */
97 uint16_t index_motor_run = 0; /*!< Tmp variable for DEMO mode */
98 uint16_t test_motor_run = 1; /*!< Tmp variable for DEMO mode */
99 uint8_t Enable_start_button = TRUE; /*!< Start/stop button filter to avoid double command */
100 uint16_t index_ARR_step = 1;
101 uint32_t n_zcr_startup = 0;
102 uint16_t index_startup_motor = 1;
103 uint16_t target_speed = TARGET_SPEED; /*!< Target speed for closed loop control */
104 uint16_t shift_n_sqrt = 14;
105 uint16_t cnt_bemf_event = 0;
107 uint8_t speed_fdbk_error = 0;
108 __IO uint32_t uwTick = 0; /*!< Tick counter - 1msec updated */
110 uint16_t index_align = 1;
111 int32_t speed_sum_sp_filt = 0;
112 int32_t speed_sum_pot_filt = 0;
113 uint16_t index_pot_filt = 1;
114 int16_t potent_filtered = 0;
115 uint32_t Tick_cnt = 0;
116 uint32_t counter_ARR_Bemf = 0;
118 
120 uint16_t MC_Potentiometer_filter(uint16_t);
121 uint64_t MCM_Sqrt(uint64_t );
122 int32_t MC_GetElSpeedHz(void);
123 int32_t MC_GetMechSpeedRPM(void);
124 void MC_SixStep_NEXT_step(void);
125 void MC_Speed_Filter(void);
126 void MC_SixStep_ARR_step(void);
127 void MC_SixStep_TABLE(uint8_t);
130 void MC_Task_Speed(void);
131 void MC_SixStep_Alignment(void);
132 void MC_Bemf_Delay(void);
133 void MC_TIMx_SixStep_timebase(void);
134 void MC_ADCx_SixStep_Bemf(void);
136 void MC_SixStep_Ramp_Motor_calc(void);
142 void MC_SixStep_Stop_PWM_driving(void);
149 void MC_SixStep_ARR_Bemf(uint8_t);
150 void MC_UI_INIT(void);
151 void UART_Set_Value(void);
152 void UART_Communication_Task(void);
153 void MC_SixStep_Init_main_data(void);
154 void CMD_Parser(char* pCommandString);
156 
157 /** @defgroup MC_SixStep_TABLE MC_SixStep_TABLE
158  * @{
159  * @brief Set the peripherals (TIMx, GPIO etc.) for each step
160  * @param step_number: step number selected
161  * @retval None
162  */
163 
164 void MC_SixStep_TABLE(uint8_t step_number)
165 {
166  if(GPIO_COMM == 1)
167  {
168  HAL_GPIO_TogglePin(GPIO_PORT_COMM,GPIO_CH_COMM);
169  }
170 
171  switch (step_number)
172  {
173  case 1:
174  {
179  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[3];
180  }
181  break;
182  case 2:
183  {
188  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[2];
189  }
190  break;
191  case 3:
192  {
197  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[1];
198  }
199  break;
200  case 4:
201  {
206  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[3];
207  }
208  break;
209  case 5:
210  {
215  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[2];
216  }
217  break;
218  case 6:
219  {
224  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[1];
225  }
226  break;
227  }
228 }
229 
230 /**
231  * @}
232  */
233 
234 /** @defgroup MC_SixStep_NEXT_step MC_SixStep_NEXT_step
235  * @{
236  * @brief Generate the next step number according with the direction (CW or CCW)
237  * @retval uint8_t SIXSTEP_parameters.status
238  */
240 {
241 
242  if(SIXSTEP_parameters.CMD == TRUE)
243  {
244  SIXSTEP_parameters.CMD = FALSE;
246  }
247  ARR_LF = __HAL_TIM_GetAutoreload(&LF_TIMx);
248 
249  if(SIXSTEP_parameters.ALIGN_OK == TRUE)
250  {
251  SIXSTEP_parameters.speed_fdbk = MC_GetMechSpeedRPM();
252  SIXSTEP_parameters.demagn_counter = 1;
253  if(SIXSTEP_parameters.status_prev != SIXSTEP_parameters.step_position)
254  {
255  n_zcr_startup = 0;
256  }
257  if(PI_parameters.Reference>=0)
258  {
259  SIXSTEP_parameters.step_position++;
260  if(SIXSTEP_parameters.CL_READY == TRUE)
261  {
262  SIXSTEP_parameters.VALIDATION_OK = TRUE;
263  }
264  if(SIXSTEP_parameters.step_position>6)
265  {
266  SIXSTEP_parameters.step_position = 1;
267  }
268  }
269  else
270  {
271  SIXSTEP_parameters.step_position--;
272  if(SIXSTEP_parameters.CL_READY == TRUE)
273  {
274  SIXSTEP_parameters.VALIDATION_OK = TRUE;
275  }
276  if(SIXSTEP_parameters.step_position < 1)
277  {
278  SIXSTEP_parameters.step_position = 6;
279  }
280  }
281  }
282 
283  if(SIXSTEP_parameters.VALIDATION_OK == 1)
284  {
285  /* Motor Stall condition detection and Speed-Feedback error generation */
286  SIXSTEP_parameters.BEMF_Tdown_count++;
287  if (SIXSTEP_parameters.BEMF_Tdown_count>BEMF_CONSEC_DOWN_MAX)
288  {
289  speed_fdbk_error = 1;
290  }
291  else
292  {
293  __HAL_TIM_SetAutoreload(&LF_TIMx,0xFFFF);
294  }
295  }
296  MC_SixStep_TABLE(SIXSTEP_parameters.step_position);
297 
298  /* It controls if the changing step request appears during DOWNcounting
299  * in this case it changes the ADC channel */
300 
301  /* UP-COUNTING direction started DIR = 0*/
302  if(__HAL_TIM_DIRECTION_STATUS(&HF_TIMx))
303  {
304  switch (SIXSTEP_parameters.step_position)
305  {
306  case 1:
307  {
308  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[3];
309  }
310  break;
311  case 2:
312  {
313  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[2];
314  }
315  break;
316  case 3:
317  {
318  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[1];
319  }
320  break;
321  case 4:
322  {
323  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[3];
324  }
325  break;
326  case 5:
327  {
328  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[2];
329  }
330  break;
331  case 6:
332  {
333  SIXSTEP_parameters.CurrentRegular_BEMF_ch = SIXSTEP_parameters.Regular_channel[1];
334  }
335  break;
336  } /* end switch case*/
337 
339  }
340 
341 }
342 
343 /**
344  * @}
345  */
346 
347 /** @defgroup MC_SixStep_RESET MC_SixStep_RESET
348  * @{
349  * @brief Reset all variables used for 6Step control algorithm
350  * @retval None
351  */
352 
354 {
355  SIXSTEP_parameters.CMD = TRUE;
356  SIXSTEP_parameters.numberofitemArr = NUMBER_OF_STEPS;
357  SIXSTEP_parameters.ADC_BEMF_threshold_UP = BEMF_THRSLD_UP;
358  SIXSTEP_parameters.ADC_BEMF_threshold_DOWN = BEMF_THRSLD_DOWN;
359  SIXSTEP_parameters.Ireference = STARTUP_CURRENT_REFERENCE;
360  SIXSTEP_parameters.Speed_Loop_Time = SPEED_LOOP_TIME;
361  SIXSTEP_parameters.pulse_value = SIXSTEP_parameters.HF_TIMx_CCR;
362  SIXSTEP_parameters.Speed_target_ramp = MAX_POT_SPEED;
363  SIXSTEP_parameters.ALIGNMENT = FALSE;
364  SIXSTEP_parameters.Speed_Ref_filtered = 0;
365  SIXSTEP_parameters.demagn_value = INITIAL_DEMAGN_DELAY;
366 
367  SIXSTEP_parameters.CurrentRegular_BEMF_ch = 0;
368  SIXSTEP_parameters.status_prev = 0;
369  SIXSTEP_parameters.step_position = 0;
370 
371 
372  LF_TIMx.Init.Prescaler = SIXSTEP_parameters.LF_TIMx_PSC;
373  LF_TIMx.Instance->PSC = SIXSTEP_parameters.LF_TIMx_PSC;
374  LF_TIMx.Init.Period = SIXSTEP_parameters.LF_TIMx_ARR;
375  LF_TIMx.Instance->ARR = SIXSTEP_parameters.LF_TIMx_ARR;
376  HF_TIMx.Init.Prescaler = SIXSTEP_parameters.HF_TIMx_PSC;
377  HF_TIMx.Instance->PSC = SIXSTEP_parameters.HF_TIMx_PSC;
378  HF_TIMx.Init.Period = SIXSTEP_parameters.HF_TIMx_ARR;
379  HF_TIMx.Instance->ARR = SIXSTEP_parameters.HF_TIMx_ARR;
380  HF_TIMx.Instance->HF_TIMx_CCR1 = SIXSTEP_parameters.HF_TIMx_CCR;
381 
382  Rotor_poles_pairs = SIXSTEP_parameters.NUMPOLESPAIRS;
383  SIXSTEP_parameters.SYSCLK_frequency = HAL_RCC_GetSysClockFreq();
384 
388 
389  SIXSTEP_parameters.Regular_channel[1] = ADC_Bemf_CH1; /*BEMF1*/
390  SIXSTEP_parameters.Regular_channel[2] = ADC_Bemf_CH2; /*BEMF2*/
391  SIXSTEP_parameters.Regular_channel[3] = ADC_Bemf_CH3; /*BEMF3*/
392  SIXSTEP_parameters.ADC_SEQ_CHANNEL[0] = ADC_CH_1; /*CURRENT*/
393  SIXSTEP_parameters.ADC_SEQ_CHANNEL[1] = ADC_CH_2; /*SPEED*/
394  SIXSTEP_parameters.ADC_SEQ_CHANNEL[2] = ADC_CH_3; /*VBUS*/
395  SIXSTEP_parameters.ADC_SEQ_CHANNEL[3] = ADC_CH_4; /*TEMP*/
396 
397  SIXSTEP_parameters.step_position = 0;
398  SIXSTEP_parameters.demagn_counter = 0;
399  SIXSTEP_parameters.ALIGN_OK = FALSE;
400  SIXSTEP_parameters.VALIDATION_OK = 0;
401  SIXSTEP_parameters.ARR_OK = 0;
402  SIXSTEP_parameters.speed_fdbk_filtered = 0;
403  SIXSTEP_parameters.Integral_Term_sum = 0;
404  SIXSTEP_parameters.Current_Reference = 0;
405  SIXSTEP_parameters.Ramp_Start = 0;
406  SIXSTEP_parameters.RUN_Motor = 0;
407  SIXSTEP_parameters.speed_fdbk = 0;
408  SIXSTEP_parameters.BEMF_OK = FALSE;
409  SIXSTEP_parameters.CL_READY = FALSE;
410  SIXSTEP_parameters.SPEED_VALIDATED = FALSE;
411  SIXSTEP_parameters.BEMF_Tdown_count = 0; /* Reset of the Counter to detect Stop motor condition when a stall condition occurs*/
412 
413  uwTick = 0;
414  index_motor_run = 0;
415  test_motor_run = 1;
416  T_single_step = 0;
418  delta = 0;
419  Time_vector_tmp = 0;
421  Mech_Speed_RPM = 0;
422  El_Speed_Hz = 0;
423  index_adc_chn = 0;
424  mech_accel_hz = 0;
425  constant_k = 0;
426  ARR_LF = 0;
427  index_array = 1;
429  index_ARR_step = 1;
430  n_zcr_startup = 0;
431  cnt_bemf_event = 0;
433  speed_fdbk_error = 0;
434 
435  index_align = 1;
436  speed_sum_sp_filt = 0;
437  speed_sum_pot_filt = 0;
438  index_pot_filt = 1;
439  potent_filtered = 0;
440  Tick_cnt = 0;
441  counter_ARR_Bemf = 0;
443 
444  HFBufferIndex =0;
445  for(uint16_t i = 0; i < HFBUFFERSIZE;i++)
446  {
447  HFBuffer[i]=0;
448  }
449 
450  for(uint16_t i = 0; i < FILTER_DEEP;i++)
451  {
452  speed_tmp_array[i] = 0;
453  speed_tmp_buffer[i]= 0;
454  }
457 
458  if(PI_parameters.Reference < 0)
459  {
460  SIXSTEP_parameters.step_position = 1;
461  }
463  MC_Set_PI_param(&PI_parameters);
468 }
469 
470 /**
471  * @}
472  */
473 
474 /** @defgroup MC_SixStep_Ramp_Motor_calc MC_SixStep_Ramp_Motor_calc
475  * @{
476  * @brief Calculate the acceleration profile step by step for motor during start-up
477  * @retval None
478 */
480 {
481  uint32_t constant_multiplier = 100;
482  uint32_t constant_multiplier_2 = 4000000000;
483 
484  if(index_startup_motor == 1)
485  {
486  mech_accel_hz = SIXSTEP_parameters.ACCEL * Rotor_poles_pairs / 60;
487  constant_multiplier_tmp = (uint64_t)constant_multiplier*(uint64_t)constant_multiplier_2;
491  }
493  {
494  Time_vector_tmp = ((uint64_t) 1000 * (uint64_t)1000 * (uint64_t) MCM_Sqrt(((uint64_t)index_startup_motor * (uint64_t)constant_k)))/632455;
496  if(index_startup_motor==1)
497  {
498  T_single_step_first_value = (2 * 3141)*delta/1000;
499  SIXSTEP_parameters.ARR_value = (uint32_t)(65535);
500  }
501  else
502  {
503  T_single_step = (2 * 3141)*delta/1000;
504  SIXSTEP_parameters.ARR_value = (uint32_t)(65535 * T_single_step)/(T_single_step_first_value);
505  }
506  }
507  else index_startup_motor=1;
508 
509  if(index_startup_motor==1)
510  {
511  SIXSTEP_parameters.prescaler_value = (((SIXSTEP_parameters.SYSCLK_frequency/1000000)*T_single_step_first_value)/65535) - 1;
512  }
513  if(SIXSTEP_parameters.STATUS != ALIGNMENT && SIXSTEP_parameters.STATUS != START)
514  {
516  }
517  else Time_vector_tmp = 0;
519 
520 }
521 
522 /**
523  * @}
524  */
525 
526 
527 /**
528  * @brief It calculates the square root of a non-negative s64.
529  * It returns 0 for negative s64.
530  * @param Input uint64_t number
531  * @retval int32_t Square root of Input (0 if Input<0)
532  */
533 uint64_t MCM_Sqrt(uint64_t wInput)
534 {
535  uint8_t biter = 0u;
536  uint64_t wtemproot;
537  uint64_t wtemprootnew;
538 
539  if (wInput <= (uint64_t)((uint64_t)2097152<<shift_n_sqrt))
540  {
541  wtemproot = (uint64_t)((uint64_t)128<<shift_n_sqrt);
542  }
543  else
544  {
545  wtemproot = (uint64_t)((uint64_t)8192<<shift_n_sqrt);
546  }
547 
548  do
549  {
550  wtemprootnew = (wtemproot + wInput/wtemproot)>>1;
551  if (wtemprootnew == wtemproot)
552  {
553  biter = (shift_n_sqrt-1);
554  }
555  else
556  {
557  biter ++;
558  wtemproot = wtemprootnew;
559  }
560  }
561  while (biter < (shift_n_sqrt-1));
562 
563  return (wtemprootnew);
564 }
565 
566 
567 /** @defgroup MC_SixStep_ARR_step MC_SixStep_ARR_step
568  * @{
569  * @brief Generate the ARR value for Low Frequency TIM during start-up
570  * @retval None
571 */
572 
574 {
575 
576  if(SIXSTEP_parameters.ALIGNMENT == FALSE)
577  {
578  SIXSTEP_parameters.ALIGNMENT = TRUE;
579  }
580  if(SIXSTEP_parameters.ALIGN_OK == TRUE)
581  {
582  if(PI_parameters.Reference >= 0)
583  {
584  if(SIXSTEP_parameters.VALIDATION_OK != TRUE)
585  {
586  SIXSTEP_parameters.STATUS = STARTUP;
588  if(index_ARR_step < SIXSTEP_parameters.numberofitemArr)
589  {
590  LF_TIMx.Init.Period = SIXSTEP_parameters.ARR_value;
591  LF_TIMx.Instance->ARR = (uint32_t)LF_TIMx.Init.Period;
592  index_ARR_step++;
593  }
594  else if(SIXSTEP_parameters.ARR_OK == 0)
595  {
596  index_ARR_step = 1;
597  SIXSTEP_parameters.ACCEL>>=1;
598  if(SIXSTEP_parameters.ACCEL < MINIMUM_ACC)
599  {
600  SIXSTEP_parameters.ACCEL = MINIMUM_ACC;
601  }
602  MC_StopMotor();
603  SIXSTEP_parameters.STATUS = STARTUP_FAILURE;
604  }
605  }
606  else
607  {
608  SIXSTEP_parameters.ARR_OK = 1;
609  index_startup_motor = 1;
610  index_ARR_step = 1;
611  }
612  }
613  else
614  {
615  if(SIXSTEP_parameters.VALIDATION_OK != TRUE)
616  {
617  SIXSTEP_parameters.STATUS = STARTUP;
619  if(index_ARR_step < SIXSTEP_parameters.numberofitemArr)
620  {
621  LF_TIMx.Init.Period = SIXSTEP_parameters.ARR_value;
622  LF_TIMx.Instance->ARR = (uint32_t)LF_TIMx.Init.Period;
623  index_ARR_step++;
624  }
625  else if(SIXSTEP_parameters.ARR_OK==0)
626  {
627  index_ARR_step = 1;
628  SIXSTEP_parameters.ACCEL>>=1;
629  if(SIXSTEP_parameters.ACCEL < MINIMUM_ACC)
630  {
631  SIXSTEP_parameters.ACCEL = MINIMUM_ACC;
632  }
633  MC_StopMotor();
634  SIXSTEP_parameters.STATUS = STARTUP_FAILURE;
635  }
636  }
637  else
638  {
639  SIXSTEP_parameters.ARR_OK = 1;
640  index_startup_motor = 1;
641  index_ARR_step = 1;
642  }
643  }
644  }
645 }
646 
647 /**
648  * @}
649  */
650 
651 /** @defgroup MC_SixStep_Alignment MC_SixStep_Alignment
652  * @{
653  * @brief Generate the motor alignment
654  * @retval None
655 */
656 
658 {
659  SIXSTEP_parameters.step_position = 6;
660  LF_TIMx.Init.Period = SIXSTEP_parameters.ARR_value;
661  LF_TIMx.Instance->ARR = (uint32_t)LF_TIMx.Init.Period;
662  SIXSTEP_parameters.STATUS = ALIGNMENT;
664  index_align++;
665  if(index_align >= TIME_FOR_ALIGN+1)
666  {
667  SIXSTEP_parameters.ALIGN_OK = TRUE;
668  SIXSTEP_parameters.STATUS = STARTUP;
669  index_startup_motor = 1;
671  LF_TIMx.Init.Prescaler = SIXSTEP_parameters.prescaler_value;
672  LF_TIMx.Instance->PSC = LF_TIMx.Init.Prescaler;
673  index_align = 0;
674  }
675 }
676 
677 /**
678  * @}
679  */
680 
681 /** @defgroup MC_SixStep_Speed_Val_target_potentiometer MC_SixStep_Speed_Val_target_potentiometer
682  * @{
683  * @brief Calculate the Motor Speed validation threshold according with the potentiometer value
684  * @retval None
685 */
686 
688 {
689  target_speed = SIXSTEP_parameters.ADC_Regular_Buffer[1] * MAX_POT_SPEED/ 4096;
690 
693 
696 }
697 /**
698  * @}
699  */
700 
701 /** @defgroup MC_SixStep_Speed_Potentiometer MC_SixStep_Speed_Potentiometer
702  * @{
703  * @brief Calculate the potentiometer value to set the Motor Speed
704  * @retval None
705 */
706 
708 {
709  uint16_t i=0;
710  uint32_t sum = 0;
711  uint16_t mean = 0;
712  uint16_t max = 0;
713  for (i = 0; i < HFBUFFERSIZE; i++)
714  {
715  uint16_t val = HFBuffer[i];
716  sum += val;
717  if (val > max)
718  {
719  max = val;
720  }
721  }
722  sum -= max;
723  mean = sum / (HFBUFFERSIZE - 1);
724 
725  SIXSTEP_parameters.Speed_Ref_filtered = MC_Potentiometer_filter(mean);
726 
727 }
728 
729 /**
730  * @}
731  */
732 
733 /** @defgroup MC_Set_PI_param MC_Set_PI_param
734  * @{
735  * @brief Set all parameters for PI regulator
736  * @param PI_PARAM
737  * @retval None
738 */
739 
741 {
742  if(SIXSTEP_parameters.CW_CCW == 0)
743  PI_PARAM->Reference = target_speed;
744  else
745  PI_PARAM->Reference = -target_speed;
746 
747  PI_PARAM->Kp_Gain = SIXSTEP_parameters.KP;
748  PI_PARAM->Ki_Gain = SIXSTEP_parameters.KI;
749 
750  PI_PARAM->Lower_Limit_Output = LOWER_OUT_LIMIT;
751  PI_PARAM->Upper_Limit_Output = UPPER_OUT_LIMIT;
752  PI_PARAM->Max_PID_Output = FALSE;
753  PI_PARAM->Min_PID_Output = FALSE;
754 }
755 
756 /**
757  * @}
758  */
759 
760 /** @defgroup MC_PI_Controller MC_PI_Controller
761  * @{
762  * @brief Compute the PI output for the Current Reference
763  * @param PI_PARAM PI parameters structure
764  * @param speed_fdb motor_speed_value
765  * @retval int16_t Currente reference
766 */
767 
768 int16_t MC_PI_Controller(SIXSTEP_PI_PARAM_InitTypeDef_t *PI_PARAM, int16_t speed_fdb)
769 {
770  int32_t wProportional_Term=0, wIntegral_Term=0, wOutput_32=0,wIntegral_sum_temp=0;
771  int32_t Error =0;
772 
773  Error = (PI_PARAM->Reference - speed_fdb);
774 
775  /* Proportional term computation*/
776  wProportional_Term = PI_PARAM->Kp_Gain * Error;
777 
778  /* Integral term computation */
779  if (PI_PARAM->Ki_Gain == 0)
780  {
781  SIXSTEP_parameters.Integral_Term_sum = 0;
782  }
783  else
784  {
785  wIntegral_Term = PI_PARAM->Ki_Gain * Error;
786  wIntegral_sum_temp = SIXSTEP_parameters.Integral_Term_sum + wIntegral_Term;
787  SIXSTEP_parameters.Integral_Term_sum = wIntegral_sum_temp;
788  }
789 
790  if(SIXSTEP_parameters.Integral_Term_sum> KI_DIV * PI_PARAM->Upper_Limit_Output)
791  SIXSTEP_parameters.Integral_Term_sum = KI_DIV* PI_PARAM->Upper_Limit_Output;
792 
793  if(SIXSTEP_parameters.Integral_Term_sum<-KI_DIV* PI_PARAM->Upper_Limit_Output)
794  SIXSTEP_parameters.Integral_Term_sum = -KI_DIV* PI_PARAM->Upper_Limit_Output;
795 
796  /* WARNING: the below instruction is not MISRA compliant, user should verify
797  that Cortex-M3 assembly instruction ASR (arithmetic shift right)
798  is used by the compiler to perform the shifts (instead of LSR
799  logical shift right)*/
800 
801  wOutput_32 = (wProportional_Term/KP_DIV) + (SIXSTEP_parameters.Integral_Term_sum/KI_DIV);
802 
803  if(PI_PARAM->Reference>0)
804  {
805  if (wOutput_32 > PI_PARAM->Upper_Limit_Output)
806  {
807  wOutput_32 = PI_PARAM->Upper_Limit_Output;
808  }
809  else if (wOutput_32 < PI_PARAM->Lower_Limit_Output)
810  {
811  wOutput_32 = PI_PARAM->Lower_Limit_Output;
812  }
813  }
814  else
815 {
816  if (wOutput_32 < (- PI_PARAM->Upper_Limit_Output) )
817  {
818  wOutput_32 = - (PI_PARAM->Upper_Limit_Output);
819  }
820  else if (wOutput_32 > (-PI_PARAM->Lower_Limit_Output))
821  {
822  wOutput_32 = (-PI_PARAM->Lower_Limit_Output);
823  }
824  }
825  return((int16_t)(wOutput_32));
826 }
827 
828 /**
829  * @}
830  */
831 
832 
833 /** @defgroup MC_Task_Speed MC_Task_Speed
834  * @{
835  * @brief Main task: Speed Loop with PI regulator
836  * @retval None
837 */
839 {
840 
841  if(dac_status == TRUE)
842  {
843  SET_DAC_value(SIXSTEP_parameters.speed_fdbk_filtered);
844  }
845 
846  if((SIXSTEP_parameters.speed_fdbk_filtered > (target_speed) || SIXSTEP_parameters.speed_fdbk_filtered < (-target_speed)) && SIXSTEP_parameters.VALIDATION_OK !=TRUE)
847  {
848  SIXSTEP_parameters.STATUS = VALIDATION;
849  SIXSTEP_parameters.SPEED_VALIDATED = TRUE;
850  }
851 
852  if(SIXSTEP_parameters.SPEED_VALIDATED == TRUE && SIXSTEP_parameters.BEMF_OK == TRUE && SIXSTEP_parameters.CL_READY != TRUE)
853  {
854  SIXSTEP_parameters.CL_READY = TRUE;
855  }
856 
857  if(SIXSTEP_parameters.VALIDATION_OK == TRUE)
858  {
859  /*****************************************************************************/
860  SIXSTEP_parameters.STATUS = RUN;
861  /*****************************************************************************/
862 
863  if(PI_parameters.Reference>=0)
864  {
865  SIXSTEP_parameters.Current_Reference = (uint16_t)MC_PI_Controller(&PI_parameters,(int16_t)SIXSTEP_parameters.speed_fdbk_filtered);
866  }
867  else
868  {
869  SIXSTEP_parameters.Current_Reference = (uint16_t)(-MC_PI_Controller(&PI_parameters,(int16_t)SIXSTEP_parameters.speed_fdbk_filtered));
870  }
871 
873  // SIXSTEP_parameters.pulse_value=SIXSTEP_parameters.Current_Reference;
874 
875  }
876  MC_Bemf_Delay();
877 }
878 
879 /**
880  * @}
881  */
882 
883 /** @defgroup MC_Set_Speed MC_Set_Speed
884  * @{
885  * @brief Set the new motor speed value
886  * @param speed_value: set new motor speed
887  * @retval None
888 */
889 void MC_Set_Speed(uint16_t speed_value)
890 {
891 
892 #if (POTENTIOMETER == 1)
893  uint8_t change_target_speed = 0;
894  int16_t reference_tmp = 0;
895 
896  if (SIXSTEP_parameters.Speed_Ref_filtered > SIXSTEP_parameters.Speed_target_ramp)
897  {
898  if ((SIXSTEP_parameters.Speed_Ref_filtered - SIXSTEP_parameters.Speed_target_ramp) > ADC_SPEED_TH)
899  {
900  change_target_speed = 1;
901  }
902  else
903  {
904  /* Not change target speed because less than threshold */
905  }
906  }
907  else
908  {
909  if ((SIXSTEP_parameters.Speed_target_ramp - SIXSTEP_parameters.Speed_Ref_filtered) > ADC_SPEED_TH)
910  {
911  change_target_speed = 1;
912  }
913  else
914  {
915  /* Not change target speed because less than threshold */
916  }
917  }
918  if (change_target_speed == 1)
919  {
920  SIXSTEP_parameters.Speed_target_ramp = SIXSTEP_parameters.Speed_Ref_filtered;
921 
922  if(SIXSTEP_parameters.CW_CCW == 0)
923  {
924  reference_tmp = SIXSTEP_parameters.Speed_Ref_filtered * MAX_POT_SPEED / 4096;
925  if(reference_tmp <= MIN_POT_SPEED)
926  {
927  PI_parameters.Reference = MIN_POT_SPEED;
928  }
929  else
930  {
931  PI_parameters.Reference = reference_tmp;
932  }
933  }
934  else
935  {
936  reference_tmp = -(SIXSTEP_parameters.Speed_Ref_filtered * MAX_POT_SPEED / 4096);
937  if(reference_tmp >=- MIN_POT_SPEED)
938  {
939  PI_parameters.Reference = -MIN_POT_SPEED;
940  }
941  else
942  {
943  PI_parameters.Reference= reference_tmp;
944  }
945  }
946 
947  }
948 #else
949  if(speed_value != 0)
950  PI_parameters.Reference = speed_value;
951 #endif
952 
953 }
954 
955 /**
956  * @}
957  */
958 
959 
960 /** @defgroup MC_Bemf_Delay MC_Bemf_Delay
961  * @{
962  * @brief Take the delay time after each new 6-step commutation
963  * @retval None
964 */
966 {
967  Bemf_delay_calc();
968 }
969 /**
970  * @}
971  */
972 
973 /** @defgroup MC_StartMotor MC_StartMotor
974  * @{
975  * @brief Start the Motor
976  * @retval None
977 */
979 {
980  uwTick = 0;
981  SIXSTEP_parameters.STATUS = START;
982  HAL_TIM_Base_Start_IT(&LF_TIMx);
983  HAL_ADC_Start_IT(&ADCx);
984  SIXSTEP_parameters.RUN_Motor = 1;
986  if(dac_status == TRUE)
987  {
988  START_DAC();
989  }
990 }
991 /**
992  * @}
993  */
994 
995 /** @defgroup MC_StopMotor MC_StopMotor
996  * @{
997  * @brief Stop the Motor
998  * @retval None
999 */
1001 {
1002  uwTick = 0;
1003  SIXSTEP_parameters.STATUS = STOP;
1004  SIXSTEP_parameters.RUN_Motor = 0;
1006  HF_TIMx.Instance->CR1 &= ~(TIM_CR1_CEN);
1007  HF_TIMx.Instance->CNT = 0;
1009  HAL_TIM_Base_Stop_IT(&LF_TIMx);
1010  HAL_ADC_Stop_IT(&ADCx);
1013  MC_SixStep_RESET();
1014 }
1015 
1016 /**
1017  * @}
1018  */
1019 
1020 /** @defgroup MC_GetElSpeedHz MC_GetElSpeedHz
1021  * @{
1022  * @brief Get the Eletrical Motor Speed from ARR value of LF TIM
1023  * @retval int32_t Return the electrical motor speed
1024 */
1026 {
1027  if(__HAL_TIM_GetAutoreload(&LF_TIMx) != 0xFFFF)
1028  {
1029  uint16_t prsc = LF_TIMx.Instance->PSC;
1030  El_Speed_Hz = (int32_t)((SIXSTEP_parameters.SYSCLK_frequency)/(prsc))/(__HAL_TIM_GetAutoreload(&LF_TIMx)*6);
1031  }
1032  else
1033  El_Speed_Hz = 0;
1034  if(PI_parameters.Reference<0)
1035  return (-El_Speed_Hz);
1036  else
1037  return (El_Speed_Hz);
1038 }
1039 /**
1040  * @}
1041  */
1042 
1043 /** @defgroup MC_GetMechSpeedRPM MC_GetMechSpeedRPM
1044  * @{
1045  * @brief Get the Mechanical Motor Speed (RPM)
1046  * @retval int32_t Return the mechanical motor speed (RPM
1047 */
1048 
1050 {
1051  Mech_Speed_RPM = (int32_t)(MC_GetElSpeedHz() * 60 / Rotor_poles_pairs);
1052  return (Mech_Speed_RPM);
1053 }
1054 
1055 /**
1056  * @}
1057  */
1058 
1059 /** @defgroup MC_SixStep_Init_main_data MC_SixStep_Init_main_data
1060  * @{
1061  * @brief Init the main variables for motor driving from MC_SixStep_param.h
1062  * @retval None
1063 */
1064 
1066 {
1067  SIXSTEP_parameters.Ireference = STARTUP_CURRENT_REFERENCE;
1068  SIXSTEP_parameters.NUMPOLESPAIRS = NUM_POLE_PAIRS;
1069  SIXSTEP_parameters.ACCEL = ACC;
1070  SIXSTEP_parameters.KP = KP_GAIN;
1071  SIXSTEP_parameters.KI = KI_GAIN;
1072  SIXSTEP_parameters.CW_CCW = DIRECTION;
1073  SIXSTEP_parameters.Potentiometer = POTENTIOMETER;
1074 }
1075 
1076 /**
1077  * @}
1078  */
1079 
1080 
1081 /** @defgroup MC_SixStep_INIT MC_SixStep_INIT
1082  * @{
1083  * @brief Initialitation function for SixStep library
1084  * @retval None
1085 */
1086 
1088 {
1090  SIXSTEP_parameters.HF_TIMx_CCR = HF_TIMx.Instance->HF_TIMx_CCR1;
1091  SIXSTEP_parameters.HF_TIMx_ARR = HF_TIMx.Instance->ARR;
1092  SIXSTEP_parameters.HF_TIMx_PSC = HF_TIMx.Instance->PSC;
1093  SIXSTEP_parameters.LF_TIMx_ARR = LF_TIMx.Instance->ARR;
1094  SIXSTEP_parameters.LF_TIMx_PSC = LF_TIMx.Instance->PSC;
1095 
1096 // MC_SixStep_Current_Reference_Start();
1098 
1099  #ifdef UART_COMM
1100  SIXSTEP_parameters.Button_ready = FALSE;
1101  MC_UI_INIT(); /*!< Start the UART Communication Task*/
1102  #endif
1103 
1105 
1106  #ifndef UART_COMM
1107  SIXSTEP_parameters.Button_ready = TRUE;
1108  #endif
1109  MC_SixStep_RESET();
1110 }
1111 
1112 /**
1113  * @}
1114  */
1115 
1116 
1117 /** @defgroup MC_TIMx_SixStep_timebase MC_TIMx_SixStep_timebase
1118  * @{
1119  * @brief Low Frequency Timer Callback - Call the next step and request the filtered speed value
1120  * @retval None
1121 */
1122 
1124 {
1125  MC_SixStep_NEXT_step(); /*Change STEP number */
1126  if(SIXSTEP_parameters.ARR_OK == 0)
1127  {
1128  MC_SixStep_ARR_step(); /*BASE TIMER - ARR modification for STEP frequency changing */
1129  }
1130 
1131  MC_Speed_Filter(); /*Calculate SPEED filtered */
1132 }
1133 
1134 /**
1135  * @}
1136  */
1137 
1138 /** @defgroup MC_Speed_Filter MC_Speed_Filter
1139  * @{
1140  * @brief Calculate the speed filtered
1141  * @retval None
1142 */
1143 
1145 {
1146  if(array_completed == FALSE)
1147  {
1148  speed_tmp_array[index_array] = SIXSTEP_parameters.speed_fdbk;
1149  speed_sum_sp_filt = 0;
1150  for(uint16_t i = 1; i <= index_array;i++)
1151  {
1153  }
1154  SIXSTEP_parameters.speed_fdbk_filtered = speed_sum_sp_filt/index_array;
1155  index_array++;
1156 
1157  if(index_array >= FILTER_DEEP)
1158  {
1159  index_array = 1;
1161  }
1162  }
1163  else
1164  {
1165  index_array++;
1166  if(index_array >= FILTER_DEEP)
1167  index_array = 1;
1168 
1169  speed_sum_sp_filt = 0;
1170  speed_tmp_array[index_array] = SIXSTEP_parameters.speed_fdbk;
1171  for(uint16_t i = 1; i < FILTER_DEEP;i++)
1172  {
1174  }
1175  SIXSTEP_parameters.speed_fdbk_filtered = speed_sum_sp_filt/(FILTER_DEEP-1);
1176  }
1177 }
1178 
1179 /**
1180  * @}
1181  */
1182 
1183 /** @defgroup MC_Potentiometer_filter MC_Potentiometer_filter
1184  * @{
1185  * @brief Calculate the filtered potentiometer value
1186  * @retval uint16_t Return the filtered potentiometer value
1187 */
1188 
1189 uint16_t MC_Potentiometer_filter(uint16_t potentiometer_value)
1190 {
1191  if(buffer_completed == FALSE)
1192  {
1193  speed_tmp_buffer[index_pot_filt] = potentiometer_value;
1194  speed_sum_pot_filt = 0;
1195  for(uint16_t i = 1; i <= index_pot_filt;i++)
1196  {
1198  }
1200  index_pot_filt++;
1201 
1202  if(index_pot_filt >= FILTER_DEEP)
1203  {
1204  index_pot_filt = 1;
1206  }
1207  }
1208  else
1209  {
1210  index_pot_filt++;
1212  {
1213  index_pot_filt = 1;
1214  }
1215 
1216  speed_sum_pot_filt = 0;
1217  speed_tmp_buffer[index_pot_filt] = potentiometer_value;
1218  uint16_t speed_max = 0;
1219  for(uint16_t i = 1; i < FILTER_DEEP;i++)
1220  {
1221  uint16_t val = speed_tmp_buffer[i];
1222  if (val > speed_max)
1223  {
1224  speed_max = val;
1225  }
1226  speed_sum_pot_filt += val;
1227  }
1228  speed_sum_pot_filt -= speed_max;
1229  potent_filtered = speed_sum_pot_filt/(FILTER_DEEP-2);
1230  }
1231  if(potent_filtered==0) potent_filtered = 1;
1232 
1233 return(potent_filtered);
1234 }
1235 
1236 /**
1237  * @}
1238  */
1239 
1240 /** @defgroup MC_SysTick_SixStep_MediumFrequencyTask MC_SysTick_SixStep_MediumFrequencyTask
1241  * @{
1242  * @brief Systick Callback - Call the Speed loop
1243  * @retval None
1244 */
1245 
1247 {
1248  if(SIXSTEP_parameters.ALIGNMENT == TRUE && SIXSTEP_parameters.ALIGN_OK == FALSE)
1249  {
1251  }
1252 
1253 #ifdef UART_COMM
1255 #endif
1256 
1257 #ifdef DEMOMODE
1258  index_motor_run++;
1260  {
1261  MC_StopMotor();
1262  index_motor_run=0;
1263  test_motor_run=1;
1264  }
1266  {
1267  MC_StartMotor();
1268  test_motor_run = 0;
1269  index_motor_run=0;
1270  }
1271 #endif
1272 
1273  if(SIXSTEP_parameters.VALIDATION_OK == TRUE && SIXSTEP_parameters.Potentiometer == TRUE)
1274  {
1276  }
1277  /* Push button delay time to avoid double command */
1279  {
1281  }
1282 
1283  /* SIXSTEP_parameters.Speed_Loop_Time x 1msec */
1284  if(Tick_cnt >= SIXSTEP_parameters.Speed_Loop_Time)
1285  {
1286  if(SIXSTEP_parameters.STATUS != SPEEDFBKERROR)
1287  {
1288  MC_Task_Speed();
1289  }
1290  SIXSTEP_parameters.MediumFrequencyTask_flag = TRUE;
1291  if(SIXSTEP_parameters.VALIDATION_OK == TRUE)
1292  {
1293  MC_Set_Speed(0);
1294  }
1295  Tick_cnt=0;
1296  }
1297  else Tick_cnt++;
1298 
1299  if(startup_bemf_failure == 1)
1300  {
1301  SIXSTEP_parameters.ACCEL>>=1;
1302  if(SIXSTEP_parameters.ACCEL < MINIMUM_ACC)
1303  {
1304  SIXSTEP_parameters.ACCEL = MINIMUM_ACC;
1305  }
1306  MC_StopMotor();
1307  cnt_bemf_event = 0;
1308  SIXSTEP_parameters.STATUS = STARTUP_BEMF_FAILURE;
1309  }
1310 
1311  if(speed_fdbk_error == 1)
1312  {
1313  MC_StopMotor();
1314  SIXSTEP_parameters.STATUS = SPEEDFBKERROR;
1315  }
1316 }
1317 
1318 /**
1319  * @}
1320  */
1321 
1322 /** @defgroup MC_SixStep_ARR_Bemf MC_SixStep_ARR_Bemf
1323  * @{
1324  * @brief Calculate the new Autoreload value (ARR) for Low Frequency timer
1325  * @retval None
1326 */
1327 
1328 void MC_SixStep_ARR_Bemf(uint8_t up_bemf)
1329 {
1330 
1331  if(SIXSTEP_parameters.status_prev != SIXSTEP_parameters.step_position)
1332  {
1333  if(SIXSTEP_parameters.SPEED_VALIDATED == TRUE)
1334  {
1335  if(GPIO_ZERO_CROSS == 1)
1336  {
1337  HAL_GPIO_TogglePin(GPIO_PORT_ZCR,GPIO_CH_ZCR);
1338  }
1340  {
1341  startup_bemf_failure = 1;
1342  }
1343 
1344  if(up_bemf == 1 && SIXSTEP_parameters.BEMF_OK !=TRUE)
1345  {
1346  n_zcr_startup++;
1347  cnt_bemf_event = 0;
1348  }
1349  else if(SIXSTEP_parameters.BEMF_OK !=TRUE)
1350  {
1351  cnt_bemf_event++;
1352  }
1353 
1354  if(n_zcr_startup>= NUMBER_ZCR && SIXSTEP_parameters.BEMF_OK !=TRUE )
1355  {
1356  SIXSTEP_parameters.BEMF_OK = TRUE;
1357  n_zcr_startup = 0;
1358  }
1359  }
1360  SIXSTEP_parameters.status_prev = SIXSTEP_parameters.step_position;
1361 
1362  if(SIXSTEP_parameters.VALIDATION_OK == 1)
1363  {
1364  counter_ARR_Bemf = __HAL_TIM_GetCounter(&LF_TIMx);
1365  __HAL_TIM_SetAutoreload(&LF_TIMx,(counter_ARR_Bemf+ARR_LF/2));
1366  }
1367  }
1368 
1369 }
1370 
1371 /**
1372  * @}
1373  */
1374 
1375 /** @defgroup MC_ADCx_SixStep_Bemf MC_ADCx_SixStep_Bemf
1376  * @{
1377  * @brief Compute the zero crossing detection
1378  * @retval None
1379 */
1380 
1382 {
1383 
1384  if(__HAL_TIM_DIRECTION_STATUS(&HF_TIMx))
1385  {
1386  HAL_GPIO_WritePin(GPIO_PORT_COMM,GPIO_CH_COMM,GPIO_PIN_SET);
1387  /* UP-counting direction started */
1388  /* GET the ADC value (PHASE CURRENT)*/
1389  if(SIXSTEP_parameters.STATUS != START && SIXSTEP_parameters.STATUS != ALIGNMENT)
1390  {
1391  switch (SIXSTEP_parameters.step_position)
1392  {
1393  case 6:
1394  {
1395  if(SIXSTEP_parameters.demagn_counter >= SIXSTEP_parameters.demagn_value)
1396  {
1397  SIXSTEP_parameters.ADC_BUFFER[1] = HAL_ADC_GetValue(&ADCx);
1398  if(PI_parameters.Reference>=0)
1399  {
1400  if(SIXSTEP_parameters.ADC_BUFFER[1]> SIXSTEP_parameters.ADC_BEMF_threshold_UP)
1401  {
1403  SIXSTEP_parameters.BEMF_Tdown_count = 0;
1404  }
1405  }
1406  else
1407  {
1408  if(SIXSTEP_parameters.ADC_BUFFER[1]< SIXSTEP_parameters.ADC_BEMF_threshold_DOWN)
1409  {
1411  }
1412  }
1413  }
1414  else SIXSTEP_parameters.demagn_counter++;
1415  }
1416  break;
1417  case 3:
1418  {
1419  if(SIXSTEP_parameters.demagn_counter >= SIXSTEP_parameters.demagn_value)
1420  {
1421  SIXSTEP_parameters.ADC_BUFFER[1] = HAL_ADC_GetValue(&ADCx);
1422  if(PI_parameters.Reference>=0)
1423  {
1424  if(SIXSTEP_parameters.ADC_BUFFER[1]< SIXSTEP_parameters.ADC_BEMF_threshold_DOWN)
1425  {
1427  }
1428  }
1429  else
1430  {
1431  if(SIXSTEP_parameters.ADC_BUFFER[1]> SIXSTEP_parameters.ADC_BEMF_threshold_UP)
1432  {
1434  SIXSTEP_parameters.BEMF_Tdown_count = 0;
1435  }
1436  }
1437  }
1438  else SIXSTEP_parameters.demagn_counter++;
1439  }
1440  break;
1441  case 5:
1442  {
1443  if(SIXSTEP_parameters.demagn_counter >= SIXSTEP_parameters.demagn_value)
1444  {
1445  SIXSTEP_parameters.ADC_BUFFER[2] = HAL_ADC_GetValue(&ADCx);
1446  if(PI_parameters.Reference>=0)
1447  {
1448  if(SIXSTEP_parameters.ADC_BUFFER[2]< SIXSTEP_parameters.ADC_BEMF_threshold_DOWN)
1449  {
1450  MC_SixStep_ARR_Bemf(0);
1451  }
1452  }
1453  else
1454  {
1455  if(SIXSTEP_parameters.ADC_BUFFER[2]> SIXSTEP_parameters.ADC_BEMF_threshold_UP)
1456  {
1458  SIXSTEP_parameters.BEMF_Tdown_count = 0;
1459  }
1460  }
1461  }
1462  else SIXSTEP_parameters.demagn_counter++;
1463  }
1464  break;
1465  case 2:
1466  {
1467  if(SIXSTEP_parameters.demagn_counter >= SIXSTEP_parameters.demagn_value)
1468  {
1469  SIXSTEP_parameters.ADC_BUFFER[2] = HAL_ADC_GetValue(&ADCx);
1470  if(PI_parameters.Reference>=0)
1471  {
1472  if(SIXSTEP_parameters.ADC_BUFFER[2]> SIXSTEP_parameters.ADC_BEMF_threshold_UP)
1473  {
1475  SIXSTEP_parameters.BEMF_Tdown_count = 0;
1476  }
1477  }
1478  else
1479  {
1480  if(SIXSTEP_parameters.ADC_BUFFER[2]< SIXSTEP_parameters.ADC_BEMF_threshold_DOWN)
1481  {
1483  }
1484  }
1485  }
1486  else SIXSTEP_parameters.demagn_counter++;
1487  }
1488  break;
1489  case 4:
1490  {
1491  if(SIXSTEP_parameters.demagn_counter >= SIXSTEP_parameters.demagn_value)
1492  {
1493  SIXSTEP_parameters.ADC_BUFFER[3] = HAL_ADC_GetValue(&ADCx);
1494  if(PI_parameters.Reference>=0)
1495  {
1496  if(SIXSTEP_parameters.ADC_BUFFER[3]> SIXSTEP_parameters.ADC_BEMF_threshold_UP)
1497  {
1499  SIXSTEP_parameters.BEMF_Tdown_count = 0;
1500  }
1501  }
1502  else
1503  {
1504  if(SIXSTEP_parameters.ADC_BUFFER[3]< SIXSTEP_parameters.ADC_BEMF_threshold_DOWN)
1505  {
1507  }
1508  }
1509  }
1510  else SIXSTEP_parameters.demagn_counter++;
1511  }
1512  break;
1513  case 1:
1514  {
1515  if(SIXSTEP_parameters.demagn_counter >= SIXSTEP_parameters.demagn_value)
1516  {
1517  SIXSTEP_parameters.ADC_BUFFER[3] = HAL_ADC_GetValue(&ADCx);
1518  if(PI_parameters.Reference>=0)
1519  {
1520  if(SIXSTEP_parameters.ADC_BUFFER[3]< SIXSTEP_parameters.ADC_BEMF_threshold_DOWN)
1521  {
1523  }
1524  }
1525  else
1526  {
1527  if(SIXSTEP_parameters.ADC_BUFFER[3]> SIXSTEP_parameters.ADC_BEMF_threshold_UP)
1528  {
1530  SIXSTEP_parameters.BEMF_Tdown_count = 0;
1531  }
1532  }
1533  }
1534  else SIXSTEP_parameters.demagn_counter++;
1535  }
1536  break;
1537  }
1538  }
1539  /******************* SET ADC CHANNEL FOR SPEED/CURRENT/VBUS *******************/
1540  /* Set the channel for next ADC Regular reading */
1542  /******************************************************************************/
1543  HAL_GPIO_WritePin(GPIO_PORT_COMM,GPIO_CH_COMM,GPIO_PIN_RESET);
1544  }
1545  else
1546  {
1547  /* Down-counting direction started */
1548  /* Set the channel for next ADC Regular reading */
1549 
1550  SIXSTEP_parameters.ADC_Regular_Buffer[index_adc_chn] = HAL_ADC_GetValue(&ADCx);
1551 
1552  if (index_adc_chn == 1)
1553  {
1554  HFBuffer[HFBufferIndex++] = HAL_ADC_GetValue(&ADCx);
1555  if (HFBufferIndex >= HFBUFFERSIZE)
1556  {
1557  HFBufferIndex = 0;
1558  }
1559  }
1560  index_adc_chn++;
1561  if(index_adc_chn>3) index_adc_chn = 0;
1562  MC_SixStep_ADC_Channel(SIXSTEP_parameters.CurrentRegular_BEMF_ch);
1563  }
1564 
1565 }
1566 
1567 /**
1568  * @}
1569  */
1570 
1571 /** @defgroup MC_EXT_button_SixStep MC_EXT_button_SixStep
1572  * @{
1573  * @brief GPIO EXT Callback - Start or Stop the motor through the Blue push button on STM32Nucleo
1574  * @retval None
1575 */
1576 
1578 {
1579  if(Enable_start_button == TRUE)
1580  {
1581  if(SIXSTEP_parameters.RUN_Motor == 0 && SIXSTEP_parameters.Button_ready == TRUE)
1582  {
1583  MC_StartMotor();
1585  }
1586  else
1587  {
1588  MC_StopMotor();
1590  }
1591  }
1592 }
1593 
1594 /**
1595  * @}
1596  */
1597 
1598 /**
1599  * @brief This function is called to increment a global variable "uwTick"
1600  * used as application time base.
1601  * @note In the default implementation, this variable is incremented each 1ms
1602  * in Systick ISR.
1603  * @note This function is declared as __weak to be overwritten in case of other
1604  * implementations in user file.
1605  * @retval None
1606  */
1607 void HAL_IncTick(void)
1608 {
1609  uwTick++;
1610 }
1611 
1612 /**
1613  * @brief Povides a tick value in millisecond.
1614  * @note The function is declared as __Weak to be overwritten in case of other
1615  * implementations in user file.
1616  * @retval tick value
1617  */
1618 uint32_t HAL_GetTick(void)
1619 {
1620  return uwTick;
1621 }
1622 
1623 
1624 /**
1625  * @} end MC_6-STEP_LIB
1626  */
1627 
1628 /**
1629  * @} end MIDDLEWARES
1630  */
int32_t MC_GetMechSpeedRPM(void)
Definition: 6Step_Lib.c:1049
uint16_t index_pot_filt
Definition: 6Step_Lib.c:113
#define GPIO_PORT_COMM
void MC_SixStep_DisableInput_CH1_D_CH2_D_CH3_D(void)
Enable Input channel CH2 and CH3 for STSPIN230.
uint16_t index_adc_chn
Definition: 6Step_Lib.c:96
#define MAX_POT_SPEED
void MC_SixStep_Current_Reference_Start(void)
Enable the Current Reference generation.
#define DIRECTION
int16_t potent_filtered
Definition: 6Step_Lib.c:114
#define ADC_Bemf_CH2
uint16_t Rotor_poles_pairs
Definition: 6Step_Lib.c:77
void MC_SixStep_Current_Reference_Setvalue(uint16_t)
Set the value for Current Reference.
uint16_t shift_n_sqrt
Definition: 6Step_Lib.c:104
#define INITIAL_DEMAGN_DELAY
void MC_SixStep_Stop_PWM_driving(void)
Disable PWM channels for STSPIN230.
int16_t speed_tmp_array[FILTER_DEEP]
Definition: 6Step_Lib.c:86
uint16_t index_motor_run
Definition: 6Step_Lib.c:97
void START_DAC()
Start DAC for debug.
uint64_t MCM_Sqrt(uint64_t)
It calculates the square root of a non-negative s64.
Definition: 6Step_Lib.c:533
#define ADC_Bemf_CH3
void MC_SixStep_EnableInput_CH1_E_CH2_E_CH3_D(void)
Enable Input channel CH1 and CH2 for STSPIN230.
void MC_SixStep_EnableInput_CH1_D_CH2_E_CH3_E(void)
Enable Input channel CH2 and CH3 for STSPIN230.
void MC_StartMotor()
Definition: 6Step_Lib.c:978
void MC_Set_Speed(uint16_t speed_value)
Definition: 6Step_Lib.c:889
#define BEMF_THRSLD_UP
int32_t delta
Definition: 6Step_Lib.c:84
#define FILTER_DEEP
uint32_t mech_accel_hz
Definition: 6Step_Lib.c:78
void BSP_X_NUCLEO_FAULT_LED_OFF()
uint16_t index_array
Definition: 6Step_Lib.c:85
#define GPIO_CH_ZCR
void MC_Speed_Filter(void)
Definition: 6Step_Lib.c:1144
void MC_SixStep_Start_PWM_driving(void)
Enable PWM channels for STSPIN230.
void MC_EXT_button_SixStep()
Definition: 6Step_Lib.c:1577
void BSP_X_NUCLEO_FAULT_LED_ON()
void UART_Set_Value(void)
uint8_t Enable_start_button
Definition: 6Step_Lib.c:99
SIXSTEP_PI_PARAM_InitTypeDef_t PI_parameters
Definition: 6Step_Lib.c:74
void MC_SixStep_HF_TIMx_SetDutyCycle_CH3(uint16_t)
Set the Duty Cycle value for CH3.
void MC_StopMotor()
Definition: 6Step_Lib.c:1000
uint16_t index_align
Definition: 6Step_Lib.c:110
#define GPIO_PORT_ZCR
uint32_t Time_vector_prev_tmp
Definition: 6Step_Lib.c:81
#define TARGET_SPEED
void MC_SixStep_Speed_Potentiometer(void)
Definition: 6Step_Lib.c:707
uint8_t buffer_completed
Definition: 6Step_Lib.c:91
#define MIN_POT_SPEED
uint32_t T_single_step_first_value
Definition: 6Step_Lib.c:83
#define NUMBER_ZCR
uint8_t speed_fdbk_error
Definition: 6Step_Lib.c:107
int32_t speed_sum_pot_filt
Definition: 6Step_Lib.c:112
Definition: 6Step_Lib.h:75
void MC_SixStep_Current_Reference_Stop(void)
Disable the Current Reference generation.
#define SPEED_LOOP_TIME
void MC_SixStep_ARR_step(void)
Definition: 6Step_Lib.c:573
uint8_t UART_FLAG_RECEIVE
Definition: 6Step_Lib.c:92
void MC_SysTick_SixStep_MediumFrequencyTask(void)
Definition: 6Step_Lib.c:1246
void MC_SixStep_RESET()
Definition: 6Step_Lib.c:353
uint32_t ADC_SEQ_CHANNEL[4]
Definition: 6Step_Lib.h:111
#define MINIMUM_ACC
uint16_t cnt_bemf_event
Definition: 6Step_Lib.c:105
int32_t Mech_Speed_RPM
Definition: 6Step_Lib.c:94
uint32_t ARR_LF
Definition: 6Step_Lib.c:93
uint32_t Tick_cnt
Definition: 6Step_Lib.c:115
void MC_SixStep_ARR_Bemf(uint8_t)
Definition: 6Step_Lib.c:1328
uint16_t MC_Potentiometer_filter(uint16_t)
Definition: 6Step_Lib.c:1189
uint16_t HFBufferIndex
Definition: 6Step_Lib.c:89
int32_t MC_GetElSpeedHz(void)
Definition: 6Step_Lib.c:1025
void MC_SixStep_INIT()
Definition: 6Step_Lib.c:1087
#define BEMF_CNT_EVENT_MAX
int32_t speed_sum_sp_filt
Definition: 6Step_Lib.c:111
uint16_t index_ARR_step
Definition: 6Step_Lib.c:100
void MC_SixStep_HF_TIMx_SetDutyCycle_CH1(uint16_t)
Set the Duty Cycle value for CH1.
#define ADC_CH_2
uint16_t target_speed
Definition: 6Step_Lib.c:103
void MC_Task_Speed(void)
Definition: 6Step_Lib.c:838
#define ADC_Bemf_CH1
void MC_SixStep_Alignment(void)
Definition: 6Step_Lib.c:657
#define BEMF_THRSLD_DOWN
uint32_t HAL_GetTick(void)
Povides a tick value in millisecond.
Definition: 6Step_Lib.c:1618
#define VAL_POT_SPEED_DIV
#define KP_DIV
void MC_SixStep_HF_TIMx_SetDutyCycle_CH2(uint16_t)
Set the Duty Cycle value for CH2.
uint8_t startup_bemf_failure
Definition: 6Step_Lib.c:106
#define ADC_CH_1
Definition: 6Step_Lib.h:73
SIXSTEP_Base_SystStatus_t STATUS
Definition: 6Step_Lib.h:102
uint32_t T_single_step
Definition: 6Step_Lib.c:82
void SET_DAC_value(uint16_t dac_value)
Set DAC value for debug.
uint16_t ADC_BEMF_threshold_UP
Definition: 6Step_Lib.h:113
#define DEMO_START_TIME
__IO uint32_t uwTick
Definition: 6Step_Lib.c:108
#define KP_GAIN
SIXSTEP_Base_InitTypeDef SIXSTEP_parameters
Definition: 6Step_Lib.c:73
#define ADC_SPEED_TH
#define GPIO_COMM
void MC_SixStep_Ramp_Motor_calc(void)
Definition: 6Step_Lib.c:479
int32_t El_Speed_Hz
Definition: 6Step_Lib.c:95
#define ADC_CH_3
uint16_t MediumFrequencyTask_flag
Definition: 6Step_Lib.h:142
uint32_t CurrentRegular_BEMF_ch
Definition: 6Step_Lib.h:107
#define TIME_FOR_ALIGN
uint32_t ADC_BUFFER[4]
Definition: 6Step_Lib.h:110
Six Step parameters.
Definition: 6Step_Lib.h:94
#define TRUE
#define ADCx
This header file provides the set of functions for Motor Control library.
void CMD_Parser(char *pCommandString)
uint8_t array_completed
Definition: 6Step_Lib.c:90
#define LF_TIMx
int16_t MC_PI_Controller(SIXSTEP_PI_PARAM_InitTypeDef_t *, int16_t)
Definition: 6Step_Lib.c:768
#define ACC
uint16_t test_motor_run
Definition: 6Step_Lib.c:98
#define UPPER_OUT_LIMIT
#define ADC_CH_4
#define KI_GAIN
uint8_t dac_status
Definition: 6Step_Lib.c:109
#define DEMO_STOP_TIME
#define GPIO_ZERO_CROSS
void MC_SixStep_Speed_Val_target_potentiometer(void)
Definition: 6Step_Lib.c:687
uint16_t HFBuffer[HFBUFFERSIZE]
Definition: 6Step_Lib.c:88
uint32_t constant_k
Definition: 6Step_Lib.c:79
void MC_SixStep_TABLE(uint8_t)
Definition: 6Step_Lib.c:164
uint64_t constant_multiplier_tmp
Definition: 6Step_Lib.c:117
uint32_t n_zcr_startup
Definition: 6Step_Lib.c:101
uint16_t ADC_BEMF_threshold_DOWN
Definition: 6Step_Lib.h:114
#define HF_TIMx
uint16_t speed_tmp_buffer[FILTER_DEEP]
Definition: 6Step_Lib.c:87
void MC_SixStep_NEXT_step(void)
Definition: 6Step_Lib.c:239
#define STARTUP_CURRENT_REFERENCE
void MC_SixStep_Init_main_data(void)
Definition: 6Step_Lib.c:1065
void MC_TIMx_SixStep_timebase(void)
Definition: 6Step_Lib.c:1123
#define FALSE
uint32_t Time_vector_tmp
Definition: 6Step_Lib.c:80
uint16_t index_startup_motor
Definition: 6Step_Lib.c:102
#define NUMBER_OF_STEPS
uint32_t counter_ARR_Bemf
Definition: 6Step_Lib.c:116
void UART_Communication_Task(void)
#define KI_DIV
uint32_t Regular_channel[4]
Definition: 6Step_Lib.h:106
#define BUTTON_DELAY
#define LOWER_OUT_LIMIT
void MC_SixStep_EnableInput_CH1_E_CH2_D_CH3_E(void)
Enable Input channel CH1 and CH3 for STSPIN230.
void MC_ADCx_SixStep_Bemf(void)
Definition: 6Step_Lib.c:1381
void MC_Set_PI_param(SIXSTEP_PI_PARAM_InitTypeDef_t *)
Definition: 6Step_Lib.c:740
void MC_UI_INIT(void)
void MC_SixStep_Nucleo_Init()
Init the STM32 register.
#define HFBUFFERSIZE
void MC_SixStep_ADC_Channel(uint32_t adc_ch)
Select the new ADC Channel.
#define BEMF_CONSEC_DOWN_MAX
#define POTENTIOMETER
#define DAC_ENABLE
#define NUM_POLE_PAIRS
void Bemf_delay_calc()
Bemf delay calculation.
void HAL_IncTick(void)
This function is called to increment a global variable "uwTick" used as application time base...
Definition: 6Step_Lib.c:1607
Six PI regulator parameters.
Definition: 6Step_Lib.h:170
#define GPIO_CH_COMM
uint32_t ADC_Regular_Buffer[5]
Definition: 6Step_Lib.h:112
void MC_Bemf_Delay(void)
Definition: 6Step_Lib.c:965
Generated by   doxygen 1.8.11