Skip to content

Commit

Permalink
Harmonize using fixed 6 digit precision for latitude/longitude format…
Browse files Browse the repository at this point in the history
…ting

(fixes issue openvehicles#997)
  • Loading branch information
dexterbg authored and Jamidd committed May 23, 2024
1 parent daa6206 commit 11729e8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion vehicle/OVMS.V3/components/vehicle/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,7 @@ void OvmsVehicle::NotifyGridLog()
<< std::noboolalpha
<< "," << (StdMetrics.ms_v_pos_gpslock->AsBool() ? 1 : 0)
<< std::fixed
<< std::setprecision(8)
<< std::setprecision(6)
<< "," << StdMetrics.ms_v_pos_latitude->AsFloat()
<< "," << StdMetrics.ms_v_pos_longitude->AsFloat()
<< std::setprecision(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ void OvmsVehicleRenaultTwizy::SendGPSLog()
<< "RT-GPS-Log,"
<< (long) (StdMetrics.ms_v_pos_odometer->AsFloat(0, Miles) * 10) // in 1/10 mi
<< ",86400"
<< setprecision(8)
<< setprecision(6)
<< fixed
<< "," << StdMetrics.ms_v_pos_latitude->AsFloat()
<< "," << StdMetrics.ms_v_pos_longitude->AsFloat()
Expand Down
6 changes: 6 additions & 0 deletions vehicle/OVMS.V3/main/metrics_standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ MetricsStandard::MetricsStandard()
ms_v_pos_valet_longitude = new OvmsMetricFloat(MS_V_POS_VALET_LONGITUDE, SM_STALE_NONE, Other, true);
ms_v_pos_valet_distance = new OvmsMetricFloat(MS_V_POS_VALET_DISTANCE, SM_STALE_HIGH, Meters, true);

// Use fixed 6 digit precision for latitude/longitude formatting:
ms_v_pos_latitude->SetFormat(6, true);
ms_v_pos_longitude->SetFormat(6, true);
ms_v_pos_valet_latitude->SetFormat(6, true);
ms_v_pos_valet_longitude->SetFormat(6, true);

//
// TPMS: tyre monitoring metrics
//
Expand Down
17 changes: 15 additions & 2 deletions vehicle/OVMS.V3/main/ovms_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,8 @@ OvmsMetricFloat::OvmsMetricFloat(const char* name, uint16_t autostale, metric_un
m_value = 0.0;
m_valuep = NULL;
m_persist = persist;
m_fmt_prec = -1;
m_fmt_fixed = false;

if (m_persist)
{
Expand Down Expand Up @@ -2203,9 +2205,18 @@ std::string OvmsMetricFloat::AsString(const char* defvalue, metric_unit_t units,
std::ostringstream ss;
if (precision >= 0)
{
ss.precision(precision); // Set desired precision
// Set desired fixed precision format:
ss.precision(precision);
ss << fixed;
}
else
{
// Set standard metric format:
if (m_fmt_prec >= 0)
ss.precision(m_fmt_prec);
if (m_fmt_fixed)
ss << fixed;
}
if ((units != Other)&&(units != m_units))
ss << UnitConvert(m_units,units,m_value);
else
Expand Down Expand Up @@ -2248,7 +2259,9 @@ int OvmsMetricFloat::AsInt(const int defvalue, metric_unit_t units)
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
void OvmsMetricFloat::DukPush(DukContext &dc, metric_unit_t units)
{
dc.Push(AsFloat(0, units));
// Support custom precisions & minimize errors on float→double conversion:
std::string fval = AsString("0", units);
dc.Push(strtod(fval.c_str(), NULL));
}
#endif

Expand Down
3 changes: 3 additions & 0 deletions vehicle/OVMS.V3/main/ovms_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class OvmsMetricFloat : public OvmsMetric
~OvmsMetricFloat() override;

public:
void SetFormat(int precision = -1, bool fixed = false) { m_fmt_prec = precision; m_fmt_fixed = fixed; }
std::string AsString(const char* defvalue = "", metric_unit_t units = Other, int precision = -1) override;
std::string AsJSON(const char* defvalue = "", metric_unit_t units = Other, int precision = -1) override;
float AsFloat(const float defvalue = 0, metric_unit_t units = Other) override;
Expand All @@ -355,6 +356,8 @@ class OvmsMetricFloat : public OvmsMetric
protected:
float m_value;
float* m_valuep;
int m_fmt_prec;
bool m_fmt_fixed;
};

class OvmsMetricString : public OvmsMetric
Expand Down

0 comments on commit 11729e8

Please sign in to comment.