ev3dev-lang-java / batteryMonitor

A Java/Scripts to monitor the battery to protect EV3Dev hardware
MIT License
2 stars 0 forks source link

Problem Explanation #1

Closed ameysutavani closed 7 years ago

ameysutavani commented 7 years ago

Hello @jabrena

I have worked on battery State of Charge (SOC) determination for UAVs and would like to contribute to this project. I believe that a simple voltage based low battery failsafe trigger should be very easy to write. However, it would be nicer to run a proper discharge test on the battery and get the characteristics like the discharge curves (voltage vs capacity) and internal resistances to build a proper SOC model.

I believe that just voltage is not a good indicator of the state of charge/health of the battery.

Also, is this battery monitor supposed to work with both ev3 rechargeable battery as well as the when powered by AA cells?

jabrena commented 7 years ago

Hi @ameysutavani,

I am glad if you collaborate in this mini project. The idea about batteryMonitor is simple. Recently, I detected that one BrickPi+ unit that I use for testing start working slowly: https://github.com/ev3dev-lang-java/ev3dev-lang-java/issues/226

and after some talk with @dlech, he commented that It is necessary to format/replace the MicroSD Card.

Thinking about the problem, it is true that the SDCard in the past suffered a bit with some sessions using the battery until low levels of energy and maybe the SDCard now is not stable. So, when any users uses this generation of Mindstorms products with battery with low voltage levels, they put in risk the hardware and his own time because, in my case, I will have to invest a pair of hours reinstalling everything and configuring later. So, the idea with batteryMonitor, is the development of a simple Java process to read the battery and if the battery level reach a determined threshold, the process execute:

sudo shutdown now

Last week, I started with the development and I was playing a bit with Mockito but it is Thread testing is complex.

If you like, this week I will clean up the code a bit and we could monitor some bricks with ev3dev.

Yes, ev3dev supports rechargeable battery and AA cells.

Later, the idea is to configure the system to run the monitor at Start up. https://github.com/ev3dev-lang-java/ev3dev-lang-java/issues/228

Juan Antonio

dlech commented 7 years ago

So why have a battery monitor for just ev3dev-lang-java instead of making one that everyone can use?

I don't know if it is a "proper discharge test", but there are some discharge curves in the official LEGO EV3 hardware developer kit available on the official LEGO MINDSTORMS download page.

jabrena commented 7 years ago

Hi @dlech,

The idea about the process is generic, but we don´t have any JRE running on ev3dev at the moment to share the jar. if we could have a JVM in the distro, this process could run for everyone :) https://packages.debian.org/search?arch=armel&keywords=openjdk-8-jre

Maybe, it could be interesting to share ideas/components among different members/languages, etc... :)

Juan Antonio

dlech commented 7 years ago

It would be better to write this in a language that does not have the runtime overhead like Java, but that is just an implementation detail. The important part is figuring out the algorithm of when to warn the user that the battery is low and when to force a system shutdown.

dlech commented 7 years ago

Back to @ameysutavani's original comment... there is some more information on battery internals here. I remember doing some research on this several years ago, but I gave up because there is no way to know the current state of the battery when you boot. So, even if you can model the battery, you don't have a known initial condition to start from. Maybe you know something that can work around this?

ameysutavani commented 7 years ago

So why have a battery monitor for just ev3dev-lang-java instead of making one that everyone can use?

I second this idea. Ideally, this should a safety feature of the battery (voltage/current) driver of ev3dev.

I don't know if it is a "proper discharge test", but there are some discharge curves in the official LEGO EV3 hardware developer kit available on the official LEGO MINDSTORMS download page.

I have access to a computer controlled load bank and can run a discharge profile test on the ev3 rechargeable battery. (This yields two key informations: 1. Open circuit voltage to battery SOC, 2. Internal resistance of the battery). Both the factors change from battery to battery, cycle to cycle.

I remember doing some research on this several years ago, but I gave up because there is no way to know the current state of the battery when you boot. So, even if you can model the battery, you don't have a known initial condition to start from. Maybe you know something that can work around this?

However, for good quality batteries the deviation is usually within norm and such a open circuit voltage to battery capacity curve can be used to estimate the charge in battery.

ameysutavani commented 7 years ago

Th important part is figuring out the algorithm of when to warn the user that the battery is low and when to force a system shutdown.

This usually is done as follows:

  1. Keep monitoring the SOC of battery. (initialized from its voltage on bootup; thereafter time integrate system current and keep subtracting it from the the initialized SOC)
  2. Whenever the SOC falls below the set threshold for low battery for some time, trigger a system shutdown.
jabrena commented 7 years ago

In relation to the requirements, I agree with the ideas that @ameysutavani says:

I think that Java doesn't increase the memory so much. Maybe a bash script is enough. We could create some prototype and compare. :)

ameysutavani commented 7 years ago

Monitor the battery every X minutes.

I think if the current is less then just a simple voltage based threshold (for critical battery failsafe) and a voltage to battery SOC should be just fine.

Meanwhile, I will try to get the discharge curves for ev3 rechargeable battery.

Also, is it possible to know how the ev3 is powered (via the rechargeable battery or AA cells)?

dlech commented 7 years ago

Also, is possible to know how the ev3 is powered (via the rechargeable battery or AA cells)?

Yes, the battery driver has a technology attribute that indicates Li-on for rechargeable and Unknown for AA cells. Future versions of the driver will also be writable so that if you are using NiMH, you can specify that manually (since this cannot be automatically detected).

ameysutavani commented 7 years ago

Yes, the battery driver has a technology attribute that indicates Li-on for rechargeable and Unknown for AA cells.

How does it detect this on the hardware side?

dlech commented 7 years ago

There is a switch in the battery compartment (the little round black rubber thing). The EV3 rechargeable battery presses this switch.

jabrena commented 7 years ago

Morning mates,

one point to see is the compatibility with BrickPi+,BrickPi3 & PiStorms.

This weekend, I will update the prototype.

Cheers

Juan Antonio

jabrena commented 7 years ago

Hi mates,

I implemented & tested a first version on a EV3 Brick: https://github.com/ev3dev-lang-java/ev3dev-lang-java/issues/230

In some weeks, I will tests how to generate a specific dashboard from Kibana using information from the logs generated by batteryMonitor to analyze the energy consumption with the following cases:

https://www.elastic.co/products/kibana

Tomorrow, I will install the solution on a Brickpi+

Cheers

ameysutavani commented 7 years ago

Robot with one motors Robot with two motors Robot with three motors Robot with four motors

Hello @jabrena

From my testing experience, I have found that provide a very varying load. This load also varies as the battery voltage drops. The above test can give a rough idea about the power draw of a motor. But for a discharge profile of a battery, a constant current load is desirable.

Also, I found this: https://www.dexterindustries.com/ev3-current-consumption-measurement/

jabrena commented 7 years ago

Hi @ameysutavani, maybe you have reason. If you observe in the Task that I coded last week: https://github.com/ev3dev-lang-java/batteryMonitor/blob/master/src/main/java/ev3dev/utils/monitor/BatteryTask.java

I read the battery 10 times to get the average.

I will work in an automatic installer for the Script.

When we have the new release with Stretch, we should test the utility from EV3Dev. https://github.com/ev3dev/ev3dev/issues/861

@dlech More or less, how long to release ev3dev-stretch ?

Cheers

dlech commented 7 years ago

It depends on how much help I can get. If I have to do everything myself, I would guess 2 to 3 years.

dlech commented 7 years ago

There are ev3dev-stretch images available already, but it won't be stable for a long time.

jabrena commented 7 years ago

Oki, In my case with jessie, I am pretty happy :) The project (ev3dev) goes fantastic.

Cheers

ameysutavani commented 7 years ago

If you observe in the Task that I coded last week: https://github.com/ev3dev-lang-java/batteryMonitor/blob/master/src/main/java/ev3dev/utils/monitor/BatteryTask.java

Hi, @jabrena If there is a current draw from battery then the read voltages will be under load voltages and can be under the threshold. This may cause a false trigger.

Maybe just this modification will work:

    //TODO Manage how to manage EV3Dev exception
    private List<Double> getVoltages(){
        final List<Double> voltageList = new ArrayList<>();
        int x = 0;
        while(x < DEFAULT_MEASURES){
            if (battery.getBatteryCurrent() < IDLE_CURRENT_THRESHOLD){
                voltageList.add(Double.valueOf(battery.getVoltageMilliVolt()));
                x++;
            }
        }
        return voltageList;
    }

where,
IDLE_CURRENT_THRESHOLD is defined to quiescent idle current draw by ev3 (0.2 Amps?) 
jabrena commented 7 years ago

Hi @ameysutavani,

It is a nice idea, but if we add this block, we could have a compatibility problem with BrickPi+/PiStorms.

This method is not possible to be implemented with Batteries on BrickPi+:

https://github.com/ev3dev-lang-java/ev3dev-lang-java/blob/develop/src/main/java/ev3dev/sensors/Battery.java

/**
 * Returns the current of the battery in microamps.
 * @return current
 */
public float getBatteryCurrent() {
    if(this.getPlatform().equals(SupportedPlatform.EV3BRICK)){
        return Sysfs.readFloat(BATTERY_PATH + "/" + BATTERY_EV3 + "/" +  CURRENT);
    }else {
        log.warn("This method is not available for {} & {}", PISTORMS, BRICKPI);
        return -1f;
    }
}

Besides, if you observe if a Developer uses this method could make a mistake with the usage of the method. I am going to open again this issue: https://github.com/ev3dev-lang-java/ev3dev-lang-java/issues/164

If a specific platform doesn´t support some feature, it is better to raise a Checked exception. https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

Do you have time this weekend to make a Pull request with your idea?

If EV3, your idea,
else current block. (Support for BrickPi+/PiStorms)

In my case, I will refactor Battery Sensor to raise an exception for BrickPi/PiStorms platforms.

dlech commented 7 years ago

IDLE_CURRENT_THRESHOLD is defined to quiescent idle current draw by ev3 (0.2 Amps?)

USB devices like Wi-Fi dongles also consume a significant amount of power. Generally 100 to 200mA.

jabrena commented 7 years ago

Hi @dlech,

What is your idea about IDLE_CURRENT_THRESHOLD we could implement for this tool.

dlech commented 7 years ago

I have no idea.

jabrena commented 7 years ago

Oki.

Next week, I will start a general installer to install the monitor. For BrickPi+/PiStorms, the script will install Java directly. I have to find some way to install Java 8 at least for EV3 Brick.

Good night from Uk

Cheers

ameysutavani commented 7 years ago

Is there a way to keep logging voltage and current in a file?

jabrena commented 7 years ago

Good morning @ameysutavani, yes the whole project has Logging support using Logback. https://logback.qos.ch/

The initial release, include a minimum logging configuration: https://github.com/ev3dev-lang-java/batteryMonitor/blob/master/src/main/resources/logback.xml

But the solution, is flexible and you can define your own logging configuration. https://github.com/ev3dev-lang-java/batteryMonitor/tree/master/scripts and start the process with your custom configuration: https://github.com/ev3dev-lang-java/batteryMonitor/blob/master/scripts/start.sh

But, if you need more flexibility, I added in the project a dependency to send remotely the logs to ELK Stack: https://github.com/ev3dev-lang-java/batteryMonitor/blob/master/build.gradle https://www.elastic.co/products/kibana https://www.elastic.co/webinars/introduction-elk-stack https://static-www.elastic.co/assets/bltdf676a11b38e207c/kibana-time.jpg?q=735

Juan Antonio

ameysutavani commented 7 years ago

Very Good Morning @jabrena

I will have a look at Logback. Thanks :smile:

jabrena commented 7 years ago

Hi @ameysutavani

If you want to test the batteryMonitor, this installer will install in an automatic way everything. https://github.com/ev3dev-lang-java/installer

I am going to close this issue. If you have more doubts, open the issue here: https://github.com/ev3dev-lang-java/ev3dev-lang-java/issues

Cheers