OpenROV /
openrov-cockpit
| 1 | (function() |
||
| 2 | { |
||
| 3 | const ArduinoHelper = require('ArduinoHelper')(); |
||
|
0 ignored issues
–
show
Backwards Compatibility
introduced
by
Loading history...
|
|||
| 4 | const Periodic = require( 'Periodic' ); |
||
|
0 ignored issues
–
show
|
|||
| 5 | const Listener = require( 'Listener' ); |
||
|
0 ignored issues
–
show
|
|||
| 6 | |||
| 7 | // Encoding helper functions |
||
| 8 | function encode( floatIn ) |
||
| 9 | { |
||
| 10 | return parseInt( floatIn * 1000 ); |
||
| 11 | } |
||
| 12 | |||
| 13 | function decode( intIn ) |
||
| 14 | { |
||
| 15 | return ( intIn * 0.001 ); |
||
| 16 | } |
||
| 17 | |||
| 18 | class CameraServo |
||
|
0 ignored issues
–
show
|
|||
| 19 | { |
||
| 20 | constructor(name, deps) |
||
| 21 | { |
||
| 22 | deps.logger.debug( 'CameraServo plugin loaded' ); |
||
| 23 | |||
| 24 | this.globalBus = deps.globalEventLoop; |
||
| 25 | this.cockpitBus = deps.cockpit; |
||
| 26 | |||
| 27 | this.targetPos = 0; |
||
| 28 | this.targetPos_enc = 0; |
||
| 29 | this.mcuTargetPos_enc = 0; |
||
| 30 | |||
| 31 | this.settings = {}; |
||
| 32 | this.encodedSettings = {}; // Used to work around floating point settings |
||
| 33 | this.mcuSettings = {}; |
||
| 34 | |||
| 35 | var self = this; |
||
| 36 | |||
| 37 | this.SyncSettings = new Periodic( 1000, "timeout", function() |
||
| 38 | { |
||
| 39 | var synced = true; |
||
| 40 | |||
| 41 | // TODO: These should be used at compile time in autogenerated MCU constructors |
||
| 42 | // Check latest values from MCU against plugin's desired settings |
||
| 43 | |||
| 44 | // Inversion |
||
| 45 | if( self.mcuSettings.inverted !== self.settings.inverted ) |
||
| 46 | { |
||
| 47 | synced = false; |
||
| 48 | |||
| 49 | // Send inversion setting request to the MCU |
||
| 50 | var command = 'camServ_inv(' + ( self.settings.inverted ? 1 : 0 ) + ')'; |
||
| 51 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
| 52 | } |
||
| 53 | |||
| 54 | // Servo speed |
||
| 55 | if( self.mcuSettings.speed !== self.encodedSettings.speed ) |
||
| 56 | { |
||
| 57 | synced = false; |
||
| 58 | |||
| 59 | // Send speed setting request to the MCU |
||
| 60 | var command = 'camServ_spd(' + self.encodedSettings.speed + ')'; |
||
|
0 ignored issues
–
show
|
|||
| 61 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
| 62 | } |
||
| 63 | |||
| 64 | if( synced ) |
||
| 65 | { |
||
| 66 | // No need to continue |
||
| 67 | self.SyncSettings.stop(); |
||
| 68 | |||
| 69 | // Enable API now that the MCU settings are updated |
||
| 70 | self.listeners.setTargetPos.enable(); |
||
| 71 | } |
||
| 72 | }); |
||
| 73 | |||
| 74 | this.SyncTargetPosition = new Periodic( 33, "timeout", function() |
||
| 75 | { |
||
| 76 | var synced = true; |
||
| 77 | |||
| 78 | // Send target position to MCU until it responds with affirmation |
||
| 79 | if( self.mcuTargetPos_enc !== self.targetPos_enc ) |
||
| 80 | { |
||
| 81 | synced = false; |
||
| 82 | |||
| 83 | // Encode floating point position to integer representation |
||
| 84 | var command = 'camServ_tpos(' + self.targetPos_enc + ')'; |
||
| 85 | |||
| 86 | // Emit command to mcu |
||
| 87 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
| 88 | } |
||
| 89 | |||
| 90 | if( synced ) |
||
| 91 | { |
||
| 92 | // No need to continue |
||
| 93 | self.SyncTargetPosition.stop(); |
||
| 94 | } |
||
| 95 | }); |
||
| 96 | |||
| 97 | this.listeners = |
||
| 98 | { |
||
| 99 | settings: new Listener( this.globalBus, 'settings-change.cameraServo', true, function( settings ) |
||
| 100 | { |
||
| 101 | // Disable API until MCU sync achieved |
||
| 102 | self.listeners.setTargetPos.disable(); |
||
| 103 | |||
| 104 | // Apply settings |
||
| 105 | self.settings = settings.cameraServo; |
||
| 106 | |||
| 107 | // Store encoded settings |
||
| 108 | self.encodedSettings.speed = encode( self.settings.speed ); |
||
| 109 | |||
| 110 | // Enable MCU Status listener, if not already enabled |
||
| 111 | self.listeners.mcuStatus.enable(); |
||
| 112 | |||
| 113 | // Emit settings update to cockpit |
||
| 114 | self.cockpitBus.emit( 'plugin.cameraServo.settingsChange', self.settings ); |
||
| 115 | |||
| 116 | // Update the targetPos in case the ranges changed and it needs re-limiting |
||
| 117 | self.setTargetPos( self.targetPos ); |
||
| 118 | |||
| 119 | // Initiate a sync of the settings with the MCU |
||
| 120 | self.SyncSettings.start(); |
||
| 121 | }), |
||
| 122 | |||
| 123 | mcuStatus: new Listener( this.globalBus, 'mcu.status', false, function( data ) |
||
| 124 | { |
||
| 125 | // Servo inversion |
||
| 126 | if( 'camServ_inv' in data ) |
||
| 127 | { |
||
| 128 | self.mcuSettings.inverted = ( parseInt( data.camServ_inv ) == 1 ? true : false ); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Servo speed |
||
| 132 | if( 'camServ_spd' in data ) |
||
| 133 | { |
||
| 134 | self.mcuSettings.speed = parseInt( data.camServ_spd ); |
||
| 135 | } |
||
| 136 | |||
| 137 | // Servo position |
||
| 138 | if( 'camServ_pos' in data ) |
||
| 139 | { |
||
| 140 | // Convert from integer to float |
||
| 141 | var angle = decode( parseInt( data.camServ_pos ) ); |
||
| 142 | |||
| 143 | // Emit on cockpit bus for UI purposes |
||
| 144 | self.cockpitBus.emit( 'plugin.cameraServo.currentPos', angle ); |
||
| 145 | } |
||
| 146 | |||
| 147 | // Servo target position |
||
| 148 | if( 'camServ_tpos' in data ) |
||
| 149 | { |
||
| 150 | // Save encoded version for sync validation purposes |
||
| 151 | self.mcuTargetPos_enc = parseInt( data.camServ_tpos ); |
||
| 152 | |||
| 153 | // Convert from integer to float |
||
| 154 | var angle = decode( self.mcuTargetPos_enc ); |
||
|
0 ignored issues
–
show
|
|||
| 155 | |||
| 156 | // Emit the real target position on the cockpit bus for UI purposes |
||
| 157 | self.cockpitBus.emit( 'plugin.cameraServo.targetPos', angle ); |
||
| 158 | } |
||
| 159 | }), |
||
| 160 | |||
| 161 | setTargetPos: new Listener( this.cockpitBus, 'plugin.cameraServo.setTargetPos', false, function( posIn ) |
||
| 162 | { |
||
| 163 | // Validate inputs |
||
| 164 | var pos = parseInt( posIn ); |
||
| 165 | |||
| 166 | if( !isNaN( pos ) ) |
||
| 167 | { |
||
| 168 | // Set new target position |
||
| 169 | self.setTargetPos( pos ); |
||
| 170 | } |
||
| 171 | }) |
||
| 172 | } |
||
|
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: Loading history...
|
|||
| 173 | } |
||
| 174 | |||
| 175 | setTargetPos( posIn ) |
||
| 176 | { |
||
| 177 | var self = this; |
||
| 178 | |||
| 179 | // Apply position limits based on servo's range |
||
| 180 | if( posIn > self.settings.rangeMax ) |
||
| 181 | { |
||
| 182 | self.targetPos = self.settings.rangeMax; |
||
| 183 | } |
||
| 184 | else if( posIn < self.settings.rangeMin ) |
||
| 185 | { |
||
| 186 | self.targetPos = self.settings.rangeMin; |
||
| 187 | } |
||
| 188 | else |
||
| 189 | { |
||
| 190 | self.targetPos = posIn; |
||
| 191 | } |
||
| 192 | |||
| 193 | self.targetPos_enc = encode( self.targetPos ); |
||
| 194 | |||
| 195 | // Start targetPos sync, if not already running |
||
| 196 | self.SyncTargetPosition.start(); |
||
| 197 | } |
||
| 198 | |||
| 199 | start() |
||
| 200 | { |
||
| 201 | this.listeners.settings.enable(); |
||
| 202 | } |
||
| 203 | |||
| 204 | stop() |
||
| 205 | { |
||
| 206 | this.listeners.settings.disable(); |
||
| 207 | this.listeners.mcuStatus.disable(); |
||
| 208 | this.listeners.setTargetPos.disable(); |
||
| 209 | } |
||
| 210 | |||
| 211 | getSettingSchema() |
||
| 212 | { |
||
| 213 | //from http://json-schema.org/examples.html |
||
| 214 | return [{ |
||
| 215 | 'title': 'Camera Servo', |
||
| 216 | 'type': 'object', |
||
| 217 | 'id': 'cameraServo', |
||
| 218 | 'category': 'general', |
||
| 219 | |||
| 220 | 'properties': { |
||
| 221 | 'speed': { |
||
| 222 | 'title': 'Servo Speed (deg/s)', |
||
| 223 | 'type': 'number', |
||
| 224 | 'default': 45.0 |
||
| 225 | }, |
||
| 226 | 'controlSensitivity': { |
||
| 227 | 'title': 'Control Sensitivity (multiplier)', |
||
| 228 | 'type': 'number', |
||
| 229 | 'default': 1.0 |
||
| 230 | }, |
||
| 231 | 'rangeMax': { |
||
| 232 | 'title': 'Max Angle (deg)', |
||
| 233 | 'type': 'number', |
||
| 234 | 'default': 32.8 |
||
| 235 | }, |
||
| 236 | 'rangeMin': { |
||
| 237 | 'title': 'Min Angle (deg)', |
||
| 238 | 'type': 'number', |
||
| 239 | 'default': -40.6 |
||
| 240 | }, |
||
| 241 | 'stepResolution': { |
||
| 242 | 'title': 'Step Resolution (deg/step)', |
||
| 243 | 'type': 'number', |
||
| 244 | 'default': 10.0 |
||
| 245 | }, |
||
| 246 | 'inverted': { |
||
| 247 | 'title': 'Inverted', |
||
| 248 | 'type': 'boolean', |
||
| 249 | 'format': 'checkbox', |
||
| 250 | 'default': false |
||
| 251 | } |
||
| 252 | }, |
||
| 253 | |||
| 254 | // TODO: Need to classify which settings are used at various levels: MCU Model, Local Model, UI model |
||
| 255 | 'required': [ |
||
| 256 | 'speed', // Max speed of servo |
||
| 257 | 'controlSensitivity', // When doing joystick or buttonHeld controls, multiplier against servo speed for moving the targetPos |
||
| 258 | 'rangeMax', // Furthest position in positive direction |
||
| 259 | 'rangeMin', // Furthest position in negative direction |
||
| 260 | 'stepResolution', // (UI) |
||
| 261 | 'inverted' // (MCU) Used when remapping angle inputs to microseconds. Unrelated to min and max range |
||
| 262 | ] |
||
| 263 | }]; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | |||
| 268 | module.exports = function(name, deps) |
||
| 269 | { |
||
| 270 | if( process.env.PRODUCTID == "trident" ) |
||
| 271 | { |
||
| 272 | deps.logger.debug( "Camera Servo Not supported on trident" ); |
||
| 273 | return {}; |
||
| 274 | } |
||
| 275 | |||
| 276 | return new CameraServo(name, deps); |
||
| 277 | }; |
||
| 278 | }()); |