Example 2 - Distance from velocity
Example 2 - Distance from speed:
Task:
The mileage of a vehicle VEH_DIST_KM
, which only counts whole "km", is to be extended with the help of the speed signal VEH_SPEED_KMPH
so that a finer resolution is available.
Solution:
// Constants for unit conversion
uConv_kmph2mps = 1.0 / 3.6; // km/h -> m/s
uConv_m2km = 1e-3; // m -> km
SPEED = VEH_SPEED_KMPH * uConv_kmph2mps; // m/s
DIST_KM = integrate(SPEED, {storage: 'sto_DIST'})
* uConv_m2km; // km
DIST_FRAC = integrate(SPEED, posedge(VEH_DIST_KM), {storage: 'sto_DIST_frac'})
* uConv_m2km; // km
VEH_DISTFINE = VEH_DIST_KM + DIST_FRAC; // km
Functions used
Explanation:
-
uConv_kmph2mps, uConv_m2km
: First, factors for unit conversion are defined. This improves the readability of the following code. Such definitions are best placed centrally at the beginning of the formula set. -
SPEED
: The speed signal of the vehicle, which is measured in km/h, is first converted into m/s. This speed is easy to integrate. -
DIST_KM
: (Bonus!) The total mileage can be determined from the speed signal. The integratorintegrate()
adds the dimension time with the unit s (second) to its integrand. This results in the distance in m (meters), which is then converted into the desired target unit km. -
DIST_FRAC
: A distance is also integrated from the speed signal here. However, this is always reset to zero if a jump to the next mileage is detected in the vehicle's odometer usingposedge()
. -
VEH_DISTFINE
: The more precisely resolved distance of the vehicle results when the previously determined partial amount is added to the counter reading of the vehicle.