ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Moving average
    Autonomous Lawn Mower/Embedded C 2025. 2. 9. 12:21
    반응형

    ADC값이 자주 변동이 있어서 moving average를 사용하고자 한다. STM32F401에서는 동작이 버거운듯하다. moving average 코드를 넣으면 모터가 구동이 안된다. T.T

     

    • 1 channel ADC moving average

     

    #include <stdio.h>
    #include <stdint.h>

    #define AVERAGE_NUMBER 10  // Moving average window size

    int32_t sum = 0;
    int pos = 0;
    int16_t arrNumbers[AVERAGE_NUMBER] = {0};  // Array buffer for moving average

    /**
     * @brief Moving Average Calculation Function
     * @param ptrArrNumbers Pointer to the array holding the samples
     * @param ptrSum Pointer to the running sum
     * @param pos Current buffer position
     * @param len Size of the buffer
     * @param nextNum New sample value
     * @return Calculated moving average
     */
    int movingAvg(int16_t *ptrArrNumbers, int32_t *ptrSum, int *pos, int len, int nextNum) {
        // Subtract the oldest sample and add the new one
        *ptrSum = *ptrSum - ptrArrNumbers[*pos] + nextNum;

        // Store the new sample in the current position
        ptrArrNumbers[*pos] = nextNum;

        // Update position and wrap around if necessary
        *pos = (*pos + 1) % len;

        // Return the average
        return *ptrSum / len;
    }

    int main() {
        int adc_buf[1] = {300};  // Example ADC reading
        int voltage_avg;

        for (int i = 0; i < 20; i++) {  // Simulate 20 ADC readings
            adc_buf[0] = 300 + (i * 5);  // Example varying ADC values
            voltage_avg = movingAvg(arrNumbers, &sum, &pos, AVERAGE_NUMBER, adc_buf[0]);
            printf("ADC Reading: %d, Voltage Average: %d\n", adc_buf[0], voltage_avg);
        }

        return 0;
    }

     

     

    • 4channel ADC moving average

    #include <stdio.h>
    #include <stdint.h>

    #define AVERAGE_NUMBER 10   // Moving average window size
    #define ADC_CHANNEL_COUNT 4 // Number of ADC channels

    int32_t sum[ADC_CHANNEL_COUNT] = {0};  // Running sum for each channel
    int pos[ADC_CHANNEL_COUNT] = {0};     // Buffer position for each channel
    int16_t arrNumbers[ADC_CHANNEL_COUNT][AVERAGE_NUMBER] = {0};  // Buffer for samples

    /**
     * @brief Moving Average Calculation Function for Multiple ADC Channels
     * @param channel ADC channel index
     * @param nextNum New sample value for the corresponding channel
     * @return Calculated moving average
     */
    int movingAvg(int channel, int nextNum) {
        // Subtract the oldest sample and add the new sample for the specific channel
        sum[channel] = sum[channel] - arrNumbers[channel][pos[channel]] + nextNum;

        // Store the new sample in the current position for the channel
        arrNumbers[channel][pos[channel]] = nextNum;

        // Update position and wrap around if necessary
        pos[channel] = (pos[channel] + 1) % AVERAGE_NUMBER;

        // Return the moving average
        return sum[channel] / AVERAGE_NUMBER;
    }

    int main() {
        int adc_buf[ADC_CHANNEL_COUNT] = {300, 400, 500, 600};  // Example ADC readings for 4 channels
        int voltage_avg[ADC_CHANNEL_COUNT];

        for (int i = 0; i < 20; i++) {  // Simulate 20 ADC readings for each channel
            // Simulate ADC values increasing for demonstration
            for (int channel = 0; channel < ADC_CHANNEL_COUNT; channel++) {
                adc_buf[channel] = 300 + (channel * 50) + (i * 5);
                voltage_avg[channel] = movingAvg(channel, adc_buf[channel]);
                printf("ADC Channel %d Reading: %d, Voltage Average: %d\n", channel, adc_buf[channel], voltage_avg[channel]);
            }
            printf("\n");
        }

        return 0;
    }

     

    반응형

    'Autonomous Lawn Mower > Embedded C' 카테고리의 다른 글

    Static, extern, volatile, const  (1) 2023.05.11
    Micro Inspector Pro  (0) 2022.12.31
    __STATIC_INLINE  (0) 2022.12.26
Designed by Tistory.