From 7cf96b23f2a2929333a6a02bbad22182088c4860 Mon Sep 17 00:00:00 2001 From: benassai Date: Tue, 10 Nov 2020 14:24:15 +0100 Subject: [PATCH 1/6] Ajout de PWM input --- MyDrivers/Timer.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/MyDrivers/Timer.c b/MyDrivers/Timer.c index b0b5cad..023998d 100644 --- a/MyDrivers/Timer.c +++ b/MyDrivers/Timer.c @@ -165,7 +165,41 @@ void Timer_conf(TIM_TypeDef * timer, int arr, int psc) /**************************************************************************** * PWM INPUT ***************************************************************************/ +void PWMi_conf(TIM_TypeDef * TIMx, uint32_t channels){ + //Periode à recuperer dans TIMx_CCR1, duty cycle dans TIMx_CCR2 + // Validation horloge locale + if (TIMx == TIM1) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1); + else if (TIMx == TIM2) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2); + else if (TIMx == TIM3) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3); + else LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4); + + //LL_TIM_CC_EnableChannel(TIMx, channels); + //LL_TIM_IC_Init + TIMx -> CCMR1 |= TIM_CCMR1_CC1S_0; //01 + //TIM_CCMR1_IC1F_0; Potentiellement utile, à voir plus tard + TIMx -> CCER &= ~(TIM_CCER_CC1P); //0 = Rising edge + TIMx -> CCMR1 &= ~(TIM_CCMR1_IC1PSC); + + TIMx -> CCMR1 |= TIM_CCMR1_CC2S_1; + TIMx -> CCER |= TIM_CCER_CC2P; + TIMx -> SMCR |= TIM_SMCR_TS_0 | TIM_SMCR_TS_2; //101 + TIMx -> SMCR |= TIM_SMCR_SMS_2; //100 + + TIMx -> CCER |= TIM_CCER_CC1E; + TIMx -> CCER |= TIM_CCER_CC2E; + TIMx -> DIER |= TIM_DIER_CC1IE; + //TIM_DIER_CC1DE_Pos; Probablement pas utile +} +int get_input_period(TIM_TypeDef * TIMx) { + + return TIMx -> CCR1; +} + +int get_input_duty_cycle(TIM_TypeDef * TIMx) { + + return TIMx -> CCR2; +} /**************************************************************************** * PWM OUTPUT From 48a92e07f98ad6e297ad12e000ebabe02688181b Mon Sep 17 00:00:00 2001 From: benassai Date: Tue, 10 Nov 2020 15:31:47 +0100 Subject: [PATCH 2/6] Correction de bug PWM input et ajout des fonctions de Voltage --- MyDrivers/Timer.c | 4 ++-- MyDrivers/Timer.h | 6 ++++-- Services/Voltage.c | 20 ++++++++++++++++++++ Services/Voltage.h | 7 +++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/MyDrivers/Timer.c b/MyDrivers/Timer.c index 023998d..2b29356 100644 --- a/MyDrivers/Timer.c +++ b/MyDrivers/Timer.c @@ -191,12 +191,12 @@ void PWMi_conf(TIM_TypeDef * TIMx, uint32_t channels){ //TIM_DIER_CC1DE_Pos; Probablement pas utile } -int get_input_period(TIM_TypeDef * TIMx) { +int PWMi_getPeriod(TIM_TypeDef * TIMx) { return TIMx -> CCR1; } -int get_input_duty_cycle(TIM_TypeDef * TIMx) { +int PWMi_getDutyCycle(TIM_TypeDef * TIMx) { return TIMx -> CCR2; } diff --git a/MyDrivers/Timer.h b/MyDrivers/Timer.h index cc484f7..800446c 100644 --- a/MyDrivers/Timer.h +++ b/MyDrivers/Timer.h @@ -59,9 +59,11 @@ void Timer_stop(TIM_TypeDef * timer); * PWM INPUT ***************************************************************************/ -void PWMi_conf(TIM_TypeDef * timer, int channel); +void PWMi_conf(TIM_TypeDef * timer, uint32_t channel); -int PWMi_getDutyCycle(TIM_TypeDef * timer, int channel); +int PWMi_getDutyCycle(TIM_TypeDef * timer); + +int PWMi_getPeriod(TIM_TypeDef * TIMx); /**************************************************************************** * PWM OUTPUT diff --git a/Services/Voltage.c b/Services/Voltage.c index e47b4a9..603852b 100644 --- a/Services/Voltage.c +++ b/Services/Voltage.c @@ -1 +1,21 @@ #include "Voltage.h" + + +void Voltage_conf() { + + //On configure le pin qui recevra le signal, ici PC2 + GPIO_conf(GPIOC, LL_GPIO_PIN_2, LL_GPIO_MODE_ANALOG, 0, 0); + + //On configure l'ADC + ADC_conf(ADC2, 12); + + //On démarre l'ADC + ADC_start(ADC2); + +} + +double Voltage_getVoltage() { + + ADC_readVolt(ADC2); + +} \ No newline at end of file diff --git a/Services/Voltage.h b/Services/Voltage.h index 141c358..ae8b4d2 100644 --- a/Services/Voltage.h +++ b/Services/Voltage.h @@ -1,4 +1,11 @@ #ifndef VOLTAGE_H #define VOLTAGE_H +#include "ADC.h" +#include "GPIO.h" + +void Voltage_conf(); + +double Voltage_getVoltage(); + #endif From 45c2080a923a6730a19c3dd59086756c4cff75cf Mon Sep 17 00:00:00 2001 From: benassai Date: Tue, 10 Nov 2020 15:49:38 +0100 Subject: [PATCH 3/6] Correction d'erreurs dans Voltage et ajout de RFEmitter --- Services/RFEmitter.c | 17 +++++++++++++++++ Services/RFEmitter.h | 7 +++++++ Services/Voltage.c | 4 ++-- Services/Voltage.h | 4 ++-- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Services/RFEmitter.c b/Services/RFEmitter.c index e140be2..5c3f56d 100644 --- a/Services/RFEmitter.c +++ b/Services/RFEmitter.c @@ -1 +1,18 @@ #include "RFEmitter.h" + + +void RFEmitter_conf() { + + //On configure l'USART + Usart_conf(USART1); + + //On active l'USART + Usart_enable(USART1); + +} + +void RFEmitter_send(char * message, int longueur) { + + Usart_send(USART1, message, longueur); + +} diff --git a/Services/RFEmitter.h b/Services/RFEmitter.h index d76d926..3f092d7 100644 --- a/Services/RFEmitter.h +++ b/Services/RFEmitter.h @@ -1,4 +1,11 @@ #ifndef RFEMITTER_H #define RFEMITTER_H +#include "USART.h" + +void RFEmitter_conf(void); + +void RFEmitter_send(char * message, int longueur); + #endif + diff --git a/Services/Voltage.c b/Services/Voltage.c index 603852b..b982d45 100644 --- a/Services/Voltage.c +++ b/Services/Voltage.c @@ -16,6 +16,6 @@ void Voltage_conf() { double Voltage_getVoltage() { - ADC_readVolt(ADC2); + return ADC_readVolt(ADC2); -} \ No newline at end of file +} diff --git a/Services/Voltage.h b/Services/Voltage.h index ae8b4d2..bbb164f 100644 --- a/Services/Voltage.h +++ b/Services/Voltage.h @@ -4,8 +4,8 @@ #include "ADC.h" #include "GPIO.h" -void Voltage_conf(); +void Voltage_conf(void); -double Voltage_getVoltage(); +double Voltage_getVoltage(void); #endif From 491fd3af6be516f99895522d08a270587a26fd21 Mon Sep 17 00:00:00 2001 From: benassai Date: Wed, 11 Nov 2020 16:46:17 +0100 Subject: [PATCH 4/6] Reecriture de PWM input et ecriture des fonctions RFReiceiver --- MyDrivers/Timer.c | 51 ++++++++++++++++++++++++++++++------------- MyDrivers/Timer.h | 2 +- Services/RFReceiver.c | 16 ++++++++++++++ Services/RFReceiver.h | 6 +++++ 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/MyDrivers/Timer.c b/MyDrivers/Timer.c index 2b29356..311e027 100644 --- a/MyDrivers/Timer.c +++ b/MyDrivers/Timer.c @@ -165,29 +165,50 @@ void Timer_conf(TIM_TypeDef * timer, int arr, int psc) /**************************************************************************** * PWM INPUT ***************************************************************************/ -void PWMi_conf(TIM_TypeDef * TIMx, uint32_t channels){ - //Periode à recuperer dans TIMx_CCR1, duty cycle dans TIMx_CCR2 +void PWMi_conf(TIM_TypeDef * TIMx, int channel){ + + //Periode à recuperer dans TIMx_CCR1, duty cycle dans TIMx_CCR2. Seul les 2 premiers channels peuvent être utilisés (cf 315). + // Validation horloge locale if (TIMx == TIM1) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1); else if (TIMx == TIM2) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2); else if (TIMx == TIM3) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3); else LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4); - //LL_TIM_CC_EnableChannel(TIMx, channels); - //LL_TIM_IC_Init - TIMx -> CCMR1 |= TIM_CCMR1_CC1S_0; //01 - //TIM_CCMR1_IC1F_0; Potentiellement utile, à voir plus tard - TIMx -> CCER &= ~(TIM_CCER_CC1P); //0 = Rising edge - TIMx -> CCMR1 &= ~(TIM_CCMR1_IC1PSC); - TIMx -> CCMR1 |= TIM_CCMR1_CC2S_1; - TIMx -> CCER |= TIM_CCER_CC2P; - TIMx -> SMCR |= TIM_SMCR_TS_0 | TIM_SMCR_TS_2; //101 - TIMx -> SMCR |= TIM_SMCR_SMS_2; //100 + if (channel == 1) { + // + TIMx -> CCMR1 |= TIM_CCMR1_CC1S_0; + TIMx -> CCMR1 |= TIM_CCMR1_CC2S_1; + + //TIM_CCMR1_IC1F_0; Potentiellement utile, à voir plus tard + + //On met le channel principal en rising edge, le secondaire en falling edge + TIMx -> CCER &= ~TIM_CCER_CC1P; + TIMx -> CCER |= TIM_CCER_CC2P; + + TIMx -> SMCR |= TIM_SMCR_TS_0 | TIM_SMCR_TS_2; //101 + TIMx -> SMCR |= TIM_SMCR_SMS_2; //100 + } - TIMx -> CCER |= TIM_CCER_CC1E; - TIMx -> CCER |= TIM_CCER_CC2E; - TIMx -> DIER |= TIM_DIER_CC1IE; + else { + TIMx -> CCMR1 |= TIM_CCMR1_CC1S_1; + TIMx -> CCMR1 |= TIM_CCMR1_CC2S_0; + + TIMx -> CCER |= TIM_CCER_CC1P; + TIMx -> CCER &= ~TIM_CCER_CC2P; + + TIMx -> SMCR |= TIM_SMCR_TS_1 | TIM_SMCR_TS_2; //110 + TIMx -> SMCR |= TIM_SMCR_SMS_2; //100 + } + + //On met les prescalers à 0, on veut compter chaque transition + TIMx -> CCMR1 &= ~TIM_CCMR1_IC1PSC; + TIMx -> CCMR1 &= ~TIM_CCMR1_IC2PSC; + + TIMx -> CCER |= TIM_CCER_CC1E | TIM_CCER_CC2E; + + //TIMx -> DIER |= TIM_DIER_CC1IE; gestion des interrupts, probablement pas utile //TIM_DIER_CC1DE_Pos; Probablement pas utile } diff --git a/MyDrivers/Timer.h b/MyDrivers/Timer.h index 800446c..f106812 100644 --- a/MyDrivers/Timer.h +++ b/MyDrivers/Timer.h @@ -59,7 +59,7 @@ void Timer_stop(TIM_TypeDef * timer); * PWM INPUT ***************************************************************************/ -void PWMi_conf(TIM_TypeDef * timer, uint32_t channel); +void PWMi_conf(TIM_TypeDef * timer, int channel); int PWMi_getDutyCycle(TIM_TypeDef * timer); diff --git a/Services/RFReceiver.c b/Services/RFReceiver.c index c560906..87a3d81 100644 --- a/Services/RFReceiver.c +++ b/Services/RFReceiver.c @@ -1 +1,17 @@ #include "RFReceiver.h" + +void RFReceiver_conf() { + + PWMi_conf(TIM4, 1); + + } + +double RFReceiver_getData(){ + + int duty_cycle = PWMi_getDutyCycle(TIM4); + int period = PWMi_getPeriod(TIM4); + double duree_impulsion = duty_cycle * period; + + return (duree_impulsion -1) * 200 - 100; + +} \ No newline at end of file diff --git a/Services/RFReceiver.h b/Services/RFReceiver.h index e14454b..ce84ce4 100644 --- a/Services/RFReceiver.h +++ b/Services/RFReceiver.h @@ -1,4 +1,10 @@ #ifndef RFRECEIVER_H #define RFRECEIVER_H +#include "Timer.c" + +void RFReceiver_conf(void); + +double RFReceiver_getData(void); + #endif From 92945a99affe47ba650d0411857d0e2c61074f90 Mon Sep 17 00:00:00 2001 From: benassai Date: Wed, 11 Nov 2020 17:37:59 +0100 Subject: [PATCH 5/6] Premiere version de DCMotor, a modifier pour correspondre au fonctionnement en reel. --- Services/DCMotor.c | 34 ++++++++++++++++++++++++++++++++++ Services/DCMotor.h | 8 ++++++++ Services/RFReceiver.c | 2 +- Services/RFReceiver.h | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Services/DCMotor.c b/Services/DCMotor.c index 4b7e7e9..bf33cd8 100644 --- a/Services/DCMotor.c +++ b/Services/DCMotor.c @@ -1 +1,35 @@ #include "DCMotor.h" + +void DCMotor_conf(double speed) { + + double speedAbs = (speed > 0.) ? speed : -speed; + int sens = (speed > 0.) ? 1 : 0; + + //On règle la vitesse en valeur absolue + Timer_pwmo_conf(TIM2, LL_TIM_CHANNEL_CH2, 50, speedAbs); + + //On règle le sens du moteur + GPIO_conf(GPIOA, LL_GPIO_PIN_2, LL_GPIO_MODE_OUTPUT, LL_GPIO_OUTPUT_OPENDRAIN, LL_GPIO_PULL_DOWN); + GPIO_setPin(GPIOA, LL_GPIO_PIN_2, sens); +} + +void DCMotor_setSpeed(double speed) { + + double speedAbs = (speed > 0.) ? speed : -speed; + int sens = (speed > 0.) ? 1 : 0; + + Timer_pwmo_setDutyCycle(TIM2, LL_TIM_CHANNEL_CH2, speedAbs); + GPIO_setPin(GPIOA, LL_GPIO_PIN_2, sens); + +} + +double DCMotor_getSpeed(){ + + double speedAbs = Timer_pwmo_getDutyCycle(TIM2, LL_TIM_CHANNEL_CH2); + int sens = GPIO_readPin(GPIOA, LL_GPIO_PIN_2); + + double speed = (sens) ? speedAbs : -speedAbs; + + return speed; + +} diff --git a/Services/DCMotor.h b/Services/DCMotor.h index 924b7d4..417a320 100644 --- a/Services/DCMotor.h +++ b/Services/DCMotor.h @@ -1,5 +1,13 @@ #ifndef DCMOTOR_H #define DCMOTOR_H +#include "GPIO.h" +#include "Timer.h" + +void DCMotor_conf(double duty_cycle); + +void DCMotor_setSpeed(double speed); + +double DCMotor_getSpeed(void); #endif diff --git a/Services/RFReceiver.c b/Services/RFReceiver.c index 87a3d81..784dba2 100644 --- a/Services/RFReceiver.c +++ b/Services/RFReceiver.c @@ -14,4 +14,4 @@ double RFReceiver_getData(){ return (duree_impulsion -1) * 200 - 100; -} \ No newline at end of file +} diff --git a/Services/RFReceiver.h b/Services/RFReceiver.h index ce84ce4..d275b20 100644 --- a/Services/RFReceiver.h +++ b/Services/RFReceiver.h @@ -1,7 +1,7 @@ #ifndef RFRECEIVER_H #define RFRECEIVER_H -#include "Timer.c" +#include "Timer.h" void RFReceiver_conf(void); From c973c13ba51aace5c0e84a99e4de6500fb90ff2e Mon Sep 17 00:00:00 2001 From: benassai Date: Wed, 11 Nov 2020 17:58:17 +0100 Subject: [PATCH 6/6] Modification des arguments de DCMotor_conf et ecriture de Orientation --- Services/DCMotor.c | 13 +++++-------- Services/DCMotor.h | 2 +- Src/Orientation.c | 18 ++++++++++++++++++ Src/Orientation.h | 7 +++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Services/DCMotor.c b/Services/DCMotor.c index bf33cd8..f65bcc8 100644 --- a/Services/DCMotor.c +++ b/Services/DCMotor.c @@ -1,16 +1,13 @@ #include "DCMotor.h" -void DCMotor_conf(double speed) { +void DCMotor_conf() { - double speedAbs = (speed > 0.) ? speed : -speed; - int sens = (speed > 0.) ? 1 : 0; + //On règle la vitesse en valeur absolue, ici à 0 + Timer_pwmo_conf(TIM2, LL_TIM_CHANNEL_CH2, 50, 0); - //On règle la vitesse en valeur absolue - Timer_pwmo_conf(TIM2, LL_TIM_CHANNEL_CH2, 50, speedAbs); - - //On règle le sens du moteur + //On règle le sens du moteur, ici sens direct (?) GPIO_conf(GPIOA, LL_GPIO_PIN_2, LL_GPIO_MODE_OUTPUT, LL_GPIO_OUTPUT_OPENDRAIN, LL_GPIO_PULL_DOWN); - GPIO_setPin(GPIOA, LL_GPIO_PIN_2, sens); + GPIO_setPin(GPIOA, LL_GPIO_PIN_2, 0); } void DCMotor_setSpeed(double speed) { diff --git a/Services/DCMotor.h b/Services/DCMotor.h index 417a320..1305ee9 100644 --- a/Services/DCMotor.h +++ b/Services/DCMotor.h @@ -4,7 +4,7 @@ #include "GPIO.h" #include "Timer.h" -void DCMotor_conf(double duty_cycle); +void DCMotor_conf(void); void DCMotor_setSpeed(double speed); diff --git a/Src/Orientation.c b/Src/Orientation.c index 020f22b..ea05c7c 100644 --- a/Src/Orientation.c +++ b/Src/Orientation.c @@ -1 +1,19 @@ #include "Orientation.h" +#define SEUIL 30 + +void Orientation_conf() { + + DCMotor_conf(); + RFReceiver_conf(); + +} + +void Orientation_background(){ + + double speed = RFReceiver_getData(); + + //Si la vitesse (en valeur absolue) ne dépasse pas un certain seuil, on ne démarre pas le moteur + if (-SEUILspeed) DCMotor_setSpeed(0); + else DCMotor_setSpeed(speed); + +} diff --git a/Src/Orientation.h b/Src/Orientation.h index d6c0abd..fdba2d4 100644 --- a/Src/Orientation.h +++ b/Src/Orientation.h @@ -1,4 +1,11 @@ #ifndef ORIENTATION_H #define ORIENTATION_H +#include "DCMotor.h" +#include "RFReceiver.h" + +void Orientation_conf(void); + +void Orientation_background(void); + #endif