MODBUS SLAVE MONITOR 1.0
Loading...
Searching...
No Matches
ADC Processing and RMS Calculation

Classes

struct  ADC_Sample
 ADC sample with value and associated channel. More...
struct  RMS_FIFO
 Circular buffer for efficient RMS calculation. More...

Functions

void IRAM_ATTR on_adc_data_ready ()
 Interrupt service routine for completed ADC conversions.
void task_adquisicion (void *pvParameters)
 ADC data acquisition task with improved multiplexer handling.
void task_procesamiento (void *pvParameters)
 Sample processing and RMS calculation task.
int get_rms_history (int channel, float *output_buffer, int count)
 Gets RMS value history for a specific channel.

Variables

const float CONVERSION_FACTORS [NUM_CHANNELS]
 Channel-specific conversion factors for each ADC channel.
QueueHandle_t queue_adc_samples
 Communication queue for ADC samples.
RMS_FIFO fifos [NUM_CHANNELS]
 Array of FIFOs for each measurement channel.
float rms_history_ch0 [RMS_HISTORY_SIZE]
 RMS value history for channel 0.
float rms_history_ch1 [RMS_HISTORY_SIZE]
 RMS value history for channel 1.
float rms_history_ch2 [RMS_HISTORY_SIZE]
 RMS value history for channel 2.
volatile int rms_history_head = 0
 Write index for RMS histories.
SemaphoreHandle_t rms_history_mutex
 Mutex for concurrent access protection to RMS histories.
volatile bool adc_data_ready = false
 ADC data ready flag (ISR).
volatile uint8_t current_isr_channel = 0
 Current channel in sampling sequence (ISR).
const int FIFO_SIZE = 320
 Circular FIFO buffer size for RMS calculation.
const int PROCESS_INTERVAL_MS = 1000
 RMS processing interval in milliseconds.
const int RMS_HISTORY_SIZE = 100
 RMS value history size for each channel.
const float CONVERSION_FACTOR = 0.618f
 Global conversion factor (maintained for compatibility).

Detailed Description

Overview

This module implements analog signal acquisition via ADS1015 and the complete flow of:

  • ADC sample acquisition in round-robin mode between 3 channels using interrupts.
  • Real-time processing with circular FIFOs for efficient RMS calculation.
  • RMS value history storage in thread-safe buffers.
  • Application of channel-specific conversion factors for individual calibration.

Structure and Components

  • Data and configuration
    • ADC_Sample: sample structure with value and associated channel.
    • RMS_FIFO: circular buffer with cumulative sums for incremental calculation.
    • CONVERSION_FACTORS[]: channel-specific calibration factors.
  • Acquisition and processing
    • queue_adc_samples: FreeRTOS queue for acquisition → processing transfer.
    • fifos[NUM_CHANNELS]: array of circular FIFOs for each channel.
    • rms_history_ch0/ch1/ch2: independent histories of RMS values.
  • Main tasks

Main API

Usage

Flow summary:

1) ADC initialization

  • ADS1015 configuration: TWOTHIRDS gain (±6.144V), 3300 SPS speed.
  • Interrupt configuration on ALERT pin for completed conversions.
  • Creation of queue_adc_samples queue with FIFO_SIZE capacity.
  • Initialization of rms_history_mutex mutex for history protection.

2) Sample acquisition

3) RMS processing

  • task_procesamiento consumes samples from queue on core 0 with priority 3.
  • Circular FIFO update with incremental algorithm:
    • Sum maintenance: sum_x (∑x) and sum_x2 (∑x²).
    • Circular buffer with automatic replacement upon reaching FIFO_SIZE=320.
    • Mean calculation: μ = sum_x / count.
    • Variance calculation: σ² = (sum_x2 / count) - μ².
    • RMS value: √σ² with negative variance protection.

4) History management

  • RMS calculation every PROCESS_INTERVAL_MS=1000ms.
  • Storage in independent circular buffers per channel.
  • Thread-safe protection with rms_history_mutex.
  • get_rms_history() function for extracting last N values.

5) Channel calibration

  • CONVERSION_FACTORS[] array with specific factors:
    • Channel 0: 0.653f
    • Channel 1: 0.679f
    • Channel 2: 1.133f
  • Applied in dataUpdateTask to compensate sensor differences.
  • Allows individual adjustment of measurement ranges and conditioning.

6) Incremental RMS algorithm

  • O(1) efficiency for new sample insertion.
  • No need to traverse entire buffer for calculation.
  • Automatic maintenance of cumulative statistics.
  • Robust overflow management in circular buffer.

Warnings and best practices:

  • Verify ADS1015 power supply stability for accurate conversions.
  • Configure external pullups on I2C lines if communication issues occur.
  • Validate ALERT pin connection before enabling interrupts.
  • Protect RMS history access with appropriate mutex timeout.
  • Calibrate CONVERSION_FACTORS[] according to actual sensor characteristics.
  • Monitor processing latency to avoid queue saturation.
  • Verify that FIFO_SIZE is sufficient for required analysis window.

Function Documentation

◆ get_rms_history()

int get_rms_history ( int channel,
float * output_buffer,
int count )

Gets RMS value history for a specific channel.

Extracts the last N RMS values in a thread-safe manner using mutex. Values are returned in chronological order (oldest first).

Parameters
channelChannel number (0, 1 or 2).
output_bufferOutput buffer for RMS values.
countNumber of requested values.
Returns
Number of values actually copied.

◆ on_adc_data_ready()

void IRAM_ATTR on_adc_data_ready ( )

Interrupt service routine for completed ADC conversions.

Executes when ADS1015 completes a conversion and activates ALERT line.

◆ task_adquisicion()

void task_adquisicion ( void * pvParameters)

ADC data acquisition task with improved multiplexer handling.

Manages ADC conversion sequence in round-robin mode between channels. Implements explicit multiplexer configuration selection for each channel. Enqueues samples in queue_adc_samples for subsequent processing.

Parameters
pvParametersTask parameters (unused).

Explicit multiplexer configuration selection per channel.

Direct mapping from channel number to ADS1015 multiplexer configuration constant to avoid addressing errors.

◆ task_procesamiento()

void task_procesamiento ( void * pvParameters)

Sample processing and RMS calculation task.

Consumes samples from queue_adc_samples, stores them in circular FIFOs and periodically calculates RMS values using efficient incremental algorithms. Updates RMS history in a thread-safe manner.

Parameters
pvParametersTask parameters (unused).

Variable Documentation

◆ CONVERSION_FACTOR

const float CONVERSION_FACTOR = 0.618f

Global conversion factor (maintained for compatibility).

Deprecated
Use CONVERSION_FACTORS array for channel-specific factors.

◆ CONVERSION_FACTORS

const float CONVERSION_FACTORS[NUM_CHANNELS]
Initial value:
= {
0.653f,
0.679f,
1.133f
}

Channel-specific conversion factors for each ADC channel.

Enables individual channel calibration to compensate for differences in sensors, signal conditioning or specific measurement ranges.