1 | 'use strict';
|
2 |
|
3 | var path = require('path')
|
4 | , normalize = require(path.join(__dirname, 'normalize'))
|
5 | ;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var COMMAND_LENGTH = 9;
|
11 |
|
12 | function 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 |
|
21 | Command.prototype.toBuffer = function () {
|
22 | var command = new Buffer(COMMAND_LENGTH);
|
23 | |
24 |
|
25 |
|
26 | command.writeUInt8(0xaa, 0);
|
27 | command.writeUInt8(0x64, 1);
|
28 | command.writeUInt8(0xbb, 8);
|
29 |
|
30 | |
31 |
|
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 |
|
43 | Command.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 |
|
52 | module.exports = Command;
|