From b61941fe4119b962c198e271d816cec58a781279 Mon Sep 17 00:00:00 2001 From: Neluji <38362829+Neluji@users.noreply.github.com> Date: Tue, 24 Nov 2020 22:56:56 +0100 Subject: [PATCH] Zi projet iz finich --- MDK-ARM/Project.uvoptx | 127 ++++++++-- MDK-ARM/Project.uvprojx | 50 ++++ .../ADC_antichavirement.c => MyDrivers/ADC.c | 41 ++- MyDrivers/ADC.h | 4 + MyDrivers/GPIO.h | 1 - MyDrivers/MyPWM.c | 41 +++ MyDrivers/MyPWM.h | 10 + MyDrivers/MyTimer.c | 28 ++- MyDrivers/MyTimer.h | 8 + {Services => MyDrivers}/girouette.c | 77 +++--- MyDrivers/girouette.h | 7 + Services/Chrono.c | 235 ++++++++++++++++++ Services/Chrono.h | 107 ++++++++ Services/MyRF.c | 18 +- Services/MyRF.h | 2 +- Services/MySequencer.c | 17 +- Services/antichavirement.c | 28 +++ Services/antichavirement.h | 3 + Src/main.c | 11 + 19 files changed, 724 insertions(+), 91 deletions(-) rename Services/ADC_antichavirement.c => MyDrivers/ADC.c (78%) create mode 100644 MyDrivers/ADC.h rename {Services => MyDrivers}/girouette.c (54%) create mode 100644 MyDrivers/girouette.h create mode 100644 Services/Chrono.c create mode 100644 Services/Chrono.h create mode 100644 Services/antichavirement.c create mode 100644 Services/antichavirement.h diff --git a/MDK-ARM/Project.uvoptx b/MDK-ARM/Project.uvoptx index 1b5bf42..aa5db46 100644 --- a/MDK-ARM/Project.uvoptx +++ b/MDK-ARM/Project.uvoptx @@ -317,7 +317,7 @@ 0 DLGDARM - (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=504,37,1150,710,0)(110=60,88,280,548,0)(111=752,104,972,564,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=874,38,1295,465,0)(121=892,96,1313,523,0)(122=674,103,1095,530,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=517,8,1111,759,0)(131=150,13,744,764,0)(132=599,17,1193,768,0)(133=-1,-1,-1,-1,0)(160=674,53,1122,467,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) + (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=458,245,834,481,0)(1009=-1,-1,-1,-1,0)(100=504,37,1150,710,0)(110=60,88,280,548,0)(111=752,104,972,564,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=205,153,626,580,0)(121=892,96,1313,523,0)(122=674,103,1095,530,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=517,8,1111,759,0)(131=150,13,744,764,0)(132=537,17,1131,768,0)(133=-1,-1,-1,-1,0)(160=674,53,1122,467,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) 0 @@ -350,7 +350,40 @@ -U-O142 -O2254 -S0 -C0 -A0 -N00("ARM CoreSight SW-DP") -D00(1BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC800 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM) - + + + 0 + 0 + 46 + 1 +
134231148
+ 0 + 0 + 0 + 0 + 0 + 1 + F:\Etudes\4A\µC - Périphériques\Periph-Voilier\Src\main.c + + \\NUCLEO_F103RB\../Src/main.c\46 +
+ + 1 + 0 + 58 + 1 +
134231188
+ 0 + 0 + 0 + 0 + 0 + 1 + F:\Etudes\4A\µC - Périphériques\Periph-Voilier\Src\main.c + + \\NUCLEO_F103RB\../Src/main.c\58 +
+
0 @@ -412,7 +445,7 @@ 0 ((porta & 0x00000800) >> 11 & 0x800) >> 11 - 00800000000000000000000000000000E0FFEF400000000000000000000000000000000028706F7274612026203078303030303038303029203E3E2031310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000F03F170000000000000000000000000000000000000076150008 + 00800000000000000000000000000000E0FFEF400100000000000000000000000000000028706F7274612026203078303030303038303029203E3E2031310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000F03F170000000000000000000000000000000000000076150008 @@ -487,6 +520,42 @@ 0 0 + + 2 + 5 + 1 + 0 + 0 + 0 + ..\MyDrivers\girouette.c + girouette.c + 0 + 0 + + + 2 + 6 + 1 + 0 + 0 + 0 + ..\Services\antichavirement.c + antichavirement.c + 0 + 0 + + + 2 + 7 + 1 + 0 + 0 + 0 + ..\Services\Chrono.c + Chrono.c + 0 + 0 + @@ -497,7 +566,7 @@ 0 3 - 5 + 8 1 0 0 @@ -509,7 +578,7 @@ 3 - 6 + 9 1 0 0 @@ -521,9 +590,9 @@ 3 - 7 + 10 1 - 1 + 0 0 0 ..\MyDrivers\MyUSART.c @@ -533,7 +602,7 @@ 3 - 8 + 11 1 0 0 @@ -545,7 +614,7 @@ 3 - 9 + 12 1 0 0 @@ -555,6 +624,18 @@ 0 0 + + 3 + 13 + 1 + 0 + 0 + 0 + ..\MyDrivers\ADC.c + ADC.c + 0 + 0 + @@ -565,7 +646,7 @@ 0 4 - 10 + 14 1 0 0 @@ -577,7 +658,7 @@ 4 - 11 + 15 1 0 0 @@ -589,7 +670,7 @@ 4 - 12 + 16 1 0 0 @@ -601,7 +682,7 @@ 4 - 13 + 17 1 0 0 @@ -613,7 +694,7 @@ 4 - 14 + 18 1 0 0 @@ -623,6 +704,18 @@ 0 0 + + 4 + 19 + 1 + 0 + 0 + 0 + ..\LLDrivers\src\stm32f1xx_ll_adc.c + stm32f1xx_ll_adc.c + 0 + 0 + @@ -633,7 +726,7 @@ 0 5 - 15 + 20 5 0 0 @@ -653,7 +746,7 @@ 0 6 - 16 + 21 1 0 0 @@ -673,7 +766,7 @@ 0 7 - 17 + 22 2 0 0 diff --git a/MDK-ARM/Project.uvprojx b/MDK-ARM/Project.uvprojx index d91e682..b970f90 100644 --- a/MDK-ARM/Project.uvprojx +++ b/MDK-ARM/Project.uvprojx @@ -407,6 +407,21 @@ 1 ..\Services\MySequencer.c + + girouette.c + 1 + ..\MyDrivers\girouette.c + + + antichavirement.c + 1 + ..\Services\antichavirement.c + + + Chrono.c + 1 + ..\Services\Chrono.c + @@ -437,6 +452,11 @@ 1 ..\MyDrivers\GPIO.c + + ADC.c + 1 + ..\MyDrivers\ADC.c + @@ -467,6 +487,11 @@ 1 ..\LLDrivers\src\stm32f1xx_ll_usart.c + + stm32f1xx_ll_adc.c + 1 + ..\LLDrivers\src\stm32f1xx_ll_adc.c + @@ -905,6 +930,21 @@ 1 ..\Services\MySequencer.c + + girouette.c + 1 + ..\MyDrivers\girouette.c + + + antichavirement.c + 1 + ..\Services\antichavirement.c + + + Chrono.c + 1 + ..\Services\Chrono.c + @@ -935,6 +975,11 @@ 1 ..\MyDrivers\GPIO.c + + ADC.c + 1 + ..\MyDrivers\ADC.c + @@ -965,6 +1010,11 @@ 1 ..\LLDrivers\src\stm32f1xx_ll_usart.c + + stm32f1xx_ll_adc.c + 1 + ..\LLDrivers\src\stm32f1xx_ll_adc.c + diff --git a/Services/ADC_antichavirement.c b/MyDrivers/ADC.c similarity index 78% rename from Services/ADC_antichavirement.c rename to MyDrivers/ADC.c index 78eda8e..6de484d 100644 --- a/Services/ADC_antichavirement.c +++ b/MyDrivers/ADC.c @@ -1,17 +1,17 @@ -#include "ADC_antichavirement.h" -#include "Chrono.h" -#include "MyTimer.h" +#include "ADC.h" #include "stm32f1xx_ll_bus.h" -#include "stm32f1xx_ll_tim.h" #include "stm32f1xx_ll_adc.h" #include "stm32f1xx_ll_rcc.h" -#define seuil_g_pos 1.0 -#define seuil_g_neg -1.0 - +/** + * @brief Configure l'ADC pour les différents channels + * @note + * @param None + * @retval None + */ void conf_ADC (void) { - //ADC_REG + //Activation de la clock de l'ADC LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1); @@ -19,7 +19,7 @@ void conf_ADC (void) { LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSRC_PCLK2_DIV_6); - //Bit ADON à 1 + //Bit ADON à 1 pour lancer la conversion LL_ADC_Enable(ADC1); @@ -39,14 +39,18 @@ void conf_ADC (void) { LL_ADC_REG_Init(ADC1,&My_LL_ADC_REG_Init_Struct); - - //ADC Sampling Time + //Configuration du Sampling Time LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_10, LL_ADC_SAMPLINGTIME_239CYCLES_5); LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_11, LL_ADC_SAMPLINGTIME_239CYCLES_5); } - +/** + * @brief Règle les différentes entrées et sortie nécessaire à la girouette + * @note + * @param None + * @retval valeur du rapport "X/Y" [0 infini] + */ uint32_t X; uint32_t Y; float rapport; @@ -86,16 +90,3 @@ float start_ADC (void) { return rapport; } - - -void antichavirement (float rapport) { - - if (rapport >= seuil_g_pos || rapport <= seuil_g_neg){ - - //Voiles à 90° - MyPWM_Set_Impulse_Duration(TIM1,10000*10/100,LL_TIM_CHANNEL_CH1); - - } - - -} diff --git a/MyDrivers/ADC.h b/MyDrivers/ADC.h new file mode 100644 index 0000000..a310d07 --- /dev/null +++ b/MyDrivers/ADC.h @@ -0,0 +1,4 @@ +#include "stm32f103xb.h" + +void conf_ADC (void); +float start_ADC (void); diff --git a/MyDrivers/GPIO.h b/MyDrivers/GPIO.h index 9e75fe1..5fb39d7 100644 --- a/MyDrivers/GPIO.h +++ b/MyDrivers/GPIO.h @@ -8,7 +8,6 @@ Configuration de toutes les broches utilis - Accéléromètre : PC.0 et PC.1 - Servo : PA.8 - Girouette : PA.5, PA.6 et PA.7 -Manque PC.2 = adc batterie */ #include "stm32f103xb.h" diff --git a/MyDrivers/MyPWM.c b/MyDrivers/MyPWM.c index 1cffb8f..de3ecb5 100644 --- a/MyDrivers/MyPWM.c +++ b/MyDrivers/MyPWM.c @@ -18,6 +18,32 @@ void MyPWM_Conf_Output(TIM_TypeDef * Timer, int channel) } +/** + * @brief Configuration du timer en mode PWM + * @note + * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 + * @retval None + */ +void MyPWM_girouette_Conf_Output(TIM_TypeDef *TIMx, uint32_t Channel) +{ + + //Définition + LL_TIM_OC_InitTypeDef My_LL_Tim_OC_Init_Struct; + + //Configuration du output channel en PWM + LL_TIM_OC_StructInit(&My_LL_Tim_OC_Init_Struct); + My_LL_Tim_OC_Init_Struct.OCMode = LL_TIM_OCMODE_PWM1; + LL_TIM_OC_Init(TIMx,Channel,&My_LL_Tim_OC_Init_Struct); + + // Réglage du ARR et du PSC du Timer en PWM pour une période de 20ms + LL_TIM_SetAutoReload(TIMx,0x707F); + LL_TIM_SetPrescaler(TIMx,0x31); + + //Activation du channel (CH3) pour le timer considéré + LL_TIM_CC_EnableChannel(TIMx, Channel); +} + + //Configurer obligatoirement les channels 1 et 2 sur IC1 et IC2 ou IC3 et IC4 void MyPWM_Conf_Input(TIM_TypeDef * Timer, int channel1, int channel2) { @@ -56,6 +82,21 @@ void MyPWM_Set_Impulse_Duration(TIM_TypeDef * Timer, uint32_t CompareValue, int } +void MyPWM_girouette_Set_Impulse_Duration(TIM_TypeDef * Timer, int Percentage, int channel) +{ + static uint32_t Val_ARR; + static uint32_t CompareValue; + + Val_ARR = LL_TIM_GetAutoReload(Timer); + CompareValue = (Val_ARR * Percentage)/10000; + + if (channel==LL_TIM_CHANNEL_CH1) LL_TIM_OC_SetCompareCH1(Timer, CompareValue); + else if (channel==LL_TIM_CHANNEL_CH2) LL_TIM_OC_SetCompareCH2(Timer, CompareValue); + else if (channel==LL_TIM_CHANNEL_CH3) LL_TIM_OC_SetCompareCH3(Timer, CompareValue); + else LL_TIM_OC_SetCompareCH4(Timer, CompareValue); +} + + int MyPWM_Duty_Cycle_Permilles(TIM_TypeDef * Timer, int channel1, int channel2) { if(channel1 == LL_TIM_CHANNEL_CH1 && channel2 == LL_TIM_CHANNEL_CH2) { return LL_TIM_IC_GetCaptureCH2(Timer) / LL_TIM_IC_GetCaptureCH1(Timer) * 1000; diff --git a/MyDrivers/MyPWM.h b/MyDrivers/MyPWM.h index a54be15..9fabbb5 100644 --- a/MyDrivers/MyPWM.h +++ b/MyDrivers/MyPWM.h @@ -15,6 +15,14 @@ Driver pour la gestion de la PWM de Timers 1 */ void MyPWM_Conf_Output(TIM_TypeDef * Timer, int channel); +/** + * @brief Configuration du timer en mode PWM + * @note + * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 + * @retval None + */ +void MyPWM_girouette_Conf_Output(TIM_TypeDef *TIMx, uint32_t Channel); + /** * @brief Configure un timer en mode PWM Input * @note Penser à appeler Timer_Conf() de Timer.h avant ! @@ -31,6 +39,8 @@ void MyPWM_Conf_Input(TIM_TypeDef * Timer, int channel1, int channel2); */ void MyPWM_Set_Impulse_Duration(TIM_TypeDef * Timer, uint32_t CompareValue, int channel); +void MyPWM_girouette_Set_Impulse_Duration(TIM_TypeDef * Timer, int Percentage, int channel); + /** * @brief Renvoie le duty cycle de la PWM en entrée * @note Fct utilisée en réception diff --git a/MyDrivers/MyTimer.c b/MyDrivers/MyTimer.c index 6df39fe..ee52320 100644 --- a/MyDrivers/MyTimer.c +++ b/MyDrivers/MyTimer.c @@ -143,7 +143,33 @@ void MyTimer_IT_Enable(TIM_TypeDef * Timer) void MyTimer_IT_Disable(TIM_TypeDef * Timer) { LL_TIM_DisableIT_UPDATE(Timer); -} +} + + +/** + * @brief Configuration du timer en mode encodeur + * @note + * @param None + * @retval None + */ +void MyTimer_girouette_Conf(void) +{ + + //Définition + LL_TIM_ENCODER_InitTypeDef My_LL_Tim_Init_Struct; + + // Validation horloge locale + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3); + + //Initilisation de la structure au Timer 3 + LL_TIM_ENCODER_StructInit(&My_LL_Tim_Init_Struct); + LL_TIM_ENCODER_Init(TIM3, &My_LL_Tim_Init_Struct); + + // Réglage Timer 3 + LL_TIM_SetAutoReload(TIM3,719); + LL_TIM_SetPrescaler(TIM3,0); + +} /* diff --git a/MyDrivers/MyTimer.h b/MyDrivers/MyTimer.h index 4be9606..28ccd21 100644 --- a/MyDrivers/MyTimer.h +++ b/MyDrivers/MyTimer.h @@ -67,4 +67,12 @@ void MyTimer_IT_Enable(TIM_TypeDef * Timer); */ void MyTimer_IT_Disable(TIM_TypeDef * Timer); +/** + * @brief Configuration du timer en mode encodeur + * @note + * @param None + * @retval None + */ +void MyTimer_girouette_Conf(void); + #endif diff --git a/Services/girouette.c b/MyDrivers/girouette.c similarity index 54% rename from Services/girouette.c rename to MyDrivers/girouette.c index 4224052..09e8188 100644 --- a/Services/girouette.c +++ b/MyDrivers/girouette.c @@ -1,12 +1,15 @@ #include "stm32f1xx_ll_gpio.h" #include "girouette.h" #include "MyTimer.h" -#include "stm32f1xx_ll_bus.h" +#include "MyPWM.h" #include "stm32f1xx_ll_tim.h" -#include "stm32f1xx_ll_adc.h" -#include "stm32f1xx_ll_rcc.h" -//Configuration de l'interruption exterieure +/** + * @brief Configuration de l'interruption exterieure + * @note + * @param None + * @retval None + */ void exti_ref(void){ LL_GPIO_AF_SetEXTISource(LL_GPIO_AF_EXTI_PORTA, LL_GPIO_AF_EXTI_LINE5); @@ -17,13 +20,23 @@ void exti_ref(void){ EXTI->RTSR |= EXTI_RTSR_TR5_Msk; } -//Configuration de l'interruption du NVIC +/** + * @brief Configuration de l'interruption du NVIC + * @note + * @param None + * @retval None + */ void nvic_exti_conf(void){ NVIC->IP[23]=2<<4; NVIC->ISER[0]=1<<23; } -//redirection de l'interruption +/** + * @brief redirection de l'interruption + * @note + * @param None + * @retval None + */ void EXTI9_5_IRQHandler(void){ //CNT A ZERO LL_TIM_SetCounter(TIM3, 0); @@ -32,31 +45,38 @@ void EXTI9_5_IRQHandler(void){ } - -//configuration interruption + timer 3 en mode encodeur et 4 en mode PWM +/** + * @brief Configuration des interruptions et des timers + * @note + * @param None + * @retval None + */ void conf_girouette(void){ - //interruption + //Configuration des interruptions exti_ref(); nvic_exti_conf(); - - - //Timer en mode encodeur + + //Configuration timer en mode encodeur MyTimer_girouette_Conf(); - //Timer en mode PWM - MyPWM_Conf_Output(TIM1,LL_TIM_CHANNEL_CH1); + //Configuration timer en mode PWM + MyPWM_girouette_Conf_Output(TIM1,LL_TIM_CHANNEL_CH1); //Activation Timer 1 LL_TIM_EnableAllOutputs(TIM1); - } +/** + * @brief Calcul position-angle à partir de l'encodeur + * @note + * @param None + * @retval Pourcentage "Temps haut/Période" de la PWM [5 10]% (180 valeurs) + */ -//Calcul position-angle - -float pos_Min=0; -float pos_Max=135; +//Initialisation des variables +float pos_Min=90; +float pos_Max=270; float pos_actuelle; float duree_Min=1; @@ -71,16 +91,16 @@ float Pourcentage_calc; float Calcul_pourcentage_duree (void){ - pos_actuelle=LL_TIM_GetCounter(TIM3); + pos_actuelle=LL_TIM_GetCounter(TIM3);//Récupération de la valeur du compteur //Calcul pourcentage min et max Pourcentage_Min=duree_Min/Periode; Pourcentage_Max=duree_Max/Periode; - //Calcul fctn affine + //Calcul fonction affine "durée Temps haut" en fonction de la valeur du compteur if (pos_actuelle <= 360){ - if (pos_actuelle > 90 && pos_actuelle < 270) { - duree_calc=((duree_Max-duree_Min)/(pos_Max-pos_Min))*pos_actuelle+duree_Min; + if (pos_actuelle > 90 && pos_actuelle <= 270) { + duree_calc=((duree_Max-duree_Min)/(pos_Max-pos_Min))*pos_actuelle+duree_Min/duree_Max; } else if (pos_actuelle > 270){ duree_calc=duree_Max; @@ -89,10 +109,11 @@ float Calcul_pourcentage_duree (void){ duree_calc=duree_Min; } } - - /*if (pos_actuelle > 360 && pos_actuelle < 720){ + //Calcul fonction affine "durée Temps haut" en fonction de la valeur du compteur + //Si la girouette dépasse 180° ? + if (pos_actuelle > 360 && pos_actuelle < 720){ if (pos_actuelle > 450 && pos_actuelle < 630) { - duree_calc=((duree_Max-duree_Min)/(pos_Max-pos_Min))*(720-pos_actuelle)+duree_Min; + duree_calc=((duree_Max-duree_Min)/(pos_Max-pos_Min))*(720-pos_actuelle)+duree_Min/duree_Max; } else if (pos_actuelle < 450){ duree_calc=duree_Max; @@ -100,9 +121,9 @@ float Calcul_pourcentage_duree (void){ else { duree_calc=duree_Min; } - }*/ + } - //Calcul pourcentage + //Calcul du pourcentage en fonction de la "durée Temps haut" Pourcentage_calc=((Pourcentage_Max-Pourcentage_Min)/(duree_Max-duree_Min))*duree_calc; return Pourcentage_calc; diff --git a/MyDrivers/girouette.h b/MyDrivers/girouette.h new file mode 100644 index 0000000..d619fcd --- /dev/null +++ b/MyDrivers/girouette.h @@ -0,0 +1,7 @@ +#include "stm32f103xb.h" + +void conf_girouette(void); + +float Calcul_pourcentage_duree (void); + +float Angle_Vent(void); diff --git a/Services/Chrono.c b/Services/Chrono.c new file mode 100644 index 0000000..f577732 --- /dev/null +++ b/Services/Chrono.c @@ -0,0 +1,235 @@ +// A COMPLETER + +/* +Service permettant de chornométrer jusqu'à 59mn 59s 99 1/100 +Utilise un timer au choix (TIMER1 à TIMER4). +Utilise la lib MyTimers.h /.c +*/ + + + +#include "Chrono.h" +#include "MyTimer.h" +#include "stm32f1xx_ll_gpio.h" +#include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges + +// variable privée de type Time qui mémorise la durée mesurée +static Time Chrono_Time; // rem : static rend la visibilité de la variable Chrono_Time limitée à ce fichier + +// variable privée qui mémorise pour le module le timer utilisé par le module +static TIM_TypeDef * Chrono_Timer=TIM1; // init par défaut au cas où l'utilisateur ne lance pas Chrono_Conf avant toute autre fct. + +//mémorise l'état précédent du bouton Start/Stop +static int Prev_Start_Stop_BP_Status=0; + +//mémorise l'état du chrono (lancé ou pas) +static int Started=1; + +// déclaration callback appelé toute les 10ms +void Chrono_Task_10ms(void); + +//Configuration des I/O +void Chrono_Conf_io(void); + +/** + * @brief Configure le chronomètre. + * @note A lancer avant toute autre fonction. + * @param Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 + * @retval None + */ +void Chrono_Conf(TIM_TypeDef * Timer) +{ + // Reset Time + Chrono_Time.Hund=0; + Chrono_Time.Sec=0; + Chrono_Time.Min=0; + + // Fixation du Timer + Chrono_Timer=Timer; + + // Réglage Timer par défaut + MyTimer_Conf(Chrono_Timer,0xFFFF,0); + + // Réglage interruption du Timer avec callback : Chrono_Task_10ms() + MyTimer_IT_Conf(Chrono_Timer, Chrono_Task_10ms,3); + + // Validation IT + MyTimer_IT_Enable(Chrono_Timer); + +} + + +/** + * @brief Démarre le chronomètre. + * @note si la durée dépasse 59mn 59sec 99 Hund, elle est remise à zéro et repart + * @param Aucun + * @retval Aucun + */ +void Chrono_Start(void) +{ + Chrono_Timer=TIM3; + MyTimer_Start(Chrono_Timer); //Start Timer 3 + + Chrono_Timer=TIM1; + MyTimer_Start(Chrono_Timer); //Start Timer 1 +} + + +/** + * @brief Arrête le chronomètre. + * @note + * @param Aucun + * @retval Aucun + */ +void Chrono_Stop(void) +{ + MyTimer_Stop(Chrono_Timer); +} + + +/** + * @brief Remet le chronomètre à 0 + * @note + * @param Aucun + * @retval Aucun + */ +void Chrono_Reset(void) +{ + // Arrêt Chrono + MyTimer_Stop(Chrono_Timer); + + // Reset Time + Chrono_Time.Hund=0; + Chrono_Time.Sec=0; + Chrono_Time.Min=0; +} + + +/** + * @brief Renvoie l'adresse de la variable Time privée gérée dans le module Chrono.c + * @note + * @param Aucun + * @retval adresse de la variable Time + */ +Time * Chrono_Read(void) +{ + return &Chrono_Time; +} + + + + +/** + * @brief incrémente la variable privée Chron_Time modulo 60mn + * @note + * @param Aucun + * @retval Aucun + */ +void Chrono_Task_10ms(void) +{ + Chrono_Time.Hund++; + if (Chrono_Time.Hund==100) + { + Chrono_Time.Sec++; + Chrono_Time.Hund=0; + LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5); + } + if (Chrono_Time.Sec==60) + { + Chrono_Time.Min++; + Chrono_Time.Sec=0; + } + if (Chrono_Time.Min==60) + { + Chrono_Time.Hund=0; + } + +} + + +/** + * @brief polle les 3 entrées et gènère les actions à faire + * @note Fct à lancer en tâche de fond (non bloquante) + * @param None + * @retval None + */ + +void Chrono_Background(void) { + + //Lecture PC8 bouton Start/Stop + int Start_Stop_BP_Status = LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_8); + + //Changement de l'état du chrono sur front montant du BP + if(Start_Stop_BP_Status && (Start_Stop_BP_Status != Prev_Start_Stop_BP_Status)) { + if(!Started){ + Chrono_Start(); + Started = 1; + } + else { + Chrono_Stop(); + Started = 0; + } + } + + //Actualisation mémoire BP + Prev_Start_Stop_BP_Status = Start_Stop_BP_Status; + + //Reset sur PC13 high + if(!LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_13)) { + Chrono_Time.Hund=0; + Chrono_Time.Sec=0; + Chrono_Time.Min=0; + } + +} + +/** + * @brief configure les 3 pins d'I/O + * @note + * @param None + * @retval None + */ + +void Chrono_Conf_io(void) { + + //Validation horloge locale + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC); + + GPIO_TypeDef * GPIO; + LL_GPIO_InitTypeDef My_GPIO_Init_Struct; + + //PA5 (LED de la carte) en output push-pull + + GPIO = GPIOA; + My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_5; + My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_OUTPUT; + My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; + My_GPIO_Init_Struct.Speed = LL_GPIO_MODE_OUTPUT_10MHz; + My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN; + + LL_GPIO_Init(GPIO, &My_GPIO_Init_Struct); + + //PC8 en floating input + + GPIO = GPIOC; + My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_8; + My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_FLOATING; + My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; + My_GPIO_Init_Struct.Speed = LL_GPIO_SPEED_FREQ_LOW; + My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN; + + LL_GPIO_Init(GPIO, &My_GPIO_Init_Struct); + + //PC13 (USER Button bleu carte) en floating input + + GPIO = GPIOC; + My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_13; + My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_FLOATING; + My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; + My_GPIO_Init_Struct.Speed = LL_GPIO_SPEED_FREQ_LOW; + My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN; + + LL_GPIO_Init(GPIO, &My_GPIO_Init_Struct); + +} diff --git a/Services/Chrono.h b/Services/Chrono.h new file mode 100644 index 0000000..4b03760 --- /dev/null +++ b/Services/Chrono.h @@ -0,0 +1,107 @@ +// RIEN A MODIFIER // + +#ifndef CHRONO_H +#define CHRONO_H + +/* +Service permettant de chornométrer jusqu'à 59mn 59s 99 1/100 +Utilise un timer au choix (TIMER1 à TIMER4). +Utilise la lib MyTimers.h /.c +*/ + +#include "stm32f103xb.h" + +/* ===================================================================================== +Les fonctions qui gèrent les IO (ajout par rapport à l'activité 1) +=======================================================================================*/ + + +#define PinStart LL_GPIO_PIN_8 +#define PinStop LL_GPIO_PIN_6 +// à compléter pour la pin reset du chronometre +#define PinLED LL_GPIO_PIN_10 +#define GPIO_Pin GPIOC +#define GPIO_LED GPIOC + + + + + + +/** + * @brief polle les 3 entrées et gènère les actions à faire + * @note Fct à lancer en tâche de fond (non bloquante) + * @param None + * @retval None + */ + +void Chrono_Background(void); + +//=======================================================================================// + + + + +// Type de donnée utilisé dans le module + +typedef struct { + char Hund; + char Sec; + char Min; +} Time; + + + + +/** + * @brief Configure le chronomètre. + * @note A lancer avant toute autre fonction. + * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 + * @retval None + */ +void Chrono_Conf(TIM_TypeDef * Timer); + + + + + + +/** + * @brief Démarre le chronomètre. + * @note si la durée dépasse 59mn 59sec 99 cent, elle est remise à zéro et repart + * @param Aucun + * @retval Aucun + */ +void Chrono_Start(void); + + +/** + * @brief Arrête le chronomètre. + * @note + * @param Aucun + * @retval Aucun + */ +void Chrono_Stop(void); + + +/** + * @brief Remet le chronomètre à 0 + * @note + * @param Aucun + * @retval Aucun + */ +void Chrono_Reset(void); + + +/** + * @brief Renvoie l'adresse de la variable Time privée gérée dans le module Chrono.c + * @note + * @param Aucun + * @retval adresse de la variable Time + */ +Time * Chrono_Read(void); + + + + +#endif diff --git a/Services/MyRF.c b/Services/MyRF.c index db6f37a..c36e1cc 100644 --- a/Services/MyRF.c +++ b/Services/MyRF.c @@ -71,12 +71,8 @@ void MyRF_Transmit_3s(int bordage_pc, int angle_vent) { "au grand largue.", "au vent arrière." }; - - char heure[2] = "xx"; - char min[2] = "xx"; - char sec[2] = "xx"; - char data1[14] = "\" - Bordage : "; + char data1[10] = "Bordage : "; char data2[24] = " === Le voilier navigue "; int i; @@ -84,17 +80,7 @@ void MyRF_Transmit_3s(int bordage_pc, int angle_vent) { //Activation de l'émetteur RF LL_GPIO_SetOutputPin(GPIOOut ,PinTXEn); - //Heure - MyUSART_Transmit_Data_8b(USARTOut, heure[0]); - MyUSART_Transmit_Data_8b(USARTOut, heure[1]); - MyUSART_Transmit_Data_8b(USARTOut, 'h'); - //Minutes - MyUSART_Transmit_Data_8b(USARTOut, min[0]); - MyUSART_Transmit_Data_8b(USARTOut, min[1]); - MyUSART_Transmit_Data_8b(USARTOut, '\''); - //Secondes - MyUSART_Transmit_Data_8b(USARTOut, sec[0]); - MyUSART_Transmit_Data_8b(USARTOut, sec[1]); + //Texte 1 (bordage) for(i=0; i<14; i++) { MyUSART_Transmit_Data_8b(USARTOut, data1[i]); diff --git a/Services/MyRF.h b/Services/MyRF.h index 8627a75..eeb0f6d 100644 --- a/Services/MyRF.h +++ b/Services/MyRF.h @@ -38,7 +38,7 @@ void MyRF_Conf(void); * @param None * @retval int : [-100; 100] pour correspondre à la plage du moteur */ -int MyRF_Duty_Cycle_Moteur(void); +int MyRF_Input_Duty_Cycle(void); /** * @brief Transmet un message via l'USART vers l'émetteur RF diff --git a/Services/MySequencer.c b/Services/MySequencer.c index 88a6599..5bfeede 100644 --- a/Services/MySequencer.c +++ b/Services/MySequencer.c @@ -3,8 +3,15 @@ #include "Moteur.h" #include "MyRF.h" #include "girouette.h" +#include "antichavirement.h" +#include "ADC.h" +#include "MyPWM.h" +#include "stm32f1xx_ll_tim.h" int cnt_3sec = 0; +float pourcentage = 0.0; + +void MySequencer_Task_10ms (void); void MySequencer_Conf(void) { @@ -28,12 +35,12 @@ void MySequencer_Task_10ms (void) { //Transmission données toutes les 3 secondes if(cnt_3sec == 300) { - MyRF_Transmit_3s((int)((Calcul_pourcentage_duree()-5.0)*20.0), (int)(Angle_Vent())); + MyRF_Transmit_3s((int)((Calcul_pourcentage_duree()-0.05)*2000.0), (int)(Angle_Vent())); cnt_3sec = 0; } //Réglage sens et vitesse moteur - int Duty_Cycle = MyRF_Duty_Cycle_Moteur(); + int Duty_Cycle = MyRF_Input_Duty_Cycle(); if (Duty_Cycle >= 0) { Moteur_Speed(Duty_Cycle); Moteur_Sens(1); @@ -43,6 +50,12 @@ void MySequencer_Task_10ms (void) { Moteur_Sens(0); } + //Gestion de l'antichavirement + antichavirement(start_ADC()); + //Gestion du bordage + pourcentage = Calcul_pourcentage_duree(); + pourcentage *= 1000; + MyPWM_girouette_Set_Impulse_Duration(TIM1,pourcentage,LL_TIM_CHANNEL_CH1); } diff --git a/Services/antichavirement.c b/Services/antichavirement.c new file mode 100644 index 0000000..21e9a36 --- /dev/null +++ b/Services/antichavirement.c @@ -0,0 +1,28 @@ +#include "antichavirement.h" +#include "MyPWM.h" +#include "stm32f1xx_ll_tim.h" +#include "stm32f1xx_ll_adc.h" +#include "MyRF.h" + +#define seuil_g_pos 1.0 +#define seuil_g_neg -1.0 + +/** + * @brief Configure l'antichavirement pour des valeurs seuils + * @note + * @param Rapport "X/Y" [0 infini] (Si Rapport dépasse les valeurs seuils alors on déclenche les voiles) + * @retval None + */ +void antichavirement (float rapport) { + + if (rapport >= seuil_g_pos || rapport <= seuil_g_neg){ + + //Application des voiles à 90° + MyPWM_girouette_Set_Impulse_Duration(TIM1,10000*10/100,LL_TIM_CHANNEL_CH1); + + MyRF_Transmit_Limite_Roulis(); + + } + + +} diff --git a/Services/antichavirement.h b/Services/antichavirement.h new file mode 100644 index 0000000..7dd463e --- /dev/null +++ b/Services/antichavirement.h @@ -0,0 +1,3 @@ +#include "stm32f103xb.h" + +void antichavirement (float rapport); diff --git a/Src/main.c b/Src/main.c index a7a0fb5..33d6acc 100644 --- a/Src/main.c +++ b/Src/main.c @@ -24,6 +24,9 @@ #include "Moteur.h" #include "MySequencer.h" #include "girouette.h" +#include "GPIO.h" +#include "ADC.h" +#include "Chrono.h" void SystemClock_Config(void); @@ -42,9 +45,17 @@ int main(void) /* Configure the system clock to 72 MHz */ SystemClock_Config(); + Chrono_Conf(TIM3); + Chrono_Conf(TIM1); + + Chrono_Start(); + MyRF_Conf(); Moteur_Conf(); MySequencer_Conf(); + conf_girouette(); + conf_GPIO(); + conf_ADC(); /* Infinite loop */ while (1)