Subversion Repositories FlightCtrl

Rev

Rev 2125 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2125 Rev 2135
1
#include <avr/io.h>
1
#include <avr/io.h>
2
#include <avr/interrupt.h>
2
#include <avr/interrupt.h>
3
#include "eeprom.h"
3
#include "eeprom.h"
4
#include "output.h"
4
#include "output.h"
5
#include "flight.h"
5
#include "flight.h"
6
#include "attitude.h"
6
#include "attitude.h"
7
#include "timer2.h"
7
#include "timer2.h"
8
 
8
 
9
// #define COARSERESOLUTION 1
9
// #define COARSERESOLUTION 1
10
 
10
 
11
#ifdef COARSERESOLUTION
11
#ifdef COARSERESOLUTION
12
#define NEUTRAL_PULSELENGTH 938
12
#define NEUTRAL_PULSELENGTH ((int16_t)(F_CPU/32000*1.5f + 0.5f))
13
#define STABILIZATION_LOG_DIVIDER 6
13
#define STABILIZATION_LOG_DIVIDER 6
14
#define SERVOLIMIT 500
14
#define SERVOLIMIT ((int16_t)(F_CPU/32000*0.8f + 0.5f))
15
#define SCALE_FACTOR 4
15
#define SCALE_FACTOR 4
16
#define CS2 ((1<<CS21)|(1<<CS20))
16
#define CS2 ((1<<CS21)|(1<<CS20))
17
 
17
 
18
#else
18
#else
19
#define NEUTRAL_PULSELENGTH 3750
19
#define NEUTRAL_PULSELENGTH ((int16_t)(F_CPU/8000.0f * 1.5f + 0.5f))
20
#define STABILIZATION_LOG_DIVIDER 4
20
#define STABILIZATION_LOG_DIVIDER 4
21
#define SERVOLIMIT 2000
21
#define SERVOLIMIT ((int16_t)(F_CPU/8000.0f * 0.8f + 0.5f))
22
#define SCALE_FACTOR 16
22
#define SCALE_FACTOR 16
23
#define CS2 (1<<CS21)
23
#define CS2 (1<<CS21)
24
#endif
24
#endif
25
 
25
 
26
#define FRAMELEN ((NEUTRAL_PULSELENGTH + SERVOLIMIT) * staticParams.servoCount + 128)
26
#define FRAMELENGTH ((uint16_t)(NEUTRAL_PULSELENGTH + SERVOLIMIT) * (uint16_t)staticParams.servoCount + 128)
27
#define MIN_PULSELENGTH (NEUTRAL_PULSELENGTH - SERVOLIMIT)
27
#define MIN_PULSELENGTH (NEUTRAL_PULSELENGTH - SERVOLIMIT)
28
#define MAX_PULSELENGTH (NEUTRAL_PULSELENGTH + SERVOLIMIT)
28
#define MAX_PULSELENGTH (NEUTRAL_PULSELENGTH + SERVOLIMIT)
29
 
29
 
30
volatile uint8_t recalculateServoTimes = 0;
30
volatile uint8_t recalculateServoTimes = 0;
31
volatile uint16_t servoValues[MAX_SERVOS];
31
volatile uint16_t servoValues[MAX_SERVOS];
32
volatile uint16_t previousManualValues[2];
32
volatile uint16_t previousManualValues[2];
33
 
33
 
34
#define HEF4017R_ON     PORTC |=  (1<<PORTC6)
34
#define HEF4017R_ON     PORTC |=  (1<<PORTC6)
35
#define HEF4017R_OFF    PORTC &= ~(1<<PORTC6)
35
#define HEF4017R_OFF    PORTC &= ~(1<<PORTC6)
36
 
-
 
37
//#define HEF4017R_ON ;
-
 
38
//#define HEF4017R_OFF ;
-
 
39
 
36
 
40
/*****************************************************
37
/*****************************************************
41
 *              Initialize Timer 2
38
 *              Initialize Timer 2
42
 *****************************************************/
39
 *****************************************************/
43
void timer2_init(void) {
40
void timer2_init(void) {
44
    uint8_t sreg = SREG;
41
    uint8_t sreg = SREG;
45
 
42
 
46
    // disable all interrupts before reconfiguration
43
    // disable all interrupts before reconfiguration
47
    cli();
44
    cli();
48
 
45
 
49
    // set PD7 as output of the PWM for pitch servo
46
    // set PD7 as output of the PWM for pitch servo
50
    DDRD |= (1 << DDD7);
47
    DDRD |= (1 << DDD7);
51
    PORTD &= ~(1 << PORTD7); // set PD7 to low
48
    PORTD &= ~(1 << PORTD7); // set PD7 to low
52
 
49
 
53
    DDRC |= (1 << DDC6); // set PC6 as output (Reset for HEF4017)
50
    DDRC |= (1 << DDC6); // set PC6 as output (Reset for HEF4017)
54
    HEF4017R_ON; // reset
51
    HEF4017R_ON; // reset
55
 
52
 
56
    // Timer/Counter 2 Control Register A
53
    // Timer/Counter 2 Control Register A
57
    // Timer Mode is CTC (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 0)
54
    // Timer Mode is CTC (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 0)
58
    // PD7: Output OCR2 match, (Bits: COM2A1 = 1, COM2A0 = 0)
55
    // PD7: Output OCR2 match, (Bits: COM2A1 = 1, COM2A0 = 0)
59
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
56
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
60
    TCCR2A &= ~((1 << COM2A0) | (1 << COM2B1) | (1 << COM2B0) | (1 << WGM20) | (1 << WGM22));
57
    TCCR2A &= ~((1 << COM2A0) | (1 << COM2B1) | (1 << COM2B0) | (1 << WGM20) | (1 << WGM22));
61
    TCCR2A |= (1 << COM2A1) | (1 << WGM21);
58
    TCCR2A |= (1 << COM2A1) | (1 << WGM21);
62
 
59
 
63
    // Timer/Counter 2 Control Register B
60
    // Timer/Counter 2 Control Register B
64
 
61
 
65
    // Set clock divider for timer 2 to 20MHz / 8 = 2.5 MHz
62
    // Set clock divider for timer 2 to 20MHz / 8 = 2.5 MHz
66
    // The timer increments from 0x00 to 0xFF with an update rate of 2.5 kHz or 0.4 us
63
    // The timer increments from 0x00 to 0xFF with an update rate of 2.5 kHz or 0.4 us
67
    // hence the timer overflow interrupt frequency is 625 kHz / 256 = 9.765 kHz or 0.1024ms
64
    // hence the timer overflow interrupt frequency is 625 kHz / 256 = 9.765 kHz or 0.1024ms
68
 
65
 
69
    TCCR2B &= ~((1 << FOC2A) | (1 << FOC2B) | (1 << CS20) | (1 << CS21) | (1 << CS22));
66
    TCCR2B &= ~((1 << FOC2A) | (1 << FOC2B) | (1 << CS20) | (1 << CS21) | (1 << CS22));
70
    TCCR2B |= CS2;
67
    TCCR2B |= CS2;
71
 
68
 
72
    // Initialize the Timer/Counter 2 Register
69
    // Initialize the Timer/Counter 2 Register
73
    TCNT2 = 0;
70
    TCNT2 = 0;
74
 
71
 
75
    // Initialize the Output Compare Register A used for signal generation on port PD7.
72
    // Initialize the Output Compare Register A used for signal generation on port PD7.
76
    OCR2A = 255;
73
    OCR2A = 255;
77
 
74
 
78
    // Timer/Counter 2 Interrupt Mask Register
75
    // Timer/Counter 2 Interrupt Mask Register
79
    // Enable timer output compare match A Interrupt only
76
    // Enable timer output compare match A Interrupt only
80
    TIMSK2 &= ~((1 << OCIE2B) | (1 << TOIE2));
77
    TIMSK2 &= ~((1 << OCIE2B) | (1 << TOIE2));
81
    TIMSK2 |= (1 << OCIE2A);
78
    TIMSK2 |= (1 << OCIE2A);
82
 
79
 
83
    for (uint8_t axis=0; axis<2; axis++)
80
    for (uint8_t axis=0; axis<2; axis++)
84
      previousManualValues[axis] = dynamicParams.gimbalServoManualControl[axis] * SCALE_FACTOR;
81
      previousManualValues[axis] = dynamicParams.gimbalServoManualControl[axis] * SCALE_FACTOR;
85
 
82
 
86
    SREG = sreg;
83
    SREG = sreg;
87
}
84
}
88
 
85
 
89
/*****************************************************
86
/*****************************************************
90
 * Control (camera gimbal etc.) servos
87
 * Control (camera gimbal etc.) servos
91
 *****************************************************/
88
 *****************************************************/
92
int16_t calculateStabilizedServoAxis(uint8_t axis) {
89
int16_t calculateStabilizedServoAxis(uint8_t axis) {
93
  int32_t value = attitude[axis] >> STABILIZATION_LOG_DIVIDER; // between -500000 to 500000 extreme limits. Just about
90
  int32_t value = attitude[axis] >> STABILIZATION_LOG_DIVIDER; // between -500000 to 500000 extreme limits. Just about
94
  // With full blast on stabilization gain (255) we want to convert a delta of, say, 125000 to 2000.
91
  // With full blast on stabilization gain (255) we want to convert a delta of, say, 125000 to 2000.
95
  // That is a divisor of about 1<<14. Same conclusion as H&I.
92
  // That is a divisor of about 1<<14. Same conclusion as H&I.
96
  value *= staticParams.gimbalServoConfigurations[axis].stabilizationFactor;
93
  value *= staticParams.gimbalServoConfigurations[axis].stabilizationFactor;
97
  value = value >> 8;
94
  value = value >> 8;
98
  if (staticParams.gimbalServoConfigurations[axis].flags & SERVO_STABILIZATION_REVERSE)
95
  if (staticParams.gimbalServoConfigurations[axis].flags & SERVO_STABILIZATION_REVERSE)
99
    return -value;
96
    return -value;
100
  return value;
97
  return value;
101
}
98
}
102
 
99
 
103
// With constant-speed limitation.
100
// With constant-speed limitation.
104
uint16_t calculateManualServoAxis(uint8_t axis, uint16_t manualValue) {
101
uint16_t calculateManualServoAxis(uint8_t axis, uint16_t manualValue) {
105
  int16_t diff = manualValue - previousManualValues[axis];
102
  int16_t diff = manualValue - previousManualValues[axis];
106
  uint8_t maxSpeed = staticParams.gimbalServoMaxManualSpeed;
103
  uint8_t maxSpeed = staticParams.gimbalServoMaxManualSpeed;
107
  if (diff > maxSpeed) diff = maxSpeed;
104
  if (diff > maxSpeed) diff = maxSpeed;
108
  else if (diff < -maxSpeed) diff = -maxSpeed;
105
  else if (diff < -maxSpeed) diff = -maxSpeed;
109
  manualValue = previousManualValues[axis] + diff;
106
  manualValue = previousManualValues[axis] + diff;
110
  previousManualValues[axis] = manualValue;
107
  previousManualValues[axis] = manualValue;
111
  return manualValue;
108
  return manualValue;
112
}
109
}
113
 
110
 
114
// add stabilization and manual, apply soft position limits.
111
// add stabilization and manual, apply soft position limits.
115
// All in a [0..255*SCALE_FACTOR] space (despite signed types used internally)
112
// All in a [0..255*SCALE_FACTOR] space (despite signed types used internally)
116
int16_t featuredServoValue(uint8_t axis) {
113
int16_t featuredServoValue(uint8_t axis) {
117
  int16_t value = calculateManualServoAxis(axis, dynamicParams.gimbalServoManualControl[axis] * SCALE_FACTOR);
114
  int16_t value = calculateManualServoAxis(axis, dynamicParams.gimbalServoManualControl[axis] * SCALE_FACTOR);
118
  value += calculateStabilizedServoAxis(axis);
115
  value += calculateStabilizedServoAxis(axis);
119
  int16_t limit = staticParams.gimbalServoConfigurations[axis].minValue * SCALE_FACTOR;
116
  int16_t limit = staticParams.gimbalServoConfigurations[axis].minValue * SCALE_FACTOR;
120
  if (value < limit) value = limit;
117
  if (value < limit) value = limit;
121
  limit = staticParams.gimbalServoConfigurations[axis].maxValue * SCALE_FACTOR;
118
  limit = staticParams.gimbalServoConfigurations[axis].maxValue * SCALE_FACTOR;
122
  if (value > limit) value = limit;
119
  if (value > limit) value = limit;
123
  value -= (128 * SCALE_FACTOR);
120
  value -= (128 * SCALE_FACTOR);
124
  if (value < -SERVOLIMIT) value = -SERVOLIMIT;
121
  if (value < -SERVOLIMIT) value = -SERVOLIMIT;
125
  else if (value > SERVOLIMIT) value = SERVOLIMIT;
122
  else if (value > SERVOLIMIT) value = SERVOLIMIT;
126
  // Shift into the [NEUTRAL_PULSELENGTH-SERVOLIMIT..NEUTRAL_PULSELENGTH+SERVOLIMIT] space.
123
  // Shift into the [NEUTRAL_PULSELENGTH-SERVOLIMIT..NEUTRAL_PULSELENGTH+SERVOLIMIT] space.
127
  return value + NEUTRAL_PULSELENGTH;
124
  return value + NEUTRAL_PULSELENGTH;
128
}
125
}
129
 
126
 
130
void calculateControlServoValues(void) {
127
void calculateControlServoValues(void) {
131
  int16_t value;
128
  int16_t value;
132
  //int16_t minLimit = staticParams.controlServoMinValue * SCALE_FACTOR;
129
  //int16_t minLimit = staticParams.controlServoMinValue * SCALE_FACTOR;
133
  //int16_t maxLimit = staticParams.controlServoMaxValue * SCALE_FACTOR;
130
  //int16_t maxLimit = staticParams.controlServoMaxValue * SCALE_FACTOR;
134
  for (uint8_t axis=0; axis<4; axis++) {
131
  for (uint8_t axis=0; axis<4; axis++) {
135
        value = controlServos[axis];
132
        value = controlServos[axis];
136
        if (value < -SERVOLIMIT) value = -SERVOLIMIT;
133
        if (value < -SERVOLIMIT) value = -SERVOLIMIT;
137
    else if (value > SERVOLIMIT) value = SERVOLIMIT;
134
    else if (value > SERVOLIMIT) value = SERVOLIMIT;
138
        servoValues[axis] = value + NEUTRAL_PULSELENGTH;
135
        servoValues[axis] = value + NEUTRAL_PULSELENGTH;
139
  }
136
  }
140
}
137
}
141
 
138
 
142
void calculateFeaturedServoValues(void) {
139
void calculateFeaturedServoValues(void) {
143
  int16_t value;
140
  int16_t value;
144
  uint8_t axis;
141
  uint8_t axis;
145
 
142
 
146
  // Save the computation cost of computing a new value before the old one is used.
143
  // Save the computation cost of computing a new value before the old one is used.
147
  if (!recalculateServoTimes) return;
144
  if (!recalculateServoTimes) return;
148
 
145
 
149
  for (axis= MAX_CONTROL_SERVOS; axis<MAX_CONTROL_SERVOS+2; axis++) {
146
  for (axis= MAX_CONTROL_SERVOS; axis<MAX_CONTROL_SERVOS+2; axis++) {
150
        value = featuredServoValue(axis-MAX_CONTROL_SERVOS);
147
        value = featuredServoValue(axis-MAX_CONTROL_SERVOS);
151
        servoValues[axis] = value;
148
        servoValues[axis] = value;
152
  }
149
  }
153
  for (axis=MAX_CONTROL_SERVOS+2; axis<MAX_SERVOS; axis++) {
150
  for (axis=MAX_CONTROL_SERVOS+2; axis<MAX_SERVOS; axis++) {
154
        value = 128 * SCALE_FACTOR;
151
        value = 128 * SCALE_FACTOR;
155
        servoValues[axis] = value;
152
        servoValues[axis] = value;
156
  }
153
  }
157
 
154
 
158
  recalculateServoTimes = 0;
155
  recalculateServoTimes = 0;
159
}
156
}
160
 
157
 
161
ISR(TIMER2_COMPA_vect) {
158
ISR(TIMER2_COMPA_vect) {
162
  static uint16_t remainingPulseTime;
159
  static uint16_t remainingPulseTime;
163
  static uint8_t servoIndex = 0;
160
  static uint8_t servoIndex = 0;
164
  static uint16_t sumOfPulseTimes = 0;
161
  static uint16_t sumOfPulseTimes = 0;
165
 
162
 
166
  if (!remainingPulseTime) {
163
  if (!remainingPulseTime) {
167
    // Pulse is over, and the next pulse has already just started. Calculate length of next pulse.
164
    // Pulse is over, and the next pulse has already just started. Calculate length of next pulse.
168
    if (servoIndex < staticParams.servoCount) {
165
    if (servoIndex < staticParams.servoCount) {
169
      // There are more signals to output.
166
      // There are more signals to output.
170
      sumOfPulseTimes += (remainingPulseTime = servoValues[servoIndex]);
167
      sumOfPulseTimes += (remainingPulseTime = servoValues[servoIndex]);
171
      servoIndex++;
168
      servoIndex++;
172
    } else {
169
    } else {
173
      // There are no more signals. Reset the counter and make this pulse cover the missing frame time.
170
      // There are no more signals. Reset the counter and make this pulse cover the missing frame time.
174
      remainingPulseTime = FRAMELEN - sumOfPulseTimes;
171
      remainingPulseTime = FRAMELENGTH - sumOfPulseTimes;
175
      sumOfPulseTimes = servoIndex = 0;
172
      sumOfPulseTimes = servoIndex = 0;
176
      recalculateServoTimes = 1;
173
      recalculateServoTimes = 1;
177
      HEF4017R_ON;
174
      HEF4017R_ON;
178
    }
175
    }
179
  }
176
  }
180
 
177
 
181
  // Schedule the next OCR2A event. The counter is already reset at this time.
178
  // Schedule the next OCR2A event. The counter is already reset at this time.
182
  if (remainingPulseTime > 256+128) {
179
  if (remainingPulseTime > 256+128) {
183
    // Set output to reset to zero at next OCR match. It does not really matter when the output is set low again,
180
    // Set output to reset to zero at next OCR match. It does not really matter when the output is set low again,
184
    // as long as it happens once per pulse. This will, because all pulses are > 255+128 long.
181
    // as long as it happens once per pulse. This will, because all pulses are > 255+128 long.
185
    OCR2A = 255;
182
    OCR2A = 255;
186
    TCCR2A &= ~(1<<COM2A0);
183
    TCCR2A &= ~(1<<COM2A0);
187
    remainingPulseTime-=256;
184
    remainingPulseTime-=256;
188
  } else if (remainingPulseTime > 256) {
185
  } else if (remainingPulseTime > 256) {
189
    // Remaining pulse lengths in the range [256..256+128] might cause trouble if handled the standard
186
    // Remaining pulse lengths in the range [256..256+128] might cause trouble if handled the standard
190
    // way, which is in chunks of 256. The remainder would be very small, possibly causing an interrupt on interrupt
187
    // way, which is in chunks of 256. The remainder would be very small, possibly causing an interrupt on interrupt
191
    // condition. Instead we now make a chunk of 128. The remaining chunk will then be in [128..255] which is OK.
188
    // condition. Instead we now make a chunk of 128. The remaining chunk will then be in [128..255] which is OK.
192
    remainingPulseTime-=128;
189
    remainingPulseTime-=128;
193
    OCR2A=127;
190
    OCR2A=127;
194
  } else {
191
  } else {
195
    // Set output to high at next OCR match. This is when the 4017 counter will advance by one. Also set reset low
192
    // Set output to high at next OCR match. This is when the 4017 counter will advance by one. Also set reset low
196
    TCCR2A |= (1<<COM2A0);
193
    TCCR2A |= (1<<COM2A0);
197
    OCR2A = remainingPulseTime-1;
194
    OCR2A = remainingPulseTime-1;
198
    remainingPulseTime=0;
195
    remainingPulseTime=0;
199
    HEF4017R_OFF; // implement servo-disable here, by only removing the reset signal if ServoEnabled!=0.
196
    HEF4017R_OFF; // implement servo-disable here, by only removing the reset signal if ServoEnabled!=0.
200
  }
197
  }
201
}
198
}
202
 
199