Revision eb7af729
Arduino code measures PWM up and PWM down time, and if U+D!= contant, we reject the signal
arduino/RadioBuggyMega/receiver.c | ||
---|---|---|
20 | 20 |
#define THR_RIGHTMOST -1 |
21 | 21 |
#define THR_CENTERMOST -1 |
22 | 22 |
|
23 |
#define AIL_RECEIVER_PIN 3
|
|
24 |
#define AIL_RECEIVER_INT 1
|
|
23 |
#define AIL_RECEIVER_PIN 2
|
|
24 |
#define AIL_RECEIVER_INT 0
|
|
25 | 25 |
|
26 |
#define THR_RECEIVER_PIN 2 |
|
27 |
#define THR_RECEIVER_INT 0 |
|
26 |
#define THR_RECEIVER_PIN 3 |
|
27 |
#define THR_RECEIVER_INT 1 |
|
28 |
|
|
29 |
#define PWM_TIME 21800 |
|
30 |
#define PWM_THRESH 130 |
|
28 | 31 |
|
29 | 32 |
// Defined in receiver.h |
30 | 33 |
// #define THR_INDEX 0 |
31 | 34 |
// #define AIL_INDEX 1 |
32 | 35 |
|
33 | 36 |
// Note: arr[0] is thr, arr[1] is ail |
34 |
static volatile unsigned long start_time[2]; |
|
35 |
static volatile unsigned long rc_value[2]; |
|
36 | 37 |
|
38 |
static volatile unsigned long up_switch_time[2]; |
|
39 |
static volatile unsigned long down_switch_time[2]; |
|
40 |
static volatile unsigned long rc_value[2]; |
|
37 | 41 |
|
42 |
// NOTE THAT WE ARE ASSUMING: |
|
43 |
// Main Loop-length is shorter than PWM pulse length. |
|
44 |
// Otherwise, you could recieve stale values in your main loop. |
|
45 |
// Loop reading rc_values must set rc_available false after reading |
|
38 | 46 |
// When our code gets really busy this will become inaccurate, |
39 | 47 |
// (i believe since micros gets shifted a bit) but for |
40 | 48 |
// the current application its easy to understand and works very well |
... | ... | |
42 | 50 |
// TODO if this starts twitching, also micros has a resolution of 4us. |
43 | 51 |
static void receiver_on_ail_interrupt() { |
44 | 52 |
if(digitalRead(AIL_RECEIVER_PIN) == HIGH) { |
45 |
start_time[AIL_INDEX] = micros(); |
|
53 |
// High Received |
|
54 |
if((micros() - up_switch_time[AIL_INDEX] > PWM_TIME - PWM_THRESH) && |
|
55 |
(micros() - up_switch_time[AIL_INDEX] < PWM_TIME + PWM_THRESH)) { |
|
56 |
// TODO this prevent an instantaneous start-up error, where the |
|
57 |
// above condition is true, and up_switch_time |
|
58 |
if((down_switch_time[AIL_INDEX] > up_switch_time[AIL_INDEX]) && |
|
59 |
(rc_available[AIL_INDEX] == 0)) { |
|
60 |
rc_value[AIL_INDEX] = (down_switch_time[AIL_INDEX] - |
|
61 |
up_switch_time[AIL_INDEX]); |
|
62 |
rc_available[AIL_INDEX] = 1; |
|
63 |
if(rc_value[AIL_INDEX]/(PWM_TIME/10) >= 1 || rc_value[AIL_INDEX]/(PWM_TIME/21) == 0) |
|
64 |
rc_available[AIL_INDEX] == 0; |
|
65 |
} |
|
66 |
} |
|
67 |
up_switch_time[AIL_INDEX] = micros(); |
|
46 | 68 |
} else { |
47 |
if(start_time[AIL_INDEX] && (rc_available[AIL_INDEX] == 0)) { |
|
48 |
rc_value[AIL_INDEX] = (micros() - start_time[AIL_INDEX]); |
|
49 |
start_time[AIL_INDEX] = 0; |
|
50 |
rc_available[AIL_INDEX] = 1; |
|
69 |
// Low received |
|
70 |
if(up_switch_time[AIL_INDEX]) { |
|
71 |
down_switch_time[AIL_INDEX] = micros(); |
|
51 | 72 |
} |
52 | 73 |
} |
53 | 74 |
} |
54 | 75 |
|
55 | 76 |
static void receiver_on_thr_interrupt() { |
56 | 77 |
if(digitalRead(THR_RECEIVER_PIN) == HIGH) { |
57 |
start_time[THR_INDEX] = micros(); |
|
78 |
// High Received |
|
79 |
if((micros() - up_switch_time[THR_INDEX] > PWM_TIME - PWM_THRESH) && |
|
80 |
(micros() - up_switch_time[THR_INDEX] < PWM_TIME + PWM_THRESH)) { |
|
81 |
// TODO this prevent an instantaneous start-up error, where the |
|
82 |
// above condition is true, and up_switch_time |
|
83 |
if((down_switch_time[THR_INDEX] > up_switch_time[THR_INDEX]) && |
|
84 |
(rc_available[THR_INDEX] == 0)) { |
|
85 |
rc_value[THR_INDEX] = (down_switch_time[THR_INDEX] - |
|
86 |
up_switch_time[THR_INDEX]); |
|
87 |
rc_available[THR_INDEX] = 1; |
|
88 |
if(rc_value[THR_INDEX]/(PWM_TIME/10) >= 1 || rc_value[THR_INDEX]/(PWM_TIME/21) == 0) |
|
89 |
rc_available[THR_INDEX] == 0; |
|
90 |
} |
|
91 |
} |
|
92 |
up_switch_time[THR_INDEX] = micros(); |
|
58 | 93 |
} else { |
59 |
if(start_time[THR_INDEX] && (rc_available[THR_INDEX] == 0)) { |
|
60 |
rc_value[THR_INDEX] = (int)(micros() - start_time[THR_INDEX]); |
|
61 |
start_time[THR_INDEX] = 0; |
|
62 |
rc_available[THR_INDEX] = 1; |
|
94 |
// Low received |
|
95 |
if(up_switch_time[THR_INDEX]) { |
|
96 |
down_switch_time[THR_INDEX] = micros(); |
|
63 | 97 |
} |
64 | 98 |
} |
65 | 99 |
} |
66 | 100 |
|
67 | 101 |
// Returns error code |
68 | 102 |
int receiver_init() { |
103 |
up_switch_time[0] = 0; |
|
104 |
up_switch_time[1] = 0; |
|
105 |
down_switch_time[0] = 0; |
|
106 |
down_switch_time[1] = 0; |
|
69 | 107 |
attachInterrupt(THR_RECEIVER_INT, receiver_on_thr_interrupt, CHANGE); |
70 | 108 |
attachInterrupt(AIL_RECEIVER_INT, receiver_on_ail_interrupt, CHANGE); |
71 | 109 |
} |
... | ... | |
76 | 114 |
// TODO measure throttle positions. |
77 | 115 |
int receiver_get_angle(int index) { |
78 | 116 |
// Math to convert nThrottleIn to 0-180. |
79 |
int ret_val = (rc_value[index]-AIL_RIGHTMOST)*3/17; |
|
117 |
int ret_val = (int)(rc_value[index]-AIL_RIGHTMOST)*3/17;
|
|
80 | 118 |
rc_available[index] = 0; |
81 | 119 |
return ret_val; |
82 | 120 |
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
Also available in: Unified diff