-
Notifications
You must be signed in to change notification settings - Fork 0
/
antenna-tuner.ino
485 lines (407 loc) · 16.2 KB
/
antenna-tuner.ino
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Magnetic Loop Antenna Tuner
*
* This source file is part of the Magnetic Loop Antenna Tuner Arduino firmware
* found under http://www.github.com/microfarad-de/antenna-tuner
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 1.0.3
* Date: March 2020
*/
#define VERSION_MAJOR 1 // major version
#define VERSION_MINOR 0 // minor version
#define VERSION_MAINT 3 // maintenance version
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <Servo.h>
#include "src/Adc/Adc.h"
#include "src/Nvm/Nvm.h"
#include "src/Led/Led.h"
#include "src/Button/Button.h"
#include "src/MathMf/MathMf.h"
//#define SERIAL_DEBUG // activate debug printing over RS232
#define SERIAL_BAUD 115200 // serial baud rate
// use these macros for printing to serial port
#ifdef SERIAL_DEBUG
#define PRINT(...) Serial.print (__VA_ARGS__)
#define PRINTLN(...) Serial.println (__VA_ARGS__)
#else
#define PRINT(...)
#define PRINTLN(...)
#endif
#define LED_PIN 13 // output LED pin number (digital pin)
#define PPM_PIN 9 // output pin number for the PPM output (digital pin with PWM support, pin 9 or 10 on ATmega328)
#define MOSFET_PIN 7 // output pin for controlling the power MOSFET
#define BUTTON_PWR_PIN 10 // input pin for "power" button
#define BUTTON_INC_PIN 12 // input pin for "increase" button
#define BUTTON_DEC_PIN 11 // input pin for "decrease" button
#define POT_APIN A0 // analog pin connected to the potentiometer
#define SERVO_MIN 500 // minimum servo PPM pulse width (us)
#define SERVO_MAX 2500 // maximum servo PPM pulse width (us)
#define SERVO_SB_STARTUP 100 // setback the servo by this amount when starting-up (us)
#define SERVO_FINE_RANGE 100 // range for servo fine adjustment (us)
#define SERVO_SETBACK_MIN 0 // minimum value for servo setback when changing direction (us)
#define SERVO_SETBACK_MAX 200// maximum value for servo setback when changing direction (us)
#define SERVO_SB_DELAY 60 // delay duration after servo setback (ms)
#define SERVO_COMP_MIN 0 // minimum compensaton value when changing direction (us)
#define SERVO_COMP_MAX 10 // maximum compensaton value when changing direction (us)
#define BUTTON_DELAY 5 // delay for repeating actions when holding the inc/dec buttons, affects the coarse adjustment speed (ms)
#define SETTINGS_ADJ_DELAY 5000 // press the power button for this amount of time to enter the settings adjustment mode (ms)
#define FIR_FILTER_TAPS 32 // number of FIR filter taps (for potentiometer ADC signal smoothing)
#define IIR_FILTER_TAPS 4 // number of IIR filter taps (for potentiometer ADC signal smoothing)
#define AUTO_POWER_OFF_DELAY (10*60000) // auto power-off timout (ms)
/*
* Global variables
*/
struct {
Servo Srv; // Servo object
LedClass Led; // LED object
ButtonClass ButtonPwr, ButtonInc, ButtonDec; // Push buttons
FirFilterClass FirFilter; // FIR Filter object
int16_t firMemory [FIR_FILTER_TAPS]; // FIR Filter memory
int16_t potPosition; // Fine tuning potentionmeter position [0..1023]
uint32_t autoPowerOffTs; // timestamp for calculating auto power-off time
int16_t coarsePosition; // coarse servo position (us)
int16_t minPosition; // minimum servo postion (us)
int16_t maxPosition; // maximum servo postion (us)
int8_t direction; // servo rotation direction (-1, 1)
} G;
/*
* Non-volatile memory contents (EEPROM)
*/
struct {
int16_t coarsePosition; // coarse servo position (us)
int16_t minPosition; // minimum servo postion (us)
int16_t maxPosition; // maximum servo postion (us)
int16_t servoSetback; // setback the servo by this amount when changing direction (us) 60
int16_t servoComp; // compensaton value when changing direction (us) 3
int8_t direction; // servo rotation direction (-1, 1)
} Nvm;
/*
* Function prototypes
*/
void buttonRead (void);
void servoInit (void);
int16_t servoControl (bool, bool);
void powerSave (void);
/*
* Arduino setup routine
*/
void setup() {
MCUSR = 0; // clear MCU status register
wdt_disable (); // and disable watchdog
#ifdef SERIAL_DEBUG
// initialize serial port
Serial.begin (SERIAL_BAUD);
#endif
PRINTLN (" ");
PRINTLN ("+ + + A N T E N N A T U N E R + + +");
PRINTLN (" ");
pinMode (PPM_PIN, OUTPUT);
pinMode (MOSFET_PIN, OUTPUT);
digitalWrite (MOSFET_PIN, LOW);
pinMode (BUTTON_PWR_PIN, INPUT_PULLUP);
pinMode (BUTTON_INC_PIN, INPUT_PULLUP);
pinMode (BUTTON_DEC_PIN, INPUT_PULLUP);
//analogReference (INTERNAL);
// read the non-volatile memory
eepromRead (0x0, (uint8_t *)&Nvm, sizeof (Nvm));
if (Nvm.coarsePosition < SERVO_MIN || Nvm.coarsePosition > SERVO_MAX - SERVO_FINE_RANGE) Nvm.coarsePosition = SERVO_MIN;
if (Nvm.minPosition < SERVO_MIN || Nvm.minPosition > SERVO_MAX) Nvm.minPosition = SERVO_MIN;
if (Nvm.maxPosition < SERVO_MIN || Nvm.maxPosition > SERVO_MAX) Nvm.maxPosition = SERVO_MAX;
if (Nvm.servoSetback < SERVO_SETBACK_MIN || Nvm.servoSetback > SERVO_SETBACK_MAX) Nvm.servoSetback = SERVO_SETBACK_MIN;
if (Nvm.servoComp < SERVO_COMP_MIN || Nvm.servoComp > SERVO_COMP_MAX) Nvm.servoComp = SERVO_COMP_MIN;
if (Nvm.direction != -1 && Nvm.direction != 1) Nvm.direction = 1;
// initialize various objects
Adc.initialize ();
G.Led.initialize (LED_PIN);
G.FirFilter.initialize (G.firMemory, FIR_FILTER_TAPS);
servoInit ();
G.autoPowerOffTs = millis(); // reset the auto power-off timer
// enable the watchdog
//wdt_enable (WDTO_8S);
}
/*
* Arduino main loop
*/
void loop () {
static enum { STARTUP, RUNNING_E, RUNNING, SHUTDOWN_E, SHUTDOWN, ADJUST_MIN_E, ADJUST_MIN,
ADJUST_MAX_E, ADJUST_MAX, ADJUST_COMP_E, ADJUST_COMP, ADJUST_SETBACK_E, ADJUST_SETBACK } state = STARTUP, lastState = STARTUP;
static uint32_t veryLongPressTs;
static int16_t servoVal, lastServoVal;
int32_t ts = millis ();
buttonRead ();
G.Led.loopHandler ();
// power button - rising edge
G.ButtonPwr.rising ();
if (state != STARTUP) {
// power button - long press - shutdown
if (G.ButtonPwr.longPress ()) state = SHUTDOWN_E;
// auto power-off timeout
if (ts - G.autoPowerOffTs > AUTO_POWER_OFF_DELAY) state = SHUTDOWN_E;
}
// Main state machine
switch (state) {
// startup
case STARTUP:
// power button - long press - startup
if (G.ButtonPwr.longPress ()) state = RUNNING_E;
break;
// normal operation
case RUNNING_E:
G.Led.turnOn (); // turn on the led indicator
digitalWrite (MOSFET_PIN, HIGH); // turn on the power MOSFET
state = lastState = RUNNING;
case RUNNING:
servoControl (true, true);
break;
// adjust the servo direction compensaton value
case ADJUST_COMP_E:
G.Led.blinkBlocking (1, 200, 400);
state = lastState = ADJUST_COMP;
case ADJUST_COMP:
// power button - falling - adjust min servo limit
if (G.ButtonPwr.falling ()) state = ADJUST_MIN_E;
// inc button - rising - increment value
if (G.ButtonInc.rising ()) {
if (Nvm.servoComp < SERVO_COMP_MAX) Nvm.servoComp++;
G.Led.blinkBlocking (Nvm.servoComp, 100, 300);
G.autoPowerOffTs = ts;
}
// dec button - rising - decrement value
if (G.ButtonDec.rising ()) {
if (Nvm.servoComp > SERVO_COMP_MIN) Nvm.servoComp--;
if (Nvm.servoComp > 0 ) G.Led.blinkBlocking (Nvm.servoComp, 100, 300);
else G.Led.blinkBlocking (1, 300, 300);
G.autoPowerOffTs = ts;
}
break;
// adjust the minimum servo limit
case ADJUST_MIN_E:
G.Led.blinkBlocking (2, 200, 400);
G.coarsePosition = G.minPosition;
G.minPosition = SERVO_MIN;
G.maxPosition = Nvm.maxPosition;
lastServoVal = servoControl (false, false);
state = lastState = ADJUST_MIN;
case ADJUST_MIN:
// power button - falling - adjust max servo position
if (G.ButtonPwr.falling ()) state = ADJUST_MAX_E;
servoVal = servoControl (false, true);
if (servoVal != lastServoVal) {
Nvm.minPosition = servoVal;
lastServoVal = servoVal;
}
break;
// adjust the maximum servo limit
case ADJUST_MAX_E:
G.Led.blinkBlocking (3, 200, 400);
G.coarsePosition = G.maxPosition - SERVO_FINE_RANGE;
G.minPosition = Nvm.minPosition;
G.maxPosition = SERVO_MAX;
lastServoVal = servoControl (false, false);
state = lastState = ADJUST_MAX;
case ADJUST_MAX:
// power button - falling - adjust servo setback value
if (G.ButtonPwr.falling ()) state = ADJUST_SETBACK_E;
servoVal = servoControl (false, true);
if (servoVal != lastServoVal) {
Nvm.maxPosition = servoVal;
lastServoVal = servoVal;
}
break;
// adjust the servo setback value upon direction change
case ADJUST_SETBACK_E:
G.Led.blinkBlocking (4, 200, 400);
G.Srv.writeMicroseconds (Nvm.maxPosition);
delay (SERVO_SB_DELAY);
G.coarsePosition = Nvm.minPosition;
servoVal = servoControl (false, false);
state = lastState = ADJUST_SETBACK;
case ADJUST_SETBACK:
// power button - falling - adjust direction compensation value
if (G.ButtonPwr.falling ()) state = ADJUST_COMP_E;
// inc button - rising - increment value
if (G.ButtonInc.rising ()) {
Nvm.servoSetback += 5;
if (Nvm.servoSetback > SERVO_SETBACK_MAX) Nvm.servoSetback = SERVO_SETBACK_MAX;
else {
G.Led.blink (1, 100, 100);
G.Srv.writeMicroseconds (servoVal + Nvm.servoSetback);
delay (SERVO_SB_DELAY);
G.Srv.writeMicroseconds (servoVal);
}
G.autoPowerOffTs = ts;
}
// dec button - rising - decrement value
if (G.ButtonDec.rising ()) {
Nvm.servoSetback -= 5;
if (Nvm.servoSetback < SERVO_SETBACK_MIN) Nvm.servoSetback = SERVO_SETBACK_MIN;
else {
G.Led.blink (1, 100, 100);
G.Srv.writeMicroseconds (servoVal + Nvm.servoSetback);
delay (SERVO_SB_DELAY);
G.Srv.writeMicroseconds (servoVal);
}
G.autoPowerOffTs = ts;
}
break;
case SHUTDOWN_E:
G.Led.toggle (); // toggle LED indicator
if (lastState == RUNNING) {
Nvm.coarsePosition = G.coarsePosition; // remember the last servo position
Nvm.direction = G.direction; // remember the direction of rotation
}
veryLongPressTs = ts;
state = SHUTDOWN;
case SHUTDOWN:
// enter the adjustment mode if power button was pressed long enough
if ( G.ButtonPwr.pressed && lastState == RUNNING) {
if (ts - veryLongPressTs > SETTINGS_ADJ_DELAY) {
state = ADJUST_COMP_E;
G.Srv.attach (PPM_PIN, SERVO_MIN, SERVO_MAX);
break;
}
}
// execute the shutdown sequence
else {
eepromWrite (0x0, (uint8_t *)&Nvm, sizeof (Nvm)); // write-back NVM settings
if (G.Led.powerOn) { // keep LED on for confirming saved settings
delay (1000); // wait a seond
G.Led.turnOff (); // turn off LED if it was on
}
digitalWrite (MOSFET_PIN, LOW); // turn off the power MOSFET
while (1) {}; // stay here until power down
}
break;
}
// call the power-save routine
powerSave ();
}
/*
* Read push buttons and potentiometer values
*/
void buttonRead () {
static IirFilterClass ButtonPwrFilter, ButtonIncFilter, ButtonDecFilter, PotFilter;
int16_t value, adcVal;
// read push buttons and digitally debounce
value = ButtonPwrFilter.process (1023 * digitalRead (BUTTON_PWR_PIN), 8);
if (value < 512) G.ButtonPwr.press ();
else G.ButtonPwr.release ();
value = ButtonIncFilter.process (1023 * digitalRead (BUTTON_INC_PIN), 8);
if (value < 512) G.ButtonInc.press ();
else G.ButtonInc.release ();
value = ButtonDecFilter.process (1023 * digitalRead (BUTTON_DEC_PIN), 8);
if (value < 512) G.ButtonDec.press ();
else G.ButtonDec.release ();
// check if ADC finished detecting the value
adcVal = Adc.readVal ();
// ADC finished
if (adcVal >= 0) {
// smoothen ADC readings
adcVal = G.FirFilter.process (adcVal);
G.potPosition = PotFilter.process (adcVal, IIR_FILTER_TAPS);
}
// start a new ADC conversion (will be ignored if already started)
Adc.start (POT_APIN);
}
/*
* Initialize the servo
* Restore the last servo position, taking the direction of rotation into account
*/
void servoInit () {
G.potPosition = 0;
G.minPosition = Nvm.minPosition;
G.maxPosition = Nvm.maxPosition;
G.Srv.attach (PPM_PIN, G.minPosition, G.maxPosition);
// overshoot the target position
if (Nvm.direction < 0) G.coarsePosition = Nvm.coarsePosition + SERVO_SB_STARTUP + SERVO_FINE_RANGE;
else G.coarsePosition = Nvm.coarsePosition - SERVO_SB_STARTUP;
// move the servo to overshoot position
servoControl (false, false);
// reset to target position - servo will move there once servoControl () is called
G.coarsePosition = Nvm.coarsePosition;
}
/*
* Control the servo position
*/
int16_t servoControl (bool setback, bool blink) {
static uint32_t buttonTs = 0;
static uint32_t setbackTs = 0;
static int16_t lastServoVal = 0;
static int16_t lastDelta = 0;
int16_t servoVal;
int16_t delta;
uint32_t ts = millis ();
if (G.ButtonInc.pressed && ts - buttonTs > BUTTON_DELAY) {
if (G.coarsePosition < G.maxPosition - SERVO_FINE_RANGE) G.coarsePosition++;
buttonTs = ts;
}
else if (G.ButtonDec.pressed && ts - buttonTs > BUTTON_DELAY) {
if (G.coarsePosition > G.minPosition) G.coarsePosition--;
buttonTs = ts;
}
// calculate servo position
servoVal = G.coarsePosition + map (G.potPosition, 0, 1023, 0, SERVO_FINE_RANGE);
delta = servoVal - lastServoVal;
// if PPM value has changed
if (delta != 0) {
G.direction = sgn (delta);
// handle changed rotation direction
if (sgn (delta) == -sgn (lastDelta) && setback && ts - setbackTs > 100) {
setbackTs = ts; // prevent too frequent setback
if (blink) G.Led.blink (2, 50, 100); // blink twice
if (delta > 0) {
G.Srv.writeMicroseconds (lastServoVal - Nvm.servoSetback);
PRINTLN ("---");
}
else {
G.Srv.writeMicroseconds (lastServoVal + Nvm.servoSetback);
PRINTLN ("+++");
}
delay (SERVO_SB_DELAY);
}
if (blink) G.Led.blink (1, 50, 100); // blink once
G.autoPowerOffTs = ts; // reset the auto power-off timer
G.Srv.writeMicroseconds (servoVal + sgn(delta) * Nvm.servoComp );
PRINT ("adcVal = ");
PRINT (G.potPosition, DEC);
PRINT ("; servoVal = ");
PRINT (servoVal, DEC);
PRINT ("; delta = ");
PRINTLN (delta, DEC);
lastServoVal = servoVal;
lastDelta = delta;
}
return servoVal;
}
/*
* Power-save routine, enables CPU sleep mode
*/
void powerSave (void) {
// configure lowest sleep mode that keeps clk_IO for Timer 1
set_sleep_mode (SLEEP_MODE_IDLE);
// enter sleep, wakeup will be triggered by the next Timer 1 interrupt
sleep_enable ();
sleep_cpu ();
sleep_disable ();
}