55,10 → 55,30 |
#include "rc.h" |
#include "attitude.h" |
|
volatile int16_t ServoPitchValue = 0; |
volatile int16_t ServoRollValue = 0; |
volatile uint8_t ServoActive = 0; |
#define SLOW 1 |
|
#ifdef SLOW |
#define NEUTRAL_PULSELENGTH 938 |
#define SERVOLIMIT 500 |
#define SCALE_FACTOR 4 |
#define CS2 ((1<<CS21)|(1<<CS20)) |
#else |
#define NEUTRAL_PULSELENGTH 3750 |
#define SERVOLIMIT 2000 |
#define SCALE_FACTOR 16 |
#define CS2 (<<CS21) |
#endif |
|
#define MAX_SERVOS 8 |
#define FRAMELEN ((NEUTRAL_PULSELENGTH + SERVOLIMIT) * staticParams.servoCount + 128) |
#define MIN_PULSELENGTH (NEUTRAL_PULSELENGTH - SERVOLIMIT) |
#define MAX_PULSELENGTH (NEUTRAL_PULSELENGTH + SERVOLIMIT) |
|
//volatile uint8_t servoActive = 0; |
volatile uint8_t recalculateServoTimes = 0; |
volatile uint16_t servoValues[MAX_SERVOS]; |
volatile uint16_t previousManualValues[2]; |
|
#define HEF4017R_ON PORTC |= (1<<PORTC6) |
#define HEF4017R_OFF PORTC &= ~(1<<PORTC6) |
|
65,8 → 85,6 |
/***************************************************** |
* Initialize Timer 2 |
*****************************************************/ |
// The timer 2 is used to generate the PWM at PD7 (J7) |
// to control a camera servo for pitch compensation. |
void timer2_init(void) { |
uint8_t sreg = SREG; |
|
78,220 → 96,155 |
PORTD &= ~(1 << PORTD7); // set PD7 to low |
|
DDRC |= (1 << DDC6); // set PC6 as output (Reset for HEF4017) |
//PORTC &= ~(1<<PORTC6); // set PC6 to low |
HEF4017R_ON; // enable reset |
|
// Timer/Counter 2 Control Register A |
// Timer Mode is CTC (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 0) |
// PD7: Output OCR2 match, (Bits: COM2A1 = 1, COM2A0 = 0) |
// PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
TCCR2A &= ~((1 << COM2A0) | (1 << COM2B1) | (1 << COM2B0) | (1 << WGM20) | (1 << WGM22)); |
TCCR2A |= (1 << COM2A1) | (1 << WGM21); |
|
// Timer/Counter 2 Control Register B |
|
// Set clock divider for timer 2 to 20MHz / 8 = 2.5 MHz |
// The timer increments from 0x00 to 0xFF with an update rate of 2.5 kHz or 0.4 us |
// hence the timer overflow interrupt frequency is 625 kHz / 256 = 9.765 kHz or 0.1024ms |
|
TCCR2B &= ~((1 << FOC2A) | (1 << FOC2B) | (1 << CS20) | (1 << CS21) | (1 << CS22)); |
TCCR2B |= CS2; |
|
// Initialize the Timer/Counter 2 Register |
TCNT2 = 0; |
|
// Initialize the Output Compare Register A used for signal generation on port PD7. |
OCR2A = 255; |
|
// Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
// PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
// PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
TCCR2A &= ~((1 << COM2A1) | (1 << COM2A0) | (1 << COM2B1) | (1 << COM2B0)); |
TCCR2A |= (1 << WGM21) | (1 << WGM20); |
|
// Timer/Counter 2 Control Register B |
|
// Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
// The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
// hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
|
// divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
TCCR2B &= ~((1 << FOC2A) | (1 << FOC2B) | (1 << CS22)); |
TCCR2B |= (1 << CS21) | (1 << CS20) | (1 << WGM22); |
|
// Initialize the Timer/Counter 2 Register |
TCNT2 = 0; |
|
// Initialize the Output Compare Register A used for PWM generation on port PD7. |
OCR2A = 255; |
TCCR2A |= (1 << COM2A1); // set or clear at compare match depends on value of COM2A0 |
|
// Timer/Counter 2 Interrupt Mask Register |
// Enable timer output compare match A Interrupt only |
TIMSK2 &= ~((1 << OCIE2B) | (1 << TOIE2)); |
TIMSK2 |= (1 << OCIE2A); |
|
for (uint8_t axis=0; axis<2; axis++) |
previousManualValues[axis] = dynamicParams.servoManualControl[axis] * SCALE_FACTOR; |
|
SREG = sreg; |
} |
|
void Servo_On(void) { |
ServoActive = 1; |
/* |
void servo_On(void) { |
servoActive = 1; |
} |
|
void Servo_Off(void) { |
ServoActive = 0; |
void servo_Off(void) { |
servoActive = 0; |
HEF4017R_ON; // enable reset |
} |
*/ |
|
/***************************************************** |
* Control Servo Position |
*****************************************************/ |
ISR(TIMER2_COMPA_vect) |
{ |
// frame len 22.5 ms = 14063 * 1.6 us |
// stop pulse: 0.3 ms = 188 * 1.6 us |
// min servo pulse: 0.6 ms = 375 * 1.6 us |
// max servo pulse: 2.4 ms = 1500 * 1.6 us |
// resolution: 1500 - 375 = 1125 steps |
|
#define PPM_STOPPULSE 188 |
#define PPM_FRAMELEN (1757 * .ServoRefresh) // 22.5 ms / 8 Channels = 2.8125ms per Servo Channel |
#define MINSERVOPULSE 375 |
#define MAXSERVOPULSE 1500 |
#define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
/*typedef struct { |
uint8_t manualControl; |
uint8_t compensationFactor; |
uint8_t minValue; |
uint8_t maxValue; |
uint8_t flags; |
} servo_t;*/ |
|
#if defined(USE_NON_4017_SERVO_OUTPUTS) || defined(USE_4017_SERVO_OUTPUTS) |
static uint8_t isGeneratingPulse = 0; |
static uint16_t remainingPulseLength = 0; |
static uint16_t ServoFrameTime = 0; |
static uint8_t ServoIndex = 0; |
int16_t calculateStabilizedServoAxis(uint8_t axis) { |
int32_t value = angle[axis] / 64L; // between -500000 to 500000 extreme limits. Just about |
// With full blast on stabilization gain (255) we want to convert a delta of, say, 125000 to 2000. |
// That is a divisor of about 1<<14. Same conclusion as H&I. |
value *= staticParams.servoConfigurations[axis].stabilizationFactor; |
value /= 256L; |
if (staticParams.servoConfigurations[axis].flags & SERVO_STABILIZATION_REVERSE) |
return -value; |
return value; |
} |
|
#define MULTIPLIER 4 |
static int16_t ServoPitchOffset = (255 / 2) * MULTIPLIER; // initial value near center position |
static int16_t ServoRollOffset = (255 / 2) * MULTIPLIER; // initial value near center position |
#endif |
#ifdef USE_NON_4017_SERVO_OUTPUTS |
//--------------------------- |
// Pitch servo state machine |
//--------------------------- |
if (!isGeneratingPulse) { // pulse output complete on _next_ interrupt |
if(TCCR2A & (1<<COM2A0)) { // we are still outputting a high pulse |
TCCR2A &= ~(1<<COM2A0); // make a low pulse on _next_ interrupt, and now |
remainingPulseLength = MINSERVOPULSE + SERVORANGE / 2; // center position ~ 1.5ms |
ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl) / 4; // lowpass offset |
if(staticParams.ServoPitchCompInvert & 0x01) { |
// inverting movement of servo |
// todo: function. |
ServoPitchValue = ServoPitchOffset + (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L)); |
} else { |
// todo: function. |
// non inverting movement of servo |
ServoPitchValue = ServoPitchOffset - (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L)); |
} |
// limit servo value to its parameter range definition |
if(ServoPitchValue < (int16_t)staticParams.ServoPitchMin) { |
ServoPitchValue = (int16_t)staticParams.ServoPitchMin; |
} else if(ServoPitchValue > (int16_t)staticParams.ServoPitchMax) { |
ServoPitchValue = (int16_t)staticParams.ServoPitchMax; |
} |
// With constant-speed limitation. |
uint16_t calculateManualServoAxis(uint8_t axis, uint16_t manualValue) { |
int16_t diff = manualValue - previousManualValues[axis]; |
uint8_t maxSpeed = staticParams.servoManualMaxSpeed; |
if (diff > maxSpeed) diff = maxSpeed; |
else if (diff < -maxSpeed) diff = -maxSpeed; |
manualValue = previousManualValues[axis] + diff; |
previousManualValues[axis] = manualValue; |
return manualValue; |
} |
|
remainingPulseLength = (ServoPitchValue - 256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position |
// add stabilization and manual, apply soft position limits. |
// All in a [0..255*SCALE_FACTOR] space (despite signed types used internally) |
int16_t featuredServoValue(uint8_t axis) { |
int16_t value = calculateManualServoAxis(axis, dynamicParams.servoManualControl[axis] * SCALE_FACTOR); |
value += calculateStabilizedServoAxis(axis); |
int16_t limit = staticParams.servoConfigurations[axis].minValue * SCALE_FACTOR; |
if (value < limit) value = limit; |
limit = staticParams.servoConfigurations[axis].maxValue * SCALE_FACTOR; |
if (value > limit) value = limit; |
return value; |
} |
|
// range servo pulse width |
if(remainingPulseLength > MAXSERVOPULSE ) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit |
else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit |
uint16_t servoValue(uint8_t axis) { |
int16_t value; |
if (axis<2) value = featuredServoValue(axis); |
else value = 128 * SCALE_FACTOR; // dummy. Replace by something useful for servos 3..8. |
// Shift out of the [0..255*SCALE_FACTOR] space |
value -= (128 * SCALE_FACTOR); |
if (value < -SERVOLIMIT) value = -SERVOLIMIT; |
else if (value > SERVOLIMIT) value = SERVOLIMIT; |
// Shift into the [NEUTRAL_PULSELENGTH-SERVOLIMIT..NEUTRAL_PULSELENGTH+SERVOLIMIT] space. |
return value + NEUTRAL_PULSELENGTH; |
} |
|
// accumulate time for correct update rate |
ServoFrameTime = remainingPulseLength; |
} else { // we had a high pulse |
TCCR2A |= (1<<COM2A0); // make a low pulse |
remainingPulseLength = PPM_FRAMELEN - ServoFrameTime; |
} |
// set pulse output active |
isGeneratingPulse = 1; |
} // EOF Pitch servo state machine |
void calculateServoValues(void) { |
if (!recalculateServoTimes) return; |
for (uint8_t axis=0; axis<MAX_SERVOS; axis++) { |
servoValues[axis] = servoValue(axis); |
} |
recalculateServoTimes = 0; |
} |
|
#elseif defined(USE_4017_SERVOS) |
//----------------------------------------------------- |
// PPM state machine, onboard demultiplexed by HEF4017 |
//----------------------------------------------------- |
if(!isGeneratingPulse) { // pulse output complete |
if(TCCR2A & (1<<COM2A0)) { // we had a low pulse |
TCCR2A &= ~(1<<COM2A0);// make a high pulse |
ISR(TIMER2_COMPA_vect) { |
static uint16_t remainingPulseTime; |
static uint8_t servoIndex = 0; |
static uint16_t sumOfPulseTimes = 0; |
|
if (!remainingPulseTime) { |
// Pulse is over, and the next pulse has already just started. Calculate length of next pulse. |
if (servoIndex < staticParams.servoCount) { |
// There are more signals to output. |
sumOfPulseTimes += (remainingPulseTime = servoValues[servoIndex]); |
servoIndex++; |
} else { |
// There are no more signals. Reset the counter and make this pulse cover the missing frame time. |
remainingPulseTime = FRAMELEN - sumOfPulseTimes; |
sumOfPulseTimes = servoIndex = 0; |
recalculateServoTimes = 1; |
HEF4017R_ON; |
} |
} |
|
if(ServoIndex == 0) { // if we are at the sync gap |
remainingPulseLength = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
ServoFrameTime = 0; // reset servo frame time |
HEF4017R_ON; // enable HEF4017 reset |
} else { // servo channels |
remainingPulseLength = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
switch(ServoIndex) { // map servo channels |
case 1: // Pitch Compensation Servo |
ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl * MULTIPLIER) / 4; // lowpass offset |
ServoPitchValue = ServoPitchOffset; // offset (Range from 0 to 255 * 3 = 765) |
if(staticParams.ServoPitchCompInvert & 0x01) { |
// inverting movement of servo |
ServoPitchValue += (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) ); |
} else { // non inverting movement of servo |
ServoPitchValue -= (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) ); |
} |
// limit servo value to its parameter range definition |
if(ServoPitchValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) { |
ServoPitchValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER; |
} else if(ServoPitchValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) { |
ServoPitchValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER; |
} |
remainingPulseLength += ServoPitchValue - (256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position |
ServoPitchValue /= MULTIPLIER; |
break; |
|
case 2: // Roll Compensation Servo |
ServoRollOffset = (ServoRollOffset * 3 + (int16_t)80 * MULTIPLIER) / 4; // lowpass offset |
ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765) |
//if(staticParams.ServoRollCompInvert & 0x01) |
{ // inverting movement of servo |
ServoRollValue += (int16_t)( ( (int32_t) 50 * MULTIPLIER * (integralGyroRoll / 128L ) ) / (256L) ); |
} |
/* else |
{ // non inverting movement of servo |
ServoRollValue -= (int16_t)( ( (int32_t) 40 * MULTIPLIER * (IntegralGyroRoll / 128L ) ) / (256L) ); |
} |
*/// limit servo value to its parameter range definition |
if(ServoRollValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) { |
ServoRollValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER; |
} else if(ServoRollValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) { |
ServoRollValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER; |
} |
remainingPulseLength += ServoRollValue - (256 / 2) * MULTIPLIER; // shift ServoRollValue to center position |
ServoRollValue /= MULTIPLIER; |
break; |
|
default: // other servo channels |
remainingPulseLength += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
break; |
} |
// range servo pulse width |
if(remainingPulseLength > MAXSERVOPULSE) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit |
else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit |
// substract stop pulse width |
remainingPulseLength -= PPM_STOPPULSE; |
// accumulate time for correct sync gap |
ServoFrameTime += remainingPulseLength; |
} |
} else { // we had a high pulse |
TCCR2A |= (1<<COM2A0); // make a low pulse |
// set pulsewidth to stop pulse width |
remainingPulseLength = PPM_STOPPULSE; |
// accumulate time for correct sync gap |
ServoFrameTime += remainingPulseLength; |
if(ServoActive && RC_Quality > 180) HEF4017R_OFF; // disable HEF4017 reset |
ServoIndex++; // change to next servo channel |
if(ServoIndex > staticParams.ServoRefresh) ServoIndex = 0; // reset to the sync gap |
} |
// set pulse output active |
isGeneratingPulse = 1; |
} |
#endif |
|
/* |
* Cases: |
* 1) 255 + 128 <= remainingPulseLength --> delta = 255 |
* 2) 255 <= remainingPulseLength < 255 + 128 --> delta = 255 - 128 |
* this is to avoid a too short delta on the last cycle, which would cause |
* an interupt-on-interrupt condition and the loss of the last interrupt. |
* 3) remainingPulseLength < 255 --> delta = remainingPulseLength |
*/ |
#if defined(USE_NON_4017_SERVO_OUTPUTS) || defined(USE_4017_SERVO_OUTPUTS) |
uint8_t delta; |
if (remainingPulseLength >= (255 + 128)) { |
delta = 255; |
} else if (remainingPulseLength >= 255) { |
delta = 255- 128; |
} else { |
delta = remainingPulseLength; |
isGeneratingPulse = 0; // trigger to stop pulse |
} |
OCR2A = delta; |
remainingPulseLength -= delta; |
#endif |
// Schedule the next OCR2A event. The counter is already reset at this time. |
if (remainingPulseTime > 256+128) { |
// Set output to reset to zero at next OCR match. It does not really matter when the output is set low again, |
// as long as it happens once per pulse. This will, because all pulses are > 255+128 long. |
OCR2A = 255; |
TCCR2A &= ~(1<<COM2A0); |
remainingPulseTime-=256; |
} else if (remainingPulseTime > 256) { |
// Remaining pulse lengths in the range [256..256+128] might cause trouble if handled the standard |
// way, which is in chunks of 256. The remainder would be very small, possibly causing an interrupt on interrupt |
// condition. Instead we now make a chunk of 128. The remaining chunk will then be in [128..255] which is OK. |
remainingPulseTime-=128; |
OCR2A=127; |
} else { |
// Set output to high at next OCR match. This is when the 4017 counter will advance by one. Also set reset low |
TCCR2A |= (1<<COM2A0); |
OCR2A = remainingPulseTime-1; |
remainingPulseTime=0; |
HEF4017R_OFF; // implement servo-disable here, by only removing the reset signal if ServoEnabled!=0. |
} |
} |