Predictive maintenance is a technique used to carry out maintenance tasks before their need arises. It relies on real time data and uses advanced sensors and analytics for the monitoring of equipment condition for the prediction of failures. It includes the analysis of vibration, temperature, and the equipment’s normal usage to suggest possible maintenance schedules. This kind of maintenance enables effective utilization of the equipment by minimizing both the downtime of the maintenance and the equipment itself.
Edge ML-based predictive maintenance enables data processing directly on edge devices, such as battery-powered embedded systems installed on machines. By analyzing parameters like vibration, temperature, and usage frequency, Edge ML can predict failures and detect anomalies without relying on cloud connectivity.
This AI-driven approach offers several advantages over traditional cloud-based solutions, including:
- Reduce latency
- Lower bandwidth consumption
- Enhanced data privacy,
- Reliable performance
- Works on low-connectivity environments.
Additionally, it significantly reduces resource consumption by leveraging real-time data analysis to:
- Accelerate decision-making
- Optimize maintenance schedules
- Minimize unnecessary repairs
- Reduce downtime
- Benchmarking ML Model Inference: Evaluating the performance of inference code in both Python and C.
- Real-time Anomaly Detection: Implementing on-device anomaly prediction for CNC milling operations using the XIAO BLE Sense.
The hardware setup includes a 3.7V 250mAh battery-powered Seeed Studio XIAO BLE Sense, which facilitates the collection of vibration data. Equipped with a 6-axis IMU, the XIAO BLE Sense efficiently captures real-time vibrations during CNC milling operations.
We integrated a sensing component into the Tormach XS Tech CNC machine to collect vibration data during the efficient and precise milling of small squares on an acrylic sheet. The square patterns were initially designed in CAD, specifying their sizes and positions, and then imported into CAM software, which generated the G-Code instructions required to operate the CNC machine.
Milling operations were performed using a flat-end mill bit, with the spindle speed tested at various RPMs ranging from 4, 000 to 12, 000 to determine the optimal settings.
Process flow :
The data collected from the previous steps serves as the foundation for further analysis and the development of machine learning (ML) models. These models are designed to predict CNC milling failures and trigger alerts effectively. To achieve this, the process involves the following steps:
- Data Preparation
- Feature extraction
- Model training
- Model optimization
- Model conversion to run on Micro-controller
1.Data Preparation and Understanding:
Data details:
- Sampling rate : 30Hz
- Data length : Approx. 40-50 sec
- Data label : Within Tolerance, Out of Tolerance
Data Augmentation:
To enhance the dataset and ensure the model had sufficient variety for training, data augmentation techniques are applied. The resulting dataset comprised approximately 24 hours of data.
2. Pre-Processing Block : Feature Extraction Raw data requires significant processing to extract meaningful features that can be fed into models for training and prediction.
The data distribution of raw data looks like following plot
The feature extraction process involves extracting key statistical features from the sensor data (accelerometer sensor).
The features extracted for each axis (x, y, z) included:
- Average: Mean value
- Minimum and Maximum: Range of the data
- RMS (Root Mean Square): Captures energy of the signal
- Standard Deviation: Measures variability
- Skewness: Indicates asymmetry of the data distribution
- Kurtosis: Calculates degree of flatness or peakness in the region of the curve.
The implementation of the Pre-Processing Block ensures that only essential characteristics of the data are fed into the model for training.
3.Model Training
The pre-processed data is used for training with TensorFlow, leveraging the Sequential Keras model in Python. The architecture of the trained model, when visualized using Netron.app, appears as shown below:
4.Model Optimization with TensorFlow Lite
To optimize the model for deployment in resource-constrained environments, TensorFlow Lite (TFLite) is used to create a lightweight and efficient version. Following quantization techniques are tried out:
- Integer Quantization (int8): Converts 32-bit floating-point weights, activation, inputs, and outputs into 8-bit integers, enabling fully integer-only operations during inference.
- Integer Quantization with int16 Activations (int8 weights, int16 activations): Converts weights to 8-bit integers and activations to 16-bit integers, providing higher numerical accuracy for tasks that require it.
Trained dataset is used as representative dataset during quantization to ensure that the quantized model is optimized with high accuracy while significantly reducing its size and computational demands.
5. Model conversion to run on Micro-controller
After quantizing the model, the model undergoes further optimization for deployment on microcontrollers, addressing constraints related to limited RAM and storage. The model is converted into a file format that microcontroller understands, ensuring efficient execution on resource-limited edge devices. This conversion significantly reduces memory footprint while maintaining inference accuracy.
Following command is used to convert tensorflow lite model to executable C format.
xxd -i converted_model.tflite > model_data.cc
ML Model BenchmarkingTFLite ML Model Inference in PYTHON
1. TFLite Model Inference
The prediction
function loads a pre-trained TFLite model, allocates tensors, and performs inference on extracted features. The predicted class label is mapped to predefined categories (Within Tolerance, Out of Tolerance
).
interpreter = tf.lite.Interpreter(model_content=model_content)
interpreter.allocate_tensors()
The extracted feature set is reshaped and converted into a format compatible with the model’s expected input dimensions.
input_data = features.numpy().reshape(1, input_details['shape'][1])
interpreter.set_tensor(input_details['index'], input_data)
interpreter.invoke()
The output tensor provides a class prediction, which is retrieved using np.argmax(output_data)
.
2. File Handling and Feature Extraction
The file_handling
function processes either a single CSV file or a directory of CSV files. It leverages the FeatureGenerator
class to extract meaningful features from raw XYZ sensor data.
df = pd.read_csv(csv)
raw_features = df[['x', 'y', 'z']].values.flatten()
raw_data = raw_features.reshape(int(len(raw_features) / 3), 3)
features = fg.generate_features(raw_data)
This ensures that each dataset is transformed into a suitable feature vector before being passed to the model.
3. Performance Evaluation
The script calculates the total inference time, aiding in performance benchmarking for real-time applications.
start = time.time()
file_handling(args.csv, args.tflite_model)
end = time.time()
print(f"Prediction Time: {prediction_time:.6f} seconds")
TFLite ML Model Inference in C
Converted and optimized TFlite model is used to run the inference script in C same as used in the python language.
1.Structure Definition
typedef struct {
float timestamp;
float x;
float y;
float z;
} AccelerometerData;
Each accelerometer sample consists of a timestamp and three-axis acceleration values.
2.Feature Extraction Functions
The following functions compute key statistical features used for classification:
Mean Calculationfloat averageOfArray(float *array, int len) {
float sum = 0;
for (int i = 0; i < len; i++) {
sum += array[i];
}
return sum / len;
}
Standard Deviation Calculationfloat stddevOfArray(float *arr, int len) {
float mean = averageOfArray(arr, len);
float sum_sq_diff = 0.0f;
for (int i = 0; i < len; i++) {
sum_sq_diff += (arr[i] - mean) * (arr[i] - mean);
}
return sqrtf(sum_sq_diff / len);
}
Skewness and Kurtosisfloat skewnessOfArray(float *data, int num_samples) {
float mean = averageOfArray(data, num_samples);
float variance = stddevOfArray(data, num_samples);
float sum_cubed_diff = 0;
for (int i = 0; i < num_samples; i++) {
sum_cubed_diff += pow(data[i] - mean, 3);
}
return sum_cubed_diff / (num_samples * pow(variance, 3));
}
float kurtosisOfArray(float *data, int num_samples) {
float mean = averageOfArray(data, num_samples);
float variance = stddevOfArray(data, num_samples);
float sum_fourth_power_diff = 0;
for (int i = 0; i < num_samples; i++) {
sum_fourth_power_diff += pow(data[i] - mean, 4);
}
return sum_fourth_power_diff / (num_samples * pow(variance, 4)) - 3;
}
Data Loading from CSVThe LoadRawDataFromCSV()
function reads accelerometer data from a CSV file and stores it in memory:
AccelerometerData *LoadRawDataFromCSV(const char *csv_path, int *num_samples) {
FILE *fp = fopen(csv_path, "r");
if (fp == NULL) {
fprintf(stderr, "Error opening CSV file: %s\n", csv_path);
exit(1);
}
*num_samples = 0;
char line[1024];
while (fgets(line, sizeof(line), fp)) {
(*num_samples)++;
}
rewind(fp);
AccelerometerData *data = (AccelerometerData *)malloc(sizeof(AccelerometerData) * (*num_samples));
if (!data) {
fprintf(stderr, "Memory allocation error\n");
exit(1);
}
int i = 0;
while (fgets(line, sizeof(line), fp)) {
sscanf(line, "%f,%f,%f,%f", &data[i].timestamp, &data[i].x, &data[i].y, &data[i].z);
i++;
}
fclose(fp);
return data;
}
Running the TensorFlow Lite ModelThe function TFLITEModelRun()
loads and runs the TFLite model:
int TFLITEModelRun(char *modelFilename, AccelerometerData *raw_data, int num_samples) {
TfLiteModel* model = TfLiteModelCreateFromBuffer(model_data, model_data_size);
if (!model) {
fprintf(stderr, "Error loading model\n");
return 1;
}
TfLiteInterpreterOptions *options = TfLiteInterpreterOptionsCreate();
TfLiteInterpreter *interpreter = TfLiteInterpreterCreate(model, options);
TfLiteInterpreterOptionsDelete(options);
if (!interpreter) {
fprintf(stderr, "Error creating interpreter\n");
return 1;
}
TfLiteInterpreterAllocateTensors(interpreter);
float *input_data = preProcessing(raw_data, num_samples);
TfLiteTensor *input_tensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
memcpy(TfLiteTensorData(input_tensor), input_data, sizeof(float) * 21);
TfLiteInterpreterInvoke(interpreter);
TfLiteTensor *output_tensor = TfLiteInterpreterGetOutputTensor(interpreter, 0);
float *output_data = (float *)TfLiteTensorData(output_tensor);
return findMaxIndex(output_data, 4);
}
This C-based implementation of an accelerometer-driven milling operation classification system efficiently extracts motion features and performs inference using a TensorFlow Lite model.
This project provided insights into key aspects of implementing ML-driven anomaly detection for CNC milling operations, including:
- Data Collection: Capturing relevant sensor data from the CNC milling process.
- Data Transmission: Utilizing BLE for efficient data transfer via XIAO BLE Sense.
- Feature Extraction: Processing raw data to extract meaningful patterns.
- ML Model Training: Developing predictive models for anomaly detection.
- Model Optimization: Adapting the model for efficient execution on microcontrollers.
- Performance Benchmarking: Evaluating the ML model’s efficiency in both Python and C.
In the second phase, we will explore deploying the converted TFLite model on a microcontroller specifically, the XIAO BLE to predict milling operations and monitor the condition of CNC equipment.
Comments
Please log in or sign up to comment.