Adjusted mutex lock and delay for modbus
This commit is contained in:
@@ -146,7 +146,7 @@ const uint8_t digitalIO::getInNum()
|
||||
void digitalIO::writeLocal(const uint8_t ch, const bool value)
|
||||
{
|
||||
uint8_t retries(0);
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
if (m_localOuts.setOut(ch, value))
|
||||
{
|
||||
@@ -166,7 +166,7 @@ void digitalIO::writeLocalPort(const std::vector<bool> &values)
|
||||
if (values[i])
|
||||
decValue |= High << i;
|
||||
}
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
if (m_localOuts.setPort(decValue))
|
||||
{
|
||||
@@ -182,7 +182,7 @@ void digitalIO::writeRemote(const uint8_t ch, const bool value)
|
||||
uint8_t retries(0);
|
||||
const uint8_t selectedRemote(floor(ch / (float)remoteIO::CH_MAX));
|
||||
const uint8_t selectedChannel(ch % remoteIO::CH_MAX);
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
if (m_remotes[selectedRemote].setOut((remoteIO::channel_t)selectedChannel, value))
|
||||
{
|
||||
@@ -196,7 +196,7 @@ void digitalIO::writeRemote(const uint8_t ch, const bool value)
|
||||
void digitalIO::writeRemotePort(const std::vector<bool> &values)
|
||||
{
|
||||
uint8_t retries(0);
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
bool ok(true);
|
||||
for (uint8_t i(0); i < values.size(); i += remoteIO::CH_MAX)
|
||||
@@ -228,7 +228,7 @@ const bool digitalIO::readLocalOut(const uint8_t ch)
|
||||
{
|
||||
bool value(false);
|
||||
uint8_t retries(0);
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
if (m_localOuts.readOut(ch, value))
|
||||
{
|
||||
@@ -256,7 +256,7 @@ const std::vector<bool> digitalIO::readLocalOutPort()
|
||||
uint8_t retries(0);
|
||||
uint8_t state(0);
|
||||
std::vector<bool> values(getLocalOutNum());
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
if (m_localOuts.readPort(state))
|
||||
{
|
||||
@@ -279,7 +279,7 @@ const bool digitalIO::readRemoteIn(const uint8_t ch)
|
||||
const uint8_t selectedRemote(floor(ch / 8.0f));
|
||||
const uint8_t selectedChannel(ch % remoteIO::CH_MAX);
|
||||
bool value;
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
if (m_remotes[selectedRemote].getIn((remoteIO::channel_t)selectedChannel, value))
|
||||
{
|
||||
@@ -297,7 +297,7 @@ const bool digitalIO::readRemoteOut(const uint8_t ch)
|
||||
const uint8_t selectedRemote(floor(ch / (float)remoteIO::CH_MAX));
|
||||
const uint8_t selectedChannel(ch % remoteIO::CH_MAX);
|
||||
bool value;
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
if (m_remotes[selectedRemote].getOut((remoteIO::channel_t)selectedChannel, value))
|
||||
{
|
||||
@@ -314,7 +314,7 @@ const std::vector<bool> digitalIO::readRemoteInPort()
|
||||
uint8_t retries(0);
|
||||
std::vector<bool> values;
|
||||
values.reserve(getRemoteInNum());
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
bool ok(true);
|
||||
for (uint8_t i(0); i < getRemoteInNum(); i += remoteIO::CH_MAX)
|
||||
@@ -343,7 +343,7 @@ const std::vector<bool> digitalIO::readRemoteOutPort()
|
||||
uint8_t retries(0);
|
||||
std::vector<bool> values;
|
||||
values.reserve(getRemoteOutNum());
|
||||
while (retries++ < maxRetries)
|
||||
while (retries++ < c_maxRetries)
|
||||
{
|
||||
bool ok(true);
|
||||
for (uint8_t i(0); i < getRemoteOutNum(); i += remoteIO::CH_MAX)
|
||||
|
||||
@@ -28,7 +28,7 @@ private:
|
||||
DI_MAX
|
||||
};
|
||||
|
||||
const uint8_t maxRetries = 5;
|
||||
const uint8_t c_maxRetries = 5;
|
||||
|
||||
public:
|
||||
digitalIO(drivers::I2C &i2c, drivers::MODBUS &bus, std::vector<uint8_t> remotes);
|
||||
|
||||
27
src/main.cpp
27
src/main.cpp
@@ -140,17 +140,8 @@ void loop()
|
||||
{
|
||||
LOG_INFO("Temperature channel", i++, "->", v);
|
||||
}
|
||||
i = 0;
|
||||
delay(10);
|
||||
for (auto v : tmp.getCorrection())
|
||||
{
|
||||
LOG_INFO("Temperature correction channel", i++, "tc", v);
|
||||
}
|
||||
|
||||
delay(100);
|
||||
drivers::S50140::powerinfo_t pinfo = seneca.getAll();
|
||||
LOG_INFO("Power Info ==> V:", pinfo.v, "- A:", pinfo.a, "- W:", pinfo.pAct, "- F:", pinfo.f, "- Wh_t:", pinfo.whTot, "- Wh_p:", pinfo.whPar);
|
||||
|
||||
|
||||
LOG_INFO("Read Red");
|
||||
if (io.digitalInRead(0)) // rosso
|
||||
{
|
||||
std::vector<bool> v1 = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
|
||||
@@ -159,13 +150,11 @@ void loop()
|
||||
|
||||
LOG_INFO("SET Digital Outputs V1: ", printBoolVec(v1).c_str());
|
||||
io.digitalOutWritePort(v1);
|
||||
delay(100);
|
||||
LOG_INFO("GET Digital Outputs V1: ", printBoolVec(io.digitalOutReadPort()).c_str());
|
||||
delay(2000);
|
||||
|
||||
LOG_INFO("SET Digital Outputs V2: ", printBoolVec(v2).c_str());
|
||||
io.digitalOutWritePort(v2);
|
||||
delay(100);
|
||||
LOG_INFO("GET Digital Outputs V2: ", printBoolVec(io.digitalOutReadPort()).c_str());
|
||||
delay(2000);
|
||||
|
||||
@@ -173,9 +162,10 @@ void loop()
|
||||
delay(2000);
|
||||
|
||||
io.digitalOutWritePort(v0);
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
delay(100);
|
||||
LOG_INFO("Read Blue");
|
||||
if (io.digitalInRead(8)) // blu
|
||||
{
|
||||
if (!buzzing)
|
||||
@@ -192,17 +182,22 @@ void loop()
|
||||
}
|
||||
LOG_INFO("Buzzing -> ", buzzing ? "True" : "False");
|
||||
}
|
||||
|
||||
|
||||
LOG_INFO("Read Green");
|
||||
if (io.digitalInRead(9))
|
||||
{ // verde
|
||||
conf.resetConfig();
|
||||
}
|
||||
|
||||
|
||||
LOG_INFO("Read Yellow");
|
||||
if (io.digitalInRead(10))
|
||||
{ // giallo
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
drivers::S50140::powerinfo_t pinfo = seneca.getAll();
|
||||
LOG_INFO("Power Info ==> V:", pinfo.v, "- A:", pinfo.a, "- W:", pinfo.pAct, "- F:", pinfo.f, "- Wh_t:", pinfo.whTot, "- Wh_p:", pinfo.whPar);
|
||||
|
||||
delay(conf.m_globalLoopDelay);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,14 @@ remoteIO::remoteIO(const uint8_t address, drivers::MODBUS &bus) : m_address(addr
|
||||
{
|
||||
LOG_INFO("Initializing relay module");
|
||||
std::vector<uint16_t> response;
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
if (!m_bus.readHoldingRegisters(m_address, REG_VERSION, 1, response))
|
||||
{
|
||||
LOG_ERROR("Unable to inizialize relay module");
|
||||
};
|
||||
LOG_INFO("Software version", std::to_string(response.at(0) / 100.0f).c_str());
|
||||
m_initialized = true;
|
||||
m_lastRequest = millis();
|
||||
resetAll(false);
|
||||
}
|
||||
|
||||
@@ -20,11 +21,23 @@ remoteIO::~remoteIO()
|
||||
resetAll(false);
|
||||
}
|
||||
|
||||
void remoteIO::delayRequest()
|
||||
{
|
||||
auto now = millis();
|
||||
if ((now - m_lastRequest) < c_minDelay)
|
||||
{ // minimum m_lastRequest between requests
|
||||
LOG_DEBUG("remoteIO delay request", (now - m_lastRequest));
|
||||
delay(now - m_lastRequest);
|
||||
}
|
||||
m_lastRequest = millis();
|
||||
}
|
||||
|
||||
const bool remoteIO::setOut(const channel_t ch, const bool value)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
delayRequest();
|
||||
LOG_DEBUG("Write Channel", ch, "->", value ? "True" : "False");
|
||||
return m_bus.writeCoil(m_address, REG_COILS + ch, value);
|
||||
}
|
||||
@@ -33,7 +46,8 @@ const bool remoteIO::toggleOut(const channel_t ch)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
delayRequest();
|
||||
std::vector<bool> value;
|
||||
if (!m_bus.readCoils(m_address, REG_COILS + ch, 1, value))
|
||||
return false;
|
||||
@@ -45,7 +59,8 @@ const bool remoteIO::setOutPort(const std::vector<bool> values)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
delayRequest();
|
||||
LOG_DEBUG("Write Port", CH_MAX);
|
||||
return m_bus.writeCoils(m_address, REG_COILS, values);
|
||||
}
|
||||
@@ -54,7 +69,8 @@ const bool remoteIO::getOut(const channel_t ch, bool &value)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
delayRequest();
|
||||
std::vector<bool> values;
|
||||
if (!m_bus.readCoils(m_address, REG_COILS + ch, 1, values))
|
||||
return false;
|
||||
@@ -67,7 +83,8 @@ const bool remoteIO::getOutPort(std::vector<bool> &values)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
delayRequest();
|
||||
LOG_DEBUG("Read Port", CH_MAX);
|
||||
return m_bus.readCoils(m_address, REG_COILS, CH_MAX, values);
|
||||
}
|
||||
@@ -76,7 +93,8 @@ const bool remoteIO::getIn(const channel_t input, bool &value)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
delayRequest();
|
||||
std::vector<bool> values;
|
||||
if (!m_bus.readInputs(m_address, REG_INPUT + input, 1, values))
|
||||
return false;
|
||||
@@ -89,7 +107,8 @@ const bool remoteIO::getInPort(std::vector<bool> &values)
|
||||
{
|
||||
if (!m_initialized)
|
||||
return false;
|
||||
auto lock = m_bus.getLock();
|
||||
std::lock_guard<std::mutex> lock(m_bus.getMutex());
|
||||
delayRequest();
|
||||
LOG_DEBUG("Read Inputs", CH_MAX);
|
||||
return m_bus.readInputs(m_address, REG_INPUT, CH_MAX, values);
|
||||
}
|
||||
|
||||
@@ -8,9 +8,21 @@
|
||||
class remoteIO
|
||||
{
|
||||
public:
|
||||
typedef enum {CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH8, CH_MAX} channel_t;
|
||||
typedef enum
|
||||
{
|
||||
CH1,
|
||||
CH2,
|
||||
CH3,
|
||||
CH4,
|
||||
CH5,
|
||||
CH6,
|
||||
CH7,
|
||||
CH8,
|
||||
CH_MAX
|
||||
} channel_t;
|
||||
|
||||
private:
|
||||
const uint32_t c_minDelay = 100;
|
||||
const uint16_t REG_VERSION = 0x8000;
|
||||
const uint16_t REG_COILS = 0x0000;
|
||||
const uint16_t REG_INPUT = 0x0000;
|
||||
@@ -32,8 +44,12 @@ public:
|
||||
|
||||
void resetAll(const bool value);
|
||||
|
||||
private:
|
||||
void delayRequest();
|
||||
|
||||
private:
|
||||
bool m_initialized;
|
||||
drivers::MODBUS &m_bus;
|
||||
const uint8_t m_address;
|
||||
uint32_t m_lastRequest;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user