-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathkk.c
1492 lines (1338 loc) · 36.5 KB
/
kk.c
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* KK board flight controller software for AVR microcontrollers
*
* Based on XXcontrol_KR_v1.5 by Minsoo Kim
* Based on XXcontrol by Mike Barton
* Based on excellent assembly code by Rolf R Bakke (kapteinkuk)
* With ideas from Rune Hasvold (CyCrow) and OlliW on rcgroups
* Thanks, everyone!
*
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. Always test
* without propellers! Please do not ship derivative works without
* source; keep this code open as Rolf first so kindly released his
* design and code to the community.
*
* Should fit on 48, 88, 168, and 328. I've tested TRICOPTER mode on an
* ATmega88A. You may wish to use avrdude -t to "dump calibration" and
* check timings on a digital scope. Temperature and voltage shift the
* oscillator frequency a little, and each chip responds differently.
* See doc8271.pdf page 401.
*
* I notice a few microseconds of output jitter still with the internal
* oscillator. It seems that only an external resonator or crystal will
* solve this, but those pins are currently used for Rx and the LED, and
* the Rx pin cannot be moved to another pin that does not share another
* PCINT unless RESET is used for that purpose. If using all hardware
* PPM, it may be acceptable to have a more expensive interrupt handler
* that simply logs the interrupt time and pin states and do the rest of
* the processing in RxGetChannels().
*
* See http://www.kkmulticopter.com/
*
* Hardware PPM supported on motor outputs M1, M2, M5 and M6; software
* PPM on M3 and M4 (Rx interrupts can cause some jitter). M3 and M4
* outputs will be copied to M5 and M6, when not otherwise used, to allow
* use of full hardware PPM.
*
* General motor output setup:
*
* Single
* M1 CCW
* |
* |
*
* M2 (Servo)
* |
* |
* M5 ---- ---- M3
* (Servo) | (Servo)
* |
* M4 (Servo)
*
* Dual
* M1 CCW
* |
* M2 CW
* |
*
* |
* |
* M5/M3 ----+----
* (Servo) |
* |
* M6/M4 (Servo)
*
* Twin
* / --- \
* / | \
* M1 CW | M2 CCW
* |
* M3 | M4
* (Servo) | (Servo)
* |
* |
* M5 (Tail Servo, Optional)
* M6 (Tail Servo Reverse, Optional)
*
* Tri
* M1 CW M2 CCW
* \ /
* \.---./
* | |
* `---'
* |
* |M4/M6 (Tail Servo)
* M3/M5 CCW
*
* Quad-+
* M1 CW
* |
* |
* |
* .---.
* M2 CCW----| |----M3/M5 CCW
* `---'
* |
* |
* |
* M4/M6 CW
*
* Quad-X
*
* M1 CW M2 CCW
* \ /
* \.--./
* | |
* /`--'\
* / \
* M4/M6 CCW M3/M5 CW
*
* Hex
* M1 CW
* |
* M6 CCW | M2 CCW
* \ | /
* \ .---. /
* -| |-
* / `---' \
* / | \
* M5 CW | M3 CW
* |
* M4 CCW
*
* Y6
*
* M1,4 M2,5 M1->3 = CW
* \ / M4->6 = CCW
* \.---./
* | |
* `---'
* |
* |
* M3,6
*
*/
/*+- Configurables ---------------------------------------------------------+*/
/* Multicopter Type */
//#define SINGLE_COPTER
//#define DUAL_COPTER
//#define TWIN_COPTER
#define TRI_COPTER
//#define QUAD_COPTER
//#define QUAD_X_COPTER
//#define Y4_COPTER
//#define HEX_COPTER
//#define Y6_COPTER
/* Servo and gain pot reversing */
//#define SERVO_REVERSE
//#define GAIN_POT_REVERSE
/*
* ESC PPM output rate -
* Do not set lower than 122 Hz without a slower/higher t0/t1 clkdiv
* as the period needs to fit in the 16-bit timer.
*/
//#define ESC_RATE 300 // in Hz
//#define ESC_RATE 400 // in Hz (at SINGLE_COPTER Only)
#define ESC_RATE 450 // in Hz
//#define ESC_RATE 495 // in Hz
// NOTE: Set to 50 for analog servos, 250 for digital servos.
#define SERVO_RATE 50 // in Hz
// Stick arming and throw detection (in % * 10 eg 1000 steps)
#define STICK_THROW 300
// Gyro gain shift-right (after 32-bit multiplication of GainInADC[] value).
#define GYRO_GAIN_SHIFT 5
// Stick gain shift-right (after 32-bit multiplication of GainInADC[] value).
#define STICK_GAIN_SHIFT 8
// Skip yaw gyro calculations if using external yaw gyro
//#define EXTERNAL_YAW_GYRO
// Max Collective
// limits the maximum stick collective (range 80->100 100=Off)
// this allows gyros to stabilise better when full throttle applied
#define MAX_COLLECTIVE 1000 // 95
/*+- Helper Macros ---------------------------------------------------------+*/
#define PWM_LOW_PULSE_US ((1000000 / ESC_RATE) - 2000)
#define ADC_MAX 1023
#ifdef GAIN_POT_REVERSE
#undef GAIN_POT_REVERSE
#define GAIN_POT_REVERSE ADC_MAX -
#else
#define GAIN_POT_REVERSE
#endif
#ifdef SERVO_REVERSE
#undef SERVO_REVERSE
#define SERVO_REVERSE -
#else
#define SERVO_REVERSE
#endif
/*+- Main Code -------------------------------------------------------------+*/
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "typedefs.h"
#include "io_cfg.h"
#define EEPROM_DATA_START_POS 0 // Settings save offset in eeprom
enum GyroDirection { GYRO_NORMAL = 0, GYRO_REVERSED };
enum GyroArrayIndex { ROLL = 0, PITCH, YAW };
// eeProm data structure
static struct config {
uint8_t setup; // byte to identify if already setup
uint8_t RollGyroDirection;
uint8_t PitchGyroDirection;
uint8_t YawGyroDirection;
} Config; // Holds configuration (from eeProm)
bool Armed;
static uint16_t GainInADC[3]; // ADC result
#if defined(SINGLE_COPTER) || defined(DUAL_COPTER) || defined(TWIN_COPTER) || defined(TRI_COPTER)
static uint16_t servo_skip_divider;
#endif
static int16_t RxInRoll;
static int16_t RxInPitch;
static int16_t RxInCollective;
static int16_t RxInYaw;
/*
* Careful! Making these volatile makes it spew r24 crap in the _middle_ of
* asm volatile statements. Always check assembler output of interrupt
* routines.
*/
static uint16_t RxChannel1;
static uint16_t RxChannel2;
static uint16_t RxChannel3;
static uint16_t RxChannel4;
register uint16_t i_tmp asm("r2"); // ISR vars
register uint16_t RxChannel1Start asm("r4");
register uint16_t RxChannel2Start asm("r6");
register uint16_t RxChannel3Start asm("r8");
register uint16_t RxChannel4Start asm("r10");
register uint8_t i_sreg asm("r12");
#ifdef TWIN_COPTER
static int16_t RxInOrgPitch;
#endif
static int16_t gyroADC[3]; // Holds Gyro ADC's
static int16_t gyroZero[3]; // used for calibrating Gyros on ground
static int16_t integral[3]; // PID integral term
static int16_t last_error[3]; // Last proportional error
static uint16_t ModeDelayCounter;
static int16_t MotorOut1;
static int16_t MotorOut2;
static int16_t MotorOut3;
static int16_t MotorOut4;
static int16_t MotorOut5;
static int16_t MotorOut6;
static int16_t MotorStartTCNT1;
#if defined(SINGLE_COPTER) || defined(DUAL_COPTER) || defined(TWIN_COPTER) || defined(TRI_COPTER)
static uint8_t servo_skip;
#endif
static void setup();
static void init_adc();
static void ReadGyros();
static void CalibrateGyros();
static void ReadGainPots();
static void RxGetChannels();
static void read_adc(uint8_t channel);
static void output_motor_ppm();
static void Initial_EEPROM_Config_Load();
static void Save_Config_to_EEPROM();
static void Set_EEPROM_Default_Config();
static void eeprom_write_byte_changed( uint8_t * addr, uint8_t value);
static void eeprom_write_block_changes( const uint8_t * src, void * dest, size_t size);
#if 1
/*
* Rx interrupts with inline assembler that makes them much faster.
* Please verify that GCC does not inject anything crazy in here,
* such as completely unused movs that clobber other registers.
*/
ISR(PCINT2_vect, ISR_NAKED)
{
if (RX_ROLL) { // rising
asm volatile("lds %A0, %1" : "=r" (RxChannel1Start) : "i" (&TCNT1L));
asm volatile("lds %B0, %1" : "=r" (RxChannel1Start) : "i" (&TCNT1H));
asm volatile("reti");
} else { // falling
asm volatile(
"lds %A0, %3\n"
"lds %B0, %4\n"
"in %1, __SREG__\n"
"sub %A0, %A2\n"
"sbc %B0, %B2\n"
"out __SREG__, %1\n"
: "+r" (i_tmp), "+r" (i_sreg), "+r" (RxChannel1Start)
: "i" (&TCNT1L), "i" (&TCNT1H));
RxChannel1 = i_tmp;
}
asm volatile ("reti");
}
ISR(INT0_vect, ISR_NAKED)
{
if (RX_PITCH) { // rising
asm volatile("lds %A0, %1" : "=r" (RxChannel2Start) : "i" (&TCNT1L));
asm volatile("lds %B0, %1" : "=r" (RxChannel2Start) : "i" (&TCNT1H));
asm volatile("reti");
} else { // falling
asm volatile(
"lds %A0, %3\n"
"lds %B0, %4\n"
"in %1, __SREG__\n"
"sub %A0, %A2\n"
"sbc %B0, %B2\n"
"out __SREG__, %1\n"
: "+r" (i_tmp), "+r" (i_sreg), "+r" (RxChannel2Start)
: "i" (&TCNT1L), "i" (&TCNT1H));
RxChannel2 = i_tmp;
}
asm volatile ("reti");
}
ISR(INT1_vect, ISR_NAKED)
{
if (RX_COLL) { // rising
asm volatile("lds %A0, %1" : "=r" (RxChannel3Start) : "i" (&TCNT1L));
asm volatile("lds %B0, %1" : "=r" (RxChannel3Start) : "i" (&TCNT1H));
asm volatile("reti");
} else { // falling
asm volatile(
"lds %A0, %3\n"
"lds %B0, %4\n"
"in %1, __SREG__\n"
"sub %A0, %A2\n"
"sbc %B0, %B2\n"
"out __SREG__, %1\n"
: "+r" (i_tmp), "+r" (i_sreg), "+r" (RxChannel3Start)
: "i" (&TCNT1L), "i" (&TCNT1H));
RxChannel3 = i_tmp;
}
asm volatile ("reti");
}
ISR(PCINT0_vect, ISR_NAKED)
{
if (RX_YAW) { // rising
asm volatile("lds %A0, %1" : "=r" (RxChannel4Start) : "i" (&TCNT1L));
asm volatile("lds %B0, %1" : "=r" (RxChannel4Start) : "i" (&TCNT1H));
asm volatile("reti");
} else { // falling
asm volatile(
"lds %A0, %3\n"
"lds %B0, %4\n"
"in %1, __SREG__\n"
"sub %A0, %A2\n"
"sbc %B0, %B2\n"
"out __SREG__, %1\n"
: "+r" (i_tmp), "+r" (i_sreg), "+r" (RxChannel4Start)
: "i" (&TCNT1L), "i" (&TCNT1H));
RxChannel4 = i_tmp;
}
asm volatile ("reti");
}
#else
/*
* Rx interrupts without inline assembler (just a bit slower)
*/
ISR(PCINT2_vect)
{
if (RX_ROLL) { // rising edge
RxChannel1Start = TCNT1;
} else { // falling edge
RxChannel1 = TCNT1 - RxChannel1Start;
i_sreg = 0;
}
}
ISR(INT0_vect)
{
if (RX_PITCH) {
RxChannel2Start = TCNT1;
} else {
RxChannel2 = TCNT1 - RxChannel2Start;
i_sreg = 0;
}
}
ISR(INT1_vect)
{
if (RX_COLL) {
RxChannel3Start = TCNT1;
} else {
RxChannel3 = TCNT1 - RxChannel3Start;
i_sreg = 0;
}
}
ISR(PCINT0_vect)
{
if (RX_YAW) {
RxChannel4Start = TCNT1;
} else {
RxChannel4 = TCNT1 - RxChannel4Start;
i_sreg = 0;
}
}
#endif
static void setup()
{
uint8_t i;
MCUCR = _BV(PUD); // Disable hardware pull-up
#if 0
RX_ROLL_DIR = INPUT;
RX_PITCH_DIR = INPUT;
RX_COLL_DIR = INPUT;
RX_YAW_DIR = INPUT;
GYRO_YAW_DIR = INPUT;
GYRO_PITCH_DIR = INPUT;
GYRO_ROLL_DIR = INPUT;
GAIN_YAW_DIR = INPUT;
GAIN_PITCH_DIR = INPUT;
GAIN_ROLL_DIR = INPUT;
M1_DIR = OUTPUT;
M2_DIR = OUTPUT;
M3_DIR = OUTPUT;
M4_DIR = OUTPUT;
M5_DIR = OUTPUT;
M6_DIR = OUTPUT;
LED_DIR = OUTPUT;
LED = 0;
RX_ROLL = 0;
RX_PITCH = 0;
RX_COLL = 0;
RX_YAW = 0;
#else
DDRB = 0b01111111;
DDRC = 0b11000000;
DDRD = 0b11110001;
#endif
/*
* This suits my ATmega88A: no Tx trim, output timings perfect.
* See doc8271.pdf page ~401; beware the step at 128. -Simon
*/
if (OSCCAL == 0x9d)
OSCCAL = 0x9f;
/*
* timer0 (8bit) - run at 8MHz, used to control ESC pulses
* We use 8Mhz instead of 1MHz (1 usec) to avoid alignment jitter.
*/
TCCR0B = _BV(CS00); /* NOTE: Specified again below with FOC0x bits */
/*
* timer1 (16bit) - run at 8MHz, used to measure Rx pulses
* and to control ESC/servo pulse
*/
TCCR1B = _BV(CS10);
/*
* timer2 8bit - run at 8MHz / 1024 = 7812.5KHz, just used for arming
*/
TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
/*
* Enable Rx pin interrupts
*/
PCICR = _BV(PCIE0) | _BV(PCIE2); // PCINT0..7, PCINT16..23 enable
PCMSK0 = _BV(PCINT7); // PB7
PCMSK2 = _BV(PCINT17); // PD1
EICRA = _BV(ISC00) | _BV(ISC10); // Any change INT0, INT1
EIMSK = _BV(INT0) | _BV(INT1); // External Interrupt Mask Register
#if defined(SINGLE_COPTER) || defined(DUAL_COPTER) || defined(TWIN_COPTER) || defined(TRI_COPTER)
/*
* Calculate the servo rate divider (pulse loop skip count
* needed to avoid burning analog servos)
*/
for (servo_skip_divider = 1;;servo_skip_divider++)
if (servo_skip_divider * SERVO_RATE >= ESC_RATE)
break;
#endif
Initial_EEPROM_Config_Load();
init_adc();
Armed = false;
/*
* Flash the LED once at power on
*/
LED = 1;
_delay_ms(150);
LED = 0;
sei();
_delay_ms(1500);
ReadGainPots();
ReadGainPots();
// clear config
if (GainInADC[PITCH] < (ADC_MAX * 5) / 100 &&
GainInADC[ROLL] < (ADC_MAX * 5) / 100 &&
GainInADC[YAW] < (ADC_MAX * 5) / 100) {
Set_EEPROM_Default_Config();
while (1)
;
}
// Stick Centering Test
if (GainInADC[PITCH] < (ADC_MAX * 5) / 100) {
while (1) {
RxGetChannels();
i = abs(RxInRoll) + abs(RxInPitch) + abs(RxInYaw);
LED = 1;
while (i) {
LED = 0;
i--;
}
}
}
// Gyro direction reversing
if (GainInADC[ROLL] < (ADC_MAX * 5) / 100) { // less than 5% (5 / 100) * 1023 = 51
// flash LED 3 times
for (i = 0;i < 3;i++) {
LED = 1;
_delay_ms(25);
LED = 0;
_delay_ms(25);
}
while (1) {
RxGetChannels();
if (RxInRoll < -STICK_THROW) { // normal(left)
Config.RollGyroDirection = GYRO_NORMAL;
Save_Config_to_EEPROM();
LED = 1;
} if (RxInRoll > STICK_THROW) { // reverse(right)
Config.RollGyroDirection = GYRO_REVERSED;
Save_Config_to_EEPROM();
LED = 1;
} else if (RxInPitch < -STICK_THROW) { // normal(up)
Config.PitchGyroDirection = GYRO_NORMAL;
Save_Config_to_EEPROM();
LED = 1;
} else if (RxInPitch > STICK_THROW) { // reverse(down)
Config.PitchGyroDirection = GYRO_REVERSED;
Save_Config_to_EEPROM();
LED = 1;
} else if (RxInYaw < -STICK_THROW) { // normal(left)
Config.YawGyroDirection = GYRO_NORMAL;
Save_Config_to_EEPROM();
LED = 1;
} else if (RxInYaw > STICK_THROW) { // reverse(right)
Config.YawGyroDirection = GYRO_REVERSED;
Save_Config_to_EEPROM();
LED = 1;
}
_delay_ms(50);
LED = 0;
}
}
// ESC throttle calibration
if (GainInADC[YAW] < (ADC_MAX * 5) / 100) { // less than 5%
// flash LED 3 times
for (i = 0;i < 3;i++) {
LED = 1;
_delay_ms(25);
LED = 0;
_delay_ms(25);
}
Armed = true;
while (1) {
RxGetChannels();
#ifdef SINGLE_COPTER
MotorOut1 = RxInCollective;
MotorOut2 = 1400; // Center: 140
MotorOut3 = 1400;
MotorOut4 = 1400;
MotorOut5 = 1400;
#elif defined(DUAL_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = 500; // Center: 50
MotorOut4 = 500;
#elif defined(TWIN_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = 500; // Center: 50
MotorOut4 = 500;
MotorOut5 = 500;
MotorOut6 = 500; // Center: 50, Reverse
#elif defined(TRI_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = RxInCollective;
MotorOut4 = 500+RxInYaw*2; // Center: 50
#elif defined(QUAD_COPTER) || defined(QUAD_X_COPTER) || defined(Y4_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = RxInCollective;
MotorOut4 = RxInCollective;
#elif defined(HEX_COPTER) || defined(Y6_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = RxInCollective;
MotorOut4 = RxInCollective;
MotorOut5 = RxInCollective;
MotorOut6 = RxInCollective;
#else
#error No Copter configuration defined !!!!
#endif
output_motor_ppm(); // this regulates rate at which we output signals
}
}
}
static inline void loop()
{
// static uint8_t i;
static uint16_t Change_Arming = 0;
static uint8_t Arming_TCNT2 = 0;
int16_t error, emax = 1023;
int16_t imax, derivative;
RxGetChannels();
if (RxInCollective <= 0) {
// Check for stick arming (Timer2 at 8MHz/1024 = 7812.5KHz)
Change_Arming+= (uint8_t)(TCNT2 - Arming_TCNT2);
Arming_TCNT2 = TCNT2;
if (Armed) {
if (RxInYaw < STICK_THROW || abs(RxInPitch) > STICK_THROW)
Change_Arming = 0; // re-set count
} else {
if (RxInYaw > -STICK_THROW || abs(RxInPitch) > STICK_THROW)
Change_Arming = 0; // re-set count
}
// 3Sec / 0.000128 = 23437 = 0x5B8D or
// 2.5Sec / 0.000128 = 19531 = 0x4C4B
// 0.5Sec / 0.000128 = 3906 = 0x0F42
if (Change_Arming > 0x0F42) {
Armed = !Armed;
if (Armed)
CalibrateGyros();
ModeDelayCounter = 0;
}
}
ReadGyros();
LED = Armed;
gyroADC[ROLL]-= gyroZero[ROLL];
gyroADC[PITCH]-= gyroZero[PITCH];
gyroADC[YAW]-= gyroZero[YAW];
//--- Start mixing by setting collective to motor outputs
RxInCollective = (RxInCollective * 10) >> 3; // 0-800 -> 0-1000
#ifndef SINGLE_COPTER
if (RxInCollective > MAX_COLLECTIVE)
RxInCollective = MAX_COLLECTIVE;
#endif
#ifdef SINGLE_COPTER
MotorOut1 = RxInCollective;
MotorOut2 = 840; // 840
MotorOut3 = 840; // 840
MotorOut4 = 945; // 840 + 840/8
MotorOut5 = 945; // 840 + 840/8
#elif defined(DUAL_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = 500;
MotorOut4 = 500;
#elif defined(TWIN_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = 500;
MotorOut4 = 500;
MotorOut5 = 500; // Optional
MotorOut6 = 500; // Optional
#elif defined(TRI_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = RxInCollective;
MotorOut4 = 500;
#elif defined(QUAD_COPTER) || defined(QUAD_X_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = RxInCollective;
MotorOut4 = RxInCollective;
#elif defined(Y4_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = RxInCollective * 3 / 4; // 25% Down
MotorOut4 = RxInCollective * 3 / 4; // 25% Down
#elif defined(HEX_COPTER) || defined(Y6_COPTER)
MotorOut1 = RxInCollective;
MotorOut2 = RxInCollective;
MotorOut3 = RxInCollective;
MotorOut4 = RxInCollective;
MotorOut5 = RxInCollective;
MotorOut6 = RxInCollective;
#endif
imax = RxInCollective;
if (imax < 0)
imax = 0;
imax>>= 3; /* 1000 -> 200 */
/* Calculate roll output - Test without props!! */
RxInRoll = ((int32_t)RxInRoll * (uint32_t)GainInADC[ROLL]) >> STICK_GAIN_SHIFT;
gyroADC[ROLL] = ((int32_t)gyroADC[ROLL] * (uint32_t)GainInADC[ROLL]) >> GYRO_GAIN_SHIFT;
if (Config.RollGyroDirection == GYRO_NORMAL)
gyroADC[ROLL] = -gyroADC[ROLL];
if (Armed) {
if (0) {
error = RxInRoll - gyroADC[ROLL];
if (error > emax)
error = emax;
else if (error < -emax)
error = -emax;
integral[ROLL]+= error;
if (integral[ROLL] > imax)
integral[ROLL] = imax;
else if (integral[ROLL] < -imax)
integral[ROLL] = -imax;
derivative = error - last_error[ROLL];
last_error[ROLL] = error;
RxInRoll+= error + (integral[ROLL] >> 2) + (derivative >> 2);
} else {
RxInRoll-= gyroADC[ROLL];
}
}
#ifdef SINGLE_COPTER
MotorOut2+= RxInRoll;
MotorOut4-= RxInRoll;
#elif defined(DUAL_COPTER)
MotorOut4+= RxInRoll;
#elif defined(TWIN_COPTER)
RxInRoll = (RxInRoll * 7) >> 3; // Approximation of sin(60) without div
MotorOut1+= RxInRoll;
MotorOut2-= RxInRoll;
#elif defined(TRI_COPTER)
RxInRoll = (RxInRoll * 7) >> 3; // (.875 versus .86602540)
MotorOut1+= RxInRoll;
MotorOut2-= RxInRoll;
#elif defined(QUAD_COPTER)
MotorOut2+= RxInRoll;
MotorOut3-= RxInRoll;
#elif defined(QUAD_X_COPTER)
RxInRoll = RxInRoll >> 1;
MotorOut1+= RxInRoll;
MotorOut2-= RxInRoll;
MotorOut3-= RxInRoll;
MotorOut4+= RxInRoll;
#elif defined(Y4_COPTER)
RxInRoll = (RxInRoll * 7) >> 3;
MotorOut1+= RxInRoll;
MotorOut2-= RxInRoll;
#elif defined(HEX_COPTER)
RxInRoll = (RxInRoll * 7) >> 3;
MotorOut2-= RxInRoll;
MotorOut3-= RxInRoll;
MotorOut5+= RxInRoll;
MotorOut6+= RxInRoll;
#elif defined(Y6_COPTER)
RxInRoll = (RxInRoll * 7) >> 3;
MotorOut1+= RxInRoll;
MotorOut2+= RxInRoll;
MotorOut3-= RxInRoll;
MotorOut4-= RxInRoll;
#endif
/* Calculate pitch output - Test without props!! */
RxInPitch = ((int32_t)RxInPitch * (uint32_t)GainInADC[PITCH]) >> STICK_GAIN_SHIFT;
gyroADC[PITCH] = ((int32_t)gyroADC[PITCH] * (uint32_t)GainInADC[PITCH]) >> GYRO_GAIN_SHIFT;
if (Config.PitchGyroDirection == GYRO_NORMAL)
gyroADC[PITCH] = -gyroADC[PITCH];
if (Armed) {
if (0) {
error = RxInPitch - gyroADC[PITCH];
if (error > emax)
error = emax;
else if (error < -emax)
error = -emax;
integral[PITCH]+= error;
if (integral[PITCH] > imax)
integral[PITCH] = imax;
else if (integral[PITCH] < -imax)
integral[PITCH] = -imax;
derivative = error - last_error[PITCH];
last_error[PITCH] = error;
RxInPitch+= error + (integral[PITCH] >> 2) + (derivative >> 2);
} else {
RxInPitch-= gyroADC[PITCH];
}
}
#ifdef SINGLE_COPTER
MotorOut3+= RxInPitch;
MotorOut5-= RxInPitch;
#elif defined(DUAL_COPTER)
MotorOut3+= RxInPitch;
#elif defined(TWIN_COPTER)
MotorOut3-= SERVO_REVERSE RxInPitch;
MotorOut4+= SERVO_REVERSE RxInPitch;
// Stick Only, Optional
RxInOrgPitch = abs(RxInOrgPitch);
MotorOut5+= RxInOrgPitch; // Tain Servo-Optional, Down Only
MotorOut6-= RxInOrgPitch; // Tain Servo-Optional, Down Only (Reverse)
#elif defined(TRI_COPTER)
MotorOut3-= RxInPitch;
RxInPitch = (RxInPitch >> 1); // cosine of 60
MotorOut1+= RxInPitch;
MotorOut2+= RxInPitch;
#elif defined(QUAD_COPTER)
MotorOut1+= RxInPitch;
MotorOut4-= RxInPitch;
#elif defined(QUAD_X_COPTER)
RxInPitch = (RxInPitch >> 1); // cosine of 60
MotorOut1+= RxInPitch;
MotorOut2+= RxInPitch;
MotorOut3-= RxInPitch;
MotorOut4-= RxInPitch;
#elif defined(Y4_COPTER)
MotorOut1+= RxInPitch;
MotorOut2+= RxInPitch;
MotorOut3-= RxInPitch;
MotorOut4-= RxInPitch;
#elif defined(HEX_COPTER)
MotorOut1+= RxInPitch;
MotorOut4-= RxInPitch;
RxInPitch = (RxInPitch >> 2);
MotorOut2+= RxInPitch;
MotorOut3-= RxInPitch;
MotorOut5-= RxInPitch;
MotorOut6+= RxInPitch;
#elif defined(Y6_COPTER)
MotorOut5-= RxInPitch;
MotorOut6-= RxInPitch;
RxInPitch = (RxInPitch >> 1); // cosine of 60
MotorOut1+= RxInPitch;
MotorOut2+= RxInPitch;
MotorOut3+= RxInPitch;
MotorOut4+= RxInPitch;
#endif
/* Calculate yaw output - Test without props!! */
RxInYaw = ((int32_t)RxInYaw * (uint32_t)GainInADC[YAW]) >> STICK_GAIN_SHIFT;
gyroADC[YAW] = ((int32_t)gyroADC[YAW] * (uint32_t)GainInADC[YAW]) >> GYRO_GAIN_SHIFT;
if (Config.YawGyroDirection == GYRO_NORMAL)
gyroADC[YAW] = -gyroADC[YAW];
if (Armed) {
error = RxInYaw - gyroADC[YAW];
if (error > emax)
error = emax;
else if (error < -emax)
error = -emax;
integral[YAW]+= error;
if (integral[YAW] > imax)
integral[YAW] = imax;
else if (integral[YAW] < -imax)
integral[YAW] = -imax;
derivative = error - last_error[YAW];
last_error[YAW] = error;
RxInYaw+= error + (integral[YAW] >> 4) + (derivative >> 4);
}
#ifdef SINGLE_COPTER
MotorOut2+= RxInYaw;
MotorOut3+= RxInYaw;
MotorOut4+= RxInYaw;
MotorOut5+= RxInYaw;
#elif defined(DUAL_COPTER)
MotorOut1-= RxInYaw;
MotorOut2+= RxInYaw;
#elif defined(TWIN_COPTER)
MotorOut3+= SERVO_REVERSE(RxInYaw >> 1);
MotorOut4+= SERVO_REVERSE(RxInYaw >> 1);
#elif defined(TRI_COPTER)
MotorOut4+= SERVO_REVERSE RxInYaw;
#elif defined(QUAD_COPTER)
MotorOut1-= RxInYaw;
MotorOut2+= RxInYaw;
MotorOut3+= RxInYaw;
MotorOut4-= RxInYaw;
#elif defined(QUAD_X_COPTER)
MotorOut1-= RxInYaw;
MotorOut2+= RxInYaw;
MotorOut3-= RxInYaw;
MotorOut4+= RxInYaw;
#elif defined(Y4_COPTER)
if ((MotorOut3 - RxInYaw) < 100)
RxInYaw = MotorOut3 - 100; // Yaw Range Limit
if ((MotorOut3 - RxInYaw) > 1000)
RxInYaw = MotorOut3 - 1000; // Yaw Range Limit
if ((MotorOut4 + RxInYaw) < 100)
RxInYaw = 100 - MotorOut4; // Yaw Range Limit
if ((MotorOut4 + RxInYaw) > 1000)
RxInYaw = 1000 - MotorOut4; // Yaw Range Limit
MotorOut3-= RxInYaw;
MotorOut4+= RxInYaw;
#elif defined(HEX_COPTER)
MotorOut1-= RxInYaw;
MotorOut2+= RxInYaw;
MotorOut3-= RxInYaw;
MotorOut4+= RxInYaw;
MotorOut5-= RxInYaw;
MotorOut6+= RxInYaw;
#elif defined(Y6_COPTER)
MotorOut1-= RxInYaw;
MotorOut4-= RxInYaw;
MotorOut5-= RxInYaw;
MotorOut2+= RxInYaw;
MotorOut3+= RxInYaw;
MotorOut6+= RxInYaw;
#endif
#if defined(TRI_COPTER)
/*
* Rather than clipping the motor outputs and causing instability
* at throttle saturation, we pull down the throttle of the other
* motors. This gives priority to stabilization without a fixed
* collective limit.
*/
imax = MotorOut1;
if (MotorOut2 > imax)
imax = MotorOut2;
if (MotorOut3 > imax)
imax = MotorOut3;
imax-= 1000;
if (imax > 0) {
MotorOut1-= imax;