robobuggy / arduino / InterruptSingleChannel / receiver.c @ 0c873a67
History | View | Annotate | Download (1.98 KB)
1 |
/**
|
---|---|
2 |
* @file receiver.cpp
|
3 |
* @brief Contains Code for dealing with RC Receiver
|
4 |
*/
|
5 |
//#include <stdint.h>
|
6 |
#include <Arduino.h> |
7 |
#include "receiver.h" |
8 |
|
9 |
|
10 |
#define AIL_LEFTMOST 2000 |
11 |
#define AIL_RIGHTMOST 980 |
12 |
#define AIL_CENTERMOST 1480 |
13 |
|
14 |
#define THR_LEFTMOST -1 |
15 |
#define THR_RIGHTMOST -1 |
16 |
#define THR_CENTERMOST -1 |
17 |
|
18 |
#define AIL_RECEIVER_PIN 3 |
19 |
#define AIL_RECEIVER_INT 1 |
20 |
|
21 |
#define THR_RECEIVER_PIN 2 |
22 |
#define THR_RECEIVER_INT 0 |
23 |
|
24 |
//#define THR_INDEX 0
|
25 |
//#define AIL_INDEX 1
|
26 |
|
27 |
// Note: arr[0] is thr, arr[1] is ail
|
28 |
static volatile int start_time[2]; |
29 |
static volatile int rc_value[2]; |
30 |
|
31 |
|
32 |
// When our code gets really busy this will become inaccurate,
|
33 |
// (i believe since micros gets shifted a bit) but for
|
34 |
// the current application its easy to understand and works very well
|
35 |
// TODO if things start twitching, move to using registers directly.
|
36 |
static void receiver_on_ail_interrupt() { |
37 |
if(digitalRead(AIL_RECEIVER_PIN) == HIGH) {
|
38 |
start_time[AIL_INDEX] = micros(); |
39 |
} else {
|
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;
|
44 |
} |
45 |
} |
46 |
} |
47 |
|
48 |
static void receiver_on_thr_interrupt() { |
49 |
if(digitalRead(THR_RECEIVER_PIN) == HIGH) {
|
50 |
start_time[THR_INDEX] = micros(); |
51 |
} else {
|
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;
|
56 |
} |
57 |
} |
58 |
} |
59 |
|
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); |
64 |
} |
65 |
|
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) { |
71 |
// Math to convert nThrottleIn to 0-180.
|
72 |
int ret_val = (rc_value[index]-AIL_RIGHTMOST)*3/17; |
73 |
rc_available[index] = 0;
|
74 |
return ret_val;
|
75 |
} |
76 |
|