-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKKMulticopterBoard.cpp
More file actions
147 lines (128 loc) · 3.83 KB
/
Copy pathKKMulticopterBoard.cpp
File metadata and controls
147 lines (128 loc) · 3.83 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "KKMulticopterBoard.h"
KKMulticopterBoard::KKMulticopterBoard(int a_pin,
int e_pin,
int t_pin,
int r_pin)
{
_controlMax = KKMulticopterBoard_CONTROL_MAX;
_controlMin = KKMulticopterBoard_CONTROL_MIN;
_ail.attach(a_pin, _controlMin, _controlMax);
_ele.attach(e_pin, _controlMin, _controlMax);
_thr.attach(t_pin, _controlMin, _controlMax);
_rud.attach(r_pin, _controlMin, _controlMax);
idle();
_isArmed = false;
}
int KKMulticopterBoard::_getControlValue(float value)
{
return map(value * 1000, 0, 1000, _controlMin, _controlMax);
}
void KKMulticopterBoard::idle()
{
_ail.writeMicroseconds(_getControlValue(0.5));
_ele.writeMicroseconds(_getControlValue(0.5));
_thr.writeMicroseconds(_getControlValue(0.0));
_rud.writeMicroseconds(_getControlValue(0.5));
}
void KKMulticopterBoard::level()
{
_ail.writeMicroseconds(_getControlValue(0.5));
_ele.writeMicroseconds(_getControlValue(0.5));
_rud.writeMicroseconds(_getControlValue(0.5));
}
void KKMulticopterBoard::arm()
{
idle();
delay(5000);
_ail.writeMicroseconds(_getControlValue(0.5));
_ele.writeMicroseconds(_getControlValue(0.5));
_thr.writeMicroseconds(_getControlValue(0.0));
_rud.writeMicroseconds(_getControlValue(1.0));
delay(5000);
idle();
delay(5000);
_isArmed = true;
}
void KKMulticopterBoard::disarm()
{
idle();
delay(5000);
_ail.writeMicroseconds(_getControlValue(0.5));
_ele.writeMicroseconds(_getControlValue(0.5));
_thr.writeMicroseconds(_getControlValue(0.0));
_rud.writeMicroseconds(_getControlValue(0.0));
delay(5000);
idle();
delay(5000);
}
void KKMulticopterBoard::setMode(int mode)
{
if (!_isArmed) arm();
switch (mode) {
case KKMulticopterBoard_MODE_NORMAL:
setThrottle(0);
setRudder(-100);
setElevator(100);
delay(1000);
_mode = KKMulticopterBoard_MODE_NORMAL;
break;
case KKMulticopterBoard_MODE_ACRO:
setThrottle(0);
setRudder(-100);
setElevator(-100);
delay(1000);
_mode = KKMulticopterBoard_MODE_ACRO;
break;
case KKMulticopterBoard_MODE_UFO:
setThrottle(0);
setRudder(100);
setElevator(-100);
delay(1000);
_mode = KKMulticopterBoard_MODE_UFO;
break;
}
}
int KKMulticopterBoard::getMode()
{
return _mode;
}
void KKMulticopterBoard::setAileron(int value)
{
_aileron = map(value, -100, 100, _controlMin, _controlMax);
_aileron = constrain(_aileron, _controlMin, _controlMax);
_ail.writeMicroseconds(_aileron);
}
int KKMulticopterBoard::getAileron()
{
return map(_aileron, _controlMin, _controlMax, -100, 100);
}
void KKMulticopterBoard::setElevator(int value)
{
_elevator = map(value, -100, 100, _controlMin, _controlMax);
_elevator = constrain(_elevator, _controlMin, _controlMax);
_ele.writeMicroseconds(_elevator);
}
int KKMulticopterBoard::getElevator()
{
return map(_elevator, _controlMin, _controlMax, -100, 100);
}
void KKMulticopterBoard::setThrottle(int percentage)
{
_throttle = map(percentage, 0, 100, _controlMin, _controlMax);
_throttle = constrain(_throttle, _controlMin, _controlMax);
_thr.writeMicroseconds(_throttle);
}
int KKMulticopterBoard::getThrottle()
{
return map(_throttle, _controlMin, _controlMax, 0, 100);
}
void KKMulticopterBoard::setRudder(int value)
{
_rudder = map(value, -100, 100, _controlMin, _controlMax);
_rudder = constrain(_elevator, _controlMin, _controlMax);
_rud.writeMicroseconds(_rudder);
}
int KKMulticopterBoard::getRudder()
{
return map(_rudder, _controlMin, _controlMax, -100, 100);
}