Break Module

The header-only file for the BreakModule class. This class allows interfacing with a break to dynamically control the motion of break-coupled objects.

Dependencies:

  • Arduino.h for Arduino platform functions and macros and cross-compatibility with Arduino IDE (to an extent).

  • digitalWriteFast.h for fast digital pin manipulation methods.

  • module.h for the shared Module class API access (integrates the custom module into runtime flow).

  • shared_assets.h for globally shared static message byte-codes and parameter structures.

template<const uint8_t kPin, const bool kNormallyEngaged, const bool kStartEngaged = true>
class BreakModule : public Module
#include <break_module.h>

Sends Pulse-Width-Modulated (PWM) signals to variably engage the managed break.

This module is specifically designed to send PWM signals that trigger Field-Effect-Transistor (FET) gated relay hardware to deliver voltage that variably engages the break. Depending on configuration, this module is designed to work with both Normally Engaged (NE) and Normally Disengaged (ND) breaks.

Template Parameters:
  • kPin – the analog pin connected to the break FET-gated relay. Depending on the active command, the pin will be used to output either digital or analog PWM signal to engage the break.

  • kNormallyEngaged – determines whether the managed break is engaged (active) or disengaged (inactive) when unpowered. This is used to adjust the class behavior so that toggle OFF always means the break is disabled and toggle ON means the break is engaged at maximum strength.

  • kStartEngaged – determines the initial state of the break during class initialization. This works together with kNormallyEngaged parameter to deliver the desired initial voltage level for the break to either be maximally engaged or completely disengaged after hardware configuration.

Public Types

enum class kCustomStatusCodes : uint8_t

Assigns meaningful names to byte status-codes used to communicate module events to the PC. Note, this enumeration has to use codes 51 through 255 to avoid interfering with shared kCoreStatusCodes enumeration inherited from base Module class.

Values:

enumerator kOutputLocked

The break pin is in a global locked state and cannot be used to output signals.

enumerator kEngaged

The break is maximally engaged.

enumerator kDisengaged

The break is completely disengaged.

enumerator kVariable

The break is engaged with variable strength.

enum class kModuleCommands : uint8_t

Assigns meaningful names to module command byte-codes.

Values:

enumerator kToggleOn

Sets the break to permanently engage at maximum strength.

enumerator kToggleOff

Sets the break to permanently disengage.

enumerator kSetBreakingPower

Sets the break to engage with the strength determined by breaking_strength parameter.

Public Functions

inline BreakModule(const uint8_t module_type, const uint8_t module_id, Communication &communication, const axmc_shared_assets::DynamicRuntimeParameters &dynamic_parameters)

Initializes the class by subclassing the base Module class.

inline bool SetCustomParameters() override

Overwrites the custom_parameters structure memory with the data extracted from the Communication reception buffer.

inline bool RunActiveCommand() override

Executes the currently active command.

inline bool SetupModule() override

Sets up module hardware parameters.

~BreakModule() override = default

Private Functions

inline void EnableBreak()

Sets the Break to be continuously engaged (enabled) by outputting the appropriate digital signal.

inline void DisableBreak()

Sets the Break to be continuously disengaged (disabled) by outputting the appropriate digital signal.

inline void SetBreakingPower()

Flexibly sets the breaking power by sending a square wave pulse with a certain PWM duty-cycle.

Private Members

struct BreakModule::CustomRuntimeParameters _custom_parameters

Private Static Attributes

static constexpr bool kEngage = kNormallyEngaged ? LOW : HIGH

Depending on the break configuration, stores the digital signal that needs to be sent to the output pin to engage the break at maximum strength.

static constexpr bool kDisengage = kNormallyEngaged ? HIGH : LOW

Depending on the break configuration, stores the digital signal that needs to be sent to the output pin to disengage the break.

struct CustomRuntimeParameters

Stores custom addressable runtime parameters of the module.

Public Members

uint8_t breaking_strength = 128

Determines the strength of the break when it uses the PWM mode.

Encoder Module

The header-only file for the EncoderModule class. This class allows interfacing with a quadrature rotary encoder to monitor the direction and magnitude of connected object’s rotation.

Attention

This file is written in a way that is NOT compatible with any other library or class that uses AttachInterrupt(). Disable the ‘ENCODER_USE_INTERRUPTS’ macro defined at the top of the file to make this file compatible with other interrupt libraries.

Dependencies:

  • Arduino.h for Arduino platform functions and macros and cross-compatibility with Arduino IDE (to an extent).

  • Encoder.h for the low-level API that detects and tracks quadrature encoder pulses using interrupt pins.

  • module.h for the shared Module class API access (integrates the custom module into runtime flow).

  • shared_assets.h for globally shared static message byte-codes and parameter structures.

Defines

ENCODER_USE_INTERRUPTS
template<const uint8_t kPinA, const uint8_t kPinB, const uint8_t kPinX, const bool kInvertDirection = false>
class EncoderModule : public Module
#include <encoder_module.h>

Wraps an Encoder class instance and provides access to its pulse counter to monitor the direction and magnitude of connected object’s rotation.

This module is specifically designed to interface with quadrature encoders and track the direction and number of encoder pulses between class method calls. The class only works with encoder pulses and expects the ModuleInterface class that receives the data on the PC to perform the necessary conversions to translate pulses into distance.

Note

Largely, this class is an API wrapper around the Paul Stoffregen’s Encoder library, and it relies on efficient interrupt logic to increase encoder tracking precision. To achieve the best performance, make sure both Pin A and Pin B are hardware interrupt pins.

Template Parameters:
  • kPinA – the digital interrupt pin connected to the ‘A’ channel of the quadrature encoder.

  • kPinB – the digital interrupt pin connected to the ‘B’ channel of the quadrature encoder.

  • kPinX – the digital pin connected to the index (‘X’) channel of the quadrature encoder.

  • kInvertDirection – if true, inverts the sign of the value returned by the encoder. By default, when Pin B is triggered before Pin A, the pulse counter decreases, which corresponds to CW movement. When pin A is triggered before pin B, the counter increases, which corresponds to the CCW movement. This flag allows reversing this relationship, which may be helpful, depending on how the encoder is mounted and wired.

Public Types

enum class kCustomStatusCodes : uint8_t

Assigns meaningful names to byte status-codes used to communicate module events to the PC. Note, this enumeration has to use codes 51 through 255 to avoid interfering with shared kCoreStatusCodes enumeration inherited from base Module class.

Values:

enumerator kRotatedCCW

The encoder was rotated in the CCW direction.

enumerator kRotatedCW

The encoder was rotated in the CW.

enumerator kPPR

The estimated Pulse-Per-Revolution (PPR) value of the encoder.

enum class kModuleCommands : uint8_t

Assigns meaningful names to module command byte-codes.

Values:

enumerator kCheckState

Gets the change in pulse counts and sign relative to last check.

enumerator kReset

Resets the encoder’s pulse tracker to 0.

enumerator kGetPPR

Estimates the Pulse-Per-Revolution (PPR) of the encoder.

Public Functions

inline EncoderModule(const uint8_t module_type, const uint8_t module_id, Communication &communication, const axmc_shared_assets::DynamicRuntimeParameters &dynamic_parameters)

Initializes the class by subclassing the base Module class and instantiating the internal Encoder class.

inline bool SetCustomParameters() override

Overwrites the custom_parameters structure memory with the data extracted from the Communication reception buffer.

inline bool RunActiveCommand() override

Executes the currently active command.

inline bool SetupModule() override

Sets up module hardware parameters.

~EncoderModule() override = default

Private Functions

inline void ReadEncoder()

Reads and resets the current encoder pulse counter. Sends the result to the PC if it is significantly different from previous readout. Note, the result will be transformed into an absolute value and its direction will be codified as the event code of the message transmitted to PC.

inline void ResetEncoder()

Resets the encoder pulse counter back to 0. This can be used to reset the encoder without reading its data.

inline void GetPPR()

Estimates the Pulse-Per-Revolution (PPR) of the encoder by using the index pin to precisely measure the number of pulses per encoder rotation. Measures up to 11 full rotations and averages the pulse counts per each rotation to improve the accuracy of the computed PPR value.

Private Members

struct EncoderModule::CustomRuntimeParameters _custom_parameters
Encoder _encoder = Encoder(kPinA, kPinB)

The encoder class that abstracts low-level access to the Encoder pins and provides an easy API to retrieve the automatically incremented encoder pulse vector. The vector can be reset by setting it to 0, and the class relies on hardware interrupt functionality to maintain the desired precision.

int32_t _overflow = 0

This variable is used to accumulate insignificant encoder readouts to be reused during further calls. This allows filtering the rotation jitter of the tracked object while still accurately tracking small, incremental movements.

Private Static Attributes

static constexpr int32_t kMultiplier = kInvertDirection ? -1 : 1

The multiplier is used to optionally invert the pulse counter sign to virtually flip the direction of encoder readouts. This is helpful if the encoder is mounted and wired in a way where CW rotation of the tracked object produces CCW readout in the encoder. This is used to virtually align the encoder readout direction with the tracked object rotation direction.

struct CustomRuntimeParameters

Stores custom addressable runtime parameters of the module.

Public Members

bool report_CCW = true

Determines whether to report changes in the CCW direction.

bool report_CW = true

Determines whether to report changes in the CW direction.

uint32_t delta_threshold = 15

The minimum pulse count change (delta) for reporting changes.

Lick Module

The header-only file for the LickModule class. This class allows interfacing with a custom conductive lick sensor used to monitor the licking behavior of animals.

This class was specifically designed to monitor the licking behavior of laboratory mice during experiments. This class will likely work for other purposes that benefit from detecting wet or dry contacts, such as touch or lick sensors.

Dependencies:

  • Arduino.h for Arduino platform functions and macros and cross-compatibility with Arduino IDE (to an extent).

  • digitalWriteFast.h for fast digital pin manipulation methods.

  • module.h for the shared Module class API access (integrates the custom module into runtime flow).

  • shared_assets.h for globally shared static message byte-codes and parameter structures.

template<const uint8_t kPin>
class LickModule : public Module
#include <lick_module.h>

Monitors the state of a custom conductive lick sensor for significant state changes and notifies the PC when such changes occur.

This module is designed to work with a custom lick sensor. The sensor works by directly injecting a small amount of current through one contact surface and monitoring the pin wired to the second contact surface. When the animal, such as a laboratory mouse, licks the sensor while making contact with the current injector surface, the sensor detects a positive change in voltage across the sensor. The detection threshold can be configured to distinguish between dry and wet touch, which is used to separate limb contacts from tongue contacts.

Note

This class was calibrated to work for and tested on C57BL6J Wildtype and transgenic mice.

Template Parameters:

kPin – the analog pin whose state will be monitored to detect licks.

Public Types

enum class kCustomStatusCodes : uint8_t

Assigns meaningful names to byte status-codes used to communicate module events to the PC. Note, this enumeration has to use codes 51 through 255 to avoid interfering with shared kCoreStatusCodes enumeration inherited from base Module class.

Values:

enumerator kChanged
enum class kModuleCommands : uint8_t

Assigns meaningful names to module command byte-codes.

Values:

enumerator kCheckState

Checks the state of the input pin, and if necessary informs the PC of any changes.

Public Functions

inline LickModule(const uint8_t module_type, const uint8_t module_id, Communication &communication, const axmc_shared_assets::DynamicRuntimeParameters &dynamic_parameters)

Initializes the TTLModule class by subclassing the base Module class.

inline bool SetCustomParameters() override

Overwrites the custom_parameters structure memory with the data extracted from the Communication reception buffer.

inline bool RunActiveCommand() override

Executes the currently active command.

inline bool SetupModule() override

Sets up module hardware parameters.

~LickModule() override = default

Private Functions

inline void CheckState()

Checks the signal received by the input pin and, if necessary, reports it to the PC.

Private Members

struct LickModule::CustomRuntimeParameters _custom_parameters
struct CustomRuntimeParameters

Stores custom addressable runtime parameters of the module.

Public Members

uint16_t signal_threshold = 200

The lower boundary for signals to be reported to PC.

uint16_t delta_threshold = 180

The minimum difference between checks to be reported to PC.

uint8_t average_pool_size = 0

The number of readouts to average into pin state value.

Screen Module

The header-only file for the ScreenModule class. This class allows interfacing with the screens used in the Sun lab’s Virtual Reality system. Specifically, it operates a logic gate that turns the screen displays on and off without interfering with the display configuration of the host PC by directly manipulating the HDMI-translator card hardware.

Dependencies:

  • Arduino.h for Arduino platform functions and macros and cross-compatibility with Arduino IDE (to an extent).

  • digitalWriteFast.h for fast digital pin manipulation methods.

  • module.h for the shared Module class API access (integrates the custom module into runtime flow).

  • shared_assets.h for globally shared static message byte-codes and parameter structures.

template<const uint8_t kLeftScreenPin, const uint8_t kCenterScreenPin, const uint8_t kRightScreenPin, const bool kNormallyClosed = true>
class ScreenModule : public Module
#include <screen_module.h>

Pulses the connected FET gate to simulate pressing the power button on the VR screen control panel.

This class is specifically designed to interface with a one-channel FET gate hard-soldered to the circuitry of the VR screen control panel. Upon receiving the Toggle command, this class pulses (opens and closes) the FET gate to simulate the user pressing the power button. In turn, this changes the power state of the VR screen between ON and OFF, without affecting the display configuration of the host PC.

Template Parameters:
  • kLeftScreenPin – the digital pin used to control the power state of the Left VR screen, from the animal’s perspective.

  • kCenterScreenPin – the digital pin used to control the power state of the Center VR screen, from the animal’s perspective.

  • kRightScreenPin – the digital pin used to control the power state of the Right VR screen, from the animal’s perspective.

  • kNormallyClosed – determines the type of the FET gate relay used to control the power state of the VR screens. Specifically, a NormallyClosed relay is closed (Off) when the logic signal to the relay is LOW and opened (On) when the logic signal to the relay is HIGH.

Public Types

enum class kCustomStatusCodes : uint8_t

Assigns meaningful names to byte status-codes used to communicate module events to the PC. Note, this enumeration has to use codes 51 through 255 to avoid interfering with shared kCoreStatusCodes enumeration inherited from base Module class.

Values:

enumerator kOutputLocked

The output ttl pin is in a global locked state and cannot be toggled on or off.

enumerator kOn

The relay is receiving a HIGH signal.

enumerator kOff

The relay is receiving a LOW signal.

enum class kModuleCommands : uint8_t

Assigns meaningful names to module command byte-codes.

Values:

enumerator kToggle

Pulses the connected FET gate to simulate pressing the power button on the VR screen.

Public Functions

inline ScreenModule(const uint8_t module_type, const uint8_t module_id, Communication &communication, const axmc_shared_assets::DynamicRuntimeParameters &dynamic_parameters)

Initializes the class by subclassing the base Module class.

inline bool SetCustomParameters() override

Overwrites the custom_parameters structure memory with the data extracted from the Communication reception buffer.

inline bool RunActiveCommand() override

Executes the currently active command.

inline bool SetupModule() override

Sets up module hardware parameters.

~ScreenModule() override = default

Private Functions

inline void Toggle()

Sends a digital pulse through all output pins, using the preconfigured pulse_duration of microseconds. Supports non-blocking execution and respects the global ttl_lock state.

Private Members

struct ScreenModule::CustomRuntimeParameters _custom_parameters

Private Static Attributes

static constexpr bool kOn = kNormallyClosed ? HIGH : LOW

The value the output pins have to be set for the connected gate to be activated and allow the current to flow through the gate. Computing this value statically ensures that the class behaves the same way regardless of whether it is normally opened (NO) or closed (NC)

static constexpr bool kOff = kNormallyClosed ? LOW : HIGH

The value the output pins have to be set for the connected gate to be deactivated and stop allowing the current to flow through the gate.

struct CustomRuntimeParameters

Stores custom addressable runtime parameters of the module.

Public Members

uint32_t pulse_duration = 1000000

The time, in microseconds, the pins output HIGH during pulses.

Torque Module

The header-only file for the TorqueModule class. This class allows interfacing with a torque sensor to monitor the torque applied to the connected object.

Attention

Since most torque sensors output a differential signal to code for the direction of the torque, this class is designed to work with an AD620 microvolt amplifier to straighten and amplify the output of the torque sensor.

Dependencies:

  • Arduino.h for Arduino platform functions and macros and cross-compatibility with Arduino IDE (to an extent).

  • digitalWriteFast.h for fast digital pin manipulation methods.

  • module.h for the shared Module class API access (integrates the custom module into runtime flow).

  • shared_assets.h for globally shared static message byte-codes and parameter structures.

template<const uint8_t kPin, const uint16_t kBaseline, const bool kInvertDirection = false>
class TorqueModule : public Module
#include <torque_module.h>

Monitors the signal sent by the torque sensor through an AD620 microvolt amplifier.

This module is specifically designed to work with fine-resolution torque sensors that output differential signals in the millivolt or microvolt range that is picked up and amplified by the AD620 amplifier. This module works by monitoring an analog input pin for the signal from the AD620 amplifier expected to be zeroed ~ 1.6 V. This way, torque in the CCW direction is detected as a positive deflection from baseline, and torque in the CW direction is detected as a negative deflection from baseline. The class can be flexibly configured to report torque in either direction and to only report significant torque changes.

Template Parameters:
  • kPin – the analog pin whose state will be monitored to detect torque’s direction and magnitude.

  • kBaseline – the value, in ADC units, for the analog signal sent by the sensor when it detects no torque. This value depends on the zero-point calibration of the AD620 amplifier, and it is used to shift differential bipolar signal of the torque sensor to use a positive integer scale. Signals that are originally above the baseline are interpreted as CounterClockWise (CCW) torque, and signals below the baseline are interpreted as ClockWise (CW) torque. When the class reports torque to the PC, it further adjusts the torque value to always be between 0 and Baseline, reporting the direction via the event-code of the message.

  • kInvertDirection – if true, inverts the direction the torque returned by the sensor. By default, the sensor interprets torque in the CCW direction as positive and torque in the CW direction as negative. This flag allows reversing this relationship, which may be helpful, depending on how the sensor is mounted and wired.

Public Types

enum class kCustomStatusCodes : uint8_t

Assigns meaningful names to byte status-codes used to track module states. Note, this enumeration has to use codes 51 through 255 to avoid interfering with shared kCoreStatusCodes enumeration inherited from base Module class.

Values:

enumerator kCCWTorque

The sensor detects torque in the CCW direction.

enumerator kCWTorque

The sensor detects torque in the CW direction.

enum class kModuleCommands : uint8_t

Assigns meaningful names to module command byte-codes.

Values:

enumerator kCheckState

Checks the state of the input pin, and if necessary informs the PC of any changes.

Public Functions

inline TorqueModule(const uint8_t module_type, const uint8_t module_id, Communication &communication, const axmc_shared_assets::DynamicRuntimeParameters &dynamic_parameters)

Initializes the TTLModule class by subclassing the base Module class.

inline bool SetCustomParameters() override

Overwrites the custom_parameters structure memory with the data extracted from the Communication reception buffer.

inline bool RunActiveCommand() override

Executes the currently active command.

inline bool SetupModule() override

Sets up module hardware parameters.

~TorqueModule() override = default

Private Functions

inline void CheckState()

Checks the signal received by the input pin and, if necessary, reports it to the PC.

Private Members

struct TorqueModule::CustomRuntimeParameters _custom_parameters
struct CustomRuntimeParameters

Stores custom addressable runtime parameters of the module.

Public Members

bool report_CCW = true

Determines whether to report changes in the CCW direction.

bool report_CW = true

Determines whether to report changes in the CCW direction.

uint16_t signal_threshold = 100

The minimum deviation from the baseline to be reported to PC.

uint16_t delta_threshold = 70

The minimum signal difference between checks to be reported to PC.

uint8_t average_pool_size = 5

The number of readouts to average into pin state value.

TTL Module

The header-only file for the TTLModule class. This class allows establishing bidirectional TTL communication with other hardware systems, such as microcontrollers, cameras and data recording devices.

Dependencies:

  • Arduino.h for Arduino platform functions and macros and cross-compatibility with Arduino IDE (to an extent).

  • digitalWriteFast.h for fast digital pin manipulation methods.

  • module.h for the shared Module class API access (integrates the custom module into runtime flow).

  • shared_assets.h for globally shared static message byte-codes and parameter structures.

template<const uint8_t kPin, const bool kOutput = true, const bool kStartOn = false>
class TTLModule : public Module
#include <ttl_module.h>

Sends or receives Transistor-to-Transistor Logic (TTL) signals using the specified digital pin.

The class is statically configured to either receive or output TTL signals, it cannot do both at the same time!

Template Parameters:
  • kPin – the digital pin that will be used to output or receive ttl signals. The mode of the pin (input or output) depends on kOutput flag value and is currently not changeable during runtime.

  • kOutput – determines whether the pin will be used to output TTL signals (if set to true) or receive TTL signals from other systems (if set to false).

  • kStartOn – determines the initial state of the pin when the class is configured to output TTL signals. If set to true, the TTL pin will be set to High during hardware initialization. Otherwise (by default) it will be set to Low. This parameter is ignored if the class is configured to receive TTL signals.

Public Types

enum class kCustomStatusCodes : uint8_t

Assigns meaningful names to byte status-codes used to communicate module events to the PC. Note, this enumeration has to use codes 51 through 255 to avoid interfering with shared kCoreStatusCodes enumeration inherited from base Module class.

Values:

enumerator kOutputLocked

The output ttl pin is in a global locked state and cannot be toggled on or off.

enumerator kInputOn

The input ttl pin is receiving a HIGH signal.

enumerator kInputOff

The input ttl pin is receiving a LOW signal.

enumerator kInvalidPinMode

The pin mode (input or output) is not valid for the requested command.

enumerator kOutputOn

The output ttl pin is set to HIGH.

enumerator kOutputOff

The output ttl pin is set to LOW.

enum class kModuleCommands : uint8_t

Assigns meaningful names to module command byte-codes.

Values:

enumerator kSendPulse

Sends a ttl pulse through the output pin.

enumerator kToggleOn

Sets the output pin to HIGH.

enumerator kToggleOff

Sets the output pin to LOW.

enumerator kCheckState

Checks the state of the input pin.

Public Functions

inline TTLModule(const uint8_t module_type, const uint8_t module_id, Communication &communication, const axmc_shared_assets::DynamicRuntimeParameters &dynamic_parameters)

Initializes the class by subclassing the base Module class.

inline bool SetCustomParameters() override

Overwrites the custom_parameters structure memory with the data extracted from the Communication reception buffer.

inline bool RunActiveCommand() override

Executes the currently active command.

inline bool SetupModule() override

Sets up module hardware parameters.

~TTLModule() override = default

Private Functions

inline void SendPulse()

Sends a digital pulse through the output pin, using the preconfigured pulse_duration of microseconds. Supports non-blocking execution and respects the global ttl_lock state.

inline void ToggleOn()

Sets the output pin to continuously send HIGH signal. Respects the global ttl_lock state.

inline void ToggleOff()

Sets the output pin to continuously send LOW signal. Respects the global ttl_lock state.

inline void CheckState()

Checks the state of the input pin and, if the state does not match the previous state, sends the new state to the PC.

Private Members

struct TTLModule::CustomRuntimeParameters _custom_parameters
struct CustomRuntimeParameters

Stores custom addressable runtime parameters of the module.

Public Members

uint32_t pulse_duration = 10000

The time, in microseconds, the pin outputs HIGH during pulses.

uint8_t average_pool_size = 0

The number of digital readouts to average when checking pin state.

Valve Module

The header-only file for the ValveModule class. This class allows interfacing with a solenoid valve to controllably dispense precise amounts of fluid.

Dependencies:

  • Arduino.h for Arduino platform functions and macros and cross-compatibility with Arduino IDE (to an extent).

  • digitalWriteFast.h for fast digital pin manipulation methods.

  • module.h for the shared Module class API access (integrates the custom module into runtime flow).

  • shared_assets.h for globally shared static message byte-codes and parameter structures.

template<const uint8_t kPin, const bool kNormallyClosed, const bool kStartClosed = true, const uint8_t kTonePin = 255>
class ValveModule : public Module
#include <valve_module.h>

Sends digital signals to dispense precise amounts of fluid via the managed solenoid valve.

This module is specifically designed to send digital signals that trigger Field-Effect-Transistor (FET) gated relay hardware to deliver voltage that opens or closes the controlled valve. Depending on configuration, this module is designed to work with both Normally Closed (NC) and Normally Open (NO) valves.

The class can also optionally operate a FET-gated relay connected to a piezoelectric tone buzzer. This couples valve open states with the delivery of an audible tone, which is desired for some applications. Currently, the tone is only used during valve pulse commands.

Note

This class was calibrated to work with fluid valves that deliver micro-liter-precise amounts of fluid under gravitational driving force. The current class implementation may not work as intended for other use cases. Additionally, the class is designed for dispensing predetermined amounts of fluid and not for continuous flow rate control, which would require a PWM-based approach similar to the one used by the BreakModule class.

Template Parameters:
  • kPin – the digital pin connected to the valve’s FET-gated relay.

  • kNormallyClosed – determines whether the managed valve is opened or closed when unpowered. This is used to adjust the class behavior so that toggle OFF always means the valve is closed and toggle ON means the valve is open.

  • kStartClosed – determines the initial state of the valve during class initialization. This works together with kNormallyClosed parameter to deliver the desired initial voltage level for the valve to either be opened or closed after hardware initialization.

  • kTonePin – the digital pin connected to the tone buzzer’s FET-gated relay.

Public Types

enum class kCustomStatusCodes : uint8_t

Assigns meaningful names to byte status-codes used to communicate module events to the PC. Note, this enumeration has to use codes 51 through 255 to avoid interfering with shared kCoreStatusCodes enumeration inherited from base Module class.

Values:

enumerator kOutputLocked

The output pin is in a global locked state and cannot be used to output data.

enumerator kOpen

The valve is currently open.

enumerator kClosed

The valve is currently closed.

enumerator kCalibrated

The valve calibration cycle has been completed.

enumerator kToneOn

The tone is currently audible.

enumerator kToneOff

The tone is currently silenced.

enum class kModuleCommands : uint8_t

Assigns meaningful names to module command byte-codes.

Values:

enumerator kSendPulse

Deliver a precise amount of fluid by cycling valve open and close states.

enumerator kToggleOn

Sets the valve to be permanently open.

enumerator kToggleOff

Sets the valve to be permanently closed.

enumerator kCalibrate

Repeatedly pulses the valve to map different pulse_durations to dispensed fluid volumes.

Public Functions

inline ValveModule(const uint8_t module_type, const uint8_t module_id, Communication &communication, const axmc_shared_assets::DynamicRuntimeParameters &dynamic_parameters)

Initializes the class by subclassing the base Module class.

inline bool SetCustomParameters() override

Overwrites the custom_parameters structure memory with the data extracted from the Communication reception buffer.

inline bool RunActiveCommand() override

Executes the currently active command.

inline bool SetupModule() override

Sets up module hardware parameters.

~ValveModule() override = default

Private Functions

inline void Pulse()

Cycles opening and closing the valve to deliver the precise amount of fluid.

inline void Open()

Permanently opens the valve.

inline void Close()

Permanently closes the valve.

inline void Calibrate()

Pulses the valve calibration_count times without blocking or (majorly) delaying. This is used to establish the relationship between the pulse_duration and the amount of fluid delivered during the pulse. This calibration is necessary to precisely control the amount of fluid delivered by the valve by using specific pulse durations.

Private Members

struct ValveModule::CustomRuntimeParameters _custom_parameters

Private Static Attributes

static constexpr bool kOpen = kNormallyClosed ? HIGH : LOW

Depending on the valve configuration, stores the digital signal that needs to be sent to the output pin to open the valve.

static constexpr bool kClose = kNormallyClosed ? LOW : HIGH

Depending on the valve configuration, stores the digital signal that needs to be sent to the output pin to close the valve.

struct CustomRuntimeParameters

Stores custom addressable runtime parameters of the module.

Public Members

uint32_t pulse_duration = 35590

The time, in microseconds, the valve is open during pulses.

uint32_t calibration_delay = 200000

The time, in microseconds, to wait between calibration pulses.

uint16_t calibration_count = 500

How many times to pulse the valve during calibration.

uint32_t tone_duration = 300000

The time, in microseconds, to sound the tone during valve pulses.