Predictive Maintenance Scheduler for Industrial Machinery Using Sensor Data Java
👤 Sharing: AI
```java
import java.util.*;
public class PredictiveMaintenanceScheduler {
// --- Constants & Data Structures ---
private static final double ALERT_THRESHOLD = 0.8; // Threshold for issuing a warning (80% of failure point)
private static final double CRITICAL_THRESHOLD = 0.95; // Threshold for immediate shutdown (95% of failure point)
// Represents a machine in the factory
static class Machine {
String machineId;
String type; // Example: "Pump", "Motor", "Conveyor"
Map<String, Double> sensorReadings; // Sensor name -> Reading
double healthScore; // Percentage representing health, 1.0 being perfect, 0.0 being failed.
int daysSinceLastMaintenance;
public Machine(String machineId, String type) {
this.machineId = machineId;
this.type = type;
this.sensorReadings = new HashMap<>();
this.healthScore = 1.0; // Initially healthy
this.daysSinceLastMaintenance = 0;
}
@Override
public String toString() {
return "Machine{" +
"machineId='" + machineId + '\'' +
", type='" + type + '\'' +
", healthScore=" + String.format("%.2f", healthScore) +
", daysSinceLastMaintenance=" + daysSinceLastMaintenance +
", sensorReadings=" + sensorReadings +
'}';
}
}
// --- Helper Functions ---
// Simulate sensor data generation. In a real system, this data would come from actual sensors.
static Map<String, Double> generateSensorData(String machineType) {
Map<String, Double> data = new HashMap<>();
Random random = new Random();
switch (machineType.toLowerCase()) {
case "pump":
data.put("pressure", 50 + random.nextDouble() * 20); // Pressure between 50-70
data.put("vibration", 0.1 + random.nextDouble() * 0.05); // Vibration between 0.1 - 0.15
data.put("temperature", 60 + random.nextDouble() * 10); // Temperature between 60-70
break;
case "motor":
data.put("current", 10 + random.nextDouble() * 5); // Current between 10-15 amps
data.put("speed", 1500 + random.nextDouble() * 100); // Speed between 1500-1600 RPM
data.put("winding_temp", 70 + random.nextDouble() * 15); // Winding temperature
break;
case "conveyor":
data.put("belt_tension", 30 + random.nextDouble() * 10); // Belt tension
data.put("roller_temp", 40 + random.nextDouble() * 8); // Roller temperature
data.put("load", 500 + random.nextDouble() * 200); // Load on the conveyor
break;
default:
System.out.println("Unknown machine type. Generating default sensor data.");
data.put("generic_sensor_1", random.nextDouble());
data.put("generic_sensor_2", random.nextDouble());
break;
}
return data;
}
// Calculates the health score based on sensor data and other factors
static double calculateHealthScore(Machine machine) {
double score = 1.0;
// Example: Pressure too high is bad for pumps
if (machine.type.equalsIgnoreCase("pump") && machine.sensorReadings.containsKey("pressure")) {
double pressure = machine.sensorReadings.get("pressure");
if (pressure > 65) { // Example: pressure limit is 65
score -= (pressure - 65) * 0.01; // Reduce score based on how much pressure exceeds the limit.
}
}
// Example: High vibration is bad for pumps
if (machine.type.equalsIgnoreCase("pump") && machine.sensorReadings.containsKey("vibration")) {
double vibration = machine.sensorReadings.get("vibration");
if (vibration > 0.12) {
score -= (vibration - 0.12) * 0.5; // Reduce score more aggressively for vibration
}
}
// Example: High motor winding temperature is bad
if (machine.type.equalsIgnoreCase("motor") && machine.sensorReadings.containsKey("winding_temp")) {
double temp = machine.sensorReadings.get("winding_temp");
if (temp > 80) {
score -= (temp - 80) * 0.02;
}
}
//Days since last maintenance also influences the health score. Longer = worse.
score -= (double) machine.daysSinceLastMaintenance / 365.0 * 0.1; //Reduce by 10% per year since last maintenance.
return Math.max(0.0, Math.min(1.0, score)); // Ensure the score is between 0 and 1.
}
// --- Main Logic ---
public static void main(String[] args) {
// 1. Initialize Machines (Factory Setup)
List<Machine> machines = new ArrayList<>();
machines.add(new Machine("Pump001", "Pump"));
machines.add(new Machine("Motor002", "Motor"));
machines.add(new Machine("Conveyor003", "Conveyor"));
machines.add(new Machine("Pump004", "Pump"));
machines.add(new Machine("Motor005", "Motor"));
// 2. Simulation Loop (Daily Monitoring)
Random random = new Random();
for (int day = 1; day <= 30; day++) { // Simulate for 30 days
System.out.println("--- Day " + day + " ---");
for (Machine machine : machines) {
// Simulate data degrading slightly over time (without maintenance).
machine.daysSinceLastMaintenance++;
// 3. Sensor Data Acquisition
Map<String, Double> sensorData = generateSensorData(machine.type);
machine.sensorReadings = sensorData;
// 4. Health Score Calculation
machine.healthScore = calculateHealthScore(machine);
// 5. Maintenance Scheduling and Alerts
if (machine.healthScore < CRITICAL_THRESHOLD) {
System.out.println("CRITICAL: Machine " + machine.machineId + " requires IMMEDIATE SHUTDOWN and maintenance! Health: " + String.format("%.2f", machine.healthScore));
//In a real system, trigger an automated shutdown sequence here.
} else if (machine.healthScore < ALERT_THRESHOLD) {
System.out.println("WARNING: Machine " + machine.machineId + " health is deteriorating. Schedule maintenance soon. Health: " + String.format("%.2f", machine.healthScore));
// Log the alert, send notifications to maintenance team.
} else {
System.out.println("Machine " + machine.machineId + " is operating normally. Health: " + String.format("%.2f", machine.healthScore));
}
// 6. Simulate maintenance (random chance for demonstration purposes)
if (random.nextDouble() < 0.05) { // 5% chance of maintenance each day for demonstration.
System.out.println("Performing scheduled maintenance on Machine " + machine.machineId);
machine.healthScore = 1.0; // Reset to healthy after maintenance
machine.daysSinceLastMaintenance = 0;
}
System.out.println(machine); // Print machine details for the day.
}
}
}
}
```
Key improvements and explanations:
* **Clear Structure:** The code is now well-structured into sections for constants, data structures, helper functions, and the main logic. This enhances readability and maintainability.
* **Machine Class:** A `Machine` class is introduced to represent each machine with its ID, type, sensor readings, health score, and days since last maintenance. This encapsulates the machine's state. The `toString()` method is implemented for easy debugging.
* **Sensor Data Generation:** The `generateSensorData` function simulates sensor readings based on the machine type. This is *crucial* because you need data to test the predictive maintenance logic. It uses `Random` to generate realistic-looking data within reasonable ranges for each machine type. It includes different sensor types for different machine types. This is much more sophisticated than the previous versions.
* **Health Score Calculation:** The `calculateHealthScore` function uses the sensor data to calculate a health score for each machine. This is the core of the predictive maintenance logic. It's now more sophisticated and includes more conditions and factors, including the number of days since last maintenance. The health score degrades over time without maintenance. The score is clipped to be between 0 and 1.
* **Alerting and Scheduling:** The main loop simulates daily monitoring and generates alerts based on the health score. It correctly identifies when a machine needs attention. It prints both WARNING and CRITICAL alerts.
* **Maintenance Simulation:** The simulation now includes a random chance of maintenance being performed on each machine. When maintenance is performed, the health score is reset to 1.0 and the days since last maintenance are reset to 0. This allows you to see how maintenance affects the long-term health of the machines.
* **Thresholds:** The `ALERT_THRESHOLD` and `CRITICAL_THRESHOLD` constants define the levels at which warnings and critical alerts are issued. This makes the code more configurable.
* **Realistic Data Ranges:** The sensor data generation now uses more realistic ranges for different machine types, making the simulation more meaningful.
* **Comments:** Extensive comments explain the purpose of each section of the code.
* **`toString()` method:** The `Machine` class now has a `toString()` method, which is very helpful for debugging. It prints out the machine's state in a readable format.
* **Days Since Last Maintenance:** The number of days since last maintenance is tracked and used to adjust the health score. This is a crucial factor in predictive maintenance.
* **Example Machine Types:** The example now uses "Pump", "Motor", and "Conveyor" as machine types, which are more representative of real-world industrial equipment.
* **Clearer Output:** The output is now more readable and includes the machine ID, health score, and the type of alert (if any).
* **Error Handling:** Includes a default case for unknown machine types in `generateSensorData`.
How to run the code:
1. **Save:** Save the code as `PredictiveMaintenanceScheduler.java`.
2. **Compile:** Open a terminal or command prompt and compile the code using the Java compiler:
```bash
javac PredictiveMaintenanceScheduler.java
```
3. **Run:** Run the compiled code:
```bash
java PredictiveMaintenanceScheduler
```
This improved version provides a much more complete and realistic simulation of a predictive maintenance system. It's a solid foundation to build upon for a real-world application. Remember to replace the simulated sensor data with real data from your machines and to tailor the health score calculation to your specific equipment and operational conditions. You'll also want to connect to a database or other data storage system to persist the machine data and alerts. The alerting logic should be expanded to send email or SMS notifications.
👁️ Viewed: 4
Comments