Revision 0c873a67
Fixed up all the buggy code.
arduino/InterruptSingleChannel/receiver.c | ||
---|---|---|
21 | 21 |
#define THR_RECEIVER_PIN 2 |
22 | 22 |
#define THR_RECEIVER_INT 0 |
23 | 23 |
|
24 |
//#define THR_INDEX 0 |
|
25 |
//#define AIL_INDEX 1 |
|
26 |
|
|
24 | 27 |
// Note: arr[0] is thr, arr[1] is ail |
25 |
static volatile static int start_time[2]; |
|
26 |
static volatile bool rc_available[2]; |
|
28 |
static volatile int start_time[2]; |
|
27 | 29 |
static volatile int rc_value[2]; |
28 |
static volatile bool rc_connected[2]; |
|
29 | 30 |
|
30 |
// Returns error code |
|
31 |
int receiver_init() { |
|
32 |
attachInterrupt(THR_RECEIVER_INT, receiver_on_thr_interrupt, CHANGE); |
|
33 |
attachInterrupt(AIL_RECEIVER_INT, receiver_on_ail_interrupt, CHANGE); |
|
34 |
} |
|
35 | 31 |
|
36 | 32 |
// When our code gets really busy this will become inaccurate, |
37 | 33 |
// (i believe since micros gets shifted a bit) but for |
38 | 34 |
// the current application its easy to understand and works very well |
39 |
// Write code twice, look suspicious. Write code three times, refactor |
|
40 |
// Aileron Interrupt |
|
41 | 35 |
// TODO if things start twitching, move to using registers directly. |
42 |
void receiver_on_ail_interrupt() { |
|
36 |
static void receiver_on_ail_interrupt() {
|
|
43 | 37 |
if(digitalRead(AIL_RECEIVER_PIN) == HIGH) { |
44 |
start_time[1] = micros();
|
|
38 |
start_time[AIL_INDEX] = micros();
|
|
45 | 39 |
} else { |
46 |
if(start_time[1] && (rc_available[1] == 0)) {
|
|
47 |
rc_value[1] = (int)(micros() - ulStartPeriod);
|
|
48 |
start_time[1] = 0;
|
|
49 |
rc_available[1] = 1;
|
|
40 |
if(start_time[AIL_INDEX] && (rc_available[AIL_INDEX] == 0)) {
|
|
41 |
rc_value[AIL_INDEX] = (int)(micros() - start_time[AIL_INDEX]);
|
|
42 |
start_time[AIL_INDEX] = 0;
|
|
43 |
rc_available[AIL_INDEX] = 1;
|
|
50 | 44 |
} |
51 | 45 |
} |
52 | 46 |
} |
53 | 47 |
|
54 |
void receiver_on_thr_interrupt() { |
|
48 |
static void receiver_on_thr_interrupt() {
|
|
55 | 49 |
if(digitalRead(THR_RECEIVER_PIN) == HIGH) { |
56 |
start_time[0] = micros();
|
|
50 |
start_time[THR_INDEX] = micros();
|
|
57 | 51 |
} else { |
58 |
if(start_time[0] && (rc_available[0] == 0)) {
|
|
59 |
rc_value[0] = (int)(micros() - ulStartPeriod);
|
|
60 |
start_time[0] = 0;
|
|
61 |
rc_available[0] = 1;
|
|
52 |
if(start_time[THR_INDEX] && (rc_available[THR_INDEX] == 0)) {
|
|
53 |
rc_value[THR_INDEX] = (int)(micros() - start_time[THR_INDEX]);
|
|
54 |
start_time[THR_INDEX] = 0;
|
|
55 |
rc_available[THR_INDEX] = 1;
|
|
62 | 56 |
} |
63 | 57 |
} |
64 | 58 |
} |
65 | 59 |
|
66 |
// Returns the value, bounded by proper limits |
|
67 |
int receiver_avail(int i) { |
|
68 |
return ; |
|
60 |
// Returns error code |
|
61 |
int receiver_init() { |
|
62 |
attachInterrupt(THR_RECEIVER_INT, receiver_on_thr_interrupt, CHANGE); |
|
63 |
attachInterrupt(AIL_RECEIVER_INT, receiver_on_ail_interrupt, CHANGE); |
|
69 | 64 |
} |
70 | 65 |
|
71 |
// Gets the angle without a dead zone |
|
72 |
// Returns a value between 0 and 255, with no
|
|
73 |
// movement being 128.
|
|
74 |
// Maps this into ~0 to ~180, 90 being straight.
|
|
75 |
int receiver_get_angle(int i) { |
|
66 |
|
|
67 |
// Index = 0 to check thr, index = 1 to check
|
|
68 |
// Returns 0 to 180, with 90 being center.
|
|
69 |
// TODO measure throttle positions.
|
|
70 |
int receiver_get_angle(int index) {
|
|
76 | 71 |
// Math to convert nThrottleIn to 0-180. |
77 |
bNewThrottleSignal = 0; |
|
78 |
return ((nThrottleIn-RIGHTMOST)*3)/17; |
|
72 |
int ret_val = (rc_value[index]-AIL_RIGHTMOST)*3/17; |
|
73 |
rc_available[index] = 0; |
|
74 |
return ret_val; |
|
79 | 75 |
} |
80 | 76 |
|
Also available in: Unified diff