MIKROE 4-20mA RT Click
R_Click Class Reference

Class to manage a MIKROE 4-20 mA R Click Board (MIKROE-1387). More...

#include <MIKROE_4_20mA_RT_Click.h>

Public Member Functions

 R_Click (uint8_t CS_pin, const RT_Click_Calibration calib)
 Construct a new R Click object. More...
 
 R_Click (uint8_t CS_pin, const RT_Click_Calibration calib, uint32_t EMA_interval, float EMA_LP_freq)
 Construct a new R Click object that uses an exponential moving average (EMA) on the R Click readings. More...
 
void set_SPI_clock (uint32_t clk_freq_Hz)
 Adjust the initially set SPI clock frequency of 1 MHz to another frequency. More...
 
void begin ()
 Start SPI and set up the cable select pin.
 
float bitval2mA (float bitval)
 Transform the bit value into a current [mA] given the calibration parameters. More...
 
uint16_t read_bitval ()
 Read out the R Click once and return the bit value. More...
 
float read_mA ()
 Read out the R Click once and return the current in mA. More...
 
bool poll_EMA ()
 This method is crucial for the exponential moving average (EMA) to work correctly. It should be repeatedly called in the main loop, ideally at a faster pace than the given oversampling interval EMA_interval. More...
 
float get_EMA_bitval ()
 Return the exponential moving average value of the R Click readings in bit value. More...
 
float get_EMA_mA ()
 Return the exponential moving average value of the R Click readings in mA. More...
 
uint32_t get_EMA_obtained_interval ()
 Return the last obtained interval of the oversampled R Click readings of the exponential moving average in microseconds. More...
 

Detailed Description

Class to manage a MIKROE 4-20 mA R Click Board (MIKROE-1387).

Constructor & Destructor Documentation

◆ R_Click() [1/2]

R_Click::R_Click ( uint8_t  CS_pin,
const RT_Click_Calibration  calib 
)

Construct a new R Click object.

Methods read_bitval() and read_mA() can be used to get the instanteneous R Click reading.

Parameters
CS_pinCable select SPI pin
calibStructure holding the [bitval] to [mA] calibration parameters

◆ R_Click() [2/2]

R_Click::R_Click ( uint8_t  CS_pin,
const RT_Click_Calibration  calib,
uint32_t  EMA_interval,
float  EMA_LP_freq 
)

Construct a new R Click object that uses an exponential moving average (EMA) on the R Click readings.

It does this by oversampling the R Click readings at a desired interval given by EMA_interval. Subsequently, it will low-pass filter the readings using a smoothing factor that is calculated from the low-pass filter cut-off frequency given by EMA_LP_freq.

Method poll_EMA() should be repeatedly called in the main loop, ideally at a faster pace than the desired oversampling interval.

Methods get_EMA_bitval() and get_EMA_mA() can be used to get the moving average value.

Parameters
CS_pinCable select SPI pin
calibStructure holding the [bitval] to [mA] calibration parameters
EMA_intervalDesired oversampling interval [µs]
EMA_LP_freqLow-pass filter cut-off frequency [Hz]

Member Function Documentation

◆ set_SPI_clock()

void R_Click::set_SPI_clock ( uint32_t  clk_freq_Hz)

Adjust the initially set SPI clock frequency of 1 MHz to another frequency.

The maximum SPI clock frequency listed by the datasheet of the MCP3201 ADC chip of the R Click Board is 1.6 MHz.

Parameters
clk_freq_HzThe SPI clock frequency in Hz

◆ bitval2mA()

float R_Click::bitval2mA ( float  bitval)

Transform the bit value into a current [mA] given the calibration parameters.

Currents less than 3.8 mA are considered to indicate a fault state, such as a broken wire, a disconnected device or an error happening at the transmitter side. In that case the return value will be NAN.

Parameters
bitvalThe bit value to transform
Returns
The current in mA, or NAN when the device is in a fault state

◆ read_bitval()

uint16_t R_Click::read_bitval ( )

Read out the R Click once and return the bit value.

Returns
The bit value

◆ read_mA()

float R_Click::read_mA ( )

Read out the R Click once and return the current in mA.

Returns
The current in mA, or NAN when the device is in a fault state. See bitval2mA() for more details on the fault state.

◆ poll_EMA()

bool R_Click::poll_EMA ( )

This method is crucial for the exponential moving average (EMA) to work correctly. It should be repeatedly called in the main loop, ideally at a faster pace than the given oversampling interval EMA_interval.

Returns
True when a new sample has been read and added to the moving average. False otherwise, because it was not yet time to read out a new sample.
Note
Parameters EMA_interval and EMA_LP_freq must have been passed to the R_Click() constructor.

◆ get_EMA_bitval()

float R_Click::get_EMA_bitval ( )

Return the exponential moving average value of the R Click readings in bit value.

Returns
The fractional bit value
Note
Parameters EMA_interval and EMA_LP_freq must have been passed to the R_Click() constructor and poll_EMA() must have been repeatedly called.

◆ get_EMA_mA()

float R_Click::get_EMA_mA ( )

Return the exponential moving average value of the R Click readings in mA.

Returns
The current in mA, or NAN when the device is in a fault state. See bitval2mA() for more details on the fault state.
Note
Parameters EMA_interval and EMA_LP_freq must have been passed to the R_Click() constructor and poll_EMA() must have been repeatedly called.

◆ get_EMA_obtained_interval()

uint32_t R_Click::get_EMA_obtained_interval ( )

Return the last obtained interval of the oversampled R Click readings of the exponential moving average in microseconds.

Returns
The interval in microseconds

The documentation for this class was generated from the following files: