1 | const Periodic = require( 'Periodic' ); |
||
0 ignored issues
–
show
Backwards Compatibility
introduced
by
![]() |
|||
2 | const Listener = require( 'Listener' ); |
||
0 ignored issues
–
show
|
|||
3 | |||
4 | // Encoding helper functions |
||
5 | function encode( floatIn ) |
||
6 | { |
||
7 | return parseInt( floatIn * 1000 ); |
||
8 | } |
||
9 | |||
10 | function decode( intIn ) |
||
11 | { |
||
12 | return ( intIn * 0.001 ); |
||
13 | } |
||
14 | |||
15 | class IMU |
||
0 ignored issues
–
show
|
|||
16 | { |
||
17 | constructor( name, deps ) |
||
18 | { |
||
19 | console.log( 'IMU plugin loaded' ); |
||
20 | var self = this; |
||
21 | |||
22 | // Comm buses |
||
23 | this.globalBus = deps.globalEventLoop; |
||
24 | this.cockpitBus = deps.cockpit; |
||
25 | |||
26 | // Plugin settings |
||
27 | this.settings = {}; |
||
28 | |||
29 | // Target settings |
||
30 | this.targetRollOffset = 0; |
||
31 | this.targetRollOffset_enc = 0; |
||
32 | this.targetPitchOffset = 0; |
||
33 | this.targetPitchOffset_enc = 0; |
||
34 | |||
35 | // MCU's reported settings |
||
36 | this.mcuRollOffset_enc = NaN; |
||
37 | this.mcuPitchOffset_enc = NaN; |
||
38 | this.zeroYawAck = false; |
||
39 | |||
40 | // IMU state information |
||
41 | this.roll = 0; |
||
42 | this.pitch = 0; |
||
43 | this.yaw = 0; |
||
44 | |||
45 | // Periodic function that syncs current roll and pitch offsets with the MCU |
||
46 | this.SyncSettings = new Periodic( 100, "timeout", function() |
||
47 | { |
||
48 | let synced = true; |
||
0 ignored issues
–
show
|
|||
49 | |||
50 | // Send target roll and pitch offsets until MCU is synced |
||
51 | if( ( self.mcuRollOffset_enc !== self.targetRollOffset_enc ) || |
||
52 | ( self.mcuPitchOffset_enc !== self.targetPitchOffset_enc ) ) |
||
53 | { |
||
54 | synced = false; |
||
55 | |||
56 | // Encode floating point to integer representation |
||
57 | var command = 'imu_level(' + self.targetRollOffset_enc + ',' + self.targetPitchOffset_enc + ')'; |
||
58 | |||
59 | // Emit command to mcu |
||
60 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
61 | } |
||
62 | |||
63 | // TODO: Max Attempts |
||
64 | if( synced ) |
||
65 | { |
||
66 | // No need to continue |
||
67 | self.SyncSettings.stop(); |
||
68 | |||
69 | // Enable API |
||
70 | self.listeners.zeroRollPitch.enable(); |
||
71 | self.listeners.zeroYaw.enable(); |
||
72 | self.listeners.clearRollPitchOffsets.enable(); |
||
73 | } |
||
74 | }); |
||
75 | |||
76 | // Periodic function that commands the MCU to zero the yaw value of the IMU |
||
77 | this.SyncZeroYaw = new Periodic( 100, "timeout", () => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
78 | { |
||
79 | // TODO: Max Attempts |
||
80 | if( self.zeroYawAck !== true ) |
||
81 | { |
||
82 | // Emit command to mcu |
||
83 | var command = 'imu_zyaw()'; |
||
84 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
85 | } |
||
86 | else |
||
87 | { |
||
88 | // Stop syncing |
||
89 | self.SyncZeroYaw.stop(); |
||
90 | } |
||
91 | }); |
||
92 | |||
93 | this.listeners = |
||
94 | { |
||
95 | settings: new Listener( this.globalBus, 'settings-change.imu', true, ( settings ) => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
96 | { |
||
97 | // Apply settings |
||
98 | self.settings = settings.imu; |
||
99 | |||
100 | // Set new target positions |
||
101 | self.targetRollOffset = self.settings.rollOffset; |
||
102 | self.targetPitchOffset = self.settings.pitchOffset; |
||
103 | |||
104 | // Encode offsets |
||
105 | self.targetRollOffset_enc = encode( self.targetRollOffset ); |
||
106 | self.targetPitchOffset_enc = encode( self.targetPitchOffset ); |
||
107 | |||
108 | // Enable MCU Status listener |
||
109 | self.listeners.mcuStatus.enable(); |
||
110 | |||
111 | // Start syncing the current settings with the MCU |
||
112 | self.SyncSettings.start(); |
||
113 | }), |
||
114 | |||
115 | mcuStatus: new Listener( this.globalBus, 'mcu.status', false, ( data ) => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
116 | { |
||
117 | // Roll |
||
118 | if( 'imu_r' in data ) |
||
119 | { |
||
120 | self.roll = decode( parseInt( data.imu_r ) ); |
||
121 | self.globalBus.emit( "plugin.imu.roll", self.roll ); |
||
122 | } |
||
123 | |||
124 | // Pitch |
||
125 | if( 'imu_p' in data ) |
||
126 | { |
||
127 | self.pitch = decode( parseInt( data.imu_p ) ); |
||
128 | self.globalBus.emit( "plugin.imu.pitch", self.pitch ); |
||
129 | } |
||
130 | |||
131 | // Yaw |
||
132 | if( 'imu_y' in data ) |
||
133 | { |
||
134 | self.yaw = decode( parseInt( data.imu_y ) ); |
||
135 | self.globalBus.emit( "plugin.imu.yaw", self.yaw ); |
||
136 | } |
||
137 | |||
138 | // Roll offset |
||
139 | if( 'imu_roff' in data ) |
||
140 | { |
||
141 | self.mcuRollOffset_enc = parseInt( data.imu_roff ); |
||
142 | } |
||
143 | |||
144 | // Pitch offset |
||
145 | if( 'imu_poff' in data ) |
||
146 | { |
||
147 | self.mcuPitchOffset_enc = parseInt( data.imu_poff ); |
||
148 | } |
||
149 | |||
150 | // Yaw zero ack |
||
151 | if( 'imu_zyaw' in data ) |
||
152 | { |
||
153 | if( data.imu_zyaw == "ack" ) |
||
154 | { |
||
155 | // Done syncing |
||
156 | self.zeroYawAck = true; |
||
157 | } |
||
158 | } |
||
159 | }), |
||
160 | |||
161 | zeroRollPitch: new Listener( this.cockpitBus, 'plugin.imu.zeroRollPitch', false, () => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
162 | { |
||
163 | // Zero the roll and pitch by using current values as offsets. |
||
164 | self.setNewRollPitchOffsets(); |
||
165 | }), |
||
166 | |||
167 | clearRollPitchOffsets: new Listener( this.cockpitBus, 'plugin.imu.clearRollPitchOffsets', false, () => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
168 | { |
||
169 | // Zero the roll and pitch by using current values as offsets. |
||
170 | self.clearRollPitchOffsets(); |
||
171 | }), |
||
172 | |||
173 | zeroYaw: new Listener( this.cockpitBus, 'plugin.imu.zeroYaw', false, () => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
174 | { |
||
175 | // Have the MCU zero the yaw value |
||
176 | self.setZeroYaw(); |
||
177 | }) |
||
178 | } |
||
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: ![]() |
|||
179 | } |
||
180 | |||
181 | clearRollPitchOffsets() |
||
182 | { |
||
183 | // Clear the offsets |
||
184 | this.settings.rollOffset = 0; |
||
185 | this.settings.pitchOffset = 0; |
||
186 | |||
187 | // Update offset settings and send to settings manager, which will trigger a settings update |
||
188 | this.cockpitBus.emit( 'plugin.settings-manager.saveSettings', { imu: this.settings } ); |
||
189 | } |
||
190 | |||
191 | setNewRollPitchOffsets() |
||
192 | { |
||
193 | // Add current roll and pitch to existing offsets to mark this position as 'level' |
||
194 | this.settings.rollOffset += this.roll; |
||
195 | this.settings.pitchOffset += this.pitch; |
||
196 | |||
197 | // Update offset settings and send to settings manager, which will trigger a settings update |
||
198 | this.cockpitBus.emit( 'plugin.settings-manager.saveSettings', { imu: this.settings } ); |
||
199 | } |
||
200 | |||
201 | setZeroYaw() |
||
202 | { |
||
203 | // Reset the ack and start syncing state |
||
204 | this.zeroYawAck = false; |
||
205 | this.SyncZeroYaw.start(); |
||
206 | } |
||
207 | |||
208 | start() |
||
209 | { |
||
210 | this.listeners.settings.enable(); |
||
211 | } |
||
212 | |||
213 | stop() |
||
214 | { |
||
215 | this.listeners.settings.disable(); |
||
216 | this.listeners.mcuStatus.disable(); |
||
217 | this.listeners.zeroRollPitch.disable(); |
||
218 | this.listeners.zeroYaw.disable(); |
||
219 | this.listerners.clearRollPitchOffsets.disable(); |
||
220 | |||
221 | this.SyncSettings.stop(); |
||
222 | this.SyncZeroYaw.stop(); |
||
223 | } |
||
224 | |||
225 | getSettingSchema() |
||
226 | { |
||
227 | //from http://json-schema.org/examples.html |
||
228 | return [{ |
||
229 | 'title': 'IMU', |
||
230 | 'type': 'object', |
||
231 | 'id': 'imu', |
||
232 | 'properties': { |
||
233 | 'rollOffset': { |
||
234 | 'title': 'Roll Offset (deg)', |
||
235 | 'type': 'number', |
||
236 | 'default': 0.0 |
||
237 | }, |
||
238 | 'pitchOffset': { |
||
239 | 'title': 'Pitch Offset (deg)', |
||
240 | 'type': 'number', |
||
241 | 'default': 0.0 |
||
242 | } |
||
243 | }, |
||
244 | 'required': [ |
||
245 | "rollOffset", |
||
246 | "pitchOffset" |
||
247 | ] |
||
248 | }]; |
||
249 | } |
||
250 | } |
||
251 | |||
252 | module.exports = function(name, deps) |
||
253 | { |
||
254 | return new IMU(name, deps); |
||
255 | }; |