betaflight-configurator/tabs/pid_tuning.js

413 lines
13 KiB
JavaScript
Raw Normal View History

'use strict';
TABS.pid_tuning = {
controllerChanged: true
};
2014-08-12 14:20:26 +00:00
TABS.pid_tuning.initialize = function (callback) {
var self = this;
if (GUI.active_tab != 'pid_tuning') {
GUI.active_tab = 'pid_tuning';
googleAnalytics.sendAppView('PID Tuning');
}
2014-03-08 05:25:15 +00:00
function get_pid_controller() {
if (GUI.canChangePidController) {
MSP.send_message(MSP_codes.MSP_PID_CONTROLLER, false, false, get_pid_names);
} else {
get_pid_names();
}
}
2014-05-03 20:42:11 +00:00
function get_pid_names() {
MSP.send_message(MSP_codes.MSP_PIDNAMES, false, false, get_pid_data);
2014-05-03 20:42:11 +00:00
}
function get_pid_data() {
MSP.send_message(MSP_codes.MSP_PID, false, false, get_rc_tuning_data);
}
function get_rc_tuning_data() {
MSP.send_message(MSP_codes.MSP_RC_TUNING, false, false, load_html);
}
function load_html() {
$('#content').load("./tabs/pid_tuning.html", process_html);
}
// requesting MSP_STATUS manually because it contains CONFIG.profile
MSP.send_message(MSP_codes.MSP_STATUS, false, false, get_pid_controller);
2015-03-11 20:24:46 +00:00
function pid_and_rc_to_form() {
2015-03-11 20:34:02 +00:00
// Fill in the data from PIDs array
var i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .ROLL input').each(function () {
switch (i) {
case 0:
$(this).val(PIDs[0][i++].toFixed(1));
break;
case 1:
$(this).val(PIDs[0][i++].toFixed(3));
break;
case 2:
$(this).val(PIDs[0][i++].toFixed(0));
break;
}
});
2014-03-08 05:25:15 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .PITCH input').each(function () {
switch (i) {
case 0:
$(this).val(PIDs[1][i++].toFixed(1));
break;
case 1:
$(this).val(PIDs[1][i++].toFixed(3));
break;
case 2:
$(this).val(PIDs[1][i++].toFixed(0));
break;
}
});
2014-03-08 05:25:15 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .YAW input').each(function () {
switch (i) {
case 0:
$(this).val(PIDs[2][i++].toFixed(1));
break;
case 1:
$(this).val(PIDs[2][i++].toFixed(3));
break;
case 2:
$(this).val(PIDs[2][i++].toFixed(0));
break;
}
});
2013-04-08 23:09:11 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .ALT input').each(function () {
switch (i) {
case 0:
$(this).val(PIDs[3][i++].toFixed(1));
break;
case 1:
$(this).val(PIDs[3][i++].toFixed(3));
break;
case 2:
$(this).val(PIDs[3][i++].toFixed(0));
break;
}
});
2013-04-08 23:09:11 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .Pos input').each(function () {
$(this).val(PIDs[4][i++].toFixed(2));
});
2014-03-08 05:25:15 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .PosR input').each(function () {
switch (i) {
case 0:
$(this).val(PIDs[5][i++].toFixed(1));
break;
case 1:
$(this).val(PIDs[5][i++].toFixed(2));
break;
case 2:
$(this).val(PIDs[5][i++].toFixed(3));
break;
}
});
2013-04-08 23:09:11 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .NavR input').each(function () {
switch (i) {
case 0:
$(this).val(PIDs[6][i++].toFixed(1));
break;
case 1:
$(this).val(PIDs[6][i++].toFixed(2));
break;
case 2:
$(this).val(PIDs[6][i++].toFixed(3));
break;
}
});
2013-04-08 23:09:11 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .LEVEL input').each(function () {
switch (i) {
case 0:
$(this).val(PIDs[7][i++].toFixed(1));
break;
case 1:
2014-06-09 00:56:38 +00:00
$(this).val(PIDs[7][i++].toFixed(3));
break;
case 2:
$(this).val(PIDs[7][i++].toFixed(0));
break;
}
});
2013-04-08 23:09:11 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .MAG input').each(function () {
$(this).val(PIDs[8][i++].toFixed(1));
});
2013-04-08 23:49:49 +00:00
i = 0;
2014-08-12 14:20:26 +00:00
$('.pid_tuning .Vario input').each(function () {
switch (i) {
case 0:
$(this).val(PIDs[9][i++].toFixed(1));
break;
case 1:
$(this).val(PIDs[9][i++].toFixed(3));
break;
case 2:
$(this).val(PIDs[9][i++].toFixed(0));
break;
}
});
2014-03-08 05:25:15 +00:00
// Fill in data from RC_tuning object
$('.rate-tpa input[name="roll-pitch"]').val(RC_tuning.roll_pitch_rate.toFixed(2));
$('.rate-tpa input[name="roll"]').val(RC_tuning.roll_rate.toFixed(2));
$('.rate-tpa input[name="pitch"]').val(RC_tuning.pitch_rate.toFixed(2));
$('.rate-tpa input[name="yaw"]').val(RC_tuning.yaw_rate.toFixed(2));
$('.rate-tpa input[name="tpa"]').val(RC_tuning.dynamic_THR_PID.toFixed(2));
2015-03-10 03:27:34 +00:00
$('.rate-tpa input[name="tpa-breakpoint"]').val(RC_tuning.dynamic_THR_breakpoint);
}
function form_to_pid_and_rc() {
2015-03-11 20:34:02 +00:00
// Catch all the changes and stuff the inside PIDs array
var i = 0;
$('table.pid_tuning tr.ROLL input').each(function () {
PIDs[0][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.PITCH input').each(function () {
PIDs[1][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.YAW input').each(function () {
PIDs[2][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.ALT input').each(function () {
PIDs[3][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.Vario input').each(function () {
PIDs[9][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.Pos input').each(function () {
PIDs[4][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.PosR input').each(function () {
PIDs[5][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.NavR input').each(function () {
PIDs[6][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.LEVEL input').each(function () {
PIDs[7][i++] = parseFloat($(this).val());
});
i = 0;
$('table.pid_tuning tr.MAG input').each(function () {
PIDs[8][i++] = parseFloat($(this).val());
});
// catch RC_tuning changes
RC_tuning.roll_pitch_rate = parseFloat($('.rate-tpa input[name="roll-pitch"]').val());
RC_tuning.roll_rate = parseFloat($('.rate-tpa input[name="roll"]').val());
RC_tuning.pitch_rate = parseFloat($('.rate-tpa input[name="pitch"]').val());
RC_tuning.yaw_rate = parseFloat($('.rate-tpa input[name="yaw"]').val());
RC_tuning.dynamic_THR_PID = parseFloat($('.rate-tpa input[name="tpa"]').val());
RC_tuning.dynamic_THR_breakpoint = parseInt($('.rate-tpa input[name="tpa-breakpoint"]').val());
}
2015-11-02 09:10:08 +00:00
function hideUnusedPids(sensors_detected) {
$('.tab-pid_tuning table.pid_tuning').hide();
$('#pid_main').show();
if (have_sensor(sensors_detected, 'acc')) {
$('#pid_accel').show();
}
if (have_sensor(sensors_detected, 'baro')) {
$('#pid_baro').show();
}
if (have_sensor(sensors_detected, 'mag')) {
$('#pid_mag').show();
}
if (bit_check(BF_CONFIG.features, 7)) { //This will need to be reworked to remove BF_CONFIG reference eventually
$('#pid_gps').show();
}
if (have_sensor(sensors_detected, 'sonar')) {
2015-11-09 09:33:33 +00:00
$('#pid_baro').show();
2015-11-02 09:10:08 +00:00
}
}
function process_html() {
// translate to user-selected language
localize();
2015-11-09 09:33:33 +00:00
// loading tooltip
$(document).ready(function() {
$('.cf_tip').jBox('Tooltip', {
delayOpen: 100,
delayClose: 100,
position: {
x: 'right',
y: 'center'
2015-11-09 09:33:33 +00:00
},
outside: 'x'
});
});
2015-11-02 09:10:08 +00:00
hideUnusedPids(CONFIG.activeSensors);
$('#showAllPids').on('click', function(){
if($(this).text() == "Show all PIDs") {
2015-11-02 09:10:08 +00:00
$('.tab-pid_tuning table.pid_tuning').show();
$(this).text('Hide unused PIDs');
2015-11-02 09:10:08 +00:00
} else {
hideUnusedPids(CONFIG.activeSensors);
$(this).text('Show all PIDs');
2015-11-02 09:10:08 +00:00
}
});
$('.pid_tuning tr').each(function(){
for(i = 0; i < PID_names.length; i++) {
if($(this).hasClass(PID_names[i])) {
$(this).find('td:first').text(PID_names[i]);
}
}
});
2015-03-11 20:24:46 +00:00
pid_and_rc_to_form();
2014-03-08 05:25:15 +00:00
var pidController_e = $('select[name="controller"]');
var profile_e = $('select[name="profile"]');
var form_e = $('#pid-tuning');
if (GUI.canChangePidController) {
pidController_e.val(PID.controller);
} else {
GUI.log(chrome.i18n.getMessage('pidTuningUpgradeFirmwareToChangePidController', [CONFIG.apiVersion, CONFIGURATOR.pidControllerChangeMinApiVersion]));
pidController_e.empty();
pidController_e.append('<option value="">Unknown</option>');
pidController_e.prop('disabled', true);
}
2015-11-02 09:10:08 +00:00
if (semver.lt(CONFIG.apiVersion, "1.7.0")) {
$('.rate-tpa .tpa-breakpoint').hide();
$('.rate-tpa .roll').hide();
$('.rate-tpa .pitch').hide();
} else {
$('.rate-tpa .roll-pitch').hide();
}
// Fill in currently selected profile
profile_e.val(CONFIG.profile);
2014-03-08 05:25:15 +00:00
// UI Hooks
profile_e.change(function () {
var profile = parseInt($(this).val());
2014-09-23 19:02:07 +00:00
MSP.send_message(MSP_codes.MSP_SELECT_SETTING, [profile], false, function () {
GUI.log(chrome.i18n.getMessage('pidTuningLoadedProfile', [profile + 1]));
2014-03-08 05:25:15 +00:00
2014-08-12 14:20:26 +00:00
GUI.tab_switch_cleanup(function () {
TABS.pid_tuning.initialize();
2014-07-10 16:14:33 +00:00
});
});
});
2014-03-08 05:25:15 +00:00
2014-08-12 14:20:26 +00:00
$('a.refresh').click(function () {
GUI.tab_switch_cleanup(function () {
2014-05-06 15:34:59 +00:00
GUI.log(chrome.i18n.getMessage('pidTuningDataRefreshed'));
TABS.pid_tuning.initialize();
});
});
2014-03-08 05:25:15 +00:00
form_e.find('input').each(function (k, item) {
$(item).change(function () {
pidController_e.prop("disabled", true);
TABS.pid_tuning.controllerChanged = false;
})
});
pidController_e.change(function () {
if (PID.controller != pidController_e.val()) {
form_e.find('input').each(function (k, item) {
$(item).prop('disabled', true);
TABS.pid_tuning.controllerChanged = true;
});
}
});
// update == save.
$('a.update').click(function () {
form_to_pid_and_rc();
function send_pids() {
2015-03-11 20:34:02 +00:00
if (!TABS.pid_tuning.controllerChanged) {
MSP.send_message(MSP_codes.MSP_SET_PID, MSP.crunch(MSP_codes.MSP_SET_PID), false, send_rc_tuning_changes);
}
}
function send_rc_tuning_changes() {
MSP.send_message(MSP_codes.MSP_SET_RC_TUNING, MSP.crunch(MSP_codes.MSP_SET_RC_TUNING), false, save_to_eeprom);
}
function save_to_eeprom() {
2014-08-12 14:20:26 +00:00
MSP.send_message(MSP_codes.MSP_EEPROM_WRITE, false, false, function () {
2014-05-06 15:34:59 +00:00
GUI.log(chrome.i18n.getMessage('pidTuningEepromSaved'));
});
}
if (GUI.canChangePidController && TABS.pid_tuning.controllerChanged) {
PID.controller = pidController_e.val();
MSP.send_message(MSP_codes.MSP_SET_PID_CONTROLLER, MSP.crunch(MSP_codes.MSP_SET_PID_CONTROLLER), false, function () {
MSP.send_message(MSP_codes.MSP_EEPROM_WRITE, false, false, function () {
GUI.log(chrome.i18n.getMessage('pidTuningEepromSaved'));
});
TABS.pid_tuning.initialize();
});
} else {
send_pids();
}
2013-12-15 01:28:12 +00:00
});
// status data pulled via separate timer with static speed
GUI.interval_add('status_pull', function status_pull() {
MSP.send_message(MSP_codes.MSP_STATUS);
}, 250, true);
2014-07-10 16:14:33 +00:00
GUI.content_ready(callback);
}
2014-07-10 16:14:33 +00:00
};
2014-08-12 14:20:26 +00:00
TABS.pid_tuning.cleanup = function (callback) {
2015-03-11 20:34:02 +00:00
if (callback) {
callback();
}
2015-11-02 09:10:08 +00:00
};