diff --git a/MyDrivers/Timer.c b/MyDrivers/Timer.c index b0b5cad..311e027 100644 --- a/MyDrivers/Timer.c +++ b/MyDrivers/Timer.c @@ -165,7 +165,62 @@ void Timer_conf(TIM_TypeDef * timer, int arr, int psc) /**************************************************************************** * PWM INPUT ***************************************************************************/ +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); + + + 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 + } + + 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 +} + +int PWMi_getPeriod(TIM_TypeDef * TIMx) { + + return TIMx -> CCR1; +} + +int PWMi_getDutyCycle(TIM_TypeDef * TIMx) { + + return TIMx -> CCR2; +} /**************************************************************************** * PWM OUTPUT diff --git a/MyDrivers/Timer.h b/MyDrivers/Timer.h index cc484f7..f106812 100644 --- a/MyDrivers/Timer.h +++ b/MyDrivers/Timer.h @@ -61,7 +61,9 @@ void Timer_stop(TIM_TypeDef * timer); void PWMi_conf(TIM_TypeDef * timer, int 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/DCMotor.c b/Services/DCMotor.c index 4b7e7e9..f65bcc8 100644 --- a/Services/DCMotor.c +++ b/Services/DCMotor.c @@ -1 +1,32 @@ #include "DCMotor.h" + +void DCMotor_conf() { + + //On règle la vitesse en valeur absolue, ici à 0 + Timer_pwmo_conf(TIM2, LL_TIM_CHANNEL_CH2, 50, 0); + + //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, 0); +} + +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..1305ee9 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(void); + +void DCMotor_setSpeed(double speed); + +double DCMotor_getSpeed(void); #endif 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/RFReceiver.c b/Services/RFReceiver.c index c560906..784dba2 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; + +} diff --git a/Services/RFReceiver.h b/Services/RFReceiver.h index e14454b..d275b20 100644 --- a/Services/RFReceiver.h +++ b/Services/RFReceiver.h @@ -1,4 +1,10 @@ #ifndef RFRECEIVER_H #define RFRECEIVER_H +#include "Timer.h" + +void RFReceiver_conf(void); + +double RFReceiver_getData(void); + #endif diff --git a/Services/Voltage.c b/Services/Voltage.c index e47b4a9..b982d45 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() { + + return ADC_readVolt(ADC2); + +} diff --git a/Services/Voltage.h b/Services/Voltage.h index 141c358..bbb164f 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(void); + +double Voltage_getVoltage(void); + #endif 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