time(NULL) replaced with uptime() in helper files
Reason: Zephyr does not support time(NULL). This way it is portable.
This commit is contained in:
+11
-11
@@ -7,13 +7,13 @@
|
||||
#include "bat_charger.h"
|
||||
#include "config.h"
|
||||
#include "pcb.h"
|
||||
#include "helper.h"
|
||||
|
||||
#include "device_status.h"
|
||||
extern DeviceStatus dev_stat;
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
void battery_conf_init(BatConf *bat, BatType type, int num_cells, float nominal_capacity)
|
||||
{
|
||||
@@ -252,7 +252,7 @@ void Charger::update_soc(BatConf *bat_conf)
|
||||
void Charger::enter_state(int next_state)
|
||||
{
|
||||
//printf("Enter State: %d\n", next_state);
|
||||
time_state_changed = time(NULL);
|
||||
time_state_changed = uptime();
|
||||
state = next_state;
|
||||
}
|
||||
|
||||
@@ -337,7 +337,7 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
switch (state) {
|
||||
case CHG_STATE_IDLE: {
|
||||
if (port->voltage < bat_conf->voltage_recharge * num_batteries
|
||||
&& (time(NULL) - time_state_changed) > bat_conf->time_limit_recharge
|
||||
&& (uptime() - time_state_changed) > bat_conf->time_limit_recharge
|
||||
&& bat_temperature < bat_conf->charge_temp_max - 1
|
||||
&& bat_temperature > bat_conf->charge_temp_min + 1)
|
||||
{
|
||||
@@ -372,14 +372,14 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
if (port->voltage >= num_batteries *
|
||||
(port->sink_voltage_max - port->current * port->pos_droop_res))
|
||||
{
|
||||
time_voltage_limit_reached = time(NULL);
|
||||
time_voltage_limit_reached = uptime();
|
||||
}
|
||||
|
||||
// cut-off limit reached because battery full (i.e. CV limit still
|
||||
// reached by available solar power within last 2s) or CV period long enough?
|
||||
if ((port->current < bat_conf->topping_current_cutoff &&
|
||||
(time(NULL) - time_voltage_limit_reached) < 2)
|
||||
|| (time(NULL) - time_state_changed) > bat_conf->topping_duration)
|
||||
(uptime() - time_voltage_limit_reached) < 2)
|
||||
|| (uptime() - time_state_changed) > bat_conf->topping_duration)
|
||||
{
|
||||
full = true;
|
||||
num_full_charges++;
|
||||
@@ -387,7 +387,7 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
first_full_charge_reached = true;
|
||||
|
||||
if (bat_conf->equalization_enabled && (
|
||||
(time(NULL) - time_last_equalization) / (24*60*60)
|
||||
(uptime() - time_last_equalization) / (24*60*60)
|
||||
>= bat_conf->equalization_trigger_days ||
|
||||
num_deep_discharges - deep_dis_last_equalization
|
||||
>= bat_conf->equalization_trigger_deep_cycles))
|
||||
@@ -416,10 +416,10 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
if (port->voltage >= num_batteries *
|
||||
(port->sink_voltage_max - port->current * port->pos_droop_res))
|
||||
{
|
||||
time_voltage_limit_reached = time(NULL);
|
||||
time_voltage_limit_reached = uptime();
|
||||
}
|
||||
|
||||
if (time(NULL) - time_voltage_limit_reached > bat_conf->trickle_recharge_time)
|
||||
if (uptime() - time_voltage_limit_reached > bat_conf->trickle_recharge_time)
|
||||
{
|
||||
port->pos_current_limit = bat_conf->charge_current_max;
|
||||
full = false;
|
||||
@@ -435,10 +435,10 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
bat_conf->temperature_compensation * (bat_temperature - 25));
|
||||
|
||||
// current or time limit for equalization reached
|
||||
if (time(NULL) - time_state_changed > bat_conf->equalization_duration)
|
||||
if (uptime() - time_state_changed > bat_conf->equalization_duration)
|
||||
{
|
||||
// reset triggers
|
||||
time_last_equalization = time(NULL);
|
||||
time_last_equalization = uptime();
|
||||
deep_dis_last_equalization = num_deep_discharges;
|
||||
|
||||
discharged_Ah = 0; // reset coulomb counter again
|
||||
|
||||
+4
-3
@@ -15,6 +15,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
@@ -249,10 +250,10 @@ public:
|
||||
uint16_t soc = 100; ///< State of Charge (%)
|
||||
uint16_t soh = 100; ///< State of Health (%)
|
||||
|
||||
int time_state_changed; ///< Timestamp of last state change
|
||||
int time_voltage_limit_reached; ///< Last time the CV limit was reached
|
||||
time_t time_state_changed; ///< Timestamp of last state change
|
||||
time_t time_voltage_limit_reached; ///< Last time the CV limit was reached
|
||||
|
||||
int time_last_equalization; ///< Timestamp after finish of last equalization charge
|
||||
time_t time_last_equalization; ///< Timestamp after finish of last equalization charge
|
||||
int deep_dis_last_equalization; ///< Deep discharge counter value after last equalization
|
||||
|
||||
bool full; ///< Flag to indicate if battery was fully charged
|
||||
|
||||
+7
-7
@@ -8,10 +8,10 @@
|
||||
#include "pcb.h"
|
||||
#include "device_status.h"
|
||||
#include "debug.h"
|
||||
#include "helper.h"
|
||||
|
||||
#include "half_bridge.h"
|
||||
|
||||
#include <time.h> // for time(NULL) function
|
||||
#include <math.h> // for fabs function
|
||||
#include <stdlib.h> // for min/max function
|
||||
#include <stdio.h>
|
||||
@@ -66,7 +66,7 @@ int Dcdc::duty_cycle_delta()
|
||||
}
|
||||
|
||||
if (out->power >= output_power_min) {
|
||||
power_good_timestamp = time(NULL); // reset the time
|
||||
power_good_timestamp = uptime(); // reset the time
|
||||
}
|
||||
|
||||
int pwr_inc_goal = 0; // stores if we want to increase (+1) or decrease (-1) power
|
||||
@@ -77,7 +77,7 @@ int Dcdc::duty_cycle_delta()
|
||||
out->sink_voltage_max, out->pos_current_limit, half_bridge_get_duty_cycle() * 100.0,
|
||||
state);
|
||||
|
||||
if ((time(NULL) - power_good_timestamp > 10 || out->power < -1.0) && mode != MODE_NANOGRID) {
|
||||
if ((uptime() - power_good_timestamp > 10 || out->power < -1.0) && mode != MODE_NANOGRID) {
|
||||
// swith off after 10s low power or negative power (if not in nanogrid mode)
|
||||
pwr_inc_goal = 0;
|
||||
}
|
||||
@@ -124,7 +124,7 @@ int Dcdc::check_start_conditions()
|
||||
lvs->voltage > ls_voltage_max ||
|
||||
lvs->voltage < ls_voltage_min ||
|
||||
dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE) ||
|
||||
time(NULL) < (off_timestamp + restart_interval))
|
||||
uptime() < (off_timestamp + restart_interval))
|
||||
{
|
||||
return 0; // no energy transfer allowed
|
||||
}
|
||||
@@ -173,7 +173,7 @@ void Dcdc::control()
|
||||
if (stop_reason != NULL) {
|
||||
half_bridge_stop();
|
||||
state = DCDC_STATE_OFF;
|
||||
off_timestamp = time(NULL);
|
||||
off_timestamp = uptime();
|
||||
print_info("DC/DC Stop: %s.\n",stop_reason);
|
||||
}
|
||||
}
|
||||
@@ -219,7 +219,7 @@ void Dcdc::control()
|
||||
// nanogrid not yet started up (zero voltage)
|
||||
half_bridge_start(lvs->voltage / (hvs->voltage + 1));
|
||||
}
|
||||
power_good_timestamp = time(NULL);
|
||||
power_good_timestamp = uptime();
|
||||
print_info("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
|
||||
hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100);
|
||||
startup_delay_counter = 0; // ensure we will have a delay before next start
|
||||
@@ -260,7 +260,7 @@ void Dcdc::emergency_stop()
|
||||
{
|
||||
half_bridge_stop();
|
||||
state = DCDC_STATE_OFF;
|
||||
off_timestamp = time(NULL);
|
||||
off_timestamp = uptime();
|
||||
}
|
||||
|
||||
void Dcdc::self_destruction()
|
||||
|
||||
+2
-2
@@ -115,8 +115,8 @@ public:
|
||||
float output_power_min; ///< Minimum output power (if lower, DC/DC is switched off)
|
||||
|
||||
// calibration parameters
|
||||
int restart_interval; ///< Restart interval (s): When should we retry to start charging
|
||||
///< after low output power cut-off?
|
||||
unsigned int restart_interval; ///< Restart interval (s): When should we retry to start
|
||||
///< charging after low output power cut-off?
|
||||
|
||||
private:
|
||||
/** Calculates the duty cycle change depending on operating mode and actual measurements
|
||||
|
||||
+2
-2
@@ -6,12 +6,12 @@
|
||||
|
||||
#include "eeprom.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mcu.h"
|
||||
#include "pcb.h"
|
||||
#include "thingset.h"
|
||||
#include "helper.h"
|
||||
|
||||
// versioning of EEPROM layout (2 bytes)
|
||||
// change the version number each time the data object array below is changed!
|
||||
@@ -286,7 +286,7 @@ void eeprom_restore_data() {;}
|
||||
|
||||
void eeprom_update()
|
||||
{
|
||||
if (time(NULL) % EEPROM_UPDATE_INTERVAL == 0 && time(NULL) > 0) {
|
||||
if (uptime() % EEPROM_UPDATE_INTERVAL == 0 && uptime() > 0) {
|
||||
eeprom_store_data();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -192,7 +192,7 @@ void ExtWifi::process_1s()
|
||||
state = STATE_WIFI_IDLE;
|
||||
}
|
||||
|
||||
if (time(NULL) % 10 == 0) {
|
||||
if (uptime() % 10 == 0) {
|
||||
|
||||
printf("WiFi state: %d, error counter: %d\n", state, error_counter);
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "helper.h"
|
||||
|
||||
#ifdef __MBED__
|
||||
#include "mbed.h"
|
||||
#elif defined(__ZEPHYR__)
|
||||
#include <zephyr.h>
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
|
||||
uint32_t uptime()
|
||||
{
|
||||
#ifdef __ZEPHYR__
|
||||
return k_uptime_get() / 1000;
|
||||
#else
|
||||
return time(NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
float interpolate(const float a[], const float b[], size_t size, float value_a)
|
||||
{
|
||||
if (a[0] < a[size - 1]) {
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
if (value_a <= a[i]) {
|
||||
if (i == 0) {
|
||||
return b[0]; // value_a smaller than first element
|
||||
}
|
||||
else {
|
||||
return b[i-1] + (b[i] - b[i-1]) * (value_a - a[i-1]) / (a[i] - a[i-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return b[size - 1]; // value_a larger than last element
|
||||
}
|
||||
else {
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
if (value_a >= a[i]) {
|
||||
if (i == 0) {
|
||||
return b[0]; // value_a smaller than first element
|
||||
}
|
||||
else {
|
||||
return b[i-1] + (b[i] - b[i-1]) * (value_a - a[i-1]) / (a[i] - a[i-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return b[size - 1]; // value_a larger than last element
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef HELPER_H
|
||||
#define HELPER_H
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @brief
|
||||
* General helper functions
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Interpolation in a look-up table. Values of a must be monotonically increasing/decreasing
|
||||
*
|
||||
* @returns interpolated value of array b at position value_a
|
||||
*/
|
||||
float interpolate(const float a[], const float b[], size_t size, float value_a);
|
||||
|
||||
/**
|
||||
* Framework-independent system uptime
|
||||
*
|
||||
* @returns seconds since the system booted
|
||||
*/
|
||||
uint32_t uptime();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HELPER_H */
|
||||
+6
-5
@@ -12,6 +12,7 @@
|
||||
#include "leds.h"
|
||||
#include "device_status.h"
|
||||
#include "debug.h"
|
||||
#include "helper.h"
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
#include <stm32l0xx_ll_system.h>
|
||||
@@ -288,12 +289,12 @@ void LoadOutput::control()
|
||||
|| port->current > LOAD_CURRENT_MAX * 2)
|
||||
{
|
||||
dev_stat.set_error(ERR_LOAD_OVERCURRENT);
|
||||
oc_timestamp = time(NULL);
|
||||
oc_timestamp = uptime();
|
||||
}
|
||||
|
||||
if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) {
|
||||
dev_stat.set_error(ERR_LOAD_LOW_SOC);
|
||||
lvd_timestamp = time(NULL);
|
||||
lvd_timestamp = uptime();
|
||||
}
|
||||
|
||||
// long-term overvoltage (overvoltage transients are detected as an ADC alert and switch
|
||||
@@ -324,13 +325,13 @@ void LoadOutput::control()
|
||||
|
||||
if (dev_stat.has_error(ERR_LOAD_LOW_SOC) &&
|
||||
!dev_stat.has_error(ERR_BAT_UNDERVOLTAGE) &&
|
||||
time(NULL) - lvd_timestamp > lvd_recovery_delay)
|
||||
uptime() - lvd_timestamp > lvd_recovery_delay)
|
||||
{
|
||||
dev_stat.clear_error(ERR_LOAD_LOW_SOC);
|
||||
}
|
||||
|
||||
if (dev_stat.has_error(ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP) &&
|
||||
time(NULL) - oc_timestamp > oc_recovery_delay)
|
||||
uptime() - oc_timestamp > oc_recovery_delay)
|
||||
{
|
||||
dev_stat.clear_error(ERR_LOAD_OVERCURRENT);
|
||||
dev_stat.clear_error(ERR_LOAD_VOLTAGE_DIP);
|
||||
@@ -368,6 +369,6 @@ void LoadOutput::stop(uint32_t error_flag)
|
||||
// flicker the load LED if failure was most probably caused by the user
|
||||
if (error_flag & (ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT)) {
|
||||
leds_flicker(LED_LOAD);
|
||||
oc_timestamp = time(NULL);
|
||||
oc_timestamp = uptime();
|
||||
}
|
||||
}
|
||||
|
||||
+6
-5
@@ -10,9 +10,10 @@
|
||||
|
||||
#include "adc_dma.h"
|
||||
#include "debug.h"
|
||||
#include "helper.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h> // for time(NULL) function
|
||||
#include <time.h>
|
||||
|
||||
#if CONFIG_HAS_PWM_SWITCH
|
||||
|
||||
@@ -177,7 +178,7 @@ void PwmSwitch::control()
|
||||
|| enabled == false)
|
||||
{
|
||||
pwm_signal_stop();
|
||||
off_timestamp = time(NULL);
|
||||
off_timestamp = uptime();
|
||||
print_info("PWM charger stop.\n");
|
||||
}
|
||||
else if (port_int->voltage > (port_int->sink_voltage_max -
|
||||
@@ -197,7 +198,7 @@ void PwmSwitch::control()
|
||||
else if (pwm_signal_get_duty_cycle() < 0.05) {
|
||||
// prevent very short on periods and switch completely off instead
|
||||
pwm_signal_stop();
|
||||
off_timestamp = time(NULL);
|
||||
off_timestamp = uptime();
|
||||
print_info("PWM charger stop, no further derating possible.\n");
|
||||
}
|
||||
else {
|
||||
@@ -222,7 +223,7 @@ void PwmSwitch::control()
|
||||
&& port_int->voltage > port_int->sink_voltage_min
|
||||
&& terminal->neg_current_limit < 0 // discharging allowed
|
||||
&& terminal->voltage > port_int->voltage + offset_voltage_start
|
||||
&& time(NULL) > (off_timestamp + restart_interval)
|
||||
&& uptime() > (off_timestamp + restart_interval)
|
||||
&& enabled == true)
|
||||
{
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
|
||||
@@ -236,7 +237,7 @@ void PwmSwitch::control()
|
||||
void PwmSwitch::emergency_stop()
|
||||
{
|
||||
pwm_signal_stop();
|
||||
off_timestamp = time(NULL);
|
||||
off_timestamp = uptime();
|
||||
}
|
||||
|
||||
float PwmSwitch::get_duty_cycle()
|
||||
|
||||
+2
-1
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
@@ -61,7 +62,7 @@ public:
|
||||
bool enabled; ///< Can be used to disable the PWM power stage
|
||||
float offset_voltage_start; ///< Offset voltage of solar panel vs. battery to start charging (V)
|
||||
int restart_interval; ///< Interval to wait before retrying charging after low solar power cut-off (s)
|
||||
int off_timestamp; ///< Time when charger was switched off last time
|
||||
time_t off_timestamp; ///< Time when charger was switched off last time
|
||||
};
|
||||
|
||||
#endif /* PWM_SWITCH_H */
|
||||
|
||||
@@ -20,6 +20,7 @@ target_sources(app PRIVATE ../src/dcdc.cpp)
|
||||
target_sources(app PRIVATE ../src/eeprom.cpp)
|
||||
target_sources(app PRIVATE ../src/half_bridge.cpp)
|
||||
target_sources(app PRIVATE ../src/hardware.cpp)
|
||||
target_sources(app PRIVATE ../src/helper.cpp)
|
||||
target_sources(app PRIVATE ../src/leds.cpp)
|
||||
target_sources(app PRIVATE ../src/load.cpp)
|
||||
target_sources(app PRIVATE ../src/main_zephyr.cpp)
|
||||
|
||||
Reference in New Issue
Block a user