Skip to content

Commit

Permalink
More code for relay diversion
Browse files Browse the repository at this point in the history
  • Loading branch information
FredM67 committed May 5, 2023
1 parent 09a8647 commit 9854dd0
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 187 deletions.
19 changes: 4 additions & 15 deletions Mk2_3phase_RFdatalog_temp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//#define SERIALOUT ///< Uncomment if a wired serial connection is used
//--------------------------------------------------------------------------------------------------

#include "config_system.h"
#include "debug.h"
#include "types.h"

Expand All @@ -50,7 +51,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 Down Expand Up @@ -83,7 +84,7 @@ inline constexpr uint8_t watchDogPin{ 0xff }; /**<

inline constexpr uint8_t tempSensorPin{ 0xff }; /**< for 3-phase PCB, sensor pin */

inline constexpr relayConfig relay_Config; /**< config for relay diversion, see class definition for defaults */
inline relayOutput relay_Output{ relayPin }; /**< config for relay diversion, see class definition for defaults */

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 All @@ -96,19 +97,7 @@ inline constexpr DeviceAddress sensorAddrs[]{ { 0x28, 0xBE, 0x41, 0x6B, 0x09, 0x
{ 0x28, 0x59, 0x1F, 0x6A, 0x09, 0x00, 0x00, 0xB0 },
{ 0x28, 0x1B, 0xD7, 0x6A, 0x09, 0x00, 0x00, 0xB7 } }; /**< list of temperature sensor Addresses */

//--------------------------------------------------------------------------------------------------
// for users with zero-export profile, this value will be negative
inline constexpr int16_t REQUIRED_EXPORT_IN_WATTS{ 20 }; /**< when set to a negative value, this acts as a PV generator */

//--------------------------------------------------------------------------------------------------
// other system constants, should match most of installations
inline constexpr uint8_t SUPPLY_FREQUENCY{ 50 }; /**< number of cycles/s of the grid power supply */

inline constexpr uint8_t DATALOG_PERIOD_IN_SECONDS{ 5 }; /**< Period of datalogging in seconds */
inline constexpr uint16_t DATALOG_PERIOD_IN_MAINS_CYCLES{ DATALOG_PERIOD_IN_SECONDS * SUPPLY_FREQUENCY }; /**< Period of datalogging in cycles */
//--------------------------------------------------------------------------------------------------

inline constexpr uint32_t ROTATION_AFTER_CYCLES{ 8UL * 3600UL * SUPPLY_FREQUENCY }; /**< rotates load priorities after this period of inactivity */
inline constexpr uint32_t ROTATION_AFTER_CYCLES{ 8UL * 3600UL * SUPPLY_FREQUENCY }; /**< rotates load priorities after this period of inactivity */

/* --------------------------------------
RF configuration (for the RFM12B module)
Expand Down
29 changes: 29 additions & 0 deletions Mk2_3phase_RFdatalog_temp/config_system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @file config_system.h
* @author Frédéric Metrich ([email protected])
* @brief Basic configuration values to be set by the end-user
* @version 0.1
* @date 2023-05-05
*
* @copyright Copyright (c) 2023
*
*/

#ifndef __CONFIG_SYSTEM_H__
#define __CONFIG_SYSTEM_H__

#include <Arduino.h>

//--------------------------------------------------------------------------------------------------
// for users with zero-export profile, this value will be negative
inline constexpr int16_t REQUIRED_EXPORT_IN_WATTS{ 20 }; /**< when set to a negative value, this acts as a PV generator */

//--------------------------------------------------------------------------------------------------
// other system constants, should match most of installations
inline constexpr uint8_t SUPPLY_FREQUENCY{ 50 }; /**< number of cycles/s of the grid power supply */

inline constexpr uint8_t DATALOG_PERIOD_IN_SECONDS{ 5 }; /**< Period of datalogging in seconds */
inline constexpr uint16_t DATALOG_PERIOD_IN_MAINS_CYCLES{ DATALOG_PERIOD_IN_SECONDS * SUPPLY_FREQUENCY }; /**< Period of datalogging in cycles */
//--------------------------------------------------------------------------------------------------

#endif // __CONFIG_SYSTEM_H__
14 changes: 13 additions & 1 deletion Mk2_3phase_RFdatalog_temp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static_assert(__cplusplus >= 201703L, "See also : https://github.com/FredM67/PVR
#include "processing.h"
#include "types.h"
#include "utils.h"
#include "utils_relay.h"
#include "validation.h"

// -------------- general global variables -----------------
Expand Down Expand Up @@ -353,7 +354,7 @@ void loop()
static bool bOffPeak{ false };
static int16_t iTemperature_x100{ 0 };

if (b_newMainsCycle) // flag is set after every pair of ADC conversions
if (b_newMainsCycle) // flag is set after every pair of ADC conversions
{
b_newMainsCycle = false; // reset the flag
++perSecondTimer;
Expand All @@ -373,6 +374,12 @@ void loop()
{
bOffPeak = proceedLoadPrioritiesAndOverriding(iTemperature_x100); // called every second
}

if constexpr (RELAY_DIVERSION)
{
relay_Output.inc_duration();
relay_Output.proceed_relay();
}
}
}

Expand All @@ -396,6 +403,11 @@ void loop()
{
tx_data.Vrms_L_x100[phase] = static_cast< int32_t >(100 * f_voltageCal[phase] * sqrt(copyOf_sum_Vsquared[phase] / copyOf_sampleSetsDuringThisDatalogPeriod));
}

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

if constexpr (TEMP_SENSOR_PRESENT)
Expand Down
2 changes: 1 addition & 1 deletion Mk2_3phase_RFdatalog_temp/movingAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class movingAvg

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

T _ar[N]{};
};
Expand Down
3 changes: 2 additions & 1 deletion Mk2_3phase_RFdatalog_temp/processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

#include "calibration.h"
#include "processing.h"
#include "utils.h"
#include "utils_pins.h"
#include "dualtariff.h"

/*!
* @defgroup TimeCritical Time critical functions Group
Expand Down
4 changes: 0 additions & 4 deletions Mk2_3phase_RFdatalog_temp/processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include "config.h"

#include "movingAvg.h"

// analogue input pins
inline constexpr uint8_t sensorV[NO_OF_PHASES]{ 0, 2, 4 }; /**< for 3-phase PCB, voltage measurement for each phase */
inline constexpr uint8_t sensorI[NO_OF_PHASES]{ 1, 3, 5 }; /**< for 3-phase PCB, current measurement for each phase */
Expand Down Expand Up @@ -47,8 +45,6 @@ inline volatile uint8_t copyOf_lowestNoOfSampleSetsPerMainsCycle; /**< */
inline volatile uint16_t copyOf_sampleSetsDuringThisDatalogPeriod; /**< copy of for counting the sample sets during each datalogging period */
inline volatile uint16_t copyOf_countLoadON[NO_OF_DUMPLOADS]; /**< copy of number of cycle the load was ON (over 1 datalog period) */

inline volatile movingAvg< int16_t, DATALOG_PERIOD_IN_MAINS_CYCLES > sliding_Average;

#ifdef TEMP_ENABLED
inline PayloadTx_struct< NO_OF_PHASES, size(sensorAddrs) > tx_data; /**< logging data */
#else
Expand Down
150 changes: 0 additions & 150 deletions Mk2_3phase_RFdatalog_temp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,156 +20,6 @@
#include "utils_rf.h"
#include "utils_temp.h"

inline void togglePin(const uint8_t pin) __attribute__((always_inline));

inline void setPinON(const uint8_t pin) __attribute__((always_inline));
inline void setPinsON(const uint16_t pins) __attribute__((always_inline));

inline void setPinOFF(const uint8_t pin) __attribute__((always_inline));
inline void setPinsOFF(const uint16_t pins) __attribute__((always_inline));

inline bool getPinState(const uint8_t pin) __attribute__((always_inline));

/**
* @brief Set the specified bit to 1
*
* @tparam T Type of the variable
* @param _dest Integer variable to modify
* @param bit Bit to set in _dest
*/
template< typename T > constexpr void bit_set(T& _dest, const uint8_t bit)
{
_dest |= (T)0x01 << bit;
}

/**
* @brief Read the specified bit
*
* @tparam T Type of the variable
* @param _src Integer variable to read
* @param bit Bit to read in _src
* @return constexpr uint8_t
*/
template< typename T > constexpr uint8_t bit_read(const T& _src, const uint8_t bit)
{
return (_src >> bit) & (T)0x01;
}

/**
* @brief Clear the specified bit
*
* @tparam T Type of the variable
* @param _dest Integer variable to modify
* @param bit Bit to clear in _src
* @return constexpr uint8_t
*/
template< typename T > constexpr uint8_t bit_clear(T& _dest, const uint8_t bit)
{
return _dest &= ~((T)0x01 << bit);
}

/**
* @brief Toggle the specified pin
*
*/
void togglePin(const uint8_t pin)
{
if (pin < 8)
{
bit_set(PIND, pin);
}
else
{
bit_set(PINB, pin - 8);
}
}

/**
* @brief Set the Pin state for the specified pin
*
* @param pin pin to change [2..13]
* @param bState state to be set
*/
inline void setPinState(const uint8_t pin, const bool bState)
{
if (bState)
{
setPinON(pin);
}
else
{
setPinOFF(pin);
}
}

/**
* @brief Set the Pin state to ON for the specified pin
*
* @param pin pin to change [2..13]
*/
inline void setPinON(const uint8_t pin)
{
if (pin < 8)
{
bit_set(PORTD, pin);
}
else
{
bit_set(PORTB, pin - 8);
}
}

/**
* @brief Set the Pins state to ON
*
* @param pins The pins to change
*/
inline void setPinsON(const uint16_t pins)
{
PORTD |= lowByte(pins);
PORTB |= highByte(pins);
}

/**
* @brief Set the Pin state to OFF for the specified pin
*
* @param pin pin to change [2..13]
*/
inline void setPinOFF(const uint8_t pin)
{
if (pin < 8)
{
bit_clear(PORTD, pin);
}
else
{
bit_clear(PORTB, pin - 8);
}
}

/**
* @brief Set the Pins state to OFF
*
* @param pins The pins to change
*/
inline void setPinsOFF(const uint16_t pins)
{
PORTD &= ~lowByte(pins);
PORTB &= ~highByte(pins);
}

/**
* @brief Get the Pin State
*
* @param pin The pin to read
* @return true if HIGH
* @return false if LOW
*/
inline bool getPinState(const uint8_t pin)
{
return (pin < 8) ? bitRead(PIND, pin) : bitRead(PINB, pin - 8);
}

/**
* @brief Print the configuration during start
*
Expand Down
Loading

0 comments on commit 9854dd0

Please sign in to comment.