UNPKG

1.25 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path')
4 , normalize = require(path.join(__dirname, 'normalize'))
5 ;
6
7/*
8 * CONSTANTS
9 */
10var COMMAND_LENGTH = 9;
11
12function Command(init) {
13 if (!init) init = {};
14
15 this.rotorSpeed = normalize.vector(init.rotorSpeed || 0);
16 this.trim = normalize.axial(init.trim || 0);
17 this.yaw = normalize.axial(init.yaw || 0);
18 this.pitch = normalize.axial(init.pitch || 0);
19}
20
21Command.prototype.toBuffer = function () {
22 var command = new Buffer(COMMAND_LENGTH);
23 /*
24 * bytes 1, 2, & 9 have reserved values; 7 & 8 are unused
25 */
26 command.writeUInt8(0xaa, 0);
27 command.writeUInt8(0x64, 1);
28 command.writeUInt8(0xbb, 8);
29
30 /*
31 * Buffers can come with random crap in them; init to 0
32 */
33 for (var i = 2; i < 8; i++) command.writeUInt8(0x00, i);
34
35 command.writeUInt8(this.rotorSpeed || 0, 2);
36 command.writeUInt8(this.trim || 0, 3);
37 command.writeUInt8(this.yaw || 0, 4);
38 command.writeUInt8(this.pitch || 0, 5);
39
40 return command;
41};
42
43Command.prototype.clone = function () {
44 return new Command({
45 rotorSpeed : this.rotorSpeed,
46 trim : this.trim,
47 yaw : this.yaw,
48 pitch : this.pitch
49 });
50};
51
52module.exports = Command;