Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Frédéric Metrich committed Feb 26, 2024
1 parent 839af94 commit 26f896b
Show file tree
Hide file tree
Showing 46 changed files with 2,633 additions and 1,506 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -68,7 +68,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
# uses: github/codeql-action/autobuild@v2
# uses: github/codeql-action/autobuild@v3
uses: arduino/compile-sketches@v1
with:
fqbn: 'arduino:avr:uno'
Expand All @@ -90,6 +90,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ c_cpp_properties.json
inols*
logs
docs/.$*.drawio.*
*.res.txt
58 changes: 58 additions & 0 deletions Mk2_3phase_RFdatalog_temp/Readme.fr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[![en](https://img.shields.io/badge/lang-en-red.svg)](https://github.com/FredM67/PVRouter-3-phase/blob/main/Mk2_3phase_RFdatalog_temp/Readme.md)

Ce programme doit être utilisé avec l’IDE Arduino et/ou d’autres IDE de développement comme VSCode + PlatformIO.

# Utilisation avec Arduino IDE

Vous devrez télécharger et installer la version la plus récente de l'[Arduino IDE](https://www.arduino.cc/en/software).

Téléchargez la version « standard », PAS la version du Microsoft Store.
Procurez-vous la version « Win 10 et plus récent, 64 bits » ou la version « MSI installer ».

Étant donné que le code est optimisé avec l'une des dernières normes de C++, vous devrez modifier un fichier de configuration pour activer C++17.

Veuillez rechercher le fichier '**platform.txt**' situé dans le chemin d’installation de l’IDE Arduino.

Pour **Windows**, typiquement, vous trouverez le fichier dans '**C:\Program Files (x86)\Arduino\hardware\arduino\avr**' et/ou dans '**%LOCALAPPDATA%\Arduino15\packages\arduino\hardware\avr\x.y.z**' où 'x.y.z' est la version du package **Arduino AVR Boards**.

Vous pouvez aussi taper cette commande dans un Powershell : `Get-Childitem –Path C:\ -Include platform.txt -Recurse -ErrorAction SilentlyContinue`. Cela peut prendre quelques secondes/minutes jusqu’à ce que le fichier soit trouvé.

Pour **Linux**, si vous utilisez le paquet AppImage, vous trouverez ce fichier dans '**~/.arduino15/packages/arduino/hardware/avr/1.8.6**'.
Vous pouvez exécuter `find / -name platform.txt 2>/dev/null` au cas où l’emplacement aurait été modifié.

Modifiez le fichier dans n’importe quel éditeur de texte (vous aurez besoin des **droits d’administrateur**) et remplacez le paramètre '**-std=gnu++11**' par '**-std=gnu++17**'. Voilà!

Si votre IDE Arduino a été ouvert, veuillez fermer toutes les instances et l’ouvrir à nouveau.

# Utilisation avec Visual Studio Code

Vous devrez installer des extensions supplémentaires. Les extensions les plus populaires et les plus utilisées pour ce travail sont '*Arduino*' et '*Platform IO*'.

# Aperçu rapide des fichiers

- **Mk2_3phase_RFdatalog_temp.ino** : Ce fichier est nécessaire pour l’IDE Arduino
- **calibration.h** : contient les paramètres d’étalonnage
- **config.h** : les préférences de l’utilisateur sont stockées ici (affectation des broches, fonctionnalités, ...)
- **config_system.h** : constantes système rarement modifiées
- **constants.h** : quelques constantes - *ne pas modifier*
- **debug.h** : Quelques macros pour la sortie série et le débogage
- **dualtariff.h** : définitions de la fonction double tarif
- **main.cpp** : code source principal
- **main.h** : prototypes de fonctions
- **movingAvg.h** : code source pour la moyenne glissante
- **processing.cpp** : code source du moteur de traitement
- **processing.h** : prototypes de fonctions du moteur de traitement
- **Readme.fr.md** : ce fichier
- **types.h** : définitions des types, ...
- **type_traits.h** : quelques trucs STL qui ne sont pas encore disponibles dans le paquet avr
- **type_traits** : contient des patrons STL manquants
- **utils_relay.h** : code source de la fonctionnalité *diversion par relais*
- **utils_rf.h** : code source de la fonction *RF*
- **utils_temp.h** : code source de la fonctionnalité *Température*
- **utils.h** : fonctions d’aide et trucs divers
- **validation.h** : validation des paramètres, ce code n’est exécuté qu’au moment de la compilation !
- **platformio.ini** : paramètres PlatformIO
- **inject_sketch_name.py** : script d'aide pour PlatformIO
- **Doxyfile** : paramètre pour Doxygen (documentation du code)

L’utilisateur final ne doit éditer QUE les fichiers **calibration.h** et **config.h**.
16 changes: 13 additions & 3 deletions Mk2_3phase_RFdatalog_temp/Readme.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
This program is to be used with the Arduino IDE and/or other development IDE lik
[![en](https://img.shields.io/badge/lang-en-red.svg)](https://github.com/FredM67/PVRouter-3-phase/blob/main/Mk2_3phase_RFdatalog_temp/Readme.md)
[![fr](https://img.shields.io/badge/lang-fr-blue.svg)](https://github.com/FredM67/PVRouter-3-phase/blob/main/Mk2_3phase_RFdatalog_temp/Readme.fr.md)

This program is to be used with the Arduino IDE and/or other development IDE like VSCode + PlatformIO.

# Use with Arduino IDE

You'll need to download and install the **latest** [Arduino IDE](https://www.arduino.cc/en/software).

Download the "standalone" version, NOT the version from the Microsoft Store.
Pick-up the "Win 10 and newer, 64 bits" or the "MSI installer" version.

Since the code is optimized with the latest standard of C++, you'll need to edit a config file to activate C++17.

Please search the file '**platform.txt**' located in the installation path of the Arduino IDE.

For **Windows**, typically, you'll find the file in '**C:\Program Files (x86)\Arduino\hardware\arduino\avr**' or in '**%LOCALAPPDATA%\Arduino15\packages\arduino\hardware\avr\x.y.z**' where 'x.y.z' is the version of the **Arduino AVR Boards** package.
For **Windows**, typically, you'll find the file in '**C:\Program Files (x86)\Arduino\hardware\arduino\avr**' and/or in '**%LOCALAPPDATA%\Arduino15\packages\arduino\hardware\avr\x.y.z**' where 'x.y.z' is the version of the **Arduino AVR Boards** package.

You can type this command in a Powershell : `Get-Childitem –Path C:\ -Include platform.txt -Recurse -ErrorAction SilentlyContinue`. It could take a couple of seconds/minutes until the file is found.

For **Linux**, if using the AppImage package, you'll find this file in '**~/.arduino15/packages/arduino/hardware/avr/1.8.6**'.
You can run `find / -name platform.txt 2>/dev/null` in case the location has been changed.

Edit the file in any Text Editor (you'll need Admin rights) and replace the parameter '**-std=gnu++11**' with '**-std=gnu++17**'. That's it!
Edit the file in any Text Editor (you'll need **Admin rights**) and replace the parameter '**-std=gnu++11**' with '**-std=gnu++17**'. That's it!

If your Arduino IDE was opened, please close all the instances and open it again.

Expand All @@ -37,6 +46,7 @@ You'll need to install additional extension(s). The most popular and used extens
- **Readme.md** : this file
- **types.h** : definitions of types, ...
- **type_traits.h** : some STL stuff not yet available in the avr-package
- **type_traits** : folder containing some missing STL helpers
- **utils_relay.h** : source code for the *relay-diversion* feature
- **utils_rf.h** : source code for the *RF* feature
- **utils_temp.h** : source code for the *temperature* feature
Expand Down
199 changes: 178 additions & 21 deletions Mk2_3phase_RFdatalog_temp/movingAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,85 +16,242 @@

#include "type_traits.hpp"

template< typename T, uint8_t N = 10 >
/**
* @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
*
* @tparam T Type of values to be stored
* @tparam DURATION_IN_MINUTES Size of main array
* @tparam VALUES_PER_MINUTE Size of sub array
*
* @note
* Since the Arduino RAM is tiny, we need to store a few values as possible.
* Suppose, you want a sliding window of 10 minutes and receive a value every 5 seconds.
* With one single array, 10 x 12 values would be necessary to store.
* Using 2 arrays, one to store incoming values over 1 minute and one to store values every minutes,
* we need only 12 + 10 values.
* How it works :
* - the average is calculated over 1 minute with incoming values
* - this sub-average is added to the main array
* - the average is calculated on the main array
*
* Drawback of this method: the average is updated only every minutes !
*/
template< typename T, uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10 >
class movingAvg
{
public:
/**
* @brief Reset everything
*
*/
void clear()
{
_idx = 0;

if constexpr (is_floating_point< T >::value)
_sum = 0.0;
{
_sum = 0.0F;
}
else
{
_sum = 0;
}

uint8_t i{ N };
do
{
uint8_t i{ DURATION_IN_MINUTES };
do {
--i;
if constexpr (is_floating_point< T >::value)
_ar[i] = 0.0;
{
_ar[i] = 0.0F;
}
else
{
_ar[i] = 0;
}
} while (i);

_clear_sub();
}

/**
* @brief Add a value
*
* @param _value Value to be added
*/
void addValue(const T& _value)
{
_sum -= _ar[_idx];
_ar[_idx] = _value;
_sum += _value;
++_idx;
_sub_sum -= _sub_ar[_sub_idx];
_sub_ar[_sub_idx] = _value;
_sub_sum += _value;
++_sub_idx;

if (_idx == N)
if (_sub_idx == VALUES_PER_MINUTE)
{
_idx = 0; // faster than %
_sub_idx = 0; // faster than %
_addValue(_getAverage());
}
}

void fillValue(const T& _value)
{
_idx = 0;
_sum = N * _value;
_sum = DURATION_IN_MINUTES * _value;

uint8_t i{ N };
uint8_t i{ DURATION_IN_MINUTES };
do
{
_ar[--i] = _value;
} while (i);
}

/**
* @brief Get the sliding average
*
* @return auto The sliding average
*
* @note This value is updated every minute, except for the special case of a duration of
* ONE minute. In this case, it is updated for each new input value.
*/
auto getAverage() const
{
return _sum / N;
if constexpr (DURATION_IN_MINUTES == 1)
return _getAverage();
else if constexpr (DURATION_IN_MINUTES == 2)
return _sum >> 1;
else if constexpr (DURATION_IN_MINUTES == 4)
return _sum >> 2;
else if constexpr (DURATION_IN_MINUTES == 8)
return _sum >> 3;
else if constexpr (DURATION_IN_MINUTES == 16)
return _sum >> 4;
else if constexpr (DURATION_IN_MINUTES == 32)
return _sum >> 5;
else
return _sum * invN;
}

auto getElement(uint8_t idx) const
{
if (idx >= N)
if (idx >= DURATION_IN_MINUTES)
{
if constexpr (is_floating_point< T >::value)
return 0.0;
return 0.0F;
else
return 0;
return (T)0;
}

return _ar[idx];
}

constexpr uint8_t getSize() const
{
return N;
return DURATION_IN_MINUTES;
}

private:
void _clear_sub()
{
_sub_idx = 0;

if constexpr (is_floating_point< T >::value)
_sub_sum = 0.0F;
else
_sub_sum = 0;

uint8_t i{ VALUES_PER_MINUTE };
do {
--i;
if constexpr (is_floating_point< T >::value)
_sub_ar[i] = 0.0F;
else
_sub_ar[i] = 0;
} while (i);
}

void _addValue(const T& _value)
{
_sum -= _ar[_idx];
_ar[_idx] = _value;
_sum += _value;
++_idx;

if (_idx == DURATION_IN_MINUTES)
{
_idx = 0; // faster than %
}
}

auto _getAverage() const
{
return _sub_sum * invD;
}

private:
uint8_t _idx{ 0 };
uint8_t _sub_idx{ 0 };
typename conditional< is_floating_point< T >::value, T, int32_t >::type _sum{ 0 };
typename conditional< is_floating_point< T >::value, T, int32_t >::type _sub_sum{ 0 };

T _ar[N]{};
T _sub_ar[VALUES_PER_MINUTE]{};
T _ar[DURATION_IN_MINUTES]{};

static constexpr float invN{ 1 / N };
static constexpr float invD{ 1.0 / VALUES_PER_MINUTE };
static constexpr float invN{ 1.0 / DURATION_IN_MINUTES };
};

#endif
Loading

0 comments on commit 26f896b

Please sign in to comment.