diff --git a/keil_project/Services/IncrEncoder.c b/keil_project/Services/IncrEncoder.c index b9a8b9b..23a8587 100644 --- a/keil_project/Services/IncrEncoder.c +++ b/keil_project/Services/IncrEncoder.c @@ -7,13 +7,11 @@ #include "stm32f1xx_ll_exti.h" #include "stm32f1xx_ll_tim.h" -#define RESOLUTION 1 -#define ANGLE_DEBUT 45 -#define INCR_ENCODER_MID_VALUE 719 - +#define ZERO_POSITION 0 +#define TICKS_PER_REVOLUTION 360*4 +#define DEGREE_PER_TICKS 0.25 int index_passed = 0; -int counts_per_revolution = 360; void INCR_ENCODER_Init(void){ @@ -33,7 +31,7 @@ void INCR_ENCODER_Init(void){ LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3); LL_TIM_InitTypeDef tim3_init_struct; - tim3_init_struct.Autoreload= counts_per_revolution*4-1; + tim3_init_struct.Autoreload= TICKS_PER_REVOLUTION-1; tim3_init_struct.Prescaler=0; tim3_init_struct.ClockDivision=LL_TIM_CLOCKDIVISION_DIV1; tim3_init_struct.CounterMode=LL_TIM_COUNTERMODE_UP; @@ -101,17 +99,12 @@ int INCR_ENCODER_IsAbsolute(void) return index_passed; }; -int INCR_ENCODER_GetAngle(void) +float INCR_ENCODER_GetAngle(void) { int counter_value = LL_TIM_ReadReg(TIM3, CNT); - float vabs = abs(counter_value-INCR_ENCODER_MID_VALUE); - float vIEAngleDebut = INCR_ENCODER_MID_VALUE -(ANGLE_DEBUT*4); - float nbIncrements = 90/RESOLUTION; - - if(vabs > vIEAngleDebut) - { - return 0; - }else{ - return 90 - RESOLUTION*floor(vabs/(vIEAngleDebut/nbIncrements) ) ; - } + // center + int centered_counter_value = (counter_value - ZERO_POSITION) % TICKS_PER_REVOLUTION; + // translate ticks to angle + float angle = centered_counter_value * DEGREE_PER_TICKS; + return angle; }; diff --git a/keil_project/Services/IncrEncoder.h b/keil_project/Services/IncrEncoder.h index dfa6662..e9d1f31 100644 --- a/keil_project/Services/IncrEncoder.h +++ b/keil_project/Services/IncrEncoder.h @@ -6,6 +6,10 @@ void INCR_ENCODER_Init(void); int INCR_ENCODER_IsAbsolute(void); -int INCR_ENCODER_GetAngle(void); +/** + * @brief Returns the angle of the incremental encoder + * @retval -180 to 180 where 0 is the incremental encoder pointing towards the back of the boat + */ +float INCR_ENCODER_GetAngle(void); #endif diff --git a/keil_project/Services/Sail.c b/keil_project/Services/Sail.c index cf2458d..89bc0c7 100644 --- a/keil_project/Services/Sail.c +++ b/keil_project/Services/Sail.c @@ -1,15 +1,32 @@ #include "Sail.h" -#include "Servo.h" +#include "Servo.h" +#include +#include #define SAIL_TRANSFER_FACTOR 1.0 #define SAIL_TRANSFER_OFFSET 0 +#define ANGLE_DEBUT 45 + void SAIL_Init(void) { SERVO_Init(); } +int SAIL_AngleFromGirouette(float girouette_value){ + float vabs = fabs(girouette_value); + + if(vabs < ANGLE_DEBUT) + { + return 0; + }else{ + // map 45 to 180 -> 0 to 90 and floor it to get an integer + return floor((90 / (180 - ANGLE_DEBUT)) * (vabs - ANGLE_DEBUT)); + } + +} + void SAIL_SetAngle(float angle) { float servo_angle = angle * SAIL_TRANSFER_FACTOR + SAIL_TRANSFER_OFFSET; diff --git a/keil_project/Services/Sail.h b/keil_project/Services/Sail.h index 26f8aee..c981f07 100644 --- a/keil_project/Services/Sail.h +++ b/keil_project/Services/Sail.h @@ -5,6 +5,7 @@ void SAIL_Init(void); +int SAIL_AngleFromGirouette(float girouette_value); // sets the opening angle of the sail void SAIL_SetAngle(float angle); diff --git a/keil_project/Src/main.c b/keil_project/Src/main.c index 8655e79..254e45e 100644 --- a/keil_project/Src/main.c +++ b/keil_project/Src/main.c @@ -44,6 +44,7 @@ int angle_roulis_good = 0; int angle_sail = 0; int RF_Input_Duty = 0; int TX_Flag = 0, CONTROL_LOOP_Flag = 0; +float angle_incr_encoder = 0; char wait_for_girouette[] = "En attente d'initialisation de la girouette\r\n"; @@ -79,8 +80,9 @@ int main(void) SAIL_SetAngle(90); DC_MOTOR_SetSpeed(0); }else{ - angle_sail = INCR_ENCODER_GetAngle(); - SAIL_SetAngle(angle_sail/2); + angle_incr_encoder = INCR_ENCODER_GetAngle(); + angle_sail = SAIL_AngleFromGirouette(angle_incr_encoder); + SAIL_SetAngle(angle_sail); RF_Input_Duty = RF_INPUT_GetDutyTimeRelative(); DC_MOTOR_SetSpeed(RF_Input_Duty); }