mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-11-29 07:24:13 +01:00
OP-1685 - Implement OneShot on F1
This commit is contained in:
parent
544b0357c7
commit
f290d6d89d
@ -40,8 +40,10 @@
|
||||
static const struct pios_servo_cfg *servo_cfg;
|
||||
|
||||
// determine if the related timer will work in synchronous (or OneShot/OneShot125) One Pulse mode.
|
||||
// static uint8_t pios_servo_bank_mode[PIOS_SERVO_BANKS] = { 0 };
|
||||
|
||||
static uint8_t pios_servo_bank_mode[PIOS_SERVO_BANKS] = { 0 };
|
||||
// used to skip updates when pulse length is higher than update cycle
|
||||
static uint16_t pios_servo_bank_next_update[PIOS_SERVO_BANKS] = { 0 };
|
||||
static uint16_t pios_servo_bank_max_pulse[PIOS_SERVO_BANKS] = { 0 };
|
||||
// timer associated to each bank
|
||||
static TIM_TypeDef *pios_servo_bank_timer[PIOS_SERVO_BANKS] = { 0 };
|
||||
|
||||
@ -49,7 +51,7 @@ static TIM_TypeDef *pios_servo_bank_timer[PIOS_SERVO_BANKS] = { 0 };
|
||||
static uint8_t *pios_servo_pin_bank;
|
||||
|
||||
#define PIOS_SERVO_TIMER_CLOCK 1000000
|
||||
|
||||
#define PIOS_SERVO_SAFE_MARGIN 50
|
||||
/**
|
||||
* Initialise Servos
|
||||
*/
|
||||
@ -91,6 +93,12 @@ int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg)
|
||||
pios_servo_pin_bank[j] = bank;
|
||||
}
|
||||
}
|
||||
pios_servo_bank_timer[bank] = chan->timer;
|
||||
|
||||
TIM_ARRPreloadConfig(chan->timer, ENABLE);
|
||||
TIM_CtrlPWMOutputs(chan->timer, ENABLE);
|
||||
TIM_Cmd(chan->timer, ENABLE);
|
||||
|
||||
bank++;
|
||||
}
|
||||
|
||||
@ -113,10 +121,6 @@ int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg)
|
||||
TIM_OC4PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
}
|
||||
|
||||
TIM_ARRPreloadConfig(chan->timer, ENABLE);
|
||||
TIM_CtrlPWMOutputs(chan->timer, ENABLE);
|
||||
TIM_Cmd(chan->timer, ENABLE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -166,37 +170,106 @@ void PIOS_Servo_Set(uint8_t servo, uint16_t position)
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* Update the position */
|
||||
const struct pios_tim_channel *chan = &servo_cfg->channels[servo];
|
||||
uint16_t val = position;
|
||||
uint16_t margin = chan->timer->ARR / 50; // Leave 2% of period as margin to prevent overlaps
|
||||
if (val > (chan->timer->ARR - margin)) {
|
||||
val = chan->timer->ARR - margin;
|
||||
}
|
||||
|
||||
uint8_t bank = pios_servo_pin_bank[servo];
|
||||
if (pios_servo_bank_max_pulse[bank] < val) {
|
||||
pios_servo_bank_max_pulse[bank] = val;
|
||||
}
|
||||
switch (chan->timer_chan) {
|
||||
case TIM_Channel_1:
|
||||
TIM_SetCompare1(chan->timer, position);
|
||||
TIM_SetCompare1(chan->timer, val);
|
||||
break;
|
||||
case TIM_Channel_2:
|
||||
TIM_SetCompare2(chan->timer, position);
|
||||
TIM_SetCompare2(chan->timer, val);
|
||||
break;
|
||||
case TIM_Channel_3:
|
||||
TIM_SetCompare3(chan->timer, position);
|
||||
TIM_SetCompare3(chan->timer, val);
|
||||
break;
|
||||
case TIM_Channel_4:
|
||||
TIM_SetCompare4(chan->timer, position);
|
||||
TIM_SetCompare4(chan->timer, val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PIOS_Servo_Update()
|
||||
{
|
||||
/*
|
||||
for (uint8_t i = 0; (i < PIOS_SERVO_BANKS); i++) {
|
||||
for (uint8_t i = 0; (i < PIOS_SERVO_BANKS); i++) {
|
||||
const TIM_TypeDef *timer = pios_servo_bank_timer[i];
|
||||
if (timer) {
|
||||
TIM_Cmd((TIM_TypeDef *)timer, ENABLE);
|
||||
if (timer && pios_servo_bank_mode[i] == PIOS_SERVO_BANK_MODE_SINGLE_PULSE) {
|
||||
// a pulse to be generated is longer than cycle period. skip this update.
|
||||
if (TIM_GetCounter((TIM_TypeDef *)timer) > (uint32_t)(pios_servo_bank_next_update[i] + PIOS_SERVO_SAFE_MARGIN)) {
|
||||
TIM_GenerateEvent((TIM_TypeDef *)timer, TIM_EventSource_Update);
|
||||
pios_servo_bank_next_update[i] = pios_servo_bank_max_pulse[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
pios_servo_bank_max_pulse[i] = 0;
|
||||
}
|
||||
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
||||
uint8_t bank = pios_servo_pin_bank[i];
|
||||
uint8_t mode = pios_servo_bank_mode[bank];
|
||||
if (mode == PIOS_SERVO_BANK_MODE_SINGLE_PULSE) {
|
||||
/* Update the position */
|
||||
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
||||
|
||||
switch (chan->timer_chan) {
|
||||
case TIM_Channel_1:
|
||||
TIM_SetCompare1(chan->timer, 0);
|
||||
break;
|
||||
case TIM_Channel_2:
|
||||
TIM_SetCompare2(chan->timer, 0);
|
||||
break;
|
||||
case TIM_Channel_3:
|
||||
TIM_SetCompare3(chan->timer, 0);
|
||||
break;
|
||||
case TIM_Channel_4:
|
||||
TIM_SetCompare4(chan->timer, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PIOS_Servo_SetBankMode(uint8_t bank, uint8_t mode)
|
||||
{
|
||||
PIOS_Assert(bank < PIOS_SERVO_BANKS);
|
||||
pios_servo_bank_mode[bank] = mode;
|
||||
|
||||
if (pios_servo_bank_timer[bank]) {
|
||||
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
||||
if (pios_servo_pin_bank[i] == bank) {
|
||||
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
||||
/* Set up for output compare function */
|
||||
switch (chan->timer_chan) {
|
||||
case TIM_Channel_1:
|
||||
TIM_OC1PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_2:
|
||||
TIM_OC2PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_3:
|
||||
TIM_OC3PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_4:
|
||||
TIM_OC4PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Setup the timer accordingly
|
||||
TIM_SelectOnePulseMode(pios_servo_bank_timer[bank], TIM_OPMode_Repetitive);
|
||||
TIM_Cmd(pios_servo_bank_timer[bank], ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
void PIOS_Servo_SetBankMode(__attribute__((unused)) uint8_t bank, __attribute__((unused)) uint8_t mode) {}
|
||||
|
||||
uint8_t PIOS_Servo_GetPinBank(uint8_t pin)
|
||||
{
|
||||
|
@ -3,11 +3,11 @@
|
||||
<description>Settings for the @ref ActuatorModule that controls the channel assignments for the mixer based on AircraftType</description>
|
||||
<field name="BankUpdateFreq" units="Hz" type="uint16" elements="6" defaultvalue="50"/>
|
||||
<field name="BankMode" type="enum" units="" elements="6" options="PWM,OneShot,OneShot125" defaultvalue="PWM"
|
||||
limits="%0401NE:OneShot:OneShot125,%0402NE:OneShot:OneShot125; \
|
||||
%0401NE:OneShot:OneShot125,%0402NE:OneShot:OneShot125; \
|
||||
%0401NE:OneShot:OneShot125,%0402NE:OneShot:OneShot125; \
|
||||
%0401NE:OneShot:OneShot125,%0402NE:OneShot:OneShot125; \
|
||||
%0401NE:OneShot:OneShot125,%0402NE:OneShot:OneShot125; \
|
||||
limits="; \
|
||||
; \
|
||||
; \
|
||||
; \
|
||||
; \
|
||||
%0401NE:OneShot:OneShot125,%0402NE:OneShot:OneShot125;"
|
||||
/>
|
||||
<field name="ChannelMax" units="us" type="int16" elements="12" defaultvalue="1000"/>
|
||||
|
Loading…
Reference in New Issue
Block a user