Copy link to clipboard
Copied
Hello, I am trying to write an expression to read in a data file from a flight controller (Pixhawk) with time stamps for different points of data. The format of the JSON is shown below with there being time data (PitotTimeS) and speed data (PitotSpeedMpS). The expression I have written will not work and gives me the error shown in the last image. The issue is I cannot simply frame through the data without refferencing the specific time it was recorded if I want everything to align properly. In addition, I have other sensors running at different rates and will need those aligned as well (if this indexed time method works that would be fine!). I am also open to formatting this in any other way that might work if there are reccomendations!
Thanks!
var PixhawkData = footage("AEData.json").sourceData;
var t = time;
var tround = t.toFixed(2);
var TimeIndex = PixhawkData.PitotTimeS.findIndex(tround);
PixhawkData.PitotSpeedMpS[TimeIndex];
Try it this way:
function getIndex(element){
return element >= time;
}
var PixhawkData = footage("AEData.json").sourceData;
var t = time;
var TimeIndex = PixhawkData.PitotTimeS.findIndex(getIndex);
if (PixhawkData.PitotTimeS > time && TimeIndex > 0) TimeIndex--;
PixhawkData.PitotSpeedMpS[TimeIndex];
Copy link to clipboard
Copied
Indices need to be plain integers. You have to use Math.round(), not toFixed(). Probably doesn't realyl make a lot of sense in your case, though, since no doubt there will be fractional frames, so may be actually timeToFrames() is the answer. Impossible to tell, since you haven't provided any example of the actual data.
Mylenium
Copy link to clipboard
Copied
Hi Mylenium, that portion of the expression is actually working and I am able to print the rounded time on screen. I believe the primary issue is with the findIndex portion of the code where I am trying to locate the index within the data that corresponds with the rounded time. I have atatched the JSON with the .txt extension since the adobe website doesnt allow JSON format files. Thanks!
Copy link to clipboard
Copied
Try it this way:
function getIndex(element){
return element >= time;
}
var PixhawkData = footage("AEData.json").sourceData;
var t = time;
var TimeIndex = PixhawkData.PitotTimeS.findIndex(getIndex);
if (PixhawkData.PitotTimeS > time && TimeIndex > 0) TimeIndex--;
PixhawkData.PitotSpeedMpS[TimeIndex];
Copy link to clipboard
Copied
This worked increibly! Thank you so much Dan!
For clarification (because my coding skills are limited to MatLAB and I want to make sure I understand whats taking place):
The first three lines setup the function that will search the input for an element of greater or equal value to the current time,
line 4 is the data reference, line 5 is now technically unused, and line 6 is where the magic happens with the getIndex function now pointing the findIndex to an element comparison to time? Line 7 is still a little fuzzy to me, the if is checking that all of the values of PitotTimeS are greater than time and that the time index is greater than 0 then returns the previous value, what does this acomplish?
Again, I appreciate your quick response and avid skill in coding this up! Thanks again!
Copy link to clipboard
Copied
Your observations are pretty much all correct. Line 7 makes sure we're picking up the index for the most recent time data that's prior (or equal) to the current time. Otherwise you end up with the index for next time coming up.