Skip to content

Added cartesian gps coords relative home point as additional computed fields #773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/flightlog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FlightLogIndex } from "./flightlog_index";
import { FlightLogParser } from "./flightlog_parser";
import { GPS_transform } from "./gps_transform";
import {
MAX_MOTOR_NUMBER,
DSHOT_MIN_VALUE,
Expand Down Expand Up @@ -29,7 +30,7 @@ import {
* Window based smoothing of fields is offered.
*/
export function FlightLog(logData) {
let ADDITIONAL_COMPUTED_FIELD_COUNT = 15 /** attitude + PID_SUM + PID_ERROR + RCCOMMAND_SCALED **/,
let ADDITIONAL_COMPUTED_FIELD_COUNT = 19 /** attitude + PID_SUM + PID_ERROR + RCCOMMAND_SCALED + GPS coord**/,
that = this,
logIndex = 0,
logIndexes = new FlightLogIndex(logData),
Expand All @@ -44,7 +45,8 @@ export function FlightLog(logData) {
// Map from field indexes to smoothing window size in microseconds
fieldSmoothing = {},
maxSmoothing = 0,
smoothedCache = new FIFOCache(2);
smoothedCache = new FIFOCache(2),
gpsTransform = null;

//Public fields:
this.parser = parser;
Expand Down Expand Up @@ -130,7 +132,7 @@ export function FlightLog(logData) {
index = index ?? logIndex;
return logIndexes.getIntraframeDirectory(index).maxTime;
};

this.getActualLoggedTime = function (index) {
index = index ?? logIndex;
const directory = logIndexes.getIntraframeDirectory(index);
Expand Down Expand Up @@ -281,6 +283,9 @@ export function FlightLog(logData) {
if (!that.isFieldDisabled().GYRO && !that.isFieldDisabled().SETPOINT) {
fieldNames.push("axisError[0]", "axisError[1]", "axisError[2]"); // Custom calculated error field
}
if (!that.isFieldDisabled().GPS) {
fieldNames.push("gpsCartesianCoords[0]", "gpsCartesianCoords[1]", "gpsCartesianCoords[2]", "gpsDistance"); // GPS coords in cartesian system
}

fieldNameToIndex = {};
for (let i = 0; i < fieldNames.length; i++) {
Expand Down Expand Up @@ -519,9 +524,9 @@ export function FlightLog(logData) {
}
break;
case "H":
// TODO
// contains coordinates only
// should be handled separately
const homeAltitude = frame.length > 2 ? frame[2 ] /10 : 0; // will work after BF firmware improvement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to declare with let before the switch case and assign value here.

Copy link
Contributor Author

@demvlad demvlad Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What result will in this variable with different frames type? We need it for "H" type only.
The SonarCloud shows the issue, but in my opinion, this is more good place to define it.
I've tryed to put this expression in the next code rows directly, without variable. But SonarCloud shows the issue too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonar won't complain if you wrap the case in brackets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it works.

gpsTransform = new GPS_transform(frame[0] / 10000000, frame[1] / 10000000, homeAltitude, 0.0);
break;
case "G":
// The frameValid can be false, when no GPS home (the G frames contains GPS position as diff of GPS Home position).
// But other data from the G frame can be valid (time, num sats)
Expand Down Expand Up @@ -631,6 +636,11 @@ export function FlightLog(logData) {
fieldNameToIndex["setpoint[2]"],
fieldNameToIndex["setpoint[3]"],
];
let gpsCoord = [
fieldNameToIndex["GPS_coord[0]"],
fieldNameToIndex["GPS_coord[1]"],
fieldNameToIndex["GPS_altitude"],
];

const flightModeFlagsIndex = fieldNameToIndex["flightModeFlags"]; // This points to the flightmode data

Expand Down Expand Up @@ -690,6 +700,11 @@ export function FlightLog(logData) {
axisPID = false;
}

if (!gpsCoord[0]) {
gpsCoord = false;
}


sourceChunkIndex = 0;
destChunkIndex = 0;

Expand Down Expand Up @@ -830,6 +845,22 @@ export function FlightLog(logData) {
}
}

// Calculate cartesian coords by GPS
if (!that.isFieldDisabled().GPS) {
if (gpsTransform && gpsCoord && srcFrame[gpsCoord[0]]) {
const gpsCartesianCoords = gpsTransform.WGS_BS(srcFrame[gpsCoord[0]] / 10000000, srcFrame[gpsCoord[1] ] /10000000, srcFrame[gpsCoord[2]] / 10);
destFrame[fieldIndex++] = gpsCartesianCoords.x;
destFrame[fieldIndex++] = gpsCartesianCoords.y;
destFrame[fieldIndex++] = gpsCartesianCoords.z;
destFrame[fieldIndex++] = Math.sqrt(gpsCartesianCoords.x * gpsCartesianCoords.x + gpsCartesianCoords.z * gpsCartesianCoords.z);
} else {
destFrame[fieldIndex++] = 0;
destFrame[fieldIndex++] = 0;
destFrame[fieldIndex++] = 0;
destFrame[fieldIndex++] = 0;
}
}

// Remove empty fields at the end
destFrame.splice(fieldIndex);
}
Expand Down
12 changes: 12 additions & 0 deletions src/flightlog_fields_presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ const FRIENDLY_FIELD_NAMES = {
GPS_altitude: "GPS Altitude ASL",
GPS_speed: "GPS Speed",
GPS_ground_course: "GPS Heading",

"gpsCartesianCoords[all]": "GPS Coords",
"gpsCartesianCoords[0]": "GPS Coords [X]",
"gpsCartesianCoords[1]": "GPS Coords [Y]",
"gpsCartesianCoords[2]": "GPS Coords [Z]",
gpsDistance: "GPS Home distance",
};

const DEBUG_FRIENDLY_FIELD_NAMES_INITIAL = {
Expand Down Expand Up @@ -1639,6 +1645,12 @@ FlightLogFieldPresenter.decodeFieldToFriendly = function (
}
case "GPS_ground_course":
return `${(value / 10).toFixed(1)} °`;

case "gpsCartesianCoords[0]":
case "gpsCartesianCoords[1]":
case "gpsCartesianCoords[2]":
case "gpsDistance":
return `${value.toFixed(0)} m`;

case "debug[0]":
case "debug[1]":
Expand Down
67 changes: 67 additions & 0 deletions src/gps_transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export function GPS_transform(Lat0, Lon0, H0, Heading) {

function deg2rad(deg) {
return deg * Math.PI / 180.0;
}

Lat0 = deg2rad(Lat0);
Lon0 = deg2rad(Lon0);
const Semimajor = 6378137.0,
Flat = 1.0 / 298.257223563,
Ecc_2 = Flat * (2 - Flat),
SinB = Math.sin(Lat0),
CosB = Math.cos(Lat0),
SinL = Math.sin(Lon0),
CosL = Math.cos(Lon0),
N = Semimajor / Math.sqrt(1.0 - Ecc_2 * SinB * SinB),

a11 = -SinB * CosL,
a12 = -SinB * SinL,
a13 = CosB,
a21 = -SinL,
a22 = CosL,
a23 = 0,
a31 = CosL * CosB,
a32 = CosB * SinL,
a33 = SinB,

X0 = (N + H0) * CosB * CosL,
Y0 = (N + H0) * CosB * SinL,
Z0 = (N + H0 - Ecc_2 * N) * SinB,
c11 = Math.cos( deg2rad(Heading) ),
c12 = Math.sin( deg2rad(Heading) ),
c21 = -c12,
c22 = c11;

this.WGS_ECEF = function (Lat, Lon, H) {
Lat = deg2rad(Lat);
Lon = deg2rad(Lon);
const
SinB = Math.sin(Lat),
CosB = Math.cos(Lat),
SinL = Math.sin(Lon),
CosL = Math.cos(Lon),
N = Semimajor / Math.sqrt(1 - Ecc_2 * SinB * SinB);

return {
x: (N + H) * CosB * CosL,
y: (N + H) * CosB * SinL,
z: (N + H - Ecc_2 * N) * SinB,
};
};

this.ECEF_BS = function (pos) {
const PosX1= a11 * (pos.x - X0) + a12 * (pos.y - Y0) + a13 * (pos.z - Z0);
const PosZ1= a21 * (pos.x - X0) + a22 * (pos.y - Y0) + a23 * (pos.z - Z0);

return {
x: c11 * PosX1 + c12 * PosZ1,
y: a31 * (pos.x - X0) + a32 * (pos.y - Y0) + a33 * (pos.z - Z0),
z: c21 * PosX1 + c22 * PosZ1,
};
};

this.WGS_BS = function (Lat, Lon, H) {
return this.ECEF_BS(this.WGS_ECEF(Lat, Lon, H));
};
}
7 changes: 7 additions & 0 deletions src/graph_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,13 @@ GraphConfig.getExampleGraphConfigs = function (flightLog, graphNames) {
"GPS_coord[all]",
],
});
EXAMPLE_GRAPHS.push({
label: "GPS Cartesian coords",
fields: [
"gpsCartesianCoords[all]",
"gpsDistance",
],
});
}

for (const srcGraph of EXAMPLE_GRAPHS) {
Expand Down