diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-11-03 14:11:42 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-11-03 14:11:42 +0100 |
commit | 48ac857cc3082aee3300a4d39834e109bd909f82 (patch) | |
tree | 2c8b1745a477faf929e25257230fef65bd8a4571 /sw | |
parent | 76bd2a5423b249096699d29a995467eb2ed0513d (diff) | |
download | glutte-batteries-48ac857cc3082aee3300a4d39834e109bd909f82.tar.gz glutte-batteries-48ac857cc3082aee3300a4d39834e109bd909f82.tar.bz2 glutte-batteries-48ac857cc3082aee3300a4d39834e109bd909f82.zip |
Move timer stuff to common
Diffstat (limited to 'sw')
-rw-r--r-- | sw/Makefile | 4 | ||||
-rw-r--r-- | sw/common.hpp | 93 | ||||
-rw-r--r-- | sw/main.cpp | 94 | ||||
-rw-r--r-- | sw/pins.h | 9 | ||||
-rw-r--r-- | sw/relays.cpp | 27 | ||||
-rw-r--r-- | sw/relays.hpp | 29 |
6 files changed, 183 insertions, 73 deletions
diff --git a/sw/Makefile b/sw/Makefile index 6b49420..bd9f6ff 100644 --- a/sw/Makefile +++ b/sw/Makefile @@ -23,6 +23,8 @@ BUILD_DIR=build APP_NAME = sw HEADERS = \ + common.hpp \ + relays.hpp \ lib/Arduino.h \ lib/DallasTemperature.h \ lib/delay.h \ @@ -33,7 +35,7 @@ HEADERS = \ lib/util/OneWire_direct_regtype.h # Application object files -APP_CXX_OBJECTS = main.o ltc2400.o +APP_CXX_OBJECTS = main.o relays.o ltc2400.o APP_OBJECTS = # Library object files to build and use diff --git a/sw/common.hpp b/sw/common.hpp new file mode 100644 index 0000000..345f34b --- /dev/null +++ b/sw/common.hpp @@ -0,0 +1,93 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Matthias P. Braendli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#pragma once + +#include <stdlib.h> +#include <stdint.h> + +struct timer_t { + uint32_t seconds_ = 0; /* Timer in seconds */ + uint8_t ticks_ = 0; /* Timer in 100ms steps */ + + timer_t() {} + timer_t(uint32_t seconds, uint8_t ticks) : seconds_(seconds), ticks_(ticks) {} + + timer_t get_atomic_copy() const { + cli(); + const auto t = *this; + sei(); + return t; + } + + uint32_t get_seconds_atomic() const { + cli(); + uint32_t s = seconds_; + sei(); + return s; + } + + uint8_t get_ticks_atomic() const { + /* Returning an uint8_t is atomic */ + return ticks_; + } + + bool operator>(const timer_t& rhs) const { + return (seconds_ > rhs.seconds_) or + (seconds_ == rhs.seconds_ and ticks_ > rhs.ticks_); + } + + void normalise() { + while (ticks_ >= 10) { + seconds_++; + ticks_ -= 10; + } + } + + timer_t operator+(const timer_t& rhs) const { + timer_t t; + t.seconds_ = seconds_ + rhs.seconds_; + t.ticks_ = ticks_ + rhs.ticks_; + t.normalise(); + return t; + } + + timer_t operator+(uint8_t ticks) const { + timer_t t = timer_t(0, ticks); + return *this + t; + } + + void operator+=(const timer_t& inc) { + seconds_ += inc.seconds_; + ticks_ += inc.ticks_; + normalise(); + } + + void operator+=(uint8_t ticks) { + *this += timer_t(0, ticks); + } + + static constexpr int ms_to_ticks(int ms) { return ms / 100; } +}; + diff --git a/sw/main.cpp b/sw/main.cpp index 9ac591e..f62fa27 100644 --- a/sw/main.cpp +++ b/sw/main.cpp @@ -34,6 +34,7 @@ #include <avr/eeprom.h> #include <avr/wdt.h> +#include "common.hpp" #include "pins.h" #include "ltc2400.h" @@ -44,71 +45,18 @@ extern "C" { // UART endline is usually CR LF #define ENDL "\r\n" -constexpr double R_SHUNT = 5e-3; +constexpr double R_SHUNT = 5e-3; // Ohm -struct timer_t { - uint32_t seconds_ = 0; /* Timer in seconds */ - uint8_t ticks_ = 0; /* Timer in 100ms steps */ - - timer_t() {} - timer_t(uint32_t seconds, uint8_t ticks) : seconds_(seconds), ticks_(ticks) {} - - timer_t get_atomic_copy() const { - cli(); - const auto t = *this; - sei(); - return t; - } - - uint32_t get_seconds_atomic() const { - cli(); - uint32_t s = seconds_; - sei(); - return s; - } - - uint8_t get_ticks_atomic() const { - /* Returning an uint8_t is atomic */ - return ticks_; - } - - bool operator>(const timer_t& rhs) const { - return (seconds_ > rhs.seconds_) or - (seconds_ == rhs.seconds_ and ticks_ > rhs.ticks_); - } - - void normalise() { - while (ticks_ >= 10) { - seconds_++; - ticks_ -= 10; - } - } - - timer_t operator+(const timer_t& rhs) const { - timer_t t; - t.seconds_ = seconds_ + rhs.seconds_; - t.ticks_ = ticks_ + rhs.ticks_; - t.normalise(); - return t; - } - - timer_t operator+(uint8_t ticks) const { - timer_t t = timer_t(0, ticks); - return *this + t; - } - - void operator+=(const timer_t& inc) { - seconds_ += inc.seconds_; - ticks_ += inc.ticks_; - normalise(); - } - - void operator+=(uint8_t ticks) { - *this += timer_t(0, ticks); - } - - static constexpr int ms_to_ticks(int ms) { return ms / 100; } -}; +/* Capacity counters and thresholds, in As (= Coulombs) + * + * For every relay, define a threshold below which the + * relay should be active. + */ +constexpr double THRESHOLD_K1 = 1200.0 * 3600; +constexpr double THRESHOLD_K2 = 1000.0 * 3600; +constexpr double THRESHOLD_K3 = 600.0 * 3600; +constexpr double MAX_CAPACITY = 1500.0 * 3600; +uint32_t current_capacity; /* Storage of battery capacity in mC. * 3600 mC = 1mAh */ @@ -122,8 +70,6 @@ uint32_t last_store_time; /* In seconds */ timer_t last_ltc2400_measure; timer_t last_ltc2400_print_time; -uint32_t current_capacity; - /* Timer at approximately 100ms. * Since this timer is updated in an ISR, care has to be taken * when reading it, because all operations involving variables @@ -315,14 +261,16 @@ int main() while (true) { sleep_mode(); - pins_set_status(system_timer.get_ticks_atomic() == 0); + const auto time_now = system_timer.get_atomic_copy(); + + pins_set_status(time_now.get_ticks_atomic() == 0); - if (last_store_time + 3600 * 5 >= system_timer.get_seconds_atomic()) { + if (last_store_time + 3600 * 5 >= time_now.seconds_) { store_capacity_to_eeprom(); } constexpr auto ltc2400_measure_interval = timer_t::ms_to_ticks(100); - if (last_ltc2400_measure + ltc2400_measure_interval > system_timer.get_atomic_copy()) { + if (last_ltc2400_measure + ltc2400_measure_interval > time_now) { last_ltc2400_measure += ltc2400_measure_interval; if (ltc2400_conversion_ready()) { @@ -341,12 +289,18 @@ int main() /* Vout - 2.5V = Ishunt * Rshunt * 20 */ const double i_shunt = (adc_voltage - 2.5) / (20.0 * R_SHUNT); accum += i_shunt * tick_interval; + + if (accum < 0) { accum = 0; } + if (accum > MAX_CAPACITY) { accum = MAX_CAPACITY; } + current_capacity = lrint(accum); + +#warning "Handle thresholds for relays" } } const auto ltc2400_print_interval = timer_t(10, 0); - if (last_ltc2400_print_time + ltc2400_print_interval > system_timer.get_atomic_copy()) { + if (last_ltc2400_print_time + ltc2400_print_interval > time_now) { last_ltc2400_print_time += ltc2400_print_interval; send_capacity(current_capacity); } @@ -83,12 +83,17 @@ constexpr uint8_t PIND_INIT = 0; inline void pins_set_status(bool enable) { - cli(); if (enable) { PORTB &= ~PINB_STATUSn; } else { PORTB |= PINB_STATUSn; } - sei(); } + +enum class relay_id_t { + K1, + K2, + K3, +}; + diff --git a/sw/relays.cpp b/sw/relays.cpp new file mode 100644 index 0000000..6604323 --- /dev/null +++ b/sw/relays.cpp @@ -0,0 +1,27 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Matthias P. Braendli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#include "relays.hpp" +#include <stdio.h> +#include <math.h> diff --git a/sw/relays.hpp b/sw/relays.hpp new file mode 100644 index 0000000..f6fc18b --- /dev/null +++ b/sw/relays.hpp @@ -0,0 +1,29 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Matthias P. Braendli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#pragma once + +#include <stdlib.h> +#include <stdint.h> + |