-
Notifications
You must be signed in to change notification settings - Fork 10
/
thyme_tas_gimbal.js
141 lines (115 loc) · 5.24 KB
/
thyme_tas_gimbal.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
// for viewpro Q30T pro 2 new
var SerialPort = require('serialport');
let gimbalPort = null;
let gimbalPortNum = gimbal.portnum;
let gimbalBaudrate = gimbal.baudrate;
const unit = 0.02197265625;
let strFromGimbal = '';
gimbalPortOpening();
function gimbalPortOpening() {
if (gimbalPort == null) {
gimbalPort = new SerialPort(gimbalPortNum, {
baudRate: parseInt(gimbalBaudrate, 10),
});
gimbalPort.on('open', gimbalPortOpen);
gimbalPort.on('close', gimbalPortClose);
gimbalPort.on('error', gimbalPortError);
gimbalPort.on('data', gimbalPortData);
} else {
if (gimbalPort.isOpen) {
} else {
gimbalPort.open();
}
}
}
function gimbalPortSend() {
let sendData = Buffer.from('3e3D003D00', 'hex');
if (gimbalPort !== null) {
gimbalPort.write(sendData, (err) => {
if (err)
return console.log('Error on write: ', err.message);
// console.log('Data Send...');
});
} else {
setTimeout(gimbalPortOpening, 2000);
}
}
setInterval(gimbalPortSend, 100);
function gimbalPortOpen() {
console.log('gimbalPort open. ' + gimbalPortNum + ' Data rate: ' + gimbalBaudrate);
}
function gimbalPortClose() {
console.log('gimbalPort closed.');
setTimeout(gimbalPortOpening, 2000);
}
function gimbalPortError(error) {
console.log('gimbalPort error : ' + error);
setTimeout(gimbalPortOpening, 2000);
}
let GimbalStatus = {};
function gimbalPortData(data) {
strFromGimbal += data.toString('hex').toLowerCase();
while (strFromGimbal.length >= 200) {
let index = 8;
let header = strFromGimbal.substring(0, index);
if (header === '3e3d3673') {
let strFromGimbal_arr = strFromGimbal.split(header);
if (strFromGimbal_arr[1].length === 110) {
let HexdataFromGimbal = header + strFromGimbal_arr[1];
// console.log('HexdataFromGimbal -', HexdataFromGimbal);
let roll = HexdataFromGimbal.substring(index, index + 36);
index += 36;
let pitch = HexdataFromGimbal.substring(index, index + 36);
index += 36;
let yaw = HexdataFromGimbal.substring(index, index + 36);
try {
let rollImuAngle = roll.substring(0, 4);
let rollRcTargetAngle = roll.substring(4, 8);
let rollStatorRelAngle = roll.substring(8, 16);
let pitchImuAngle = pitch.substring(0, 4);
let pitchRcTargetAngle = pitch.substring(4, 8);
let pitchStatorRelAngle = pitch.substring(8, 16);
let yawImuAngle = yaw.substring(0, 4);
let yawRcTargetAngle = yaw.substring(4, 8);
let yawStatorRelAngle = yaw.substring(8, 16);
rollImuAngle = Buffer.from(rollImuAngle, 'hex').readInt16LE() * unit;
rollRcTargetAngle = Buffer.from(rollRcTargetAngle, 'hex').readInt16LE() * unit;
rollStatorRelAngle = Buffer.from(rollStatorRelAngle, 'hex').readInt32LE() * unit;
GimbalStatus.roll = rollStatorRelAngle;
// console.log('rollImuAngle: ', rollImuAngle);
// console.log('rollRcTargetAngle: ', rollRcTargetAngle);
// console.log('rollStatorRelAngle: ', rollStatorRelAngle);
pitchImuAngle = Buffer.from(pitchImuAngle, 'hex').readInt16LE() * unit;
pitchRcTargetAngle = Buffer.from(pitchRcTargetAngle, 'hex').readInt16LE() * unit;
pitchStatorRelAngle = Buffer.from(pitchStatorRelAngle, 'hex').readInt32LE() * unit;
GimbalStatus.pitch = pitchStatorRelAngle;
// console.log('pitchImuAngle: ', pitchImuAngle);
// console.log('pitchRcTargetAngle: ', pitchRcTargetAngle);
// console.log('pitchStatorRelAngle: ', pitchStatorRelAngle);
yawImuAngle = Buffer.from(yawImuAngle, 'hex').readInt16LE() * unit;
yawRcTargetAngle = Buffer.from(yawRcTargetAngle, 'hex').readInt16LE() * unit;
yawStatorRelAngle = Buffer.from(yawStatorRelAngle, 'hex').readInt32LE() * unit;
GimbalStatus.yaw = yawStatorRelAngle;
// console.log('yawImuAngle: ', yawImuAngle);
// console.log('yawRcTargetAngle: ', yawRcTargetAngle);
// console.log('yawStatorRelAngle: ', yawStatorRelAngle);
mqtt_client.publish(my_gimbal_name, Buffer.from(JSON.stringify(GimbalStatus)));
sh_adn.crtci(my_gimbal_name + '?rcn=0', 0, GimbalStatus, null, function () {
});
}
catch (e) {
console.log(e);
}
}
// console.log('data: ', strFromGimbal);
GimbalStatus = {};
if (strFromGimbal.split(header).length > 2) {
strFromGimbal = strFromGimbal.split(header)[2];
} else {
strFromGimbal = '';
}
} else {
strFromGimbal = strFromGimbal.substr(2);
}
}
}