Skip to content
Snippets Groups Projects
Commit 70bb59fb authored by Pascal Engeler's avatar Pascal Engeler
Browse files

Added supervision checking

parent f0172b24
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <pid_controller.hpp> #include <pid_controller.hpp>
#include <cstdlib> //std::strtod #include <cstdlib> //std::strtod
#include <algorithm> //This is necessary to fix a bug that cost me 4h. #include <algorithm> //This is necessary to fix a bug that cost me 4h.
#include <Arduino.h>
/* /*
As I tracked down, a (standard) string library calls std::min() in some functions (like compare). As I tracked down, a (standard) string library calls std::min() in some functions (like compare).
...@@ -14,16 +15,28 @@ ...@@ -14,16 +15,28 @@
Holy fucking shit. Holy fucking shit.
*/ */
unsigned long Communicator::timeout_us_ = 20000; //timeout 20ms unsigned long Communicator::timeout_us_ = 20000u; //timeout 20ms
char Communicator::outbuf_[256]; //out buffer char Communicator::outbuf_[256]; //out buffer
char Communicator::cmd_ = 'X'; //command received char Communicator::cmd_ = 'X'; //command received
std::vector<char> Communicator::arg_ = std::vector<char>(256, '\0'); //arguments received std::vector<char> Communicator::arg_ = std::vector<char>(256, '\0'); //arguments received
unsigned long Communicator::last_transaction_ms_ = 0u; //time of last transaction
unsigned long Communicator::interval_assume_eow_ms_ = 1800000u; //no transaction for 30 minutes means we shut down
bool Communicator::communicate(){ bool Communicator::communicate(){
if(!serialCharAvailable_()){ if(!serialCharAvailable_()){ //no communication
if(msElapsedSinceLastTransaction_() > interval_assume_eow_ms_){
//no sign of supervision for 30 minutes, shut down
Pid_controller::turn_off();
}
else{
//have supervision, turn PID Controller on
Pid_controller::turn_on();
}
return false; return false;
} }
else{ else{
startTransactionTimer_();
Pid_controller::turn_on(); //have supervision, make sure PID Controller is enabled
cmd_ = getSerialChar_(); cmd_ = getSerialChar_();
if(isSetCommand_()){ if(isSetCommand_()){
//read argument //read argument
...@@ -223,3 +236,11 @@ void Communicator::handleInvalidCommand_(){ ...@@ -223,3 +236,11 @@ void Communicator::handleInvalidCommand_(){
sendBuf_(); sendBuf_();
return; return;
} }
void Communicator::startTransactionTimer_(){
last_transaction_ms_ = millis();
}
unsigned long Communicator::msElapsedSinceLastTransaction_(){
return static_cast<unsigned long>(millis() - last_transaction_ms_);
}
\ No newline at end of file
...@@ -24,6 +24,9 @@ private: ...@@ -24,6 +24,9 @@ private:
static void handleSetCommand_(); static void handleSetCommand_();
static void handleNopCommand_(); static void handleNopCommand_();
static void handleInvalidCommand_(); static void handleInvalidCommand_();
static void startTransactionTimer_();
static unsigned long msElapsedSinceLastTransaction_();
/*Data Members*/ /*Data Members*/
...@@ -31,6 +34,10 @@ private: ...@@ -31,6 +34,10 @@ private:
static char outbuf_[256]; static char outbuf_[256];
static char cmd_; static char cmd_;
static std::vector<char> arg_; static std::vector<char> arg_;
//Note: The timer only works for timeouts that last shorter than 50 days
static unsigned long last_transaction_ms_;
static unsigned long interval_assume_eow_ms_;
}; };
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment