Skip to content
Merged
Changes from 2 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
83 changes: 47 additions & 36 deletions src/flightlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,20 +610,13 @@ export function FlightLog(logData) {
* sourceChunks and destChunks can be the same array.
*/
function injectComputedFields(sourceChunks, destChunks) {
let gyroADC = [
fieldNameToIndex["gyroADC[0]"],
fieldNameToIndex["gyroADC[1]"],
fieldNameToIndex["gyroADC[2]"],
];
let accSmooth = [
fieldNameToIndex["accSmooth[0]"],
fieldNameToIndex["accSmooth[1]"],
fieldNameToIndex["accSmooth[2]"],
];
let magADC = [
fieldNameToIndex["magADC[0]"],
fieldNameToIndex["magADC[1]"],
fieldNameToIndex["magADC[2]"],
let gyroADC = [fieldNameToIndex["gyroADC[0]"], fieldNameToIndex["gyroADC[1]"], fieldNameToIndex["gyroADC[2]"]];
let accSmooth = [fieldNameToIndex["accSmooth[0]"], fieldNameToIndex["accSmooth[1]"], fieldNameToIndex["accSmooth[2]"]];
let magADC = [fieldNameToIndex["magADC[0]"], fieldNameToIndex["magADC[1]"], fieldNameToIndex["magADC[2]"]];
Comment on lines +613 to +615
Copy link
Member

Choose a reason for hiding this comment

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

No need to refactor seeing next line :)

let imuQuaternion = [
fieldNameToIndex["imuQuaternion[0]"],
fieldNameToIndex["imuQuaternion[1]"],
fieldNameToIndex["imuQuaternion[2]"],
];
let rcCommand = [
fieldNameToIndex["rcCommand[0]"],
Expand Down Expand Up @@ -668,15 +661,14 @@ export function FlightLog(logData) {

let sourceChunkIndex;
let destChunkIndex;
let attitude;

const sysConfig = that.getSysConfig();

if (destChunks.length === 0) {
return;
}

// Do we have mag fields? If not mark that data as absent
// Do we have mag fields? If not mark that data as absent
if (!magADC[0]) {
magADC = false;
}
Expand All @@ -689,6 +681,10 @@ export function FlightLog(logData) {
accSmooth = false;
}

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

if (!rcCommand[0]) {
rcCommand = false;
}
Expand Down Expand Up @@ -726,33 +722,48 @@ export function FlightLog(logData) {

if (!destChunk.hasAdditionalFields) {
destChunk.hasAdditionalFields = true;

let chunkIMU = new IMU(sourceChunks[sourceChunkIndex].initialIMU);
const chunkIMU = new IMU(sourceChunk.initialIMU);

for (let i = 0; i < sourceChunk.frames.length; i++) {
let srcFrame = sourceChunk.frames[i],
destFrame = destChunk.frames[i],
fieldIndex = destFrame.length - ADDITIONAL_COMPUTED_FIELD_COUNT;

if (!that.isFieldDisabled().GYRO) {
//don't calculate attitude if no gyro data
attitude = chunkIMU.updateEstimatedAttitude(
[
srcFrame[gyroADC[0]],
srcFrame[gyroADC[1]],
srcFrame[gyroADC[2]],
],
[
srcFrame[accSmooth[0]],
srcFrame[accSmooth[1]],
srcFrame[accSmooth[2]],
],
srcFrame[FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_TIME],
sysConfig.acc_1G,
sysConfig.gyroScale,
magADC
);
if (imuQuaternion) {
const scaleFromFixedInt16 = 0x7FFF; // 0x7FFF = 2^15 - 1
const q = {
x: srcFrame[imuQuaternion[0]] / scaleFromFixedInt16,
y: srcFrame[imuQuaternion[1]] / scaleFromFixedInt16,
z: srcFrame[imuQuaternion[2]] / scaleFromFixedInt16,
w: 1.0,
Copy link
Contributor

Choose a reason for hiding this comment

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

$w = sqrt(1 - (x^2 + y^2 + z^2))$

};
const xx = q.x * q.x,
xy = q.x * q.y,
xz = q.x * q.z,
wx = q.w * q.x,
yy = q.y * q.y,
yz = q.y * q.z,
wy = q.w * q.y,
zz = q.z * q.z,
wz = q.w * q.z;
let roll = Math.atan2((+2.0 * (wx + yz)), (+1.0 - 2.0 * (xx + yy)));
let pitch = ((0.5 * Math.PI) - Math.acos(+2.0 * (wy - xz)));
let heading = -Math.atan2((+2.0 * (wz + xy)), (+1.0 - 2.0 * (yy + zz)));
if (heading < 0) {
heading += 2.0 * Math.PI;
}

destFrame[fieldIndex++] = roll;
destFrame[fieldIndex++] = pitch;
destFrame[fieldIndex++] = heading;
} else {
const attitude = chunkIMU.updateEstimatedAttitude(
[srcFrame[gyroADC[0]], srcFrame[gyroADC[1]], srcFrame[gyroADC[2]]],
[srcFrame[accSmooth[0]], srcFrame[accSmooth[1]], srcFrame[accSmooth[2]]],
srcFrame[FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_TIME],
sysConfig.acc_1G,
sysConfig.gyroScale,
magADC);
destFrame[fieldIndex++] = attitude.roll;
destFrame[fieldIndex++] = attitude.pitch;
destFrame[fieldIndex++] = attitude.heading;
Expand Down