Skip to content

Commit 5c4d3da

Browse files
authored
Improve sensor handling and add configuration for sonar and opticalflow (#4327)
* Add configuration for sonar and opticalflow * Add opticalflow info and put sensortypes in separate file * Add new file * did not add this :) * rename * update comment * Do not duplicate messages * Fix sonar * Update messages * Add some helper functions * Add gps protocol types * Add opticalflow to sensor_helpers
1 parent 4d8587b commit 5c4d3da

File tree

10 files changed

+305
-166
lines changed

10 files changed

+305
-166
lines changed

locales/en/messages.json

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,11 @@
947947
},
948948
"initialSetupNotInBuild": {
949949
"message": "Not included in build",
950-
"description": "Message that pops up when hardware support not are supported in Build"
950+
"description": "Message that pops up when hardware support is not included in build"
951951
},
952952
"initialSetupNotDetected": {
953-
"message": "Not detected",
954-
"description": "Message that pops up when hardware are not detected"
953+
"message": "Not detected or disabled",
954+
"description": "Message that pops up when hardware is not detected or disabled"
955955
},
956956
"initialSetupRSSIValue": {
957957
"message": "$1 dBm"
@@ -980,6 +980,9 @@
980980
"initialSetupSensorSonar": {
981981
"message": "Sonar:"
982982
},
983+
"initialSetupSensorOpticalflow": {
984+
"message": "Optical Flow:"
985+
},
983986
"initialSetupSensorRadar": {
984987
"message": "Radar:"
985988
},
@@ -1408,6 +1411,24 @@
14081411
"configurationMagDeclinationHelp": {
14091412
"message": "The magnetic declination is the angle between magnetic north and true north. It is different for every location on earth. You can find the declination for your location on the internet"
14101413
},
1414+
"configurationRangefinder": {
1415+
"message": "$t(initialSetupSensorSonar.message)"
1416+
},
1417+
"configurationRangefinderHelp": {
1418+
"message": "Enable rangefinder to measure distance to ground in cm"
1419+
},
1420+
"configurationRangefinderType": {
1421+
"message": "Type"
1422+
},
1423+
"configurationOpticalflow": {
1424+
"message": "$t(initialSetupSensorOpticalflow.message)"
1425+
},
1426+
"configurationOpticalflowHelp": {
1427+
"message": "Enable optical flow sensor"
1428+
},
1429+
"configurationOpticalflowType": {
1430+
"message": "$t(configurationRangefinderType.message)"
1431+
},
14111432
"configurationBoardAlignment": {
14121433
"message": "Board Alignment"
14131434
},

src/js/fc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ const FC = {
592592
baro_hardware: 0,
593593
mag_hardware: 0,
594594
sonar_hardware: 0,
595+
opticalflow_hardware: 0,
595596
};
596597

597598
this.SENSOR_CONFIG_ACTIVE = { gyro_hardware: 0, ...this.SENSOR_CONFIG };

src/js/msp/MSPHelper.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,9 @@ MspHelper.prototype.process_data = function (dataHandler) {
12091209
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_46)) {
12101210
FC.SENSOR_CONFIG.sonar_hardware = data.readU8();
12111211
}
1212+
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
1213+
FC.SENSOR_CONFIG.opticalflow_hardware = data.readU8();
1214+
}
12121215
break;
12131216
case MSPCodes.MSP2_SENSOR_CONFIG_ACTIVE:
12141217
FC.SENSOR_CONFIG_ACTIVE.gyro_hardware = data.readU8();
@@ -1218,6 +1221,9 @@ MspHelper.prototype.process_data = function (dataHandler) {
12181221
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_46)) {
12191222
FC.SENSOR_CONFIG_ACTIVE.sonar_hardware = data.readU8();
12201223
}
1224+
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
1225+
FC.SENSOR_CONFIG_ACTIVE.opticalflow_hardware = data.readU8();
1226+
}
12211227
break;
12221228
case MSPCodes.MSP2_MCU_INFO:
12231229
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
@@ -2206,10 +2212,13 @@ MspHelper.prototype.crunch = function (code, modifierCode = undefined) {
22062212
buffer.push16(FC.ADVANCED_TUNING.tpaBreakpoint);
22072213
break;
22082214
case MSPCodes.MSP_SET_SENSOR_CONFIG:
2209-
buffer
2210-
.push8(FC.SENSOR_CONFIG.acc_hardware)
2211-
.push8(FC.SENSOR_CONFIG.baro_hardware)
2212-
.push8(FC.SENSOR_CONFIG.mag_hardware);
2215+
buffer.push8(FC.SENSOR_CONFIG.acc_hardware);
2216+
buffer.push8(FC.SENSOR_CONFIG.baro_hardware);
2217+
buffer.push8(FC.SENSOR_CONFIG.mag_hardware);
2218+
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
2219+
buffer.push8(FC.SENSOR_CONFIG.sonar_hardware);
2220+
buffer.push8(FC.SENSOR_CONFIG.opticalflow_hardware);
2221+
}
22132222
break;
22142223

22152224
case MSPCodes.MSP_SET_NAME:

src/js/sensor_helpers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export function have_sensor(sensors_detected, sensor_code) {
1515
return bit_check(sensors_detected, 4);
1616
case "gyro":
1717
return bit_check(sensors_detected, 5);
18+
case "opticalflow":
19+
return bit_check(sensors_detected, 6);
1820
}
1921
return false;
2022
}

src/js/sensor_types.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import semver from "semver";
2+
import FC from "./fc";
3+
import { API_VERSION_1_47 } from "./data_storage";
4+
5+
export function sensorTypes() {
6+
const sensorTypes = {
7+
acc: {
8+
name: "Accelerometer",
9+
elements: [
10+
"AUTO",
11+
"NONE",
12+
"ADXL345",
13+
"MPU6050",
14+
"MMA8452",
15+
"BMA280",
16+
"LSM303DLHC",
17+
"MPU6000",
18+
"MPU6500",
19+
"MPU9250",
20+
"ICM20601",
21+
"ICM20602",
22+
"ICM20608G",
23+
"ICM20649",
24+
"ICM20689",
25+
"ICM42605",
26+
"ICM42688P",
27+
"BMI160",
28+
"BMI270",
29+
"LSM6DSO",
30+
"LSM6DSV16X",
31+
"VIRTUAL",
32+
],
33+
},
34+
gyro: {
35+
name: "Gyroscope",
36+
elements: [
37+
"AUTO",
38+
"NONE",
39+
"MPU6050",
40+
"L3G4200D",
41+
"MPU3050",
42+
"L3GD20",
43+
"MPU6000",
44+
"MPU6500",
45+
"MPU9250",
46+
"ICM20601",
47+
"ICM20602",
48+
"ICM20608G",
49+
"ICM20649",
50+
"ICM20689",
51+
"ICM42605",
52+
"ICM42688P",
53+
"BMI160",
54+
"BMI270",
55+
"LSM6DSO",
56+
"LSM6DSV16X",
57+
"VIRTUAL",
58+
],
59+
},
60+
baro: {
61+
name: "Barometer",
62+
elements: [
63+
"DEFAULT",
64+
"NONE",
65+
"BMP085",
66+
"MS5611",
67+
"BMP280",
68+
"LPS",
69+
"QMP6988",
70+
"BMP388",
71+
"DPS310",
72+
"2SMPB_02B",
73+
"VIRTUAL",
74+
],
75+
},
76+
mag: {
77+
name: "Magnetometer",
78+
elements: [
79+
"DEFAULT",
80+
"NONE",
81+
"HMC5883",
82+
"AK8975",
83+
"AK8963",
84+
"QMC5883",
85+
"LIS2MDL",
86+
"LIS3MDL",
87+
"MPU925X_AK8963",
88+
"IST8310",
89+
],
90+
},
91+
gps: {
92+
name: "GPS",
93+
elements: ["NMEA", "UBLOX", "MSP"],
94+
},
95+
sonar: {
96+
name: "Sonar",
97+
elements: ["NONE", "HCSR04", "TFMINI", "TF02", "MTF01", "MTF02", "MTF01P", "MTF02P"],
98+
},
99+
opticalflow: {
100+
name: "Optical Flow",
101+
elements: ["NONE", "MT"],
102+
},
103+
};
104+
105+
function removeElement(elements, element) {
106+
const index = elements.indexOf(element);
107+
if (index !== -1) {
108+
elements.splice(index, 1);
109+
}
110+
}
111+
112+
function addElement(elements, element, afterElement) {
113+
const elementIndex = elements.indexOf(element);
114+
if (elementIndex === -1) {
115+
elements.splice(elements.indexOf(afterElement) + 1, 0, element);
116+
}
117+
}
118+
119+
const gyroElements = sensorTypes.gyro.elements;
120+
const accElements = sensorTypes.acc.elements;
121+
const gpsElements = sensorTypes.gps.elements;
122+
123+
// remove deprecated sensors or add new ones
124+
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
125+
removeElement(gyroElements, "L3G4200D");
126+
removeElement(gyroElements, "MPU3050");
127+
addElement(gyroElements, "IIM42653", "LSM6DSV16X");
128+
129+
removeElement(accElements, "ADXL345");
130+
removeElement(accElements, "MMA8452");
131+
removeElement(accElements, "BMA280");
132+
removeElement(accElements, "LSM303DLHC");
133+
addElement(accElements, "IIM42653", "LSM6DSV16X");
134+
135+
addElement(gpsElements, "VIRTUAL", "MSP");
136+
}
137+
138+
return sensorTypes;
139+
}

src/js/tabs/configuration.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { API_VERSION_1_45, API_VERSION_1_46, API_VERSION_1_47 } from "../data_st
1010
import { updateTabList } from "../utils/updateTabList";
1111
import $ from "jquery";
1212
import { have_sensor } from "../sensor_helpers";
13+
import { sensorTypes } from "../sensor_types";
1314

1415
const configuration = {
1516
analyticsChanges: {},
@@ -157,6 +158,36 @@ configuration.initialize = function (callback) {
157158
toggleMagCustomAlignmentInputs();
158159
});
159160

161+
// Range finder
162+
163+
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
164+
const rangeFinderType_e = $("select.rangefinderType");
165+
const sonarElements = sensorTypes().sonar.elements;
166+
167+
for (let i = 0; i < sonarElements.length; i++) {
168+
rangeFinderType_e.append(`<option value="${i}">${sonarElements[i]}</option>`);
169+
}
170+
171+
rangeFinderType_e.val(FC.SENSOR_CONFIG.sonar_hardware);
172+
} else {
173+
$(".tab-configuration .rangefinder").parent().hide();
174+
}
175+
176+
// Optical flow sensor
177+
178+
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
179+
const opticalflowType_e = $("select.opticalflowType");
180+
const opticalflowElements = sensorTypes().opticalflow.elements;
181+
182+
for (let i = 0; i < opticalflowElements.length; i++) {
183+
opticalflowType_e.append(`<option value="${i}">${opticalflowElements[i]}</option>`);
184+
}
185+
186+
opticalflowType_e.val(FC.SENSOR_CONFIG.opticalflow_hardware);
187+
} else {
188+
$(".tab-configuration .opticalflow").parent().hide();
189+
}
190+
160191
// Multi gyro config
161192

162193
const GYRO_DETECTION_FLAGS = {
@@ -442,6 +473,11 @@ configuration.initialize = function (callback) {
442473
FC.COMPASS_CONFIG.mag_declination = $('input[name="mag_declination"]').val();
443474
}
444475

476+
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
477+
FC.SENSOR_CONFIG.sonar_hardware = $("select.rangefinderType").val();
478+
FC.SENSOR_CONFIG.opticalflow_hardware = $("select.opticalflowType").val();
479+
}
480+
445481
FC.ARMING_CONFIG.small_angle = parseInt($('input[id="configurationSmallAngle"]').val());
446482

447483
FC.SENSOR_ALIGNMENT.gyro_to_use = parseInt(orientation_gyro_to_use_e.val());

src/js/tabs/gps.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { i18n } from "../localization";
22
import semver from "semver";
3-
import { API_VERSION_1_46, API_VERSION_1_47 } from "../data_storage";
3+
import { API_VERSION_1_46 } from "../data_storage";
44
import GUI, { TABS } from "../gui";
55
import FC from "../fc";
66
import MSP from "../msp";
@@ -12,6 +12,7 @@ import { updateTabList } from "../utils/updateTabList";
1212
import { initMap } from "./map";
1313
import { fromLonLat } from "ol/proj";
1414
import { ispConnected } from "../utils/connection";
15+
import { sensorTypes } from "../sensor_types";
1516

1617
const gps = {};
1718

@@ -88,11 +89,7 @@ gps.initialize = async function (callback) {
8889
checkUpdateGpsControls();
8990

9091
// generate GPS
91-
const gpsProtocols = ["NMEA", "UBLOX", "MSP"];
92-
93-
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
94-
gpsProtocols.push("VIRTUAL");
95-
}
92+
const gpsProtocols = sensorTypes().gps.elements;
9693

9794
const gpsBaudRates = ["115200", "57600", "38400", "19200", "9600"];
9895

0 commit comments

Comments
 (0)