1,36 → 1,18 |
#include <avr/io.h> |
#include <avr/interrupt.h> |
#include "eeprom.h" |
#include "rc.h" |
#include "attitude.h" |
#include "timer2.h" |
|
#define COARSERESOLUTION 1 |
|
#ifdef COARSERESOLUTION |
#define NEUTRAL_PULSELENGTH 938 |
#define STABILIZATION_LOG_DIVIDER 6 |
#define SERVOLIMIT 500 |
#define SCALE_FACTOR 4 |
#if (SERVO_RESOLUTION == COARSE) |
#define CS2 ((1<<CS21)|(1<<CS20)) |
|
#else |
|
#define NEUTRAL_PULSELENGTH 3750 |
#define STABILIZATION_LOG_DIVIDER 4 |
#define SERVOLIMIT 2000 |
#define SCALE_FACTOR 16 |
#define CS2 (1<<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) |
#define FRAMELEN (PULSELENGTH_2200 * staticParams.servoCount + 128) |
|
//volatile uint8_t servoActive = 0; |
volatile uint8_t recalculateServoTimes = 0; |
volatile uint16_t servoValues[MAX_SERVOS]; |
volatile uint16_t previousManualValues[2]; |
// volatile uint8_t servoActive = 0; |
// volatile uint8_t recalculateServoTimes = 0; |
volatile uint16_t pwmChannels[MAX_PWMCHANNELS]; |
|
#define HEF4017R_ON PORTC |= (1<<PORTC6) |
#define HEF4017R_OFF PORTC &= ~(1<<PORTC6) |
78,9 → 60,6 |
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; |
} |
|
97,52 → 76,8 |
/***************************************************** |
* Control Servo Position |
*****************************************************/ |
int16_t calculateStabilizedServoAxis(uint8_t axis) { |
int32_t value = attitude[axis] >> STABILIZATION_LOG_DIVIDER; // 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 = value >> 8; |
if (staticParams.servoConfigurations[axis].flags & SERVO_STABILIZATION_REVERSE) |
return -value; |
return value; |
} |
|
// 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; |
} |
|
// 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; |
} |
|
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; |
} |
|
/* |
void calculateServoValues(void) { |
if (!recalculateServoTimes) return; |
for (uint8_t axis=0; axis<MAX_SERVOS; axis++) { |
150,9 → 85,8 |
} |
recalculateServoTimes = 0; |
} |
*/ |
|
uint8_t servoMap[] = {2,3,0,1,4,5,6,7}; |
|
ISR(TIMER2_COMPA_vect) { |
static uint16_t remainingPulseTime; |
static uint8_t servoIndex = 0; |
162,17 → 96,16 |
// 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[servoMap[servoIndex]]); |
sumOfPulseTimes += (remainingPulseTime = pwmChannels[servoIndex]); //pwmChannels[servoMap[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; |
} |
} |
|
|
// 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, |
179,18 → 112,18 |
// as long as it happens once per pulse. This will, because all pulses are > 255+128 long. |
OCR2A = 255; |
TCCR2A &= ~(1<<COM2A0); |
remainingPulseTime-=256; |
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; |
remainingPulseTime -= 128; |
} 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; |
remainingPulseTime = 0; |
HEF4017R_OFF; // implement servo-disable here, by only removing the reset signal if ServoEnabled!=0. |
} |
} |