-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathReceiver.cpp
78 lines (65 loc) · 2.52 KB
/
Receiver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Receiver.cpp - Library for interfacing with an RC receiver
Created by Myles Grant <[email protected]>
See also: https://github.com/grantmd/QuadCopter
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WProgram.h"
#include "Definitions.h"
#include "Receiver.h"
#include "Utils.h"
Receiver::Receiver(){
// These may seem arbitrary, but they are what the AeroQuad software uses,
// so I am using the same, for consistency
//
// However, for some reason that I have not yet discovered, Yaw and Roll seem to be swapped from what my receiver is labeled
channels[THROTTLE_CHANNEL] = 4; // Throttle
channels[ROLL_CHANNEL] = 2; // Rudd. / Roll
channels[PITCH_CHANNEL] = 5; // Elev. / Pitch
channels[YAW_CHANNEL] = 6; // Aile / Yaw
channels[GEAR_CHANNEL] = 7; // Gear
channels[AUX_CHANNEL] = 8; // Aux / Flt Mode
int i;
// Assign all pins for reading
for (i=0; i<CHANNELS; i++){
pinMode(channels[i], INPUT);
}
// Init the readings at something sensible
for (i=0; i<CHANNELS; i++){
smoothed[i] = (i == THROTTLE_CHANNEL) ? 1000 : 1500;
readings[i] = smoothed[i];
}
_smoothFactor = 1.0;
}
void Receiver::updateAll(){
int temp;
int i;
// Read all the channels. Worst case, this will take 180us
for (i=0; i<CHANNELS; i++){
temp = pulseIn(channels[i], HIGH, 20000); // Attempt to read a pulse for 20us
if (temp != 0){ // A value of 0 means that the read timed out, so we keep our previous value
smoothed[i] = filterSmooth(temp, readings[i], _smoothFactor); // Apply smoothing
readings[i] = temp;
}
}
}
// TODO: Apply some sort of centering to these, since the offsets differ from receiver to receiver
int Receiver::getChannel(byte channel){
return readings[channel];
}
float Receiver::getSmoothedChannel(byte channel){
return smoothed[channel];
}
float Receiver::getAngle(byte channel){
// Scale 1000-2000 usecs to -45 to 45 degrees
return (0.09 * smoothed[channel]) - 135;
}