Got hypersensing to work pretty reliably now.
Unfortunately indentation changes.
This commit is contained in:
@@ -43,17 +43,13 @@ void ads1014_reset(ADS1014 *ads) {
|
||||
|
||||
#include <queue>
|
||||
class OhmicSensing {
|
||||
public:
|
||||
public:
|
||||
void handle(uint8_t voltageState, float inVoltage, float limitVoltage,
|
||||
float voltageDropLimit, uint32_t setupTime, uint8_t enabled,
|
||||
uint8_t &sensed);
|
||||
|
||||
// private:
|
||||
enum OhmicStates {
|
||||
OHMIC_IDLE,
|
||||
OHMIC_SETUP,
|
||||
OHMIC_PROBE
|
||||
};
|
||||
enum OhmicStates { OHMIC_IDLE, OHMIC_SETUP, OHMIC_PROBE };
|
||||
OhmicStates ohmicState = OHMIC_IDLE;
|
||||
uint64_t startTime;
|
||||
float_t oldVoltage = 0.0;
|
||||
@@ -140,8 +136,7 @@ void cb_get_inputs(void) // Set Master inputs, slave outputs, last operation
|
||||
Obj.In_Unit2.OhmicSensingVoltageLimit,
|
||||
Obj.In_Unit2.OhmicSensingVoltageDrop, Obj.In_Unit2.OhmicSensingSetupTime,
|
||||
Obj.In_Unit2.EnableOhmicSensing, Obj.Out_Unit2.OhmicSensingSensed);
|
||||
Obj.Out_Unit1.RawData = validVoltage0_2;
|
||||
Obj.Out_Unit2.RawData = Ohm2.ohmicState;
|
||||
Obj.Out_Unit1.RawData = (int)Ohm2.ohmicState;
|
||||
}
|
||||
|
||||
uint16_t dc_checker(void);
|
||||
@@ -265,7 +260,8 @@ void handleVoltageReader(float scale_in, float offset, float &outVoltage,
|
||||
MyMCP3221 *&mcp, uint8_t I2C_address,
|
||||
uint32_t &I2C_restarts) {
|
||||
float scale = scale_in;
|
||||
if (scale == 0.0) scale = 1.0;
|
||||
if (scale == 0.0)
|
||||
scale = 1.0;
|
||||
int stat = 1, data0;
|
||||
|
||||
switch (devType) {
|
||||
@@ -339,7 +335,8 @@ void handleVoltageReader(float scale_in, float offset, float &outVoltage,
|
||||
Wire2.begin();
|
||||
Wire2.setClock(I2C_BUS_SPEED);
|
||||
I2C_restarts++;
|
||||
if (devType == ADS1014_TYPE && ads != nullptr) ads1014_reset(ads);
|
||||
if (devType == ADS1014_TYPE && ads != nullptr)
|
||||
ads1014_reset(ads);
|
||||
// mcp3221 has no reset, reset the I2C bus is the best we can do
|
||||
}
|
||||
readStat = stat;
|
||||
@@ -382,12 +379,12 @@ void OhmicSensing::handle(uint8_t voltageState, float inVoltage,
|
||||
if (ohmicState == OHMIC_IDLE && inVoltage > limitVoltage) {
|
||||
ohmicState = OHMIC_SETUP;
|
||||
startTime = longTime.extendTime(micros());
|
||||
while (!voltages.empty()) voltages.pop(); // Remove history
|
||||
while (!voltages.empty())
|
||||
voltages.pop(); // Remove history
|
||||
return;
|
||||
}
|
||||
if (ohmicState == OHMIC_SETUP) {
|
||||
dTime = longTime.extendTime(micros()) - startTime;
|
||||
Obj.Out_Unit2.RawData = dTime;
|
||||
if (dTime > setupTime * 1000) {
|
||||
ohmicState = OHMIC_PROBE;
|
||||
startTime = longTime.extendTime(micros());
|
||||
@@ -398,21 +395,28 @@ void OhmicSensing::handle(uint8_t voltageState, float inVoltage,
|
||||
}
|
||||
if (ohmicState == OHMIC_PROBE) {
|
||||
dTime = longTime.extendTime(micros()) - startTime;
|
||||
Obj.Out_Unit2.RawData = dTime;
|
||||
voltages.push(inVoltage);
|
||||
while (voltages.size() > N_VOLTAGES) voltages.pop(); // Only N_VOLTAGES
|
||||
while (voltages.size() > N_VOLTAGES)
|
||||
voltages.pop(); // Only N_VOLTAGES
|
||||
if (dTime > 30000000) { // Go to IDLE after 30 seconds
|
||||
ohmicState = OHMIC_IDLE;
|
||||
return;
|
||||
}
|
||||
if ((inVoltage <= limitVoltage) || // Below starting threshold
|
||||
(fabs(voltageDropLimit) > 1e-3 &&
|
||||
refVoltage - inVoltage >=
|
||||
voltageDropLimit) || // Delta below refVoltage
|
||||
(fabs(voltageDropLimit) > 1e-3 && // Immediate drop
|
||||
oldVoltage - inVoltage >= voltageDropLimit) ||
|
||||
(fabs(voltageDropLimit) > 1e-3 && // Drop over 3 cycles
|
||||
voltages.front() - voltages.back() > voltageDropLimit)) {
|
||||
byte c1 = (inVoltage <= limitVoltage) ? 1 : 0; // Below starting threshold
|
||||
byte c2 = (fabs(voltageDropLimit) > 1e-3 &&
|
||||
refVoltage - inVoltage >= voltageDropLimit)
|
||||
? 2
|
||||
: 0; // Delta below refVoltage
|
||||
byte c3 = (fabs(voltageDropLimit) > 1e-3 && // Immediate drop
|
||||
oldVoltage - inVoltage >= voltageDropLimit)
|
||||
? 4
|
||||
: 0;
|
||||
byte c4 = (fabs(voltageDropLimit) > 1e-3 && // Drop over 3 cycles
|
||||
voltages.front() - voltages.back() > voltageDropLimit)
|
||||
? 8
|
||||
: 0;
|
||||
Obj.Out_Unit2.RawData = c1 + c2 + c3 + c4;
|
||||
if (c1 + c2 + c3 + c4 > 0) {
|
||||
sensed = 1;
|
||||
}
|
||||
oldVoltage = inVoltage;
|
||||
|
||||
Reference in New Issue
Block a user