robobuggy / arduino / InterruptSingleChannel / receiver.c @ c5d6b0e8
History | View | Annotate | Download (2.07 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 |
// Note: arr[0] is thr, arr[1] is ail
|
25 |
static volatile static int start_time[2]; |
26 |
static volatile bool rc_available[2]; |
27 |
static volatile int rc_value[2]; |
28 |
static volatile bool rc_connected[2]; |
29 |
|
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 |
|
36 |
// When our code gets really busy this will become inaccurate,
|
37 |
// (i believe since micros gets shifted a bit) but for
|
38 |
// 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 |
// TODO if things start twitching, move to using registers directly.
|
42 |
void receiver_on_ail_interrupt() {
|
43 |
if(digitalRead(AIL_RECEIVER_PIN) == HIGH) {
|
44 |
start_time[1] = micros();
|
45 |
} 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; |
50 |
} |
51 |
} |
52 |
} |
53 |
|
54 |
void receiver_on_thr_interrupt() {
|
55 |
if(digitalRead(THR_RECEIVER_PIN) == HIGH) {
|
56 |
start_time[0] = micros();
|
57 |
} 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; |
62 |
} |
63 |
} |
64 |
} |
65 |
|
66 |
// Returns the value, bounded by proper limits
|
67 |
int receiver_avail(int i) { |
68 |
return ;
|
69 |
} |
70 |
|
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) { |
76 |
// Math to convert nThrottleIn to 0-180.
|
77 |
bNewThrottleSignal = 0;
|
78 |
return ((nThrottleIn-RIGHTMOST)*3)/17; |
79 |
} |
80 |
|