Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion MAGFit/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ <h1 style="text-align:center"><a href="" style="color: #000000; text-decoration:
</fieldset>
</td>
<td style="width: 10px;"></td>
<td>
<fieldset style="width:200px;height:80px">
<legend>Location
<img id="TT" src="../images/question-circle.svg" style="width: 1em; vertical-align: bottom"
data-tippy-content='Location used to get the expected earth field from the World Magnetic Model. Filled in from the EKF origin or last position in the log, or from the AHRS_ORIGIN_LAT and AHRS_ORIGIN_LON parameters. If the log has no location, for example when flying without GPS, enter it here. The Calculate button must be clicked after any change of location.'
data-tippy-maxWidth='750px'/>
</legend>
<label for="Lat">Latitude</label>
<input id="Lat" name="Lat" type="number" min="-90" max="90" step="any" onchange="set_need_calc(true)" style="width:90px"/><br><br>
<label for="Lng">Longitude</label>
<input id="Lng" name="Lng" type="number" min="-180" max="180" step="any" onchange="set_need_calc(true)" style="width:90px"/>
</fieldset>
</td>
<td style="width: 10px;"></td>
<td>
<fieldset style="width:200px;height:80px" id="ATTITUDE">
<legend>Attitude source
Expand All @@ -90,7 +104,7 @@ <h1 style="text-align:center"><a href="" style="color: #000000; text-decoration:
<input id="fileItem" type="file" accept=".bin" onchange="readFile(this)"><br><br><br>
<input type="button" id="calculate" value="Calculate" onclick="loading_call(() => { calculate(); })">
</td>
<td style="width: 300px;"></td>
<td style="width: 90px;"></td>
<td style="text-align:right; vertical-align:top">
<input id="OpenIn" type="button" value="Open In" disabled><br><br><br>
<img id="TT" src="../images/question-circle.svg" width="30px"
Expand Down
59 changes: 51 additions & 8 deletions MAGFit/magfit.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,41 @@ function find_end_index(time) {
}

// Run all calculation steps
// Update the expected earth field from the location inputs, return false if there is no valid location
function update_earth_field() {
const Lat = parseFloat(document.getElementById("Lat").value)
const Lng = parseFloat(document.getElementById("Lng").value)
if (isNaN(Lat) || isNaN(Lng)) {
return false
}
if ((earth_field != null) && (earth_field.Lat === Lat) && (earth_field.Lng === Lng)) {
// No change
return true
}

const field = expected_earth_field_lat_lon(Lat, Lng)
if (field == null) {
return false
}
console.log("EF: " + field.vector[0] + ", " + field.vector[1] + ", " + field.vector[2] + " at Lat: " + Lat + " Lng: " + Lng)

earth_field = field
earth_field.Lat = Lat
earth_field.Lng = Lng

// Expected body frame field must be re-calculated
source = null

return true
}

function calculate() {

if (!update_earth_field()) {
alert("Enter a valid latitude and longitude in the Location box")
return
}

select_body_frame_attitude()

calculate_bins()
Expand Down Expand Up @@ -1652,7 +1685,7 @@ function calc_error(A, B) {
return ret
}

function extractLatLon(log) {
function extractLatLon(log, get_param) {
var Lat, Lng
if (("ORGN" in log.messageTypes) && ("instances" in log.messageTypes.ORGN) && (0 in log.messageTypes.ORGN.instances)) {
const ORGN = log.get_instance("ORGN", 0)
Expand All @@ -1667,6 +1700,12 @@ function extractLatLon(log) {
Lng = POS.Lng[POS.Lng.length-1] * 10**-7
return [Lat, Lng]
}
// Recorded origin params, zero if not set
const origin_lat = get_param("AHRS_ORIGIN_LAT")
const origin_lng = get_param("AHRS_ORIGIN_LON")
if ((origin_lat != null) && (origin_lng != null) && ((origin_lat != 0) || (origin_lng != 0))) {
return [origin_lat, origin_lng]
}
return [Lat, Lng]
}

Expand Down Expand Up @@ -1948,13 +1987,11 @@ async function load(log_file) {
// Assume constant earth field
// Use origin msg
// Use last EKF origin for earth field
var [Lat, Lng] = extractLatLon(log)
earth_field = expected_earth_field_lat_lon(Lat, Lng)
if (earth_field == null) {
alert("Could not get earth field for Lat: " + Lat + " Lng: " + Lng)
return
}
console.log("EF: " + earth_field.vector[0] + ", " + earth_field.vector[1] + ", " + earth_field.vector[2] + " at Lat: " + Lat + " Lng: " + Lng)
// If there is no location in the log it can be entered manually
var [Lat, Lng] = extractLatLon(log, get_param)
earth_field = null
document.getElementById("Lat").value = (Lat != null) ? Lat : ""
document.getElementById("Lng").value = (Lng != null) ? Lng : ""

// Workout which attitude source to use, Note that this is not clever enough to deal with primary changing in flight
const EKF_TYPE = get_param("AHRS_EKF_TYPE")
Expand Down Expand Up @@ -2223,6 +2260,12 @@ async function load(log_file) {
}
}

if (!update_earth_field()) {
alert("No location found in log, enter latitude and longitude in the Location box then click Calculate")
set_need_calc(true)
return
}

calculate()

}
Expand Down
Loading