micro-ELC
LoadAdjustCoarseFine.h
Go to the documentation of this file.
1#ifndef LOAD_ADJUST_COARSE_FINE_H
2#define LOAD_ADJUST_COARSE_FINE_H
3#include "LoadAdjust.h"
4#include <Arduino.h>
9public:
12
13protected:
17
18 uint16_t relayValue = 0;
20
21public:
30 LoadAdjustCoarseFine(LoadAdjust& _coarse, LoadAdjust& _fine, uint8_t _numCoarseSteps, float _maxResistance, float _fineResistance)
31 : coarse(_coarse)
32 , fine(_fine)
33 {
34 numCoarseSteps = _numCoarseSteps;
35 maxResistance = _maxResistance;
36 fineResistance = _fineResistance;
38 }
39 void setLoad(float load)
40 {
41 // the fine adjust adds extra load capability, so load is adjusted from [0,1] to a range that exceeds 1.0 to use full capacity
42 load = constrain(load, 0.0, 1.0) * (1.0 + relayStepsInFine / (numCoarseSteps));
43 // if relays could be turned on fractionally ideally we'd want this value
44 float fractionalRelayValue = (float)load * (numCoarseSteps);
45
46 // change relayValue if necessary
47 while (relayValue >= fractionalRelayValue && relayValue > 0) {
48 // the relays would be too much resistance
49 relayValue--;
50 }
51 while (relayValue + relayStepsInFine <= fractionalRelayValue && relayValue < numCoarseSteps - 1) {
52 // the fine control + the relays can't reach high enough
53 relayValue++;
54 }
55 float leftover = fractionalRelayValue - relayValue; // the fine adjust needs to make up for this much
56
57 coarse.setLoad((float)relayValue / (numCoarseSteps - 1) + 0.5 / numCoarseSteps); // map to range of 0-1, hitting center of steps
58 fine.setLoad(leftover / relayStepsInFine);
59 }
60 void begin()
61 {
62 coarse.begin();
63 fine.begin();
64 }
65};
66#endif
combines a LoadAdjust that has discrete steps with another LoadAdjust to get more precision
Definition: LoadAdjustCoarseFine.h:8
uint16_t relayValue
Definition: LoadAdjustCoarseFine.h:18
LoadAdjustCoarseFine(LoadAdjust &_coarse, LoadAdjust &_fine, uint8_t _numCoarseSteps, float _maxResistance, float _fineResistance)
combines a LoadAdjust that has discrete steps with another LoadAdjust to get more precision
Definition: LoadAdjustCoarseFine.h:30
LoadAdjust & fine
Definition: LoadAdjustCoarseFine.h:11
void begin()
set up pin states
Definition: LoadAdjustCoarseFine.h:60
void setLoad(float load)
set a new value for the adjustable load
Definition: LoadAdjustCoarseFine.h:39
float fineResistance
Definition: LoadAdjustCoarseFine.h:16
LoadAdjust & coarse
Definition: LoadAdjustCoarseFine.h:10
float relayStepsInFine
Definition: LoadAdjustCoarseFine.h:19
float maxResistance
Definition: LoadAdjustCoarseFine.h:15
uint16_t numCoarseSteps
Definition: LoadAdjustCoarseFine.h:14
This class defines an interface for any type of adjustable electrical load.
Definition: LoadAdjust.h:7
virtual void begin()=0
set up pin states
virtual void setLoad(float load)=0
set a new value for the adjustable load