betaflight-configurator/tabs/sensors.js

460 lines
17 KiB
JavaScript
Raw Normal View History

'use strict';
TABS.sensors = {};
2014-08-12 14:20:26 +00:00
TABS.sensors.initialize = function (callback) {
var self = this;
if (GUI.active_tab != 'sensors') {
GUI.active_tab = 'sensors';
2014-10-30 11:33:39 +00:00
googleAnalytics.sendAppView('Sensors');
}
2014-04-13 22:15:56 +00:00
function initSensorData(){
for (var i = 0; i < 3; i++) {
SENSOR_DATA.accelerometer[i] = 0;
SENSOR_DATA.gyroscope[i] = 0;
SENSOR_DATA.magnetometer[i] = 0;
SENSOR_DATA.sonar = 0;
SENSOR_DATA.altitude = 0;
2014-04-13 22:15:56 +00:00
SENSOR_DATA.debug[i] = 0;
}
2014-04-12 21:35:22 +00:00
}
2014-04-13 22:15:56 +00:00
function initDataArray(length) {
var data = new Array(length);
for (var i = 0; i < length; i++) {
data[i] = new Array();
data[i].min = -1;
data[i].max = 1;
}
return data;
2014-04-12 21:35:22 +00:00
}
2014-04-13 22:15:56 +00:00
function addSampleToData(data, sampleNumber, sensorData) {
for (var i = 0; i < data.length; i++) {
var dataPoint = sensorData[i];
data[i].push([sampleNumber, dataPoint]);
if (dataPoint < data[i].min) {
data[i].min = dataPoint;
}
if (dataPoint > data[i].max) {
data[i].max = dataPoint;
}
2014-04-12 21:35:22 +00:00
}
2014-04-13 22:15:56 +00:00
while (data[0].length > 300) {
for (i = 0; i < data.length; i++) {
data[i].shift();
}
2014-04-12 21:35:22 +00:00
}
2014-04-13 22:15:56 +00:00
return sampleNumber + 1;
2014-04-12 21:35:22 +00:00
}
2014-04-13 22:15:56 +00:00
var margin = {top: 20, right: 10, bottom: 10, left: 40};
2014-05-16 19:02:59 +00:00
function updateGraphHelperSize(helpers) {
helpers.width = helpers.targetElement.width() - margin.left - margin.right;
helpers.height = helpers.targetElement.height() - margin.top - margin.bottom;
2014-04-13 22:15:56 +00:00
2014-04-21 10:14:03 +00:00
helpers.widthScale.range([0, helpers.width]);
helpers.heightScale.range([helpers.height, 0]);
2014-05-05 10:11:52 +00:00
helpers.xGrid.tickSize(-helpers.height, 0, 0);
helpers.yGrid.tickSize(-helpers.width, 0, 0);
}
function initGraphHelpers(selector, sampleNumber, heightDomain) {
2014-05-16 19:02:59 +00:00
var helpers = {selector: selector, targetElement: $(selector), dynamicHeightDomain: !heightDomain};
2014-04-13 22:15:56 +00:00
helpers.widthScale = d3.scale.linear()
.clamp(true)
.domain([(sampleNumber - 299), sampleNumber]);
2014-04-13 22:15:56 +00:00
helpers.heightScale = d3.scale.linear()
.clamp(true)
.domain(heightDomain || [1, -1]);
2014-05-05 10:11:52 +00:00
helpers.xGrid = d3.svg.axis();
helpers.yGrid = d3.svg.axis();
2014-05-16 19:02:59 +00:00
updateGraphHelperSize(helpers);
2014-04-13 22:15:56 +00:00
2014-05-05 10:11:52 +00:00
helpers.xGrid
.scale(helpers.widthScale)
.orient("bottom")
.ticks(5)
.tickFormat("");
2014-04-13 22:15:56 +00:00
2014-05-05 10:11:52 +00:00
helpers.yGrid
.scale(helpers.heightScale)
.orient("left")
.ticks(5)
.tickFormat("");
2014-04-13 22:15:56 +00:00
helpers.xAxis = d3.svg.axis()
.scale(helpers.widthScale)
.ticks(5)
.orient("bottom")
2014-08-12 14:20:26 +00:00
.tickFormat(function (d) {return d;});
2014-04-13 22:15:56 +00:00
helpers.yAxis = d3.svg.axis()
.scale(helpers.heightScale)
.ticks(5)
.orient("left")
2014-08-12 14:20:26 +00:00
.tickFormat(function (d) {return d;});
2014-04-13 22:15:56 +00:00
helpers.line = d3.svg.line()
2014-08-12 14:20:26 +00:00
.x(function (d) {return helpers.widthScale(d[0]);})
.y(function (d) {return helpers.heightScale(d[1]);});
2014-04-13 22:15:56 +00:00
return helpers;
2014-04-12 21:35:22 +00:00
}
2014-04-13 22:15:56 +00:00
function drawGraph(graphHelpers, data, sampleNumber) {
var svg = d3.select(graphHelpers.selector);
2014-04-12 21:35:22 +00:00
2014-04-13 22:15:56 +00:00
if (graphHelpers.dynamicHeightDomain) {
var limits = [];
2014-08-12 14:20:26 +00:00
$.each(data, function (idx, datum) {
2014-04-13 22:15:56 +00:00
limits.push(datum.min);
limits.push(datum.max);
});
graphHelpers.heightScale.domain(d3.extent(limits));
}
graphHelpers.widthScale.domain([(sampleNumber - 299), sampleNumber]);
2014-04-12 21:35:22 +00:00
2014-04-13 22:15:56 +00:00
svg.select(".x.grid").call(graphHelpers.xGrid);
svg.select(".y.grid").call(graphHelpers.yGrid);
svg.select(".x.axis").call(graphHelpers.xAxis);
svg.select(".y.axis").call(graphHelpers.yAxis);
var group = svg.select("g.data");
2014-08-12 14:20:26 +00:00
var lines = group.selectAll("path").data(data, function (d, i) {return i;});
2014-04-13 22:15:56 +00:00
var newLines = lines.enter().append("path").attr("class", "line");
lines.attr('d', graphHelpers.line);
2014-04-12 21:35:22 +00:00
}
2014-04-13 22:15:56 +00:00
function plot_gyro(enable) {
if (enable) {
$('.wrapper.gyro').show();
} else {
$('.wrapper.gyro').hide();
}
}
2014-04-12 21:35:22 +00:00
2014-04-13 22:15:56 +00:00
function plot_accel(enable) {
if (enable) {
$('.wrapper.accel').show();
} else {
$('.wrapper.accel').hide();
}
}
2014-04-12 21:35:22 +00:00
2014-04-13 22:15:56 +00:00
function plot_mag(enable) {
if (enable) {
$('.wrapper.mag').show();
} else {
$('.wrapper.mag').hide();
}
}
function plot_baro(enable) {
if (enable) {
$('.wrapper.baro').show();
} else {
$('.wrapper.baro').hide();
}
}
2015-01-28 21:24:44 +00:00
function plot_sonar(enable) {
if (enable) {
$('.wrapper.sonar').show();
} else {
$('.wrapper.sonar').hide();
}
}
2014-04-13 22:15:56 +00:00
function plot_debug(enable) {
if (enable) {
$('.wrapper.debug').show();
} else {
$('.wrapper.debug').hide();
}
}
2014-03-08 05:25:15 +00:00
$('#content').load("./tabs/sensors.html", function load_html() {
// translate to user-selected language
localize();
// disable graphs for sensors that are missing
var checkboxes = $('.tab-sensors .info .checkboxes input');
if (!bit_check(CONFIG.activeSensors, 1)) { // baro
checkboxes.eq(3).prop('disabled', true);
}
if (!bit_check(CONFIG.activeSensors, 2)) { // mag
checkboxes.eq(2).prop('disabled', true);
}
2015-01-28 21:24:44 +00:00
if (!bit_check(CONFIG.activeSensors, 4)) { // sonar
checkboxes.eq(4).prop('disabled', true);
}
2014-08-12 14:20:26 +00:00
$('.tab-sensors .info .checkboxes input').change(function () {
2014-04-13 22:15:56 +00:00
var enable = $(this).prop('checked');
var index = $(this).parent().index();
2014-04-13 22:15:56 +00:00
switch (index) {
case 0:
plot_gyro(enable);
break;
case 1:
plot_accel(enable);
break;
case 2:
plot_mag(enable);
break;
case 3:
plot_baro(enable);
break;
case 4:
2015-01-28 21:24:44 +00:00
plot_sonar(enable);
break;
case 5:
2014-04-13 22:15:56 +00:00
plot_debug(enable);
break;
}
var checkboxes = [];
2014-08-12 14:20:26 +00:00
$('.tab-sensors .info .checkboxes input').each(function () {
2014-04-13 22:15:56 +00:00
checkboxes.push($(this).prop('checked'));
});
$('.tab-sensors .rate select:first').change();
chrome.storage.local.set({'graphs_enabled': checkboxes});
});
2014-08-12 14:20:26 +00:00
chrome.storage.local.get('graphs_enabled', function (result) {
2014-04-13 22:15:56 +00:00
if (result.graphs_enabled) {
var checkboxes = $('.tab-sensors .info .checkboxes input');
2014-04-13 22:15:56 +00:00
for (var i = 0; i < result.graphs_enabled.length; i++) {
checkboxes.eq(i).not(':disabled').prop('checked', result.graphs_enabled[i]).change();
2014-04-13 22:15:56 +00:00
}
} else {
2014-08-14 14:40:55 +00:00
$('.tab-sensors .info input:lt(4):not(:disabled)').prop('checked', true).change();
2014-04-13 22:15:56 +00:00
}
});
// Always start with default/empty sensor data array, clean slate all
2014-04-12 21:35:22 +00:00
initSensorData();
2014-03-08 05:25:15 +00:00
// Setup variables
2014-08-12 14:20:26 +00:00
var samples_gyro_i = 0,
samples_accel_i = 0,
samples_mag_i = 0,
samples_baro_i = 0,
2015-01-28 21:24:44 +00:00
samples_sonar_i = 0,
2014-08-12 14:20:26 +00:00
samples_debug_i = 0,
gyro_data = initDataArray(3),
accel_data = initDataArray(3),
mag_data = initDataArray(3),
baro_data = initDataArray(1),
2015-01-28 21:24:44 +00:00
sonar_data = initDataArray(1),
2014-08-12 14:20:26 +00:00
debug_data = [
2014-04-12 21:35:22 +00:00
initDataArray(1),
initDataArray(1),
initDataArray(1),
initDataArray(1)
];
2014-04-13 12:35:30 +00:00
var gyroHelpers = initGraphHelpers('#gyro', samples_gyro_i, [-2000, 2000]);
var accelHelpers = initGraphHelpers('#accel', samples_accel_i, [-2, 2]);
2014-04-13 14:12:31 +00:00
var magHelpers = initGraphHelpers('#mag', samples_mag_i, [-1, 1]);
2014-04-12 21:35:22 +00:00
var baroHelpers = initGraphHelpers('#baro', samples_baro_i);
2015-01-28 21:24:44 +00:00
var sonarHelpers = initGraphHelpers('#sonar', samples_sonar_i);
2014-04-12 21:35:22 +00:00
var debugHelpers = [
initGraphHelpers('#debug1', samples_debug_i),
initGraphHelpers('#debug2', samples_debug_i),
initGraphHelpers('#debug3', samples_debug_i),
initGraphHelpers('#debug4', samples_debug_i)
];
2014-03-08 05:25:15 +00:00
var raw_data_text_ements = {
x: [],
y: [],
2014-11-15 00:18:04 +00:00
z: []
};
2014-08-12 14:20:26 +00:00
$('.plot_control .x, .plot_control .y, .plot_control .z').each(function () {
2014-04-13 14:12:31 +00:00
var el = $(this);
if (el.hasClass('x')) {
raw_data_text_ements.x.push(el);
} else if (el.hasClass('y')) {
raw_data_text_ements.y.push(el);
} else {
2014-04-13 14:12:31 +00:00
raw_data_text_ements.z.push(el);
}
});
// set refresh speeds according to configuration saved in storage
2014-08-12 14:20:26 +00:00
chrome.storage.local.get('sensor_settings', function (result) {
2014-04-13 22:15:56 +00:00
if (result.sensor_settings) {
2014-04-13 15:48:47 +00:00
$('.tab-sensors select[name="gyro_refresh_rate"]').val(result.sensor_settings.rates.gyro);
$('.tab-sensors select[name="gyro_scale"]').val(result.sensor_settings.scales.gyro);
2014-03-08 05:25:15 +00:00
2014-04-13 15:48:47 +00:00
$('.tab-sensors select[name="accel_refresh_rate"]').val(result.sensor_settings.rates.accel);
$('.tab-sensors select[name="accel_scale"]').val(result.sensor_settings.scales.accel);
2014-04-13 14:12:31 +00:00
2014-04-13 15:48:47 +00:00
$('.tab-sensors select[name="mag_refresh_rate"]').val(result.sensor_settings.rates.mag);
$('.tab-sensors select[name="mag_scale"]').val(result.sensor_settings.scales.mag);
2014-04-13 14:12:31 +00:00
2014-04-13 15:48:47 +00:00
$('.tab-sensors select[name="baro_refresh_rate"]').val(result.sensor_settings.rates.baro);
2015-01-28 21:24:44 +00:00
$('.tab-sensors select[name="sonar_refresh_rate"]').val(result.sensor_settings.rates.sonar);
2014-04-13 15:48:47 +00:00
$('.tab-sensors select[name="debug_refresh_rate"]').val(result.sensor_settings.rates.debug);
2014-04-13 14:12:31 +00:00
// start polling data by triggering refresh rate change event
2014-04-13 22:15:56 +00:00
$('.tab-sensors .rate select:first').change();
} else {
// start polling immediatly (as there is no configuration saved in the storage)
2014-04-13 22:15:56 +00:00
$('.tab-sensors .rate select:first').change();
}
});
2014-03-08 05:25:15 +00:00
2014-08-12 14:20:26 +00:00
$('.tab-sensors .rate select, .tab-sensors .scale select').change(function () {
// if any of the select fields change value, all of the select values are grabbed
// and timers are re-initialized with the new settings
var rates = {
2014-04-13 12:35:30 +00:00
'gyro': parseInt($('.tab-sensors select[name="gyro_refresh_rate"]').val(), 10),
'accel': parseInt($('.tab-sensors select[name="accel_refresh_rate"]').val(), 10),
'mag': parseInt($('.tab-sensors select[name="mag_refresh_rate"]').val(), 10),
'baro': parseInt($('.tab-sensors select[name="baro_refresh_rate"]').val(), 10),
2015-01-28 21:24:44 +00:00
'sonar': parseInt($('.tab-sensors select[name="sonar_refresh_rate"]').val(), 10),
2014-04-13 12:35:30 +00:00
'debug': parseInt($('.tab-sensors select[name="debug_refresh_rate"]').val(), 10)
};
2014-03-08 05:25:15 +00:00
2014-04-13 14:12:31 +00:00
var scales = {
'gyro': parseFloat($('.tab-sensors select[name="gyro_scale"]').val()),
'accel': parseFloat($('.tab-sensors select[name="accel_scale"]').val()),
'mag': parseFloat($('.tab-sensors select[name="mag_scale"]').val())
};
// handling of "data pulling" is a little bit funky here, as MSP_RAW_IMU contains values for gyro/accel/mag but not baro
// this means that setting a slower refresh rate on any of the attributes would have no effect
// what we will do instead is = determinate the fastest refresh rate for those 3 attributes, use that as a "polling rate"
// and use the "slower" refresh rates only for re-drawing the graphs (to save resources/computing power)
2014-04-12 21:35:22 +00:00
var fastest = d3.min([rates.gyro, rates.accel, rates.mag]);
2014-03-08 05:25:15 +00:00
// store current/latest refresh rates in the storage
2014-04-13 15:48:47 +00:00
chrome.storage.local.set({'sensor_settings': {'rates': rates, 'scales': scales}});
2014-04-13 22:15:56 +00:00
// re-initialize domains with new scales
gyroHelpers = initGraphHelpers('#gyro', samples_gyro_i, [-scales.gyro, scales.gyro]);
accelHelpers = initGraphHelpers('#accel', samples_accel_i, [-scales.accel, scales.accel]);
magHelpers = initGraphHelpers('#mag', samples_mag_i, [-scales.mag, scales.mag]);
// fetch currently enabled plots
var checkboxes = [];
2014-08-12 14:20:26 +00:00
$('.tab-sensors .info .checkboxes input').each(function () {
2014-04-13 22:15:56 +00:00
checkboxes.push($(this).prop('checked'));
});
// timer initialization
2014-04-13 22:15:56 +00:00
GUI.interval_kill_all(['status_pull']);
2014-03-08 05:25:15 +00:00
// data pulling timers
2014-04-13 22:15:56 +00:00
if (checkboxes[0] || checkboxes[1] || checkboxes[2]) {
GUI.interval_add('IMU_pull', function imu_data_pull() {
MSP.send_message(MSP_codes.MSP_RAW_IMU, false, false, update_imu_graphs);
2014-04-13 22:15:56 +00:00
}, fastest, true);
}
2014-04-13 22:15:56 +00:00
if (checkboxes[3]) {
GUI.interval_add('altitude_pull', function altitude_data_pull() {
MSP.send_message(MSP_codes.MSP_ALTITUDE, false, false, update_altitude_graph);
2014-04-13 22:15:56 +00:00
}, rates.baro, true);
}
2014-03-08 05:25:15 +00:00
2014-04-13 22:15:56 +00:00
if (checkboxes[4]) {
2015-01-28 21:24:44 +00:00
GUI.interval_add('sonar_pull', function sonar_data_pull() {
MSP.send_message(MSP_codes.MSP_SONAR, false, false, update_sonar_graphs);
}, rates.sonar, true);
}
if (checkboxes[5]) {
2014-04-13 22:15:56 +00:00
GUI.interval_add('debug_pull', function debug_data_pull() {
MSP.send_message(MSP_codes.MSP_DEBUG, false, false, update_debug_graphs);
2014-04-13 22:15:56 +00:00
}, rates.debug, true);
}
2014-03-08 05:25:15 +00:00
2014-04-13 22:15:56 +00:00
function update_imu_graphs() {
if (checkboxes[0]) {
2014-05-16 19:02:59 +00:00
updateGraphHelperSize(gyroHelpers);
2014-05-05 10:11:52 +00:00
2014-04-13 22:15:56 +00:00
samples_gyro_i = addSampleToData(gyro_data, samples_gyro_i, SENSOR_DATA.gyroscope);
drawGraph(gyroHelpers, gyro_data, samples_gyro_i);
raw_data_text_ements.x[0].text(SENSOR_DATA.gyroscope[0].toFixed(2));
raw_data_text_ements.y[0].text(SENSOR_DATA.gyroscope[1].toFixed(2));
raw_data_text_ements.z[0].text(SENSOR_DATA.gyroscope[2].toFixed(2));
}
2014-03-08 05:25:15 +00:00
2014-04-13 22:15:56 +00:00
if (checkboxes[1]) {
2014-05-16 19:02:59 +00:00
updateGraphHelperSize(accelHelpers);
2014-05-05 10:11:52 +00:00
2014-04-13 22:15:56 +00:00
samples_accel_i = addSampleToData(accel_data, samples_accel_i, SENSOR_DATA.accelerometer);
drawGraph(accelHelpers, accel_data, samples_accel_i);
raw_data_text_ements.x[1].text(SENSOR_DATA.accelerometer[0].toFixed(2));
raw_data_text_ements.y[1].text(SENSOR_DATA.accelerometer[1].toFixed(2));
raw_data_text_ements.z[1].text(SENSOR_DATA.accelerometer[2].toFixed(2));
}
2014-03-08 05:25:15 +00:00
2014-04-13 22:15:56 +00:00
if (checkboxes[2]) {
2014-05-16 19:02:59 +00:00
updateGraphHelperSize(magHelpers);
2014-05-05 10:11:52 +00:00
2014-04-13 22:15:56 +00:00
samples_mag_i = addSampleToData(mag_data, samples_mag_i, SENSOR_DATA.magnetometer);
drawGraph(magHelpers, mag_data, samples_mag_i);
raw_data_text_ements.x[2].text(SENSOR_DATA.magnetometer[0].toFixed(2));
raw_data_text_ements.y[2].text(SENSOR_DATA.magnetometer[1].toFixed(2));
raw_data_text_ements.z[2].text(SENSOR_DATA.magnetometer[2].toFixed(2));
}
}
function update_altitude_graph() {
2014-05-16 19:02:59 +00:00
updateGraphHelperSize(baroHelpers);
2014-05-05 10:11:52 +00:00
2014-04-12 21:35:22 +00:00
samples_baro_i = addSampleToData(baro_data, samples_baro_i, [SENSOR_DATA.altitude]);
drawGraph(baroHelpers, baro_data, samples_baro_i);
raw_data_text_ements.x[3].text(SENSOR_DATA.altitude.toFixed(2));
}
2015-01-28 21:24:44 +00:00
function update_sonar_graphs() {
updateGraphHelperSize(sonarHelpers);
samples_sonar_i = addSampleToData(sonar_data, samples_sonar_i, [SENSOR_DATA.sonar]);
drawGraph(sonarHelpers, sonar_data, samples_sonar_i);
raw_data_text_ements.x[4].text(SENSOR_DATA.sonar.toFixed(2));
}
function update_debug_graphs() {
for (var i = 0; i < 4; i++) {
2014-05-16 19:02:59 +00:00
updateGraphHelperSize(debugHelpers[i]);
2014-05-05 10:11:52 +00:00
2014-04-12 21:35:22 +00:00
addSampleToData(debug_data[i], samples_debug_i, [SENSOR_DATA.debug[i]]);
drawGraph(debugHelpers[i], debug_data[i], samples_debug_i);
2015-01-28 21:24:44 +00:00
raw_data_text_ements.x[5 + i].text(SENSOR_DATA.debug[i]);
}
samples_debug_i++;
}
});
2014-04-13 13:40:42 +00:00
2014-04-13 22:15:56 +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);
2014-04-13 22:15:56 +00:00
}, 250, true);
2014-07-10 16:31:27 +00:00
if (callback) callback();
});
2014-07-10 16:31:27 +00:00
};
2014-08-12 14:20:26 +00:00
TABS.sensors.cleanup = function (callback) {
serial.emptyOutputBuffer();
2014-07-10 16:45:18 +00:00
2014-07-10 16:31:27 +00:00
if (callback) callback();
};