Code Monkey home page Code Monkey logo

fenice-bms-hv-sw's People

Contributors

bonnee avatar cannox227 avatar chiarasabaini avatar federico-carbone avatar gmazzucchi avatar harrisonprest70 avatar rykymai avatar simoneruffini avatar thartarus avatar tonidotpy avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fenice-bms-hv-sw's Issues

System-wide logging

Implement a centralized logging framework to log everything on the SD Card

SOC implementation

It is possible to implement State Of Charge estimation through current integration and EEPROM storage. Ask @volpx for actual implementation.

Avoid runtime allocations

The error subsystem relies on llist to store errors ordered by priority. This is problematic because llist uses dynamic allocations at runtime. With this method an insert operation is O(n) and a removal is O(1) (with the help of an array that stores the index of allocations).

To replace llist it would be adequate to find a solution that is at least as efficient as llist is now. min-heap seems to be a good candidate if paired with list_ref.

While runtime memory allocation is not ideal, it doesn't affect my sleep too much, so this issue is not really an urgent one.

SD Card

Develop SD card communication

Charging Algorithm

Develop a constant current/constant voltage charging curve in order to maximize the energy delivered in the battery.
Basically we want to maximize at-rest voltage

Improve function parameter checking

Most of the functions does not check the parameters passed to it. It would be great to fix this in order to avoid misuses (and to understand how the porco works <3)

Check IMD status

Read input from IMD to check status. Might handle it like a feedback bit

Bug On error.c

uint32_t get_timeout_delta(error_t *error) {
uint32_t delta = error->timestamp + error_timeouts[error->id] - HAL_GetTick();
return delta <= error_timeouts[error->id] ? delta : 0;
}

This function contains a series of bugs that for whatever reason ( I think the compiler helped) did never occured to you but to me and @cannox227 yes.

  1. The following sum error->timestamp + error_timeouts[error->id] - HAL_GetTick() is implicitly casted to integer and then back to uint32_t in the assignment (very dangerous). The first problem arises when HAL_GetTick() > error->timestamp + error_timeouts[error->id] (i.e. when the error is caught too late) the sum is negative value casted back to uint -> overflow.
  2. return delta <= error_timeouts[error->id] ? delta : 0; will become 0 only when delta overflows, that is not a good behaviour.
  3. error_timeouts[error->id] when equals to SOFT has values UINT32_MAX. This value should not be used because it causes a chain of very dangerous overflows: delta (overflows) which is then used by:
    volatile uint16_t delta = get_timeout_delta(error);
    uint16_t cnt = __HAL_TIM_GET_COUNTER(&HTIM_ERR);
    __HAL_TIM_SET_COMPARE(&HTIM_ERR, TIM_CHANNEL_1, cnt + TIM_MS_TO_TICKS(&HTIM_ERR, delta));

    Here delta undergoes multiplications and divisions and then another sum by cnt (which is a 32 bit counter running at KHz speeds).

Our solution:

  1. Change SOFT to (uint32_t)(UINT32_MAX>>1)
--- a/Core/Lib/errors/error.h
+++ b/Core/Lib/errors/error.h

-typedef enum { SOFT = UINT32_MAX, SHORT = 500, REGULAR = 1000, INSTANT = 0 } __attribute__((__packed__)) error_timeout;
+typedef enum {
+    SOFT    = (uint32_t)(UINT32_MAX >> 1),  //@10KHz: 2 d + 11 h + 39 min + 8.3648 s
+    SHORT   = 500,
+    REGULAR = 1000,
+    INSTANT = 0
+} __attribute__((__packed__)) error_timeout;
 
 /**
  * @brief an error is active when it enters the error list (ERROR_ACTIVE)

This will remove all overflows that happen when using soft errors
2) Change get_timout_delta to

--- a/Core/Lib/errors/error.c
+++ b/Core/Lib/errors/error.c

 uint32_t get_timeout_delta(error_t *error) {
-    uint32_t delta = error->timestamp + error_timeouts[error->id] - HAL_GetTick();
-    return delta <= error_timeouts[error->id] ? delta : 0;
+    uint32_t error_timeout = error->timestamp + error_timeouts[error->id];
+    uint32_t current_tick  = HAL_GetTick();
+    uint32_t delta         = 0;
+
+    // 2 options for delta
+    if (current_tick <= error_timeout) {
+        // 1) CurrentTick < error_timeout
+        //     err_arrival          error_timeout
+        //          |----------------------|
+        //          |------------| โ† current_tick
+        //                       |  delta  |
+        delta = error_timeout - current_tick;
+    } else {
+        // 2) CurrentTick > error_timeout
+        //     err_arrival      err_timeout
+        //          |-----------------|
+        //          |-------------------------| โ† current_tick
+        //          |                โ†‘      we missed the error timout
+        // we should have trigered here, therefore the delta will be 0 because we need to trigger instantly
+        delta = 0;
+    }
+
+    // WARNING: do not compute delta as an int becase some error_timouts[*] are
+    //          UINT32_MAX (overflow risk)
+    // EDGE CASE BUG: when current_tick overflows to 0 and err_timout not: this
+    //                happens when the uC stays on for more the 2^32 ms ~50 days
+
+    return delta;
 }

These change take into account all the overflowing that happens in point 2 and the implicit castings in point 1.

This code was tested in bms-lv-sw and works correctly.

ADC1 in DMA mode doesn't work

Starting ADC1 in DMA mode does only one conversion and then stops. Read values seem to overflow at 255, meaning only 8 bits are used. Should not be a problem since ADC2 and ADC3 are used to measure current and the feedbacks can only be read in polling mode since they are multiplexed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.