Skip to content

Commit 0f94773

Browse files
authored
Merge pull request #376 from sparkfun/Fix_#356
Fix #356
2 parents 06f6168 + 87ca0fa commit 0f94773

File tree

5 files changed

+69
-59
lines changed

5 files changed

+69
-59
lines changed

Firmware/RTK_Everywhere/States.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ void stateUpdate()
260260
// Check for <1m horz accuracy before starting surveyIn
261261
char accuracy[20];
262262
char temp[20];
263-
const char *units = getHpaUnits(hpa, temp, sizeof(temp), 2);
264-
const char *accUnits = getHpaUnits(gnssGetSurveyInStartingAccuracy(), accuracy, sizeof(accuracy), 2);
263+
const char *units = getHpaUnits(hpa, temp, sizeof(temp), 2, true);
264+
// gnssGetSurveyInStartingAccuracy is 10m max
265+
const char *accUnits = getHpaUnits(gnssGetSurveyInStartingAccuracy(), accuracy, sizeof(accuracy), 2, false);
265266
systemPrintf("Waiting for Horz Accuracy < %s (%s): %s%s%s%s, SIV: %d\r\n", accuracy, accUnits, temp,
266267
(accUnits != units) ? " (" : "", (accUnits != units) ? units : "",
267268
(accUnits != units) ? ")" : "", siv);
@@ -313,7 +314,7 @@ void stateUpdate()
313314
else
314315
{
315316
char temp[20];
316-
const char *units = getHpaUnits(meanAccuracy, temp, sizeof(temp), 3);
317+
const char *units = getHpaUnits(meanAccuracy, temp, sizeof(temp), 3, true);
317318
systemPrintf("Time elapsed: %d Accuracy (%s): %s SIV: %d\r\n", observationTime, units, temp, siv);
318319

319320
if (observationTime > maxSurveyInWait_s)

Firmware/RTK_Everywhere/System.ino

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ void printReports()
369369

370370
char modifiedHpa[20];
371371
const char *hpaUnits =
372-
getHpaUnits(hpa, modifiedHpa, sizeof(modifiedHpa), 3); // Returns string of the HPA units
372+
getHpaUnits(hpa, modifiedHpa, sizeof(modifiedHpa), 3, true); // Returns string of the HPA units
373373

374374
systemPrintf("Rover Accuracy (%s): %s, SIV: %d GNSS State: ", hpaUnits, modifiedHpa,
375375
gnssGetSatellitesInView());
@@ -716,44 +716,49 @@ void reportFatalError(const char *errorMsg)
716716
}
717717
}
718718

719-
// Returns string of the HPA units
720-
const char *getHpaUnits(double hpa, char *buffer, int length, int decimals)
719+
// This allows the measurementScaleTable to be alphabetised if desired
720+
int measurementScaleToIndex(uint8_t scale)
721721
{
722-
const char *units;
723-
724-
// Return the units
725-
if (settings.measurementScale >= MEASUREMENT_SCALE_MAX)
722+
for (int i = 0; i < MEASUREMENT_UNITS_MAX; i++)
726723
{
727-
units = "Unknown";
728-
strcpy(buffer, "Unknown");
724+
if (measurementScaleTable[i].measurementUnit == scale)
725+
return i;
729726
}
730-
else
727+
728+
return -1; // This should never happen...
729+
}
730+
731+
// Returns string of the HPA units
732+
const char *getHpaUnits(double hpa, char *buffer, int length, int decimals, bool limit)
733+
{
734+
static const char unknown[] = "Unknown";
735+
736+
int i = measurementScaleToIndex(settings.measurementScale);
737+
if (i >= 0)
731738
{
732-
units = measurementScaleUnits[settings.measurementScale];
739+
const char *units = measurementScaleTable[i].measurementScale1NameShort;
733740

734-
// Convert the HPA value to a string
735-
switch (settings.measurementScale)
741+
hpa *= measurementScaleTable[i].multiplierMetersToScale1; // Scale1: m->m or m->ft
742+
743+
bool limited = false;
744+
if (limit && (hpa > measurementScaleTable[i].reportingLimitScale1)) // Limit the reported accuracy (Scale1)
736745
{
737-
case MEASUREMENTS_IN_METERS:
738-
snprintf(buffer, length, "%.*f", decimals, hpa);
739-
break;
746+
limited = true;
747+
hpa = measurementScaleTable[i].reportingLimitScale1;
748+
}
740749

741-
case MEASUREMENTS_IN_FEET_INCHES:
742-
double inches;
743-
double feet;
744-
inches = hpa * INCHES_IN_A_METER;
745-
feet = inches / 12.;
746-
if (inches >= 36.)
747-
snprintf(buffer, length, "%.*f", decimals, feet);
748-
else
749-
{
750-
units = "in";
751-
snprintf(buffer, length, "%.*f", decimals, inches);
752-
}
753-
break;
750+
if (hpa <= measurementScaleTable[i].changeFromScale1To2At) // Scale2: m->m or ft->in
751+
{
752+
hpa *= measurementScaleTable[i].multiplierScale1To2;
753+
units = measurementScaleTable[i].measurementScale2NameShort;
754754
}
755+
756+
snprintf(buffer, length, "%s%.*f", limited ? "> " : "", decimals, hpa);
757+
return units;
755758
}
756-
return units;
759+
760+
strncpy(buffer, unknown, length);
761+
return unknown;
757762
}
758763

759764
// Helper method to convert GNSS time and date into Unix Epoch

Firmware/RTK_Everywhere/menuSystem.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ void menuSystem()
224224

225225
systemPrintln("r) Reset all settings to default");
226226

227-
systemPrintf("u) Toggle printed measurement scale: %s\r\n", measurementScaleName[settings.measurementScale]);
227+
systemPrintf("u) Printed measurement units: %s\r\n",
228+
measurementScaleTable[measurementScaleToIndex(settings.measurementScale)].measurementScaleName);
228229

229230
systemPrintf("z) Set time zone offset: %02d:%02d:%02d\r\n", settings.timeZoneHours, settings.timeZoneMinutes,
230231
settings.timeZoneSeconds);
@@ -294,7 +295,7 @@ void menuSystem()
294295
else if (incoming == 'u')
295296
{
296297
settings.measurementScale += 1;
297-
if (settings.measurementScale >= MEASUREMENT_SCALE_MAX)
298+
if (settings.measurementScale >= MEASUREMENT_UNITS_MAX)
298299
settings.measurementScale = 0;
299300
}
300301
else if (incoming == 'z')
@@ -1353,7 +1354,7 @@ void printCurrentConditions()
13531354

13541355
float hpa = gnssGetHorizontalAccuracy();
13551356
char temp[20];
1356-
const char *units = getHpaUnits(hpa, temp, sizeof(temp), 3);
1357+
const char *units = getHpaUnits(hpa, temp, sizeof(temp), 3, true);
13571358
systemPrintf(", HPA (%s): %s", units, temp);
13581359

13591360
systemPrint(", Lat: ");

Firmware/RTK_Everywhere/settings.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -599,29 +599,34 @@ enum PeriodDisplayValues
599599
#define PERIODIC_SETTING(x) (settings.periodicDisplay & PERIODIC_MASK(x))
600600
#define PERIODIC_TOGGLE(x) settings.periodicDisplay = settings.periodicDisplay ^ PERIODIC_MASK(x)
601601

602-
#define INCHES_IN_A_METER 39.37009424
602+
#define INCHES_IN_A_METER 39.37007874
603+
#define FEET_IN_A_METER 3.280839895
603604

604-
enum MeasurementScale
605-
{
606-
MEASUREMENTS_IN_METERS = 0,
607-
MEASUREMENTS_IN_FEET_INCHES,
608-
// Add new measurement scales above this line
609-
MEASUREMENT_SCALE_MAX
610-
};
611-
612-
const char * const measurementScaleName[] =
605+
typedef enum
613606
{
614-
"meters",
615-
"feet and inches",
616-
};
617-
const int measurementScaleNameEntries = sizeof(measurementScaleName) / sizeof(measurementScaleName[0]);
607+
MEASUREMENT_UNITS_METERS = 0,
608+
MEASUREMENT_UNITS_FEET_INCHES,
609+
// Add new measurement units above this line
610+
MEASUREMENT_UNITS_MAX
611+
} measurementUnits;
618612

619-
const char * const measurementScaleUnits[] =
613+
typedef struct
620614
{
621-
"m",
622-
"ft",
615+
const measurementUnits measurementUnit;
616+
const char *measurementScaleName;
617+
const char *measurementScale1NameShort;
618+
const char *measurementScale2NameShort;
619+
const double multiplierMetersToScale1;
620+
const double changeFromScale1To2At;
621+
const double multiplierScale1To2;
622+
const double reportingLimitScale1;
623+
} measurementScaleEntry;
624+
625+
const measurementScaleEntry measurementScaleTable[] = {
626+
{ MEASUREMENT_UNITS_METERS, "meters", "m", "m", 1.0, 1.0, 1.0, 30.0 },
627+
{ MEASUREMENT_UNITS_FEET_INCHES, "feet and inches", "ft", "in", FEET_IN_A_METER, 3.0, 12.0, 100.0 }
623628
};
624-
const int measurementScaleUnitsEntries = sizeof(measurementScaleUnits) / sizeof(measurementScaleUnits[0]);
629+
const int measurementScaleEntries = sizeof(measurementScaleTable) / sizeof(measurementScaleTable[0]);
625630

626631
// These are the allowable messages to broadcast and log (if enabled)
627632

@@ -1190,7 +1195,7 @@ struct Settings
11901195
uint8_t handleGnssDataTaskCore = 1; // Core where task should run, 0=core, 1=Arduino
11911196
uint8_t handleGnssDataTaskPriority = 1; // Read from the cicular buffer and dole out to end points (SD, TCP, BT).
11921197
uint8_t i2cInterruptsCore = 1; // Core where hardware is started and interrupts are assigned to, 0=core, 1=Arduino
1193-
uint8_t measurementScale = MEASUREMENTS_IN_METERS;
1198+
uint8_t measurementScale = MEASUREMENT_UNITS_METERS;
11941199
bool printBootTimes = false; // Print times and deltas during boot
11951200
bool printPartitionTable = false;
11961201
bool printTaskStartStop = false;

Firmware/RTK_Everywhere/support.ino

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,8 @@ void verifyTables()
668668
reportFatalError("Fix platformProvisionTable to match ProductVariant");
669669

670670
// Verify the measurement scales
671-
if (measurementScaleNameEntries != MEASUREMENT_SCALE_MAX)
672-
reportFatalError("Fix measurementScaleName to match MeasurementScale");
673-
if (measurementScaleUnitsEntries != MEASUREMENT_SCALE_MAX)
674-
reportFatalError("Fix measurementScaleUnits to match MeasurementScale");
671+
if (measurementScaleEntries != MEASUREMENT_UNITS_MAX)
672+
reportFatalError("Fix measurementScaleTable to match measurementUnits");
675673

676674
// Verify the consistency of the internal tables
677675
ethernetVerifyTables();

0 commit comments

Comments
 (0)