-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRC_Reader.cpp
82 lines (69 loc) · 1.79 KB
/
RC_Reader.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
79
80
81
82
//
//
//
#include "RC_Reader.h"
void RC_Reader::init()
{
sBus.begin();
lastThrottle = 0;
lastPitch = 0;
lastRoll = 0;
lastYaw = 0;
lastOn = 0;
}
void RC_Reader::readRc(int16_t *pTarget){
sBus.ProcessInput();
if (sBus.IsDataAvailable())
{
int16_t pChannelData[7];
uint8_t nStatusByte;
sBus.FetchChannelData(pChannelData, nStatusByte);
if (nStatusByte == 0){
int roll = pChannelData[0];
int pitch = pChannelData[1];
int throttle = pChannelData[2];
int yaw = pChannelData[3];
int on = pChannelData[4];
//Read and normalize throttle
if (throttle > 171 && throttle < 1812){
lastThrottle = NormalizeThrottle(throttle);
}
lastRoll = NormalizeRoll(roll);
lastPitch = NormalizePitch(pitch);
lastYaw = NormalizeYaw(yaw);
DEBUG.print("On: ");
DEBUG.println(on);
lastOn = NormalizeOnOffSwitch(on);
}
}
pTarget[0] = lastRoll;
pTarget[1] = lastPitch;
pTarget[2] = lastThrottle;
pTarget[3] = lastYaw;
pTarget[4] = lastOn;
}
float RC_Reader::NormalizeThrottle(int16_t iInput)
{
uint16_t constrained = constrain(iInput, 172, 1812);
return map(constrained, 172, 1812, 151, 220);
}
float RC_Reader::NormalizePitch(int16_t iInput)
{
uint16_t constrained = constrain(iInput, 172, 1811);
return map(constrained, 172, 1811, -100, 100);
}
float RC_Reader::NormalizeRoll(int16_t iInput)
{
uint16_t constrained = constrain(iInput, 176, 1811);
return map(constrained, 176, 1811, -100, 100);
}
float RC_Reader::NormalizeYaw(int16_t iInput)
{
uint16_t constrained = constrain(iInput, 172, 1811);
return map(constrained, 172, 1811, -100, 100);
}
float RC_Reader::NormalizeOnOffSwitch(int16_t iInput)
{
uint16_t constrained = constrain(iInput, 172, 1811);
return map(constrained, 172, 1811, 0, 1);
}