9 posts / 0 new
Last post
speculatrix
speculatrix's picture
please help debugging my efficiency module

Hi,

the MG ZS EV doesn't have a brilliant dashboard when it comes to wanting to conserve range by driving efficiently, you only know the power as a percentage of maximum.

 

This you might want to stay within 3.5mi/kWh, so should you slow to 60mph at 15% power, or can you risk driving 65mph at 20% power?

 

And thus I wrote a plugin, find it in my fork, at https://github.com/speculatrix/Open-Vehicle-Monitoring-System-3/tree/master/plugin/efficiency

 

But I don't properly understand how to get the values and do the maths. Can anyone help please?

 

thanks

dexter
dexter's picture
please help debugging my efficiency module

The metric widgets subscribe to metric changes through the "data-metric" attribute. As there are no metrics "miles_per_kwh" or "kwh_per_km", your widgets won't get any updates. Your calculation for the miles_per_kwh chart depends on metrics "v.p.speed" and "v.b.power", so you should set data-metric for that widget to "v.p.speed,v.b.power".

You should avoid polluting the framework metrics object with custom data, in this case your calculation result. That won't work as expected and will confuse others. If you really want to add custom metrics created on the web UI (thus only available in that web session), place them in the custom metrics namespace for the vehicle (e.g. "xmg") and send them as a metrics update event, so all receivers can get them. But in this case, that's totally unnecessary, simply set the result directly via setData().

On your calculation: a) metric "v.b.consumption" is provided by the framework as a standard metric, defined as power by speed (Wh/km), smoothed over 5 samples, so should be a better option to base your chart on. It's used for the chart in the upper right of the default dashboard. b) Metric values are always in their default units in the metrics object. If you want miles, you need to convert the values.

Regards,
Michael

speculatrix
speculatrix's picture
Hi Dexter, I realised I

Hi Dexter, I realised I copied and pasted the wrong metric, v.s or v.b, probaly fixed now and committed to my branch.

Also, having been writing Python, my javascript was missing brackets in the conditional statements!

I've not tested it but it doesn't crash out so it might work.

What are the units for speed? Or, how do I know whether it's returning speed in mi/h or km/hr?

thanks

Paul

dexter
dexter's picture
please help debugging my efficiency module

Metric values are always in their default units in the metrics object. If you want miles, you need to convert the values.

You can look up metric default units in the documentation or by "metric list".

Regards,
Michael

speculatrix
speculatrix's picture
I managed to test out the

I managed to test out the plugin but found that the dials didn't animate at all. My guess is that some jscript "plumbing" is missing to get them to update at regular intervals?


I tried on both my phone and a laptop (running chrome on linux). I tried refreshing the web page and didn't see much.

I also tried the main dashboard and the dials didn't change very regularly either though.

Testing is tricky because I need a co-pilot, and the edit-upload-debug cycle isn't instant.

speculatrix
speculatrix's picture
sorry if these questions seem

sorry if these questions seem dumb, but I have never tried to do anything like this before so I am just trying to intelligently copy and paste bits of jscript and html from the other examples (I used regenmon) n hope I can stumble on to make it work. please feel free to take or leave any part of whatever I have written and make a working version. thanks! I do want to learn though.

dexter
dexter's picture
please help debugging my efficiency module

You didn't follow my first hint:

The metric widgets subscribe to metric changes through the "data-metric" attribute. As there are no metrics "miles_per_kwh" or "kwh_per_km", your widgets won't get any updates. Your calculation for the miles_per_kwh chart depends on metrics "v.p.speed" and "v.b.power", so you should set data-metric for that widget to "v.p.speed,v.b.power".

That's the only thing missing from your plugin to make it work.

Also, adding a test data generator is simple and allows you to test it without driving.

Example for your metrics:

  /* Test metrics generator: */
  const debug = true;
  if (debug) {
    $('<button type="button" class="btn btn-default">Generate random data</button>')
      .appendTo('.receiver')
      .on('click', function() {
        var td = {};
        td["v.p.speed"] = Math.random() * 100;
        td["v.b.power"] = Math.random() * 120 - 20;
        console.log("Test data:", td);
        $('.receiver').trigger('msg:metrics', $.extend(metrics, td));
    });
  }

Regards,
Michael

speculatrix
speculatrix's picture
Hi, that's great, thanks, it

Hi, that's great, thanks, it seems to work, I pushed the code to my branch.

Shall I raise a pull request to submit it to master?

dexter
dexter's picture
please help debugging my efficiency module

Sure.

The units configuration is stored in config vehicle units.distance. To retrieve it from the web UI, you can e.g. do a loadjs() on OvmsConfig.Get():

loadjs('print(OvmsConfig.Get("vehicle", "units.distance", "K"))').then(function(output){ console.log(output) });

You'll get "K" for kilometers and "M" for miles. Instead of logging the output you should copy it to a variable or pass it to an init function.

Regards,
Michael

Log in or register to post comments
randomness