Skip to content

Commit

Permalink
Fixed FGMagnetometer which updated the magnetic field every time step.
Browse files Browse the repository at this point in the history
The counter was reset to zero each time the magnetic field was updated meaning that the update was processed again at the next time step or said otherwise the field was updated at each time step.

So removed the reset of the counter to zero (the variable 'counter' is unsigned int so we need more than 4 billions time step to overflow and, would that happen, the counter will be reset to zero which is completely harmless).

Also made the unit test more robust in the process.
  • Loading branch information
bcoconni committed Dec 6, 2020
1 parent 05bd64a commit 4595a18
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/models/flight_control/FGMagnetometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,19 @@ FGMagnetometer::~FGMagnetometer()
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGMagnetometer::ResetPastStates(void)
{
FGSensor::ResetPastStates();
counter = 0;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGMagnetometer::updateInertialMag(void)
{
if (counter++ % INERTIAL_UPDATE_RATE == 0)//dont need to update every iteration
{
counter = 0;

usedLat = (Propagate->GetGeodLatitudeRad());//radians, N and E lat and long are positive, S and W negative
usedLon = (Propagate->GetLongitude());//radians
usedAlt = (Propagate->GetGeodeticAltitude()*fttom*0.001);//km
Expand Down
1 change: 1 addition & 0 deletions src/models/flight_control/FGMagnetometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class FGMagnetometer : public FGSensor, public FGSensorOrientation
~FGMagnetometer();

bool Run (void) override;
void ResetPastStates(void) override;

private:
std::shared_ptr<FGPropagate> Propagate;
Expand Down
8 changes: 7 additions & 1 deletion tests/TestMagnetometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ def test(self):
tripod = FlightModel(self, "tripod")
tripod.include_system_test_file("magnetometer.xml")
fdm = tripod.start()
self.assertAlmostEqual(fdm['test/magnetic-field']/27661.1, 1.0, 4)
fdm['forces/hold-down'] = 1.0
magnetic_field_t0 = fdm['test/magnetic-field']
# Check that the magnetic field remains identical for the first 10000
# iterations given that the vehicle is motionless.
for i in range(10000):
fdm.run()
self.assertAlmostEqual(fdm['test/magnetic-field'] / magnetic_field_t0, 1.0, delta=1E-6)


RunTest(TestMagnetometer)

0 comments on commit 4595a18

Please sign in to comment.