From 89867cd01e172eb1a7e00f7d6d300fa7cc44e0e3 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Fri, 13 Nov 2020 08:49:00 +0100 Subject: [PATCH] make code more consistent --- MyDrivers/ADC.c | 5 ++++- MyDrivers/GPIO.c | 20 +++++++++++--------- MyDrivers/USART.c | 12 ++++++++---- Services/Accelerometer.c | 2 -- Services/DCMotor.c | 36 ++++++++++++++++-------------------- Services/DCMotor.h | 6 +++--- Services/RFEmitter.c | 24 +++++++++++------------- Services/RFEmitter.h | 6 ++++-- Services/RFReceiver.c | 20 +++++++++----------- Services/RFReceiver.h | 4 ++-- Services/Voltage.c | 24 ++++++++++++------------ Services/Voltage.h | 6 ++++-- Src/Display.c | 26 ++++++++++++++++++++++++++ Src/Display.h | 6 ++++++ Src/Orientation.c | 36 ++++++++++++++++++++++++------------ Src/Sail.c | 15 +++++++-------- Src/main.c | 13 +++++++++---- 17 files changed, 156 insertions(+), 105 deletions(-) diff --git a/MyDrivers/ADC.c b/MyDrivers/ADC.c index e1c6b13..de5b8a5 100644 --- a/MyDrivers/ADC.c +++ b/MyDrivers/ADC.c @@ -1,6 +1,9 @@ #include "ADC.h" #include "stm32f1xx_ll_bus.h" // Pour horloge +const float MAX_VOLTS = 3.3; +const float MAX_CONVERTED_VALUE = 4095.0; + void ADC_conf(ADC_TypeDef *adc) { if (adc == ADC1) { @@ -43,7 +46,7 @@ uint16_t ADC_readRaw(ADC_TypeDef *adc, int channel) float ADC_convertToVolt(uint16_t value) { - return ((double) value) / 4095.0 * 3.3; + return ((double) value) / MAX_CONVERTED_VALUE * MAX_VOLTS; } diff --git a/MyDrivers/GPIO.c b/MyDrivers/GPIO.c index b5fccc2..cfc20bd 100644 --- a/MyDrivers/GPIO.c +++ b/MyDrivers/GPIO.c @@ -1,8 +1,8 @@ #include "GPIO.h" #include "stm32f1xx_ll_gpio.h" -void GPIO_conf(GPIO_TypeDef * GPIOx, uint32_t PINx, uint32_t mode, uint32_t outputType, uint32_t pullMode){ - +void GPIO_conf(GPIO_TypeDef * GPIOx, uint32_t PINx, uint32_t mode, uint32_t outputType, uint32_t pullMode) +{ LL_GPIO_InitTypeDef init; //Activation de l'horloge @@ -21,15 +21,17 @@ void GPIO_conf(GPIO_TypeDef * GPIOx, uint32_t PINx, uint32_t mode, uint32_t outp LL_GPIO_Init(GPIOx, &init); } -void GPIO_setPin(GPIO_TypeDef * GPIOx, uint32_t PINx, int output){ - - if (output) LL_GPIO_SetOutputPin(GPIOx, PINx); - else LL_GPIO_ResetOutputPin(GPIOx,PINx); - +void GPIO_setPin(GPIO_TypeDef * GPIOx, uint32_t PINx, int output) +{ + if (output) { + LL_GPIO_SetOutputPin(GPIOx, PINx); + } else { + LL_GPIO_ResetOutputPin(GPIOx,PINx); + } }; -int GPIO_readPin(GPIO_TypeDef * GPIOx, uint32_t PINx){ - +int GPIO_readPin(GPIO_TypeDef * GPIOx, uint32_t PINx) +{ return LL_GPIO_IsOutputPinSet(GPIOx, PINx); } diff --git a/MyDrivers/USART.c b/MyDrivers/USART.c index 54706c1..b32464d 100644 --- a/MyDrivers/USART.c +++ b/MyDrivers/USART.c @@ -3,7 +3,8 @@ #include "stm32f1xx_ll_bus.h" // Pour horloge -void Usart_conf(USART_TypeDef *USARTx) { +void Usart_conf(USART_TypeDef *USARTx) +{ int txPin; GPIO_TypeDef *usartGpio; @@ -36,18 +37,21 @@ void Usart_conf(USART_TypeDef *USARTx) { -void Usart_enable(USART_TypeDef *USARTx) { +void Usart_enable(USART_TypeDef *USARTx) +{ LL_USART_Enable(USARTx); } -void sendChar(USART_TypeDef *USARTx, char c) { +void sendChar(USART_TypeDef *USARTx, char c) +{ LL_USART_TransmitData8(USARTx, c); while (!LL_USART_IsActiveFlag_TXE(USARTx)) {} } -void Usart_send(USART_TypeDef *USARTx, char *msg, int length) { +void Usart_send(USART_TypeDef *USARTx, char *msg, int length) +{ for (int i = 0; i < length; i++) { sendChar(USARTx, msg[i]); } diff --git a/Services/Accelerometer.c b/Services/Accelerometer.c index 99005e1..7c1e148 100644 --- a/Services/Accelerometer.c +++ b/Services/Accelerometer.c @@ -9,8 +9,6 @@ const float ZERO_G = 1.65; // 0 g -//const float MAX_G = 2.13; // 1 g -//const float MIN_G = 1.17; // -1 g const float SENSITIVITY = 0.48; void Accelerometer_conf(ADC_TypeDef *adc, GPIO_TypeDef * gpio, int pinx, int piny) diff --git a/Services/DCMotor.c b/Services/DCMotor.c index f65bcc8..0d05b01 100644 --- a/Services/DCMotor.c +++ b/Services/DCMotor.c @@ -1,32 +1,28 @@ #include "DCMotor.h" +#include "math.h" -void DCMotor_conf() { - +void DCMotor_conf(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin) +{ //On règle la vitesse en valeur absolue, ici à 0 - Timer_pwmo_conf(TIM2, LL_TIM_CHANNEL_CH2, 50, 0); + Timer_pwmo_conf(timer, channel, 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); + GPIO_conf(gpio, pin, LL_GPIO_MODE_OUTPUT, LL_GPIO_OUTPUT_OPENDRAIN, LL_GPIO_PULL_DOWN); + GPIO_setPin(gpio, pin, 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); +void DCMotor_setSpeed(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin, float speed) +{ + const int dir = (speed > 0.) ? 1 : 0; + Timer_pwmo_setDutyCycle(timer, channel, fabs(speed)); + GPIO_setPin(gpio, pin, dir); } -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; +float DCMotor_getSpeed(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin) +{ + const float speedAbs = Timer_pwmo_getDutyCycle(timer, channel); + const int dir = GPIO_readPin(gpio, pin); + return dir ? speedAbs : -speedAbs; } diff --git a/Services/DCMotor.h b/Services/DCMotor.h index 1305ee9..3180e34 100644 --- a/Services/DCMotor.h +++ b/Services/DCMotor.h @@ -4,10 +4,10 @@ #include "GPIO.h" #include "Timer.h" -void DCMotor_conf(void); +void DCMotor_conf(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin); -void DCMotor_setSpeed(double speed); +void DCMotor_setSpeed(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin, float speed); -double DCMotor_getSpeed(void); +float DCMotor_getSpeed(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin); #endif diff --git a/Services/RFEmitter.c b/Services/RFEmitter.c index 5c3f56d..c20e91d 100644 --- a/Services/RFEmitter.c +++ b/Services/RFEmitter.c @@ -1,18 +1,16 @@ #include "RFEmitter.h" - -void RFEmitter_conf() { - - //On configure l'USART - Usart_conf(USART1); - - //On active l'USART - Usart_enable(USART1); - +void RFEmitter_conf(USART_TypeDef * usart) +{ + Usart_conf(usart); } -void RFEmitter_send(char * message, int longueur) { - - Usart_send(USART1, message, longueur); - +void RFEmitter_start(USART_TypeDef * usart) +{ + Usart_enable(usart); +} + +void RFEmitter_send(USART_TypeDef * usart, char * message, int longueur) +{ + Usart_send(usart, message, longueur); } diff --git a/Services/RFEmitter.h b/Services/RFEmitter.h index 3f092d7..29ac108 100644 --- a/Services/RFEmitter.h +++ b/Services/RFEmitter.h @@ -3,9 +3,11 @@ #include "USART.h" -void RFEmitter_conf(void); +void RFEmitter_conf(USART_TypeDef * usart); -void RFEmitter_send(char * message, int longueur); +void RFEmitter_start(USART_TypeDef * usart); + +void RFEmitter_send(USART_TypeDef * usart, char * message, int longueur); #endif diff --git a/Services/RFReceiver.c b/Services/RFReceiver.c index 784dba2..c0714de 100644 --- a/Services/RFReceiver.c +++ b/Services/RFReceiver.c @@ -1,17 +1,15 @@ #include "RFReceiver.h" -void RFReceiver_conf() { - - PWMi_conf(TIM4, 1); - - } +void RFReceiver_conf(TIM_TypeDef * timer, int channel) +{ + PWMi_conf(timer, channel); +} -double RFReceiver_getData(){ - - int duty_cycle = PWMi_getDutyCycle(TIM4); - int period = PWMi_getPeriod(TIM4); - double duree_impulsion = duty_cycle * period; +float RFReceiver_getData(TIM_TypeDef * timer) +{ + const int duty_cycle = PWMi_getDutyCycle(timer); + const int period = PWMi_getPeriod(timer); + const float duree_impulsion = duty_cycle * period; return (duree_impulsion -1) * 200 - 100; - } diff --git a/Services/RFReceiver.h b/Services/RFReceiver.h index d275b20..ff53725 100644 --- a/Services/RFReceiver.h +++ b/Services/RFReceiver.h @@ -3,8 +3,8 @@ #include "Timer.h" -void RFReceiver_conf(void); +void RFReceiver_conf(TIM_TypeDef * timer, int channel); -double RFReceiver_getData(void); +float RFReceiver_getData(TIM_TypeDef * timer); #endif diff --git a/Services/Voltage.c b/Services/Voltage.c index b982d45..abeb27d 100644 --- a/Services/Voltage.c +++ b/Services/Voltage.c @@ -1,21 +1,21 @@ #include "Voltage.h" -void Voltage_conf() { - +void Voltage_conf(ADC_TypeDef * adc, GPIO_TypeDef * gpio, int pin) +{ //On configure le pin qui recevra le signal, ici PC2 - GPIO_conf(GPIOC, LL_GPIO_PIN_2, LL_GPIO_MODE_ANALOG, 0, 0); + GPIO_conf(gpio, pin, LL_GPIO_MODE_ANALOG, 0, 0); //On configure l'ADC - ADC_conf(ADC2, 12); - - //On démarre l'ADC - ADC_start(ADC2); - + ADC_conf(adc); } -double Voltage_getVoltage() { - - return ADC_readVolt(ADC2); - +void Voltage_start(ADC_TypeDef * adc) +{ + ADC_start(adc); +} + +float Voltage_getVoltage(ADC_TypeDef * adc, int channel) +{ + return ADC_readVolt(adc, channel); } diff --git a/Services/Voltage.h b/Services/Voltage.h index bbb164f..ac724ea 100644 --- a/Services/Voltage.h +++ b/Services/Voltage.h @@ -4,8 +4,10 @@ #include "ADC.h" #include "GPIO.h" -void Voltage_conf(void); +void Voltage_conf(ADC_TypeDef * adc, GPIO_TypeDef * gpio, int pin); -double Voltage_getVoltage(void); +void Voltage_start(ADC_TypeDef * adc); + +float Voltage_getVoltage(ADC_TypeDef * adc, int channel); #endif diff --git a/Src/Display.c b/Src/Display.c index 773e01b..9ae682f 100644 --- a/Src/Display.c +++ b/Src/Display.c @@ -1 +1,27 @@ #include "Display.h" +#include "Voltage.h" +#include "RFEmitter.h" + +USART_TypeDef * EMITTER_USART = USART1; + +ADC_TypeDef * VOLTAGE_ADC = ADC2; +const int VOLTAGE_CHANNEL = LL_ADC_CHANNEL_12; +GPIO_TypeDef * VOLTAGE_GPIO = GPIOC; +const int VOLTAGE_PIN = LL_GPIO_PIN_2; + +void Display_conf() +{ + Voltage_conf(VOLTAGE_ADC, VOLTAGE_GPIO, VOLTAGE_PIN); + RFEmitter_conf(EMITTER_USART); +} + +void Display_start() +{ + Voltage_start(VOLTAGE_ADC); + RFEmitter_start(EMITTER_USART); +} + +void Display_background() +{ + float voltage = Voltage_getVoltage(ADC2, LL_ADC_CHANNEL_12); +} diff --git a/Src/Display.h b/Src/Display.h index d8b1037..c11ac27 100644 --- a/Src/Display.h +++ b/Src/Display.h @@ -1,4 +1,10 @@ #ifndef DISPLAY_H #define DISPLAY_H +void Display_conf(void); + +void Display_start(void); + +void Display_background(void); + #endif diff --git a/Src/Orientation.c b/Src/Orientation.c index ea05c7c..96fb4c5 100644 --- a/Src/Orientation.c +++ b/Src/Orientation.c @@ -1,19 +1,31 @@ #include "Orientation.h" -#define SEUIL 30 +#include "math.h" -void Orientation_conf() { - - DCMotor_conf(); - RFReceiver_conf(); - +#define THRESHOLD 30 + +TIM_TypeDef * RECEIVER_TIMER = TIM4; +const int RECEIVER_CHANNEL = LL_TIM_CHANNEL_CH1; + +TIM_TypeDef * DCMOTOR_TIMER = TIM2; +const int DCMOTOR_CHANNEL = LL_TIM_CHANNEL_CH2; +GPIO_TypeDef * DCMOTOR_GPIO = GPIOA; +const int DCMOTOR_PIN = LL_GPIO_PIN_2; + +void Orientation_conf() +{ + DCMotor_conf(DCMOTOR_TIMER, DCMOTOR_CHANNEL, DCMOTOR_GPIO, DCMOTOR_PIN); + RFReceiver_conf(RECEIVER_TIMER, LL_TIM_CHANNEL_CH1); } -void Orientation_background(){ - - double speed = RFReceiver_getData(); +void Orientation_background() +{ + const float speed = RFReceiver_getData(RECEIVER_TIMER); //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); - + if (THRESHOLD < fabs(speed)) { + DCMotor_setSpeed(DCMOTOR_TIMER, DCMOTOR_CHANNEL, DCMOTOR_GPIO, DCMOTOR_PIN, 0); + } + else { + DCMotor_setSpeed(DCMOTOR_TIMER, DCMOTOR_CHANNEL, DCMOTOR_GPIO, DCMOTOR_PIN, speed); + } } diff --git a/Src/Sail.c b/Src/Sail.c index f6b6bc2..ced4b71 100644 --- a/Src/Sail.c +++ b/Src/Sail.c @@ -21,6 +21,13 @@ void Sail_conf() IncrementalEncoder_conf(ENCODER_TIMER, ENCODER_GPIO, ENCODER_PIN); } +void Sail_start() +{ + Sail_isEmergencyState = 0; + ServoMotor_start(MOTOR_TIMER); + IncrementalEncoder_start(ENCODER_TIMER); +} + int getSailAngle(int windAngle) { if (windAngle > 180) @@ -48,11 +55,3 @@ void Sail_setEmergency(int state) if (Sail_isEmergencyState) ServoMotor_setAngle(MOTOR_TIMER, MOTOR_CHANNEL, RESET_ANGLE); } - - -void Sail_start() -{ - Sail_isEmergencyState = 0; - ServoMotor_start(MOTOR_TIMER); - IncrementalEncoder_start(ENCODER_TIMER); -} diff --git a/Src/main.c b/Src/main.c index d55b4a8..166fdcd 100644 --- a/Src/main.c +++ b/Src/main.c @@ -22,6 +22,8 @@ #include "Sail.h" #include "Roll.h" +#include "Display.h" +#include "Orientation.h" #include "Scheduler.h" #include "ADC.h" @@ -45,10 +47,12 @@ void backgroundTask() counter++; Sail_background(); Roll_background(); + Orientation_background(); - angle = Accelerometer_getAngle(ADC1, LL_ADC_CHANNEL_11); // DEBUG + angle = Accelerometer_getAngle(ADC1, LL_ADC_CHANNEL_11); + adcRaw1 = ADC_readRaw(ADC1, LL_ADC_CHANNEL_11); adcRaw2 = ADC_readRaw(ADC1, LL_ADC_CHANNEL_10); adcVolt1 = ADC_convertToVolt(adcRaw1); @@ -59,6 +63,7 @@ void configurePeripherals() { Sail_conf(); Roll_conf(); + Orientation_conf(); // DEBUG ADC_conf(ADC1); @@ -68,6 +73,7 @@ void startPeripherals() { Sail_start(); Roll_start(); + Display_start(); // DEBUG ADC_start(ADC1); @@ -90,9 +96,8 @@ int main(void) Scheduler_conf(backgroundTask); Scheduler_start(); - while (1) - { - // Send to display here + while (1) { + Display_background(); } }