Merge branch 'servo' into rf_input
This commit is contained in:
commit
8f010307f7
4 changed files with 86 additions and 0 deletions
16
keil_project/Services/Sail.c
Normal file
16
keil_project/Services/Sail.c
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#include "Sail.h"
|
||||||
|
#include "Servo.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define SAIL_TRANSFER_FACTOR 1.0
|
||||||
|
#define SAIL_TRANSFER_OFFSET 0
|
||||||
|
|
||||||
|
void SAIL_Init(void)
|
||||||
|
{
|
||||||
|
SERVO_Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SAIL_SetAngle(float angle)
|
||||||
|
{
|
||||||
|
float servo_angle = angle * SAIL_TRANSFER_FACTOR + SAIL_TRANSFER_OFFSET;
|
||||||
|
}
|
6
keil_project/Services/Sail.h
Normal file
6
keil_project/Services/Sail.h
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
|
||||||
|
void SAIL_Init(void);
|
||||||
|
|
||||||
|
// sets the opening angle of the sail
|
||||||
|
void SAIL_SetAngle(float angle);
|
56
keil_project/Services/Servo.c
Normal file
56
keil_project/Services/Servo.c
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "Servo.h"
|
||||||
|
#include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges
|
||||||
|
#include "stm32f1xx_ll_tim.h"
|
||||||
|
#include "stm32f1xx_ll_gpio.h"
|
||||||
|
|
||||||
|
|
||||||
|
void SERVO_Init(void){
|
||||||
|
// use TIM2 since it has pa1 at channel 2 output
|
||||||
|
|
||||||
|
// setup timer 2
|
||||||
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
|
||||||
|
LL_TIM_InitTypeDef tim_init;
|
||||||
|
tim_init.Prescaler = 71;
|
||||||
|
tim_init.Autoreload = 19999;
|
||||||
|
tim_init.ClockDivision=LL_TIM_CLOCKDIVISION_DIV1;
|
||||||
|
tim_init.CounterMode=LL_TIM_COUNTERMODE_UP;
|
||||||
|
tim_init.RepetitionCounter=0;
|
||||||
|
LL_TIM_Init(TIM4, &tim_init);
|
||||||
|
|
||||||
|
// setup gpio
|
||||||
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
|
||||||
|
LL_GPIO_InitTypeDef servo_gpio_init;
|
||||||
|
servo_gpio_init.Pin = LL_GPIO_PIN_8;
|
||||||
|
servo_gpio_init.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||||
|
servo_gpio_init.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||||
|
servo_gpio_init.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||||
|
LL_GPIO_Init(GPIOB, &servo_gpio_init);
|
||||||
|
|
||||||
|
// setup output compare
|
||||||
|
LL_TIM_OC_InitTypeDef oc_init;
|
||||||
|
oc_init.OCMode = LL_TIM_OCMODE_PWM1;
|
||||||
|
oc_init.OCState = LL_TIM_OCSTATE_ENABLE;
|
||||||
|
oc_init.OCNState = LL_TIM_OCSTATE_ENABLE;
|
||||||
|
oc_init.CompareValue = 999; // 999 should corresponds to 0°
|
||||||
|
oc_init.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
|
||||||
|
oc_init.OCNPolarity = LL_TIM_OCPOLARITY_LOW;
|
||||||
|
oc_init.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
|
||||||
|
oc_init.OCNIdleState = LL_TIM_OCIDLESTATE_HIGH;
|
||||||
|
LL_TIM_OC_Init(TIM4, LL_TIM_CHANNEL_CH3, &oc_init);
|
||||||
|
LL_TIM_EnableCounter(TIM4);
|
||||||
|
|
||||||
|
// period of timer should be 20 ms
|
||||||
|
// input= 72mhz
|
||||||
|
// per
|
||||||
|
}
|
||||||
|
|
||||||
|
void SERVO_SetAngle(int angle){
|
||||||
|
// set ccr3 register to alter pwm output between 1 and 2 ms
|
||||||
|
float vminCOR = 999, vmaxCOR = 1999;
|
||||||
|
float diffCOR = vmaxCOR - vminCOR;
|
||||||
|
int ccr3Value = round(angle*diffCOR/90)+vminCOR;
|
||||||
|
|
||||||
|
LL_TIM_WriteReg(TIM4, CCR3, ccr3Value);
|
||||||
|
}
|
8
keil_project/Services/Servo.h
Normal file
8
keil_project/Services/Servo.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef SERVO
|
||||||
|
#define SERVO
|
||||||
|
|
||||||
|
void SERVO_Init(void);
|
||||||
|
|
||||||
|
void SERVO_SetAngle(int angle);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue