Skip to content

Commit

Permalink
Add voltage and amperage scale calibration (#1439)
Browse files Browse the repository at this point in the history
Add voltage and amperage scale calibration
  • Loading branch information
mikeller authored May 28, 2019
2 parents aa81de1 + b7f9ac0 commit 56e2ce6
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 26 deletions.
39 changes: 39 additions & 0 deletions locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3390,12 +3390,45 @@
"powerBatteryWarning": {
"message": "Warning Cell Voltage"
},
"powerCalibrationManagerButton": {
"message": "Calibration"
},
"powerCalibrationManagerHelp": {
"message": "To calibrate, use a multimeter to measure the actual voltage / current draw on your craft (with a battery plugged in), and enter the values below. Then, with the same battery still plugged in, click [Calibrate]."
},
"powerCalibrationManagerNote": {
"message": "<strong>Note:</strong> Before calibrating the scale make sure that divider and multiplier for voltage and offset for amperage is set properly.<br>Leaving the values at 0 will not apply calibration.</br><strong>Remember to remove propellers before plugging in a battery!</strong>"
},
"powerCalibrationManagerWarning": {
"message": "<span class=\"message-negative\">Warning:</span> The battery <span class=\"message-negative\">is not plugged in</span> or voltage and amperage meter sources are <span class=\"message-negative\">not set properly.</span> Make sure that the voltage and/or amperage are reading a value above 0. Otherwise you will not be able to calibrate using this tool."
},
"powerCalibrationManagerSourceNote": {
"message": "<span class=\"message-negative\">Warning:</span> Voltage and/or amperage meter sources <strong>have been changed but not saved.</strong> Please set the correct meter sources and save them before trying to calibrate."
},
"powerCalibrationSave": {
"message": "Calibrate"
},
"powerCalibrationApply": {
"message": "Apply Calibration"
},
"powerCalibrationDiscard": {
"message": "Discard Calibration"
},
"powerCalibrationConfirmHelp": {
"message": "New calibrated scales are shown here. <br>Applying them will set the scales but <strong>will not save them.</strong></br> <br>After saving make sure that the new voltage and current are correct.</br>"
},
"powerVoltageHead": {
"message": "Voltage Meter"
},
"powerVoltageValue": {
"message": "$1 V"
},
"powerVoltageCalibration": {
"message": "Measured Voltage"
},
"powerVoltageCalibratedScale": {
"message": "Calibrated Voltage Scale:"
},
"powerAmperageValue": {
"message": "$1 A"
},
Expand Down Expand Up @@ -3541,6 +3574,12 @@
"powerAmperageOffset": {
"message": "Offset [mA]"
},
"powerAmperageCalibration": {
"message": "Measured Amperage"
},
"powerAmperageCalibratedScale": {
"message": "Calibrated Amperage Scale:"
},

"powerBatteryHead": {
"message": "Battery"
Expand Down
6 changes: 5 additions & 1 deletion src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ dialog {
background-color: #fff7cd;
border: 1px solid #ffe55f;
margin-bottom: 7px;
margin-top: 3px;
margin-top: 0px;
border-radius: 3px;
font-size: 11px;
font-family: 'open_sansregular', Arial;
Expand Down Expand Up @@ -1359,6 +1359,10 @@ dialog {
box-shadow: inset 0px 1px 5px rgba(0, 0, 0, 0.35);
}

.margin-top5 {
margin-top: 5px;
}

.regular-button {
margin-top: 8px;
margin-bottom: 8px;
Expand Down
200 changes: 175 additions & 25 deletions src/js/tabs/power.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ TABS.power.initialize = function (callback) {
//googleAnalytics.sendAppView('Power');
}

if (GUI.calibrationManager) {
GUI.calibrationManager.destroy();
}
if (GUI.calibrationManagerConfirmation) {
GUI.calibrationManagerConfirmation.destroy();
}

function load_status() {
MSP.send_message(MSPCodes.MSP_STATUS, false, false, load_voltage_meters);
}
Expand Down Expand Up @@ -156,6 +163,12 @@ TABS.power.initialize = function (callback) {
$('input[name="amperagescale-' + index + '"]').val(currentDataSource[index].scale);
$('input[name="amperageoffset-' + index + '"]').val(currentDataSource[index].offset);
}

if(BATTERY_CONFIG.voltageMeterSource == 1 || BATTERY_CONFIG.currentMeterSource == 1 || BATTERY_CONFIG.currentMeterSource == 2) {
$('.calibration').show();
} else {
$('.calibration').hide();
}
}

function initDisplay() {
Expand Down Expand Up @@ -233,11 +246,14 @@ TABS.power.initialize = function (callback) {
updateDisplay(VOLTAGE_METER_CONFIGS, CURRENT_METER_CONFIGS);

var batteryMeterType_e = $('select.batterymetersource');

var sourceschanged = false;
batteryMeterType_e.val(BATTERY_CONFIG.voltageMeterSource);
batteryMeterType_e.change(function () {
BATTERY_CONFIG.voltageMeterSource = parseInt($(this).val());

updateDisplay();
sourceschanged = true;
});

var currentMeterType_e = $('select.currentmetersource');
Expand All @@ -246,6 +262,7 @@ TABS.power.initialize = function (callback) {
BATTERY_CONFIG.currentMeterSource = parseInt($(this).val());

updateDisplay();
sourceschanged = true;
});

function get_slow_data() {
Expand Down Expand Up @@ -281,6 +298,128 @@ TABS.power.initialize = function (callback) {

}

//calibration manager
var calibrationconfirmed = false;
GUI.calibrationManager = new jBox('Modal', {
width: 400,
height: 230,
closeButton: 'title',
animation: false,
attach: $('#calibrationmanager'),
title: 'Calibration Manager',
content: $('#calibrationmanagercontent'),
onCloseComplete: function() {
if (!calibrationconfirmed) {
TABS.power.initialize();
}
},
});

GUI.calibrationManagerConfirmation = new jBox('Modal', {
width: 400,
height: 230,
closeButton: 'title',
animation: false,
attach: $('#calibrate'),
title: 'Calibration Manager Confirmation',
content: $('#calibrationmanagerconfirmcontent'),
onCloseComplete: function() {
GUI.calibrationManager.close();
},
});

$('a.calibrationmanager').click(function() {
if (BATTERY_CONFIG.voltageMeterSource == 1 && BATTERY_STATE.voltage > 0.1){
$('.vbatcalibration').show();
} else {
$('.vbatcalibration').hide();
}
if ((BATTERY_CONFIG.currentMeterSource == 1 || BATTERY_CONFIG.currentMeterSource == 2) && BATTERY_STATE.amperage > 0.1) {
$('.amperagecalibration').show();
} else {
$('.amperagecalibration').hide();
}
if (BATTERY_STATE.cellCount == 0) {
$('.vbatcalibration').hide();
$('.amperagecalibration').hide();
$('.calibrate').hide();
$('.nocalib').show();
} else {
$('.calibrate').show();
$('.nocalib').hide();
}
if (sourceschanged) {
$('.srcchange').show();
$('.vbatcalibration').hide();
$('.amperagecalibration').hide();
$('.calibrate').hide();
$('.nocalib').hide();
} else {
$('.srcchange').hide();
}
});

$('input[name="vbatcalibration"]').val(0);
$('input[name="amperagecalibration"]').val(0);

var vbatscalechanged = false;
var amperagescalechanged = false;
$('a.calibrate').click(function() {
if (BATTERY_CONFIG.voltageMeterSource == 1) {
var vbatcalibration = parseFloat($('input[name="vbatcalibration"]').val());
if (vbatcalibration != 0) {
var vbatnewscale = Math.round(VOLTAGE_METER_CONFIGS[0].vbatscale * (vbatcalibration / VOLTAGE_METERS[0].voltage));
if (vbatnewscale >= 10 && vbatnewscale <= 255) {
VOLTAGE_METER_CONFIGS[0].vbatscale = vbatnewscale;
vbatscalechanged = true;
}
}
}
var ampsource = BATTERY_CONFIG.currentMeterSource;
if (ampsource == 1 || ampsource == 2) {
var amperagecalibration = parseFloat($('input[name="amperagecalibration"]').val());
var amperageoffset = CURRENT_METER_CONFIGS[ampsource - 1].offset / 1000;
if (amperagecalibration != 0) {
if (CURRENT_METERS[ampsource - 1].amperage != amperageoffset && amperagecalibration != amperageoffset) {
var amperagenewscale = Math.round(CURRENT_METER_CONFIGS[ampsource - 1].scale *
((CURRENT_METERS[ampsource - 1].amperage - amperageoffset) / (amperagecalibration - amperageoffset)));
if (amperagenewscale > -16000 && amperagenewscale < 16000 && amperagenewscale != 0) {
CURRENT_METER_CONFIGS[ampsource - 1].scale = amperagenewscale;
amperagescalechanged = true;
}
}
}
}
if (vbatscalechanged || amperagescalechanged) {
if (vbatscalechanged) {
$('.vbatcalibration').show();
} else {
$('.vbatcalibration').hide();
}
if (amperagescalechanged) {
$('.amperagecalibration').show();
} else {
$('.amperagecalibration').hide();
}

$('output[name="vbatnewscale"').val(vbatnewscale);
$('output[name="amperagenewscale"').val(amperagenewscale);

$('a.applycalibration').click(function() {
calibrationconfirmed = true;
GUI.calibrationManagerConfirmation.close();
updateDisplay(VOLTAGE_METER_CONFIGS, CURRENT_METER_CONFIGS);
$('.calibration').hide();
});

$('a.discardcalibration').click(function() {
GUI.calibrationManagerConfirmation.close();
});
} else {
GUI.calibrationManagerConfirmation.close();
}
});

$('a.save').click(function () {
for (var index = 0; index < VOLTAGE_METER_CONFIGS.length; index++) {
VOLTAGE_METER_CONFIGS[index].vbatscale = parseInt($('input[name="vbatscale-' + index + '"]').val());
Expand All @@ -298,40 +437,44 @@ TABS.power.initialize = function (callback) {
BATTERY_CONFIG.vbatwarningcellvoltage = parseFloat($('input[name="warningcellvoltage"]').val());
BATTERY_CONFIG.capacity = parseInt($('input[name="capacity"]').val());

function save_battery_config() {
MSP.send_message(MSPCodes.MSP_SET_BATTERY_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_BATTERY_CONFIG), false, save_voltage_config);
}
save_power_config();
});

function save_voltage_config() {
if (semver.gte(CONFIG.apiVersion, "1.36.0")) {
mspHelper.sendVoltageConfig(save_amperage_config);
} else {
MSP.send_message(MSPCodes.MSP_SET_VOLTAGE_METER_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_VOLTAGE_METER_CONFIG), false, save_amperage_config);
}
}
GUI.interval_add('setup_data_pull_slow', get_slow_data, 200, true); // 5hz
}

function save_amperage_config() {
if (semver.gte(CONFIG.apiVersion, "1.36.0")) {
mspHelper.sendCurrentConfig(save_to_eeprom);
} else {
MSP.send_message(MSPCodes.MSP_SET_CURRENT_METER_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_CURRENT_METER_CONFIG), false, save_to_eeprom);
}
function save_power_config() {
function save_battery_config() {
MSP.send_message(MSPCodes.MSP_SET_BATTERY_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_BATTERY_CONFIG), false, save_voltage_config);
}

function save_voltage_config() {
if (semver.gte(CONFIG.apiVersion, "1.36.0")) {
mspHelper.sendVoltageConfig(save_amperage_config);
} else {
MSP.send_message(MSPCodes.MSP_SET_VOLTAGE_METER_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_VOLTAGE_METER_CONFIG), false, save_amperage_config);
}
}

function save_to_eeprom() {
MSP.send_message(MSPCodes.MSP_EEPROM_WRITE, false, false, save_completed);
function save_amperage_config() {
if (semver.gte(CONFIG.apiVersion, "1.36.0")) {
mspHelper.sendCurrentConfig(save_to_eeprom);
} else {
MSP.send_message(MSPCodes.MSP_SET_CURRENT_METER_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_CURRENT_METER_CONFIG), false, save_to_eeprom);
}
}

function save_completed() {
GUI.log(i18n.getMessage('configurationEepromSaved'));
function save_to_eeprom() {
MSP.send_message(MSPCodes.MSP_EEPROM_WRITE, false, false, save_completed);
}

TABS.power.initialize();
}
function save_completed() {
GUI.log(i18n.getMessage('configurationEepromSaved'));

save_battery_config();
});
TABS.power.initialize();
}

GUI.interval_add('setup_data_pull_slow', get_slow_data, 200, true); // 5hz
save_battery_config();
}

function process_html() {
Expand All @@ -346,4 +489,11 @@ TABS.power.initialize = function (callback) {

TABS.power.cleanup = function (callback) {
if (callback) callback();

if (GUI.calibrationManager) {
GUI.calibrationManager.destroy();
}
if (GUI.calibrationManagerConfirmation) {
GUI.calibrationManagerConfirmation.destroy();
}
};
Loading

0 comments on commit 56e2ce6

Please sign in to comment.