robobuggy / arduino / RadioBuggyMega / receiver.c @ 5ea63f0d
History | View | Annotate | Download (4.12 KB)
1 |
/**
|
---|---|
2 |
* @file receiver.c
|
3 |
* @brief Contains Code for dealing with RC Receiver
|
4 |
* NOTE: AIL and THR are flipped.
|
5 |
* AIL is on pin 2
|
6 |
* THR is on pin 3
|
7 |
*
|
8 |
* @author Matt Sebek (msebek)
|
9 |
* @author Zach Dawson (zsd)
|
10 |
*/
|
11 |
#include <Arduino.h> |
12 |
#include "receiver.h" |
13 |
|
14 |
|
15 |
#define AIL_LEFTMOST 2000 |
16 |
#define AIL_RIGHTMOST 980 |
17 |
#define AIL_CENTERMOST 1480 |
18 |
|
19 |
#define THR_LEFTMOST -1 |
20 |
#define THR_RIGHTMOST -1 |
21 |
#define THR_CENTERMOST -1 |
22 |
|
23 |
#define AIL_RECEIVER_PIN 2 |
24 |
#define AIL_RECEIVER_INT 0 |
25 |
|
26 |
#define THR_RECEIVER_PIN 3 |
27 |
#define THR_RECEIVER_INT 1 |
28 |
|
29 |
//For 72 MHz RC reciever
|
30 |
//#define PWM_TIME 21800
|
31 |
//#define PWM_THRESH 130
|
32 |
//#define BIG_PULSE 10
|
33 |
//High pulse lasts over 1/10 of period
|
34 |
//#define SHORT_PULSE 21
|
35 |
//High pulse lasts under 1/21 of period
|
36 |
|
37 |
//For 2.4 GHz racecar reciever
|
38 |
#define PWM_TIME 18370 |
39 |
#define PWM_THRESH 130 |
40 |
#define BIG_PULSE 8 |
41 |
//High pulse lasts over 1/8 of period
|
42 |
#define SHORT_PULSE 19 |
43 |
//High pulse lasts under 1/19 of period
|
44 |
|
45 |
// Defined in receiver.h
|
46 |
// #define THR_INDEX 0
|
47 |
// #define AIL_INDEX 1
|
48 |
|
49 |
// Note: arr[0] is thr, arr[1] is ail
|
50 |
|
51 |
static volatile unsigned long up_switch_time[2]; |
52 |
static volatile unsigned long down_switch_time[2]; |
53 |
static volatile unsigned long rc_value[2]; |
54 |
|
55 |
// NOTE THAT WE ARE ASSUMING:
|
56 |
// Main Loop-length is shorter than PWM pulse length.
|
57 |
// Otherwise, you could recieve stale values in your main loop.
|
58 |
// Loop reading rc_values must set rc_available false after reading
|
59 |
// When our code gets really busy this will become inaccurate,
|
60 |
// (i believe since micros gets shifted a bit) but for
|
61 |
// the current application its easy to understand and works very well
|
62 |
// TODO if things start twitching, move to using registers directly.
|
63 |
// TODO if this starts twitching, also micros has a resolution of 4us.
|
64 |
static void receiver_on_ail_interrupt() { |
65 |
if(digitalRead(AIL_RECEIVER_PIN) == HIGH) {
|
66 |
// High Received
|
67 |
if((micros() - up_switch_time[AIL_INDEX] > PWM_TIME - PWM_THRESH) &&
|
68 |
(micros() - up_switch_time[AIL_INDEX] < PWM_TIME + PWM_THRESH)) { |
69 |
// TODO this prevent an instantaneous start-up error, where the
|
70 |
// above condition is true, and up_switch_time
|
71 |
if((down_switch_time[AIL_INDEX] > up_switch_time[AIL_INDEX]) &&
|
72 |
(rc_available[AIL_INDEX] == 0)) {
|
73 |
rc_value[AIL_INDEX] = (down_switch_time[AIL_INDEX] - |
74 |
up_switch_time[AIL_INDEX]); |
75 |
rc_available[AIL_INDEX] = 1;
|
76 |
if(rc_value[AIL_INDEX]/(PWM_TIME/BIG_PULSE) >= 1 || rc_value[AIL_INDEX]/(PWM_TIME/SHORT_PULSE) == 0) |
77 |
rc_available[AIL_INDEX] == 0;
|
78 |
} |
79 |
} |
80 |
up_switch_time[AIL_INDEX] = micros(); |
81 |
} else {
|
82 |
// Low received
|
83 |
if(up_switch_time[AIL_INDEX]) {
|
84 |
down_switch_time[AIL_INDEX] = micros(); |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
static void receiver_on_thr_interrupt() { |
90 |
if(digitalRead(THR_RECEIVER_PIN) == HIGH) {
|
91 |
// High Received
|
92 |
if((micros() - up_switch_time[THR_INDEX] > PWM_TIME - PWM_THRESH) &&
|
93 |
(micros() - up_switch_time[THR_INDEX] < PWM_TIME + PWM_THRESH)) { |
94 |
// TODO this prevent an instantaneous start-up error, where the
|
95 |
// above condition is true, and up_switch_time
|
96 |
if((down_switch_time[THR_INDEX] > up_switch_time[THR_INDEX]) &&
|
97 |
(rc_available[THR_INDEX] == 0)) {
|
98 |
rc_value[THR_INDEX] = (down_switch_time[THR_INDEX] - |
99 |
up_switch_time[THR_INDEX]); |
100 |
rc_available[THR_INDEX] = 1;
|
101 |
if(rc_value[THR_INDEX]/(PWM_TIME/10) >= 1 || rc_value[THR_INDEX]/(PWM_TIME/21) == 0) |
102 |
rc_available[THR_INDEX] == 0;
|
103 |
} |
104 |
} |
105 |
up_switch_time[THR_INDEX] = micros(); |
106 |
} else {
|
107 |
// Low received
|
108 |
if(up_switch_time[THR_INDEX]) {
|
109 |
down_switch_time[THR_INDEX] = micros(); |
110 |
} |
111 |
} |
112 |
} |
113 |
|
114 |
// Returns error code
|
115 |
int receiver_init() {
|
116 |
up_switch_time[0] = 0; |
117 |
up_switch_time[1] = 0; |
118 |
down_switch_time[0] = 0; |
119 |
down_switch_time[1] = 0; |
120 |
attachInterrupt(THR_RECEIVER_INT, receiver_on_thr_interrupt, CHANGE); |
121 |
attachInterrupt(AIL_RECEIVER_INT, receiver_on_ail_interrupt, CHANGE); |
122 |
} |
123 |
|
124 |
|
125 |
// Index = 0 to check thr, index = 1 to check
|
126 |
// Returns 0 to 180, with 90 being center.
|
127 |
// TODO measure throttle positions.
|
128 |
int receiver_get_angle(int index) { |
129 |
// Math to convert nThrottleIn to 0-180.
|
130 |
int ret_val = (int)(rc_value[index]-AIL_RIGHTMOST)*3/17; |
131 |
rc_available[index] = 0;
|
132 |
return ret_val;
|
133 |
} |
134 |
|
135 |
|
136 |
|
137 |
|