-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracking.c
More file actions
261 lines (208 loc) · 6.67 KB
/
tracking.c
File metadata and controls
261 lines (208 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include "generator.h"
#include "sleep.h"
#include "timer.h"
// Output frequencies can be multiplied with this factor if necessary
#define SPEED_FACTOR 1.0
#define PWM_SIZE (256)
#define PWM_FREQ (F_OSC / PWM_SIZE)
#define TIMER_MSEC(msec) (((uint32_t)((msec)*PWM_FREQ)) / UINT32_C(1000))
// 128*31.25ms == 4 seconds
#define STARTUP_TIMER_INTERVAL TIMER_MSEC(31.25)
#define MAX_STARTUP_TIMER 127
// Sine templates for different amplitudes.
SIN_U8_DATA(sine_normal, 0.8);
SIN_U8_DATA(sine_slow, 0.6);
SIN_U8_DATA(sine_fast, 1.0);
static const uint32_t __flash phase_inc[] = {
/* slow */ COMPUTE_PHASE_INCREMENT(SPEED_FACTOR * 37.5000, PWM_SIZE),
/* sidereal */ COMPUTE_PHASE_INCREMENT(SPEED_FACTOR * 50.1369, PWM_SIZE),
/* lunar */ COMPUTE_PHASE_INCREMENT(SPEED_FACTOR * 48.3018, PWM_SIZE),
/* solar */ COMPUTE_PHASE_INCREMENT(SPEED_FACTOR * 50.0000, PWM_SIZE),
/* fast */ COMPUTE_PHASE_INCREMENT(SPEED_FACTOR * 62.5000, PWM_SIZE),
};
static const uint8_t __flash* const __flash sine_data[] = {
/* slow */ sine_slow,
/* sidereal */ sine_normal,
/* lunar */ sine_normal,
/* solar */ sine_normal,
/* fast */ sine_fast,
};
// Generator state
static struct generator_state s_gen;
// Baseline PWM value; ramps up from 0 to 127 during startup
static volatile uint8_t s_baseline;
// Boolean indicating if generator output is enabled
static volatile uint8_t s_output_enabled;
// Debounce timer for rotary switch
static volatile uint16_t s_new_index_timer;
// Timer used for boosting amplitude when enabling output
static volatile uint16_t s_boost_timer;
// Boolean indicating when amplitude boost has finished
static volatile uint8_t s_boost_finished;
// Start-up timer for ramping up baseline value
static volatile uint16_t s_startup_timer1;
static volatile uint8_t s_startup_timer2;
//
// Timer interrupt, triggered every 256 cycles
//
// Worst case run time:
// - startup: 127 cycles
// - running: 172 cycles (142 if GENERATOR_EXTRA_ACCURACY_BITS == 0)
//
ISR(TIMER_OVERFLOW_VECTOR) {
// context switch: 36 cycles
// Generate next output sample
// 6 cycles if output disabled
// approx (28 + 5*GENERATOR_EXTRA_ACCURACY_BITS) cycles if output enabled
PWM_OUTPUT_REG = s_output_enabled ? generator_generate(&s_gen) : s_baseline;
// Reset watchdog (1 cycle)
wdt_reset();
// Now handle periodic stuff, like updating timers
// 8/13 cycles (will use 8 cycles during startup)
if (s_new_index_timer > 0) {
s_new_index_timer--;
}
// 8/16/18 cycles (will use 8 cycles during startup)
if (s_boost_timer > 0) {
if (--s_boost_timer == 0) {
s_boost_finished = 1;
}
}
// 7/15/22/29 cycles (will use 7 cycles after startup)
if (s_startup_timer1 > 0) {
if (--s_startup_timer1 == 0) {
if (++s_startup_timer2 < MAX_STARTUP_TIMER) {
s_startup_timer1 = STARTUP_TIMER_INTERVAL;
}
}
}
// context switch: 39 cycles
}
static void idle_loop(void) {
// Initialize so that we force a pin change. This ensures we set
// an initial phase increment and enable interrupts.
uint8_t last_pin = 255;
// The active index chosen by the external switch
uint8_t active_index = 0;
// The new candidate index
uint8_t new_index = active_index;
for (;;) {
/*
* ~PINA Switch
* 7654 Pos Index Speed
* -----------------------------------------
* 0000 0 0 off
* 0100 1 1 sidereal
* 1100 2 3 solar
* 1000 3 2 lunar
* xx10 - - fast (xx != 00)
* xx01 - - slow (xx != 00)
*/
uint8_t pin = (~PINA) & 0xF0;
if (pin != last_pin || (new_index != active_index && s_new_index_timer == 0)
|| s_boost_finished) {
s_boost_finished = 0;
uint8_t index = pin >> 6;
if (index != new_index) {
/*
* External switch position has changed. When turning the knob,
* it'll briefly open all switches before it locks into the next
* position. In order to avoid major output signal glitches,
* we'll wait for 100ms for the switch position to settle.
*/
new_index = index;
cli();
s_new_index_timer = TIMER_MSEC(100);
sei();
}
if (new_index != active_index && s_new_index_timer == 0) {
if (active_index == 0) {
cli();
s_boost_timer = TIMER_MSEC(1000);
sei();
}
active_index = new_index;
}
// Handle ST4 inputs
switch (pin & 0x30) {
case 0x20: // fast
PORTA |= (1 << PA3);
index = 4;
break;
case 0x10: // slow
PORTA |= (1 << PA1);
index = 0;
break;
default:
// clear ST4 indicator LEDs
PORTA &= ~((1 << PA3) | (1 << PA1));
index = active_index;
break;
}
if (active_index > 0) {
// Output is enabled
PORTA |= (1 << PA2);
// Disable interrupts here so we don't race with ISR
cli();
s_output_enabled = 1;
generator_set_phase_increment(&s_gen, phase_inc[index]);
generator_set_sine(
&s_gen, s_boost_timer > 0 ? sine_fast : sine_data[index]);
sei();
} else {
// Output is disabled
PORTA &= ~(1 << PA2);
// Disable interrupts here so we don't race with ISR
cli();
s_output_enabled = 0;
generator_init(&s_gen);
sei();
}
last_pin = pin;
}
}
}
int main(void) {
STATIC_ASSERT(PWM_FREQ <= 0xFFFF);
// Set status LED outputs
DDRA = (1 << DDA3) | (1 << DDA2) | (1 << DDA1) | (1 << DDA0);
// Set PB2 as output for PWM
DDRB = (1 << DDB2);
// Toggle through LEDs once to make sure everything's working
for (uint8_t i = 0; i < 4; ++i) {
PORTA |= 1 << (i & 0x3);
millisleep(250);
PORTA &= 0xF0;
}
// Initialize generator
generator_init(&s_gen);
// Enable pullups on PA4/PA5 (ST4) and PA6/PA7 (Mode Switch)
PORTA |= (1 << PA7) | (1 << PA6) | (1 << PA5) | (1 << PA4);
// Set up timer for fast PWM
timer_pwm_init();
s_startup_timer1 = STARTUP_TIMER_INTERVAL;
// Enable interrupts
sei();
// Toggle through LEDs while we ramp up the baseline
for (;;) {
uint8_t bl = s_startup_timer2;
if (bl != s_baseline) {
s_baseline = bl;
PORTA &= 0xF0;
if (bl >= MAX_STARTUP_TIMER) {
break;
}
bl >>= 3;
PORTA |= 1 << (bl & 0x3);
}
}
// Enable watchdog timer with 16ms timeout
WDTCSR = (1 << WDE);
PORTA |= (1 << PA0); // Power LED on
// Run idle loop
idle_loop();
return 0;
}