77 lines
4.5 KiB
C++
77 lines
4.5 KiB
C++
#ifndef LatchingRelay_h
|
|
#define LatchingRelay_h
|
|
|
|
#ifndef Arduino_H
|
|
#include <Arduino.h>
|
|
#endif
|
|
|
|
class LatchingRelay {
|
|
private:
|
|
String name = "Latching Relay";
|
|
|
|
unsigned int setpin;
|
|
unsigned int setpin_readback;
|
|
bool setpin_desired_state = false; // What we are telling the pin to be (HIGH vs LOW)
|
|
bool setpin_actual_state; // The current state of the setpin as per the readback
|
|
bool setpin_working;
|
|
|
|
unsigned int resetpin;
|
|
unsigned int resetpin_readback;
|
|
bool resetpin_desired_state = false; // What we are telling the pin to be (HIGH vs LOW)
|
|
bool resetpin_actual_state; // The current state of the resetpin
|
|
bool resetpin_working;
|
|
|
|
unsigned int voltage_read_pin;
|
|
|
|
const unsigned long extra_pin_duration = 100; // The time in ms that the coil should stay on AFTER seeing a voltage change
|
|
unsigned long setpin_turned_on_time; // The millis() that the setpin was turned on
|
|
unsigned long resetpin_turned_on_time; // The millis() that the resetpin was turned on
|
|
|
|
bool desired_state = false; // The desired state of the relay, set by the control program
|
|
bool expected_state; // The expected state of the relay based off of digital setpin and resetpin readbacks
|
|
bool actual_state; // Whether or not there actually is a voltage across the relay or not
|
|
bool relay_working;
|
|
bool startup = true; // This is used so that the object knows whether or not to use the normal or startup function to control
|
|
|
|
const unsigned long relay_delay = 500; // The max time in ms that we would expect the Arduino to see a voltage change across the relay after turning on the pin
|
|
unsigned long relay_turned_on_time; // The millis() that the relay was turned on. Useful for knowing if it is operating correctly
|
|
unsigned long relay_turned_off_time ; // The millis() that the relay was turned off. Useful for knowing if it is operating correctly
|
|
unsigned long relay_first_voltage_time; // The millis() that we first saw a voltage across the relay after trying to turn it on
|
|
unsigned long relay_first_no_voltage_time; // The millis() that we first saw no voltage across the relay after trying to turn it off
|
|
|
|
const unsigned int num_voltage_reads = 3; // How many times we should read the relay voltage and average
|
|
|
|
float read_relay_voltage(); // Get a single voltag reading across the relay
|
|
void update_actual_state(); // Average a few voltage readings and determine if the relay has ~12V across it
|
|
|
|
void update_if_pins_working(); // Perform a check to see if the pins are working based on readbacks
|
|
void update_if_relay_working(); // Check if the relay is working based on expected vs actual state
|
|
|
|
void turn_off_pins_after_duration(); // Turn the pins off after the appropriate time
|
|
|
|
void turn_on_relay();
|
|
void turn_off_relay();
|
|
|
|
void turn_on_relay_startup(); // Turns on relay as well as setting values that may have been uninitialized
|
|
void turn_off_relay_startup(); // Turns off relay as well as setting values that may have been uninitialized
|
|
|
|
void turn_on_relay_from_fail(); // Only called when the expected state != actual state due to some unkown failure (vibration, transistor for some reason turning on, etc)
|
|
void turn_off_relay_from_fail(); // Only called when the expected state != actual state due to some unkown failure (vibration, transistor for some reason turning on, etc)
|
|
|
|
public:
|
|
LatchingRelay(String name, unsigned int setpin, unsigned int setpin_readback, unsigned int resetpin, unsigned int resetpin_readback, unsigned int voltage_read_pin);
|
|
LatchingRelay(unsigned int setpin, unsigned int setpin_readback, unsigned int resetpin, unsigned int resetpin_readback, unsigned int voltage_read_pin);
|
|
|
|
bool get_setpin_working() const {return setpin_working;}
|
|
bool get_resetpin_working() const {return resetpin_working;}
|
|
|
|
bool get_desired_state() const {return desired_state;}
|
|
bool get_expected_state() const {return expected_state;}
|
|
bool get_actual_state() const {return actual_state;}
|
|
bool get_relay_working() const {return relay_working;}
|
|
|
|
void set_desired_state(unsigned char new_desired_state); // Set the desired state of the relay
|
|
|
|
void control_relay(); // Controls the relay including pins to get relay to desired state, checking if pins need to be turned off, and error detection
|
|
};
|
|
#endif |