From 2ee946c3586ed60bb14833eda697dd8842d26168 Mon Sep 17 00:00:00 2001 From: Michel Pastor Date: Thu, 28 Jun 2018 01:11:52 +0200 Subject: [PATCH] Fix battery values Allow negative values of power, mAh drawn and mWh drawn --- _locales/en/messages.json | 14 ++++++--- js/msp/MSPHelper.js | 63 ++++++++++++++++++++++++++------------- tabs/setup.html | 10 +++++-- tabs/setup.js | 12 ++++---- 4 files changed, 67 insertions(+), 32 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index e47ff905..d81152fe 100755 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -406,7 +406,7 @@ "initialSetupInfoHead": { "message": "Info" }, - "initialSetupBattery": { + "initialSetupBatteryVoltage": { "message": "Battery voltage:" }, "initialSetupBatteryDetectedCells": { @@ -445,19 +445,25 @@ "initialSetup_Wh_drawnValue": { "message": "$1 Wh" }, - "initialSetupBatteryValue": { + "initialSetupBatteryVoltageValue": { "message": "$1 V" }, "initialSetupDrawn": { "message": "Capacity drawn:" }, - "initialSetupDrawing": { + "initialSetupCurrentDraw": { "message": "Current draw:" }, + "initialSetupPowerDraw": { + "message": "Power draw:" + }, + "initialSetupPowerDrawValue": { + "message": "$1 W" + }, "initialSetupBatteryMahValue": { "message": "$1 mAh" }, - "initialSetupBatteryAValue": { + "initialSetupCurrentDrawValue": { "message": "$1 A" }, "initialSetupRSSI": { diff --git a/js/msp/MSPHelper.js b/js/msp/MSPHelper.js index 50c6aaae..3e9406dc 100644 --- a/js/msp/MSPHelper.js +++ b/js/msp/MSPHelper.js @@ -251,26 +251,49 @@ var mspHelper = (function (gui) { ANALOG.amperage = data.getInt16(5, true) / 100; // A break; case MSPCodes.MSPV2_INAV_ANALOG: - ANALOG.voltage = data.getUint16(offset, true) / 100.0; - offset += 2; - ANALOG.cell_count = data.getUint8(offset++); - ANALOG.battery_percentage = data.getUint8(offset++); - ANALOG.power = data.getUint16(offset, true); - offset += 2; - ANALOG.mAhdrawn = data.getUint16(offset, true); - offset += 2; - ANALOG.mWhdrawn = data.getUint16(offset, true); - offset += 2; - ANALOG.rssi = data.getUint16(offset, true); // 0-1023 - offset += 2; - ANALOG.amperage = data.getInt16(offset, true) / 100; // A - offset += 2; - var battery_flags = data.getUint8(offset++); - ANALOG.battery_full_when_plugged_in = (battery_flags & 1 ? true : false); - ANALOG.use_capacity_thresholds = ((battery_flags & 2) >> 1 ? true : false); - ANALOG.battery_state = (battery_flags & 12) >> 2; - ANALOG.battery_remaining_capacity = data.getUint32(offset, true); - offset += 4; + if (semver.gte(CONFIG.flightControllerVersion, "2.0.0")) { + var tmp = data.getUint8(offset++); + ANALOG.battery_full_when_plugged_in = (tmp & 1 ? true : false); + ANALOG.use_capacity_thresholds = ((tmp & 2) >> 1 ? true : false); + ANALOG.battery_state = (tmp & 12) >> 2; + ANALOG.cell_count = (tmp & 0xF0) >> 4; + ANALOG.voltage = data.getUint16(offset, true) / 100.0; + offset += 2; + ANALOG.amperage = data.getInt16(offset, true) / 100; // A + offset += 2; + ANALOG.power = data.getInt32(offset, true) / 100.0; + offset += 4; + ANALOG.mAhdrawn = data.getInt32(offset, true); + offset += 4; + ANALOG.mWhdrawn = data.getInt32(offset, true); + offset += 4; + ANALOG.battery_remaining_capacity = data.getUint32(offset, true); + offset += 4; + ANALOG.battery_percentage = data.getUint8(offset++); + ANALOG.rssi = data.getUint16(offset, true); // 0-1023 + offset += 2; + } else { + ANALOG.voltage = data.getUint16(offset, true) / 100.0; + offset += 2; + ANALOG.cell_count = data.getUint8(offset++); + ANALOG.battery_percentage = data.getUint8(offset++); + ANALOG.power = data.getUint16(offset, true); + offset += 2; + ANALOG.mAhdrawn = data.getUint16(offset, true); + offset += 2; + ANALOG.mWhdrawn = data.getUint16(offset, true); + offset += 2; + ANALOG.rssi = data.getUint16(offset, true); // 0-1023 + offset += 2; + ANALOG.amperage = data.getInt16(offset, true) / 100; // A + offset += 2; + var battery_flags = data.getUint8(offset++); + ANALOG.battery_full_when_plugged_in = (battery_flags & 1 ? true : false); + ANALOG.use_capacity_thresholds = ((battery_flags & 2) >> 1 ? true : false); + ANALOG.battery_state = (battery_flags & 12) >> 2; + ANALOG.battery_remaining_capacity = data.getUint32(offset, true); + offset += 4; + } //noinspection JSValidateTypes dataHandler.analog_last_received_timestamp = Date.now(); break; diff --git a/tabs/setup.html b/tabs/setup.html index 34c6f752..daf2400a 100644 --- a/tabs/setup.html +++ b/tabs/setup.html @@ -94,7 +94,7 @@ 0 - + 0 V @@ -114,8 +114,12 @@ 0 - - 0.00 A + + 0.00 A + + + + 0.00 W diff --git a/tabs/setup.js b/tabs/setup.js index 6946e162..8ef18505 100755 --- a/tabs/setup.js +++ b/tabs/setup.js @@ -93,8 +93,9 @@ TABS.setup.initialize = function (callback) { bat_thresh_e = $('.bat-thresh'), bat_full_e = $('.bat-full'), bat_mah_drawn_e = $('.bat-mah-drawn'), - bat_mwh_drawn_e = $('.bat-mwh-drawn'), - bat_mah_drawing_e = $('.bat-mah-drawing'), + bat_wh_drawn_e = $('.bat-mwh-drawn'), + bat_current_draw_e = $('.bat-current-draw'), + bat_power_draw_e = $('.bat-power-draw'), rssi_e = $('.rssi'), gpsFix_e = $('.gpsFixType'), gpsSats_e = $('.gpsSats'), @@ -155,7 +156,7 @@ TABS.setup.initialize = function (callback) { helper.interval.add('gui_analog_update', function () { bat_cells_e.text(chrome.i18n.getMessage('initialSetupBatteryDetectedCellsValue', [ANALOG.cell_count])); - bat_voltage_e.text(chrome.i18n.getMessage('initialSetupBatteryValue', [ANALOG.voltage])); + bat_voltage_e.text(chrome.i18n.getMessage('initialSetupBatteryVoltageValue', [ANALOG.voltage])); remaining_capacity_wh_decimals = ANALOG.battery_remaining_capacity.toString().length < 5 ? 3 : (7 - ANALOG.battery_remaining_capacity.toString().length); remaining_capacity_value = MISC.battery_capacity_unit == 'mAh' ? ANALOG.battery_remaining_capacity : (ANALOG.battery_remaining_capacity / 1000).toFixed(remaining_capacity_wh_decimals < 0 ? 0 : remaining_capacity_wh_decimals); remaining_capacity_unit = MISC.battery_capacity_unit == 'mAh' ? 'mAh' : 'Wh'; @@ -165,8 +166,9 @@ TABS.setup.initialize = function (callback) { bat_thresh_e.text(chrome.i18n.getMessage('initialSetupBatteryThresholdsValue', [ANALOG.use_capacity_thresholds])); bat_mah_drawn_e.text(chrome.i18n.getMessage('initialSetupBatteryMahValue', [ANALOG.mAhdrawn])); capacity_drawn_decimals = ANALOG.mWhdrawn.toString().length < 5 ? 3 : (7 - ANALOG.mWhdrawn.toString().length); - bat_mwh_drawn_e.text(chrome.i18n.getMessage('initialSetup_Wh_drawnValue', [(ANALOG.mWhdrawn / 1000).toFixed(capacity_drawn_decimals < 0 ? 0 : capacity_drawn_decimals)])); - bat_mah_drawing_e.text(chrome.i18n.getMessage('initialSetupBatteryAValue', [ANALOG.amperage.toFixed(2)])); + bat_wh_drawn_e.text(chrome.i18n.getMessage('initialSetup_Wh_drawnValue', [(ANALOG.mWhdrawn / 1000).toFixed(capacity_drawn_decimals < 0 ? 0 : capacity_drawn_decimals)])); + bat_current_draw_e.text(chrome.i18n.getMessage('initialSetupCurrentDrawValue', [ANALOG.amperage.toFixed(2)])); + bat_power_draw_e.text(chrome.i18n.getMessage('initialSetupPowerDrawValue', [ANALOG.power.toFixed(2)])); rssi_e.text(chrome.i18n.getMessage('initialSetupRSSIValue', [((ANALOG.rssi / 1023) * 100).toFixed(0)])); }, 100, true);