betaflight-configurator/tabs/setup.js

290 lines
10 KiB
JavaScript
Raw Normal View History

'use strict';
TABS.setup = {
yaw_fix: 0.0
};
2014-08-12 14:05:22 +00:00
TABS.setup.initialize = function (callback) {
var self = this;
if (GUI.active_tab != 'setup') {
GUI.active_tab = 'setup';
googleAnalytics.sendAppView('Setup');
}
2014-03-08 05:25:15 +00:00
2014-05-07 01:01:59 +00:00
function load_ident() {
MSP.send_message(MSP_codes.MSP_IDENT, false, false, load_misc_data);
}
function load_config() {
MSP.send_message(MSP_codes.MSP_CONFIG, false, false, load_misc_data);
2014-05-07 01:01:59 +00:00
}
function load_misc_data() {
MSP.send_message(MSP_codes.MSP_MISC, false, false, load_html);
}
function load_html() {
$('#content').load("./tabs/setup.html", process_html);
}
MSP.send_message(MSP_codes.MSP_ACC_TRIM, false, false, load_ident);
function process_html() {
// translate to user-selected language
localize();
// initialize 3D
self.initialize3D();
// set heading in interactive block
$('span.heading').text(chrome.i18n.getMessage('initialSetupheading', [0]));
// check if we have magnetometer
if (!bit_check(CONFIG.activeSensors, 2)) {
$('a.calibrateMag').addClass('disabled');
}
// UI Hooks
2014-08-12 14:05:22 +00:00
$('a.calibrateAccel').click(function () {
var self = $(this);
if (!self.hasClass('calibrating')) {
self.addClass('calibrating');
// During this period MCU won't be able to process any serial commands because its locked in a for/while loop
// until this operation finishes, sending more commands through data_poll() will result in serial buffer overflow
GUI.interval_pause('setup_data_pull');
2014-08-12 14:05:22 +00:00
MSP.send_message(MSP_codes.MSP_ACC_CALIBRATION, false, false, function () {
2014-05-06 16:07:32 +00:00
GUI.log(chrome.i18n.getMessage('initialSetupAccelCalibStarted'));
});
2014-03-08 05:25:15 +00:00
2014-08-12 14:05:22 +00:00
GUI.timeout_add('button_reset', function () {
GUI.interval_resume('setup_data_pull');
2014-03-08 05:25:15 +00:00
2014-05-06 16:07:32 +00:00
GUI.log(chrome.i18n.getMessage('initialSetupAccelCalibEnded'));
2014-03-08 05:25:15 +00:00
self.removeClass('calibrating');
}, 2000);
}
});
2014-03-08 05:25:15 +00:00
2014-08-12 14:05:22 +00:00
$('a.calibrateMag').click(function () {
var self = $(this);
if (!self.hasClass('calibrating') && !self.hasClass('disabled')) {
self.addClass('calibrating');
2014-03-08 05:25:15 +00:00
2014-08-12 14:05:22 +00:00
MSP.send_message(MSP_codes.MSP_MAG_CALIBRATION, false, false, function () {
2014-05-06 16:07:32 +00:00
GUI.log(chrome.i18n.getMessage('initialSetupMagCalibStarted'));
});
2014-08-12 14:05:22 +00:00
GUI.timeout_add('button_reset', function () {
2014-05-06 16:07:32 +00:00
GUI.log(chrome.i18n.getMessage('initialSetupMagCalibEnded'));
self.removeClass('calibrating');
}, 30000);
}
});
2014-03-08 05:25:15 +00:00
$('a.resetSettings').click(function () {
2014-08-12 14:05:22 +00:00
MSP.send_message(MSP_codes.MSP_RESET_CONF, false, false, function () {
2014-05-06 16:07:32 +00:00
GUI.log(chrome.i18n.getMessage('initialSetupSettingsRestored'));
GUI.tab_switch_cleanup(function () {
TABS.setup.initialize();
2014-03-08 05:25:15 +00:00
});
});
});
// display current yaw fix value (important during tab re-initialization)
$('div#interactive_block > a.reset').text(chrome.i18n.getMessage('initialSetupButtonResetZaxisValue', [self.yaw_fix]));
// reset yaw button hook
2014-08-12 14:05:22 +00:00
$('div#interactive_block > a.reset').click(function () {
self.yaw_fix = SENSOR_DATA.kinematics[2] * - 1.0;
$(this).text(chrome.i18n.getMessage('initialSetupButtonResetZaxisValue', [self.yaw_fix]));
console.log('YAW reset to 0 deg, fix: ' + self.yaw_fix + ' deg');
2013-12-15 01:28:12 +00:00
});
$('#content .backup').click(function () {
configuration_backup(function () {
GUI.log(chrome.i18n.getMessage('initialSetupBackupSuccess'));
googleAnalytics.sendEvent('Configuration', 'Backup', 'true');
});
});
$('#content .restore').click(function () {
configuration_restore(function () {
GUI.log(chrome.i18n.getMessage('initialSetupRestoreSuccess'));
googleAnalytics.sendEvent('Configuration', 'Restore', 'true');
// get latest settings
2014-10-12 16:00:44 +00:00
TABS.setup.initialize();
});
});
// cached elements
var bat_voltage_e = $('.bat-voltage'),
bat_mah_drawn_e = $('.bat-mah-drawn'),
bat_mah_drawing_e = $('.bat-mah-drawing'),
rssi_e = $('.rssi'),
gpsFix_e = $('.gpsFix'),
gpsSats_e = $('.gpsSats'),
gpsLat_e = $('.gpsLat'),
gpsLon_e = $('.gpsLon'),
heading_e = $('span.heading');
function get_slow_data() {
MSP.send_message(MSP_codes.MSP_ANALOG, false, false, function () {
bat_voltage_e.text(chrome.i18n.getMessage('initialSetupBatteryValue', [ANALOG.voltage]));
bat_mah_drawn_e.text(chrome.i18n.getMessage('initialSetupBatteryMahValue', [ANALOG.mAhdrawn]));
bat_mah_drawing_e.text(chrome.i18n.getMessage('initialSetupBatteryAValue', [ANALOG.amperage.toFixed(2)]));
rssi_e.text(chrome.i18n.getMessage('initialSetupRSSIValue', [((ANALOG.rssi / 1023) * 100).toFixed(0)]));
});
MSP.send_message(MSP_codes.MSP_RAW_GPS, false, false, function () {
gpsFix_e.html((GPS_DATA.fix) ? chrome.i18n.getMessage('gpsFixTrue') : chrome.i18n.getMessage('gpsFixFalse'));
gpsSats_e.text(GPS_DATA.numSat);
gpsLat_e.text((GPS_DATA.lat / 10000000).toFixed(4) + ' deg');
gpsLon_e.text((GPS_DATA.lon / 10000000).toFixed(4) + ' deg');
});
MSP.send_message(MSP_codes.MSP_STATUS);
}
function get_fast_data() {
MSP.send_message(MSP_codes.MSP_ATTITUDE, false, false, function () {
heading_e.text(chrome.i18n.getMessage('initialSetupheading', [SENSOR_DATA.kinematics[2]]));
self.render3D();
});
}
GUI.interval_add('setup_data_pull_fast', get_fast_data, 33, true); // 30 fps
GUI.interval_add('setup_data_pull_slow', get_slow_data, 250, true); // 4 fps
2014-07-10 16:14:26 +00:00
if (callback) callback();
}
2014-07-10 16:14:26 +00:00
};
TABS.setup.initialize3D = function (compatibility) {
var self = this,
2014-09-30 18:25:48 +00:00
loader, canvas, wrapper, renderer, camera, scene, light, light2, modelWrapper, model, model_file,
fallback = false;
canvas = $('.CAP_BASEFLIGHT_CONFIG #canvas');
wrapper = $('.CAP_BASEFLIGHT_CONFIG #canvas_wrapper');
// webgl capability detector
// it would seem the webgl "enabling" through advanced settings will be ignored in the future
// and webgl will be supported if gpu supports it by default (canary 40.0.2175.0), keep an eye on this one
var detector_canvas = document.createElement('canvas');
if (window.WebGLRenderingContext && (detector_canvas.getContext('webgl') || detector_canvas.getContext('experimental-webgl'))) {
renderer = new THREE.WebGLRenderer({canvas: canvas.get(0), alpha: true, antialias: true});
} else {
renderer = new THREE.CanvasRenderer({canvas: canvas.get(0), alpha: true});
2014-09-30 18:25:48 +00:00
fallback = true;
}
// modelWrapper just adds an extra axis of rotation to avoid gimbal lock withe euler angles
modelWrapper = new THREE.Object3D()
// load the model including materials
var models = [
2014-10-31 12:32:40 +00:00
'tricopter',
'quad_x',
'quad_x',
'quad_x',
'quad_x',
2014-10-31 12:32:40 +00:00
'y6',
'hex_plus',
'quad_x',
2014-10-31 12:32:40 +00:00
'y4',
'hex_x',
'quad_x',
'quad_x',
'quad_x',
'quad_x',
'quad_x',
'quad_x',
'quad_vtail',
'quad_x',
'quad_x',
'quad_x',
'quad_x',
'quad_atail',
'quad_x'
];
2014-09-30 18:25:48 +00:00
if (!fallback) {
model_file = models[CONFIG.multiType - 1];
2014-09-30 18:25:48 +00:00
} else {
model_file = 'fallback';
}
loader = new THREE.JSONLoader();
loader.load('./resources/models/' + model_file + '.json', function (geometry, materials) {
if (!fallback) {
model = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(materials));
} else {
materials = THREE.ImageUtils.loadTexture('./resources/textures/fallback_texture.png');
model = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({map: materials, overdraw: true}));
}
model.scale.set(10, 10, 10);
modelWrapper.add(model);
scene.add(modelWrapper);
});
// stacionary camera
camera = new THREE.PerspectiveCamera(50, wrapper.width() / wrapper.height(), 1, 10000);
// setup scene
scene = new THREE.Scene();
// some light
light = new THREE.AmbientLight(0x404040);
light2 = new THREE.DirectionalLight(new THREE.Color(1, 1, 1), 1.5);
light2.position.set(0, 1, 0);
// initialize render size for current canvas size
renderer.setSize(wrapper.width(), wrapper.height());
// move camera away from the model
camera.position.z = 125;
// add camera, model, light to the foreground scene
scene.add(light);
scene.add(light2);
scene.add(camera);
scene.add(modelWrapper);
this.render3D = function () {
// compute the changes
model.rotation.x = (SENSOR_DATA.kinematics[1] * -1.0) * 0.017453292519943295;
modelWrapper.rotation.y = ((SENSOR_DATA.kinematics[2] * -1.0) - self.yaw_fix) * 0.017453292519943295;
model.rotation.z = (SENSOR_DATA.kinematics[0] * -1.0) * 0.017453292519943295;
// draw
renderer.render(scene, camera);
};
// handle canvas resize
this.resize3D = function () {
renderer.setSize(wrapper.width(), wrapper.height());
camera.aspect = wrapper.width() / wrapper.height();
camera.updateProjectionMatrix();
self.render3D();
};
$(window).on('resize', this.resize3D);
};
TABS.setup.cleanup = function (callback) {
$(window).off('resize', this.resize3D);
2014-07-10 16:14:26 +00:00
if (callback) callback();
2014-10-31 12:32:40 +00:00
};