Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit fb99ae1
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Sat Mar 9 12:45:35 2024 +0100

    Add DEMA

commit 52892c2
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Fri Mar 8 12:42:21 2024 +0100

    Add parenthesis

commit 843ad1c
Merge: f817917 8ef9f7b
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Fri Mar 8 12:41:28 2024 +0100

    Merge branch 'multi-relays' of https://github.com/FredM67/PVRouter-3-phase into multi-relays

commit f817917
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Fri Mar 8 12:39:40 2024 +0100

    Try DEMA

commit 8ef9f7b
Author: Frédéric Metrich <frederic.metrich@live.fr>
Date:   Thu Mar 7 21:53:34 2024 +0100

    Try double EWMA

commit f9cfd64
Merge: 685e204 d3bedad
Author: Frédéric Metrich <frederic.metrich@siemens.com>
Date:   Wed Mar 6 10:08:53 2024 +0100

    Merge branch 'multi-relays' of https://github.com/FredM67/PVRouter-3-phase into multi-relays

commit 685e204
Author: Frédéric Metrich <frederic.metrich@siemens.com>
Date:   Wed Mar 6 10:08:49 2024 +0100

    Fix test

commit d3bedad
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Tue Mar 5 23:01:56 2024 +0100

    Some doc

commit fcf91d3
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Wed Feb 28 21:03:13 2024 +0100

    Fix links

commit cdbd3e6
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Wed Feb 28 20:59:52 2024 +0100

    Default readme to fr

commit b334503
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Wed Feb 28 20:39:10 2024 +0100

    Set default to no relay

commit a8160a0
Author: Frédéric Metrich <frederic.metrich@siemens.com>
Date:   Wed Feb 28 15:02:38 2024 +0100

    Typo

commit 47b9ebe
Author: Frédéric Metrich <frederic.metrich@siemens.com>
Date:   Wed Feb 28 14:57:37 2024 +0100

    Multi relays engine

commit 26f896b
Author: Frédéric Metrich <frederic.metrich@siemens.com>
Date:   Mon Feb 26 16:12:32 2024 +0100

    Squashed commit of the following:

    commit c73c567
    Author: Frédéric Metrich <frederic.metrich@siemens.com>
    Date:   Mon Feb 26 11:11:24 2024 +0100

        Add some alias for type_trait

    commit eb53c16
    Author: Frédéric Metrich <frederic.metrich@siemens.com>
    Date:   Mon Feb 26 11:10:55 2024 +0100

        Update color in schematic

    commit eac769e
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Thu Feb 15 21:29:47 2024 +0100

        Update Readme.md (#82)

    commit 82d84ee
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Tue Feb 13 14:37:48 2024 +0100

        Added Thermostat_tri_HC.pdf

    commit fad5e19
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Tue Feb 13 14:36:53 2024 +0100

        Update Thermostat_tri_HC.drawio

    commit f03b4ca
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Mon Feb 12 11:07:04 2024 +0100

        WIP: 1772479 Fix mvAvg

    commit 5e5646a
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Mon Feb 5 00:04:30 2024 +0100

        Update CodeQL

    commit 4684e01
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:35:01 2024 +0100

        Upt readme

    commit d957494
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:32:24 2024 +0100

        Try again

    commit 46884f3
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:30:21 2024 +0100

        Try fix

    commit c7db21e
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:28:09 2024 +0100

        Fix readme

    commit 248a89a
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:23:14 2024 +0100

        Reorganize readmes

    commit fff68e0
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:11:19 2024 +0100

        Fix branch name

    commit 656308b
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:10:39 2024 +0100

        Fix filenames

    commit b911d8d
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:08:55 2024 +0100

        Fix links

    commit 83d5591
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Sun Feb 4 23:07:18 2024 +0100

        Multilinugal readme + more details

    commit 1772479
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Fri Feb 2 11:39:13 2024 +0100

        Fix mvAvg

    commit 90204aa
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Mon Jan 22 22:02:08 2024 +0100

        Fix relay template

    commit eef84ea
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Mon Jan 22 16:36:28 2024 +0100

        Enhanced sliding average...

    commit 1ea9c53
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Tue Nov 14 05:22:20 2023 +0100

        Reorganize folders

    commit 8522341
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Tue Nov 14 05:20:39 2023 +0100

        Upd

    commit a6a660b
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Wed Nov 8 23:11:58 2023 +0100

        Update Readme.md

    commit 145c86e
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Mon Nov 6 09:13:08 2023 +0100

        Update Readme.md

    commit 4d0936b
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Mon Nov 6 07:40:13 2023 +0100

        Update Readme.md

    commit 85910b9
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Mon Nov 6 07:21:08 2023 +0100

        Update Readme.md

    commit efbda93
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Mon Nov 6 07:19:52 2023 +0100

        Update Readme.md (#79)

    commit cfb0204
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Wed Oct 4 07:58:16 2023 +0200

        Formatting

    commit e0cefcd
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Thu Sep 21 17:29:28 2023 +0200

        Fix comment

    commit 0cf1661
    Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
    Date:   Thu Sep 21 13:21:59 2023 +0200

        Update schema

commit 839af94
Author: Frédéric Metrich <45318189+FredM67@users.noreply.github.com>
Date:   Wed Sep 20 15:58:02 2023 +0200

    Move pin assignment to ctor
  • Loading branch information
FredM67 committed Mar 9, 2024
1 parent 182da5e commit 16e5ba4
Show file tree
Hide file tree
Showing 19 changed files with 913 additions and 542 deletions.
7 changes: 4 additions & 3 deletions Mk2_3phase_RFdatalog_temp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ inline constexpr bool OVERRIDE_PIN_PRESENT{ false }; /**< set
#endif

inline constexpr bool WATCHDOG_PIN_PRESENT{ false }; /**< set it to 'true' if there's a watch led */
inline constexpr bool RELAY_DIVERSION{ false }; /**< set it to 'true' if a relay is used for diversion */
inline constexpr bool RELAY_DIVERSION{ false }; /**< set it to 'true' if a relay is used for diversion */
inline constexpr bool DUAL_TARIFF{ false }; /**< set it to 'true' if there's a dual tariff each day AND the router is connected to the billing meter */

// ----------- Pinout assignments -----------
Expand All @@ -75,14 +75,15 @@ inline constexpr uint8_t physicalLoadPin[NO_OF_DUMPLOADS]{ 5, 7 }; /**<
inline constexpr uint8_t loadPrioritiesAtStartup[NO_OF_DUMPLOADS]{ 0, 1 }; /**< load priorities and states at startup */

// Set the value to 0xff when the pin is not needed (feature deactivated)
inline constexpr uint8_t relayPin{ 0xff }; /**< for 3-phase PCB, relay trigger */
inline constexpr uint8_t dualTariffPin{ 0xff }; /**< for 3-phase PCB, off-peak trigger */
inline constexpr uint8_t diversionPin{ 0xff }; /**< if LOW, set diversion on standby */
inline constexpr uint8_t rotationPin{ 0xff }; /**< if LOW, trigger a load priority rotation */
inline constexpr uint8_t forcePin{ 0xff }; /**< for 3-phase PCB, force pin */
inline constexpr uint8_t watchDogPin{ 0xff }; /**< watch dog LED */

inline constexpr relayOutput relay_Output{ relayPin, 1000, 200, 1, 1 }; /**< config for relay diversion, see class definition for defaults and advanced options */
inline constexpr uint8_t tempSensorPin{ 0xff }; /**< for 3-phase PCB, sensor pin */

inline constexpr RelayEngine relays{ { { 0xff, 1000, 200, 1, 1 } } }; /**< config for relay diversion, see class definition for defaults and advanced options */

inline constexpr uint8_t ul_OFF_PEAK_DURATION{ 8 }; /**< Duration of the off-peak period in hours */
inline constexpr pairForceLoad rg_ForceLoad[NO_OF_DUMPLOADS]{ { -3, 2 } }; /**< force config for load #1 ONLY for dual tariff */
Expand Down
100 changes: 100 additions & 0 deletions Mk2_3phase_RFdatalog_temp/ewma_avg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* @file ewma_avg.hpp
* @author Frédéric Metrich (frederic.metrich@live.fr)
* @brief This file implements an Exponentially Weighted Moving Average template class
* @version 0.1
* @date 2024-02-27
*
* @section description Description
* The Exponentially Weighted Moving Average (EWMA) is a quantitative or statistical measure used to model or describe a time series.
* The EWMA is widely used in finance, the main applications being technical analysis and volatility modeling.
*
* The moving average is designed as such that older observations are given lower weights.
* The weights fall exponentially as the data point gets older – hence the name exponentially weighted.
*
* The only decision a user of the EWMA must make is the parameter alpha.
* The parameter decides how important the current observation is in the calculation of the EWMA.
* The higher the value of alpha, the more closely the EWMA tracks the original time series.
*
* @section note Note
* This class is implemented in way to use only integer math.
* This comes with some restrictions on the alpha parameter, but the benefit of full integer math wins
* on the side-drawback.
*
* @copyright Copyright (c) 2024
*
*/

#ifndef EWMA_AVG_H
#define EWMA_AVG_H

#include <Arduino.h>

#include "type_traits.hpp"

/**
* @brief Helper compile-time function to retrieve the previous power of 2 of the given number (120 => 64 => 6)
*
* @param v The input number
* @return constexpr uint8_t The next power of two
*/
constexpr uint8_t round_up_to_power_of_2(uint16_t v)
{
if (__builtin_popcount(v) == 1) { return __builtin_ctz(v) - 1; }

uint8_t next_pow_of_2{ 0 };

while (v)
{
v >>= 1;
++next_pow_of_2;
}

return --next_pow_of_2;
}

/**
* @brief Exponentially Weighted Moving Average
*
* @details The smoothing factor is the approximate amount of values taken to calculate the average.
* Since the Arduino is very slow and does not provide any dedicated math co-processor,
* the smoothing factor will be rounded to the previous power of 2. Ie 120 will be rounded to 64.
* This allows to perform all the calculations with integer math, which is much faster !
*
* @note Because of the 'sign extension', the sign is copied into lower bits.
*
* @tparam A Smoothing factor
* @param input Input value
* @return long Output value
*/
template< uint8_t A = 10 >
class EWMA_average
{
public:
void addValue(int32_t input)
{
ema_raw = ema_raw - ema + input;
ema = ema_raw >> round_up_to_power_of_2(A);

ema_ema_raw = ema_ema_raw - ema_ema + ema;
ema_ema = ema_ema_raw >> round_up_to_power_of_2(A);
}

auto getAverageS() const
{
return ema;
}

auto getAverageD() const
{
return (ema << 1) - ema_ema;
}

private:
int32_t ema_ema_raw{ 0 };
int32_t ema_ema{ 0 };
int32_t ema_raw{ 0 };
int32_t ema{ 0 };
};

#endif
6 changes: 3 additions & 3 deletions Mk2_3phase_RFdatalog_temp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ void loop()

if constexpr (RELAY_DIVERSION)
{
relay_Output.inc_duration();
relay_Output.proceed_relay();
relays.inc_duration();
relays.proceed_relays();
}
}
}
Expand Down Expand Up @@ -407,7 +407,7 @@ void loop()

if constexpr (RELAY_DIVERSION)
{
relay_Output.update_average(tx_data.power);
relays.update_average(tx_data.power);
}

if constexpr (TEMP_SENSOR_PRESENT)
Expand Down
55 changes: 0 additions & 55 deletions Mk2_3phase_RFdatalog_temp/movingAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,6 @@

#include "type_traits.hpp"

/**
* @brief Helper compile-time function to retrieve the previous power of 2 of the given number (120 => 64 => 6)
*
* @param v The input number
* @return constexpr uint8_t The next power of two
*/
constexpr uint8_t round_up_to_power_of_2(uint16_t v)
{
if (__builtin_popcount(v) == 1) { return __builtin_ctz(v) - 1; }

uint8_t next_pow_of_2{ 0 };

while (v)
{
v >>= 1;
++next_pow_of_2;
}

return --next_pow_of_2;
}

/**
* @brief Exponentially Weighted Moving Average
*
* @details The smoothing factor is the approximate amount of values taken to calculate the average.
* Since the Arduino is very slow and does not provide any dedicated math co-processor,
* the smoothing factor will be rounded to the previous power of 2. Ie 120 will be rounded to 64.
* This allows to perform all the calculations with integer math, which is much faster !
*
* @note Because of the 'sign extension', the sign is copied into lower bits.
*
* @tparam A Smoothing factor
* @param input Input value
* @return long Output value
*/
template< uint8_t A = 10 >
class EWMA_average
{
public:
void addValue(int32_t input)
{
w = w - x + input;
x = w >> round_up_to_power_of_2(A);
}

auto getAverage() const
{
return x;
}

private:
int32_t w{ 0 };
int32_t x{ 0 };
};

/**
* @brief Template class for implementing a sliding average
*
Expand Down
7 changes: 5 additions & 2 deletions Mk2_3phase_RFdatalog_temp/processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ void initializeOptionalPins()

if constexpr (RELAY_DIVERSION)
{
pinMode(relayPin, OUTPUT);
delay(100);
relays.initializePins();
}

if constexpr (WATCHDOG_PIN_PRESENT)
Expand Down Expand Up @@ -583,6 +582,7 @@ void processMinusHalfCycle(const uint8_t phase)
*
* @ingroup TimeCritical
*/
uint8_t nextLogicalLoadToBeAdded() __attribute__((optimize("-O3")));
uint8_t nextLogicalLoadToBeAdded()
{
for (uint8_t index = 0; index < NO_OF_DUMPLOADS; ++index)
Expand All @@ -603,6 +603,7 @@ uint8_t nextLogicalLoadToBeAdded()
*
* @ingroup TimeCritical
*/
uint8_t nextLogicalLoadToBeRemoved() __attribute__((optimize("-O3")));
uint8_t nextLogicalLoadToBeRemoved()
{
uint8_t index{ NO_OF_DUMPLOADS };
Expand Down Expand Up @@ -651,6 +652,8 @@ void processLatestContribution(const uint8_t phase)
*
* @ingroup TimeCritical
*/
void processDataLogging() __attribute__((optimize("-O3")));

void processDataLogging()
{
if (++n_cycleCountForDatalogging < DATALOG_PERIOD_IN_MAINS_CYCLES)
Expand Down
14 changes: 12 additions & 2 deletions Mk2_3phase_RFdatalog_temp/type_traits/integral_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
template< typename T, T v >
struct integral_constant
{
static const T value = v;
static constexpr T value = v;
using value_type = T;
using type = integral_constant; // using injected-class-name
constexpr operator value_type() const noexcept
{
return value;
}
constexpr value_type operator()() const noexcept
{
return value;
} // since c++14
};

typedef integral_constant< bool, true > true_type;
typedef integral_constant< bool, false > false_type;
typedef integral_constant< bool, false > false_type;
19 changes: 19 additions & 0 deletions Mk2_3phase_RFdatalog_temp/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,23 @@ template< typename _Tp > constexpr size_t size(const _Tp (& /*__array*/)[0]) noe
return 0;
}

template< class... Ts >
constexpr uint8_t ival(Ts... Vs)
{
char vals[sizeof...(Vs)] = { Vs... };
uint8_t result = 0;
for (uint8_t i = 0; i < sizeof...(Vs); i++)
{
result *= 10;
result += vals[i] - '0';
}
return result;
}

template< char... Vs >
constexpr integral_constant< uint8_t, ival(Vs...) > operator""_i()
{
return {};
}

#endif // _TYPES_H
4 changes: 2 additions & 2 deletions Mk2_3phase_RFdatalog_temp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ inline void printConfiguration()
{
DBUGLN(F("is present"));

relay_Output.printRelayConfiguration();
relays.printConfiguration();
}
else
{
Expand Down Expand Up @@ -264,7 +264,7 @@ inline void printForSerialText()
if constexpr (RELAY_DIVERSION)
{
Serial.print(F("/"));
Serial.print(relay_Output.get_average());
Serial.print(relays.get_average());
}

for (phase = 0; phase < NO_OF_PHASES; ++phase)
Expand Down
Loading

0 comments on commit 16e5ba4

Please sign in to comment.