Brief Description of the Code:
This MicroBasic script for a Roboteq motor controller smooths encoder data for channels 1 and 2 using a moving average filter and transmits the smoothed data via CAN communication at 1 millisecond intervals.
What It Does:
Initialization:
- Sets up variables and constants for the moving average calculation and timing.
Data Collection:
- Continuously reads encoder data every 1ms from channels 1 and 2.
Moving Average Calculation:
- Stores the latest encoder readings in a circular buffer.
- After collecting 5 readings, calculates the average for each channel using the sum of the last 5 values.
Data Transmission:
- Transmits the smoothed data via CAN communication.
' /////////// ////////// //////// //////// //////// ROBOTEQ \\\\\\\\\ \\\\\\\\\ \\\\\\\\\ \\\\\\\\\ \\\\\\\\'Disclaimer:'The script is tested and validated by Roboteq and is believed to be fault-free.'The possibility always exists, however, that the particular configuration and/or use'condition uncovers a fault that escaped our validation test coverage. Always'extensively test the script under your use conditions prior to deploying it in the field.'*******************************************************************************************' This script is responsible for applying a moving average filter with a 1ms interval' for smoothing the encoder data of channels 1 and 2 and transmitting it via CAN communication.option explicit' Constants#defineWINDOW_SIZE5' Number of samples for moving average#defineINTERVAL_MS1' Time interval in milliseconds between data collection and averaging' VariablesdimencoderDataCh1[WINDOW_SIZE]as integer' Array to store the last WINDOW_SIZE encoder readings for channel 1dimencoderDataCh2[WINDOW_SIZE]as integer' Array to store the last WINDOW_SIZE encoder readings for channel 2dimsumCh1as integer' Sum of the encoder readings for channel 1dimsumCh2as integer' Sum of the encoder readings for channel 2dimindexas integer' Index for the current position in the circular bufferdimcountas integer' Count of the number of readings takendimsmoothedDataCh1as integer' Smoothed encoder data for channel 1dimsmoothedDataCh2as integer' Smoothed encoder data for channel 2dimias integer' Loop variable'Initializationcount=0index=0sumCh1=0sumCh2=0i=0Main:'read encoder data from each channelencoderDataCh1[index] =getvalue(_c,1)encoderDataCh2[index] =getvalue(_c,2)'print(encoderDataCh1[index],"\n")' Increment the index and countindex=index+1ifindex>=WINDOW_SIZEthen' If index exceeds the window size,index=0' reset it to 0 (circular buffer)end ififcount<WINDOW_SIZEthen' If count is less than the window size,count=count+1' increment the countend if' Apply the moving average filterifcount=WINDOW_SIZEthengosubMovingAverageFilter'print(smoothedDataCh1,"\n")' Transmit smoothed data via CAN saving in VAR 1 and VAR 2setcommand(_VAR,1,smoothedDataCh1)setcommand(_VAR,2,smoothedDataCh2)' Reset count after the first average calculationcount=5' Ensure count stays at WINDOW_SIZE for subsequent calculationsend ifwait(INTERVAL_MS)gotomain' Subroutine for calculating the moving averageMovingAverageFilter:sumCh1=0sumCh2=0fori=0andwhilei<= (WINDOW_SIZE-1)Evaluatei+=1'Loop through all the values in the window sizesumCh1=sumCh1+encoderDataCh1[i]sumCh2=sumCh2+encoderDataCh2[i]nextsmoothedDataCh1=sumCh1/WINDOW_SIZE'Calculate the average for channel 1smoothedDataCh2=sumCh2/WINDOW_SIZE'Calculate the average for channel 2'print(smoothedDataCh1,"\n")return