Project

General

Profile

Statistics
| Branch: | Revision:

robobuggy / arduino / RadioBuggyMega / receiver.c @ e6c4b5e0

History | View | Annotate | Download (2.2 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 3
24
#define AIL_RECEIVER_INT 1
25

    
26
#define THR_RECEIVER_PIN 2
27
#define THR_RECEIVER_INT 0
28

    
29
// Defined in receiver.h
30
// #define THR_INDEX 0
31
// #define AIL_INDEX 1
32

    
33
// Note: arr[0] is thr, arr[1] is ail
34
static volatile unsigned long start_time[2];
35
static volatile unsigned long rc_value[2];
36

    
37

    
38
// When our code gets really busy this will become inaccurate,
39
// (i believe since micros gets shifted a bit) but for
40
// the current application its easy to understand and works very well
41
// TODO if things start twitching, move to using registers directly.
42
// TODO if this starts twitching, also micros has a resolution of 4us.
43
static void receiver_on_ail_interrupt() {
44
  if(digitalRead(AIL_RECEIVER_PIN) == HIGH) {
45
    start_time[AIL_INDEX] = micros();
46
  } else {
47
    if(start_time[AIL_INDEX] && (rc_available[AIL_INDEX] == 0)) {
48
      rc_value[AIL_INDEX] = (micros() - start_time[AIL_INDEX]);
49
      start_time[AIL_INDEX] = 0;
50
      rc_available[AIL_INDEX] = 1;
51
    }
52
  }
53
}
54

    
55
static void receiver_on_thr_interrupt() {
56
  if(digitalRead(THR_RECEIVER_PIN) == HIGH) {
57
    start_time[THR_INDEX] = micros();
58
  } else {
59
    if(start_time[THR_INDEX] && (rc_available[THR_INDEX] == 0)) {
60
      rc_value[THR_INDEX] = (int)(micros() - start_time[THR_INDEX]);
61
      start_time[THR_INDEX] = 0;
62
      rc_available[THR_INDEX] = 1;
63
    }
64
  }
65
}
66

    
67
// Returns error code
68
int receiver_init() {
69
  attachInterrupt(THR_RECEIVER_INT, receiver_on_thr_interrupt, CHANGE);
70
  attachInterrupt(AIL_RECEIVER_INT, receiver_on_ail_interrupt, CHANGE);
71
}
72

    
73

    
74
// Index = 0 to check thr, index = 1 to check
75
// Returns 0 to 180, with 90 being center.
76
// TODO measure throttle positions.
77
int receiver_get_angle(int index) {
78
  // Math to convert nThrottleIn to 0-180.
79
  int ret_val = (rc_value[index]-AIL_RIGHTMOST)*3/17;
80
  rc_available[index] = 0;
81
  return ret_val;
82
}