atanisoft / ESP32CommandStation

An ESP32 based DCC Command Station with integrated OpenLCB (LCC) --- NOTE: this project is not under active development.
https://atanisoft.github.io/ESP32CommandStation/
GNU General Public License v3.0
90 stars 34 forks source link

Bogus currents values reported (dived by 1000) #53

Closed daniviga closed 2 years ago

daniviga commented 4 years ago

First of all I would thank you for this great project, it's really a nice way to learn an play with DCC and the ESP32.

I'm however facing an issue with current readings/reportings.

Setup

Issues

OPS is working fine, I can command my DCC engines (H0n3, H0n30 and 0n30), however I always get [OPS] 0 mA / 2000 mA even when there's a load (engine on track running); shortages are properly spotted: [OPS] Shutdown threshold breached 2.00 mA (raw: 4095 / 4080), but only 2.00 mA are reported instead of 2000 mA.

Digging into the code I've found the following:

https://github.com/atanisoft/ESP32CommandStation/blob/c52c483c61801dd48c22efe2ac033279c716be46/components/DCCSignalGenerator/private_include/MonitoredHBridge.h#L95-L102

That formula actually returns a value that looks reasonable for mA, i.e. ((50 * 2000) / 4096 = 24)

However, when getUsage() is called, the value returned is dived by 1000, like if it was returning Amps and not mA, i.e.: https://github.com/atanisoft/ESP32CommandStation/blob/c52c483c61801dd48c22efe2ac033279c716be46/components/DCCSignalGenerator/MonitoredHBridge.cpp#L263-L265

As a quick and dirty test I've changed getUsage() to return A and not mA (multiplying by 1000, so I can change only one point in the code) and now returned values do make sense:

return ((lastReading_ * maxMilliAmps_) / 4096.0f * 1000.0f);

I get back

[OPS]      7 mA / 2000 mA     # (15 * 2000 / 4096=      7) 
[OPS] Shutdown threshold breached 1999.51 mA (raw: 4095 / 4080)

Is this a bug or it's a peculiarity of my setup (maybe the L298)? I'm also unable to read CV on PROG, not sure if this could be related or it's a different issue.

As a reference the same Shield works fine (readings too) with an Arduino Uno R3 and the BaseStation-EX (via JMRI).

atanisoft commented 4 years ago

Thanks for the detailed report and after a quick review I think your analysis is correct. It is incorrectly scaling to Amps rather than tracking fully in mA.

For the PROG track, can you check the serial output and ensure it is not timing out or throwing another type of error? I'll do some digging on this and see if I can reproduce it as well.

atanisoft commented 4 years ago

The commit should fix the readings but keeping this open until we narrow down the issues with the PROG track.

atanisoft commented 2 years ago

I can no longer reproduce this issue with v2.0 code.