robobuggy / arduino / RadioBuggyMega / receiver.c @ 0c873a67
History | View | Annotate | Download (2.05 KB)
1 |
/**
|
---|---|
2 |
* @file receiver.cpp
|
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 |
//#include <stdint.h>
|
9 |
#include <Arduino.h> |
10 |
#include "receiver.h" |
11 |
|
12 |
|
13 |
#define AIL_LEFTMOST 2000 |
14 |
#define AIL_RIGHTMOST 980 |
15 |
#define AIL_CENTERMOST 1480 |
16 |
|
17 |
#define THR_LEFTMOST -1 |
18 |
#define THR_RIGHTMOST -1 |
19 |
#define THR_CENTERMOST -1 |
20 |
|
21 |
#define AIL_RECEIVER_PIN 3 |
22 |
#define AIL_RECEIVER_INT 1 |
23 |
|
24 |
#define THR_RECEIVER_PIN 2 |
25 |
#define THR_RECEIVER_INT 0 |
26 |
|
27 |
//#define THR_INDEX 0
|
28 |
//#define AIL_INDEX 1
|
29 |
|
30 |
// Note: arr[0] is thr, arr[1] is ail
|
31 |
static volatile int start_time[2]; |
32 |
static volatile int rc_value[2]; |
33 |
|
34 |
|
35 |
// When our code gets really busy this will become inaccurate,
|
36 |
// (i believe since micros gets shifted a bit) but for
|
37 |
// the current application its easy to understand and works very well
|
38 |
// TODO if things start twitching, move to using registers directly.
|
39 |
static void receiver_on_ail_interrupt() { |
40 |
if(digitalRead(AIL_RECEIVER_PIN) == HIGH) {
|
41 |
start_time[AIL_INDEX] = micros(); |
42 |
} else {
|
43 |
if(start_time[AIL_INDEX] && (rc_available[AIL_INDEX] == 0)) { |
44 |
rc_value[AIL_INDEX] = (int)(micros() - start_time[AIL_INDEX]);
|
45 |
start_time[AIL_INDEX] = 0;
|
46 |
rc_available[AIL_INDEX] = 1;
|
47 |
} |
48 |
} |
49 |
} |
50 |
|
51 |
static void receiver_on_thr_interrupt() { |
52 |
if(digitalRead(THR_RECEIVER_PIN) == HIGH) {
|
53 |
start_time[THR_INDEX] = micros(); |
54 |
} else {
|
55 |
if(start_time[THR_INDEX] && (rc_available[THR_INDEX] == 0)) { |
56 |
rc_value[THR_INDEX] = (int)(micros() - start_time[THR_INDEX]);
|
57 |
start_time[THR_INDEX] = 0;
|
58 |
rc_available[THR_INDEX] = 1;
|
59 |
} |
60 |
} |
61 |
} |
62 |
|
63 |
// Returns error code
|
64 |
int receiver_init() {
|
65 |
attachInterrupt(THR_RECEIVER_INT, receiver_on_thr_interrupt, CHANGE); |
66 |
attachInterrupt(AIL_RECEIVER_INT, receiver_on_ail_interrupt, CHANGE); |
67 |
} |
68 |
|
69 |
|
70 |
// Index = 0 to check thr, index = 1 to check
|
71 |
// Returns 0 to 180, with 90 being center.
|
72 |
// TODO measure throttle positions.
|
73 |
int receiver_get_angle(int index) { |
74 |
// Math to convert nThrottleIn to 0-180.
|
75 |
int ret_val = (rc_value[index]-AIL_RIGHTMOST)*3/17; |
76 |
rc_available[index] = 0;
|
77 |
return ret_val;
|
78 |
} |
79 |
|