diff --git a/vehicle/OVMS.V3/components/vehicle/vehicle.cpp b/vehicle/OVMS.V3/components/vehicle/vehicle.cpp index aae5fe565..95f6f131d 100644 --- a/vehicle/OVMS.V3/components/vehicle/vehicle.cpp +++ b/vehicle/OVMS.V3/components/vehicle/vehicle.cpp @@ -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) diff --git a/vehicle/OVMS.V3/components/vehicle_renaulttwizy/src/rt_notify.cpp b/vehicle/OVMS.V3/components/vehicle_renaulttwizy/src/rt_notify.cpp index 1404514ea..75dd3cbb9 100644 --- a/vehicle/OVMS.V3/components/vehicle_renaulttwizy/src/rt_notify.cpp +++ b/vehicle/OVMS.V3/components/vehicle_renaulttwizy/src/rt_notify.cpp @@ -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() diff --git a/vehicle/OVMS.V3/main/metrics_standard.cpp b/vehicle/OVMS.V3/main/metrics_standard.cpp index 9a7ad060a..ddcb3ab50 100644 --- a/vehicle/OVMS.V3/main/metrics_standard.cpp +++ b/vehicle/OVMS.V3/main/metrics_standard.cpp @@ -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 // diff --git a/vehicle/OVMS.V3/main/ovms_metrics.cpp b/vehicle/OVMS.V3/main/ovms_metrics.cpp index 03bc87ba9..443227b1d 100644 --- a/vehicle/OVMS.V3/main/ovms_metrics.cpp +++ b/vehicle/OVMS.V3/main/ovms_metrics.cpp @@ -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) { @@ -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 @@ -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 diff --git a/vehicle/OVMS.V3/main/ovms_metrics.h b/vehicle/OVMS.V3/main/ovms_metrics.h index 5e8b62651..68405af30 100644 --- a/vehicle/OVMS.V3/main/ovms_metrics.h +++ b/vehicle/OVMS.V3/main/ovms_metrics.h @@ -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; @@ -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