-
Notifications
You must be signed in to change notification settings - Fork 428
/
AtCommandCreator.js
188 lines (156 loc) · 4.48 KB
/
AtCommandCreator.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var AtCommand = require('./AtCommand');
var at = require('./at');
var exports = module.exports = AtCommandCreator;
function AtCommandCreator() {
}
AtCommandCreator.prototype.raw = function(type, args, blocks, options, callback) {
args = (Array.isArray(args))
? args
: Array.prototype.slice.call(arguments, 1);
return new AtCommand(type, args, blocks, options, callback);
};
AtCommandCreator.prototype.ctrl = function(controlMode, otherMode) {
var args = [controlMode, otherMode];
return this.raw('CTRL', args);
};
// Used for fly/land as well as emergency trigger/recover
AtCommandCreator.prototype.ref = function(options) {
options = options || {};
var args = [0];
if (options.fly) {
args[0] = args[0] | REF_FLAGS.takeoff;
}
if (options.emergency) {
args[0] = args[0] | REF_FLAGS.emergency;
}
return this.raw('REF', args);
};
// Used to fly the drone around
AtCommandCreator.prototype.pcmd = function(options) {
options = options || {};
// flags, leftRight, frontBack, upDown, clockWise
var args = [0, 0, 0, 0, 0];
for (var key in options) {
var alias = PCMD_ALIASES[key];
var value = options[key];
if (alias.invert) {
value = -value;
}
args[alias.index] = at.floatString(value);
args[0] = args[0] | PCMD_FLAGS.progressive;
}
return this.raw('PCMD', args);
};
AtCommandCreator.prototype.calibrate = function(device_num) {
var args = [device_num];
return this.raw('CALIB', args);
};
// Can be called either as config(key, value, callback) or
// config(options, callback) where options has at least keys "key" and
// "value" (but may also have other keys).
AtCommandCreator.prototype.config = function(key, value, callback) {
var options;
if (key && typeof(key) === 'object') {
// Called with signature (options, callback).
options = key;
callback = value;
key = options.key;
value = options.value;
} else {
options = {};
}
return this.raw('CONFIG', ['"' + key + '"', '"' + value + '"'], true, options, callback);
};
AtCommandCreator.prototype.ctrl = function(a, b) {
return this.raw('CTRL', [a, b]);
};
// TMP: Used to send FTRIM
AtCommandCreator.prototype.ftrim = function() {
return this.raw('FTRIM');
};
AtCommandCreator.prototype.animateLeds = function(name, hz, duration) {
// Default animation
name = name || 'redSnake';
hz = hz || 2;
duration = duration || 3;
var animationId = LED_ANIMATIONS.indexOf(name);
if (animationId < 0) {
throw new Error('Unknown led animation: ' + name);
}
hz = at.floatString(hz);
var params = [animationId, hz, duration].join(',');
return this.config('leds:leds_anim', params);
};
AtCommandCreator.prototype.animate = function(name, duration) {
var animationId = ANIMATIONS.indexOf(name);
if (animationId < 0) {
throw new Error('Unknown animation: ' + name);
}
var params = [animationId, duration].join(',');
return this.config('control:flight_anim', params);
};
// Constants
var REF_FLAGS = exports.REF_FLAGS = {
emergency : (1 << 8),
takeoff : (1 << 9),
};
var PCMD_FLAGS = exports.PCMD_FLAGS = {
progressive : (1 << 0),
};
var PCMD_ALIASES = exports.PCMD_ALIASES = {
left : {index: 1, invert: true},
right : {index: 1, invert: false},
front : {index: 2, invert: true},
back : {index: 2, invert: false},
up : {index: 3, invert: false},
down : {index: 3, invert: true},
clockwise : {index: 4, invert: false},
counterClockwise : {index: 4, invert: true},
};
// from ARDrone_SDK_2_0/ControlEngine/iPhone/Release/ARDroneGeneratedTypes.h
var LED_ANIMATIONS = exports.LED_ANIMATIONS = [
'blinkGreenRed',
'blinkGreen',
'blinkRed',
'blinkOrange',
'snakeGreenRed',
'fire',
'standard',
'red',
'green',
'redSnake',
'blank',
'rightMissile',
'leftMissile',
'doubleMissile',
'frontLeftGreenOthersRed',
'frontRightGreenOthersRed',
'rearRightGreenOthersRed',
'rearLeftGreenOthersRed',
'leftGreenRightRed',
'leftRedRightGreen',
'blinkStandard',
];
// from ARDrone_SDK_2_0/ControlEngine/iPhone/Release/ARDroneGeneratedTypes.h
var ANIMATIONS = exports.ANIMATIONS = [
'phiM30Deg',
'phi30Deg',
'thetaM30Deg',
'theta30Deg',
'theta20degYaw200deg',
'theta20degYawM200deg',
'turnaround',
'turnaroundGodown',
'yawShake',
'yawDance',
'phiDance',
'thetaDance',
'vzDance',
'wave',
'phiThetaMixed',
'doublePhiThetaMixed',
'flipAhead',
'flipBehind',
'flipLeft',
'flipRight',
];