Group
Real-Time Programming in C++
Objective
Write a C++ program that simulates the real-time control of a production machine. The program should simulate the machine's states (Idle, Running, Stopped) and update its state at regular intervals. Allow the user to input the time the machine spends in each state, and continuously run the machine, changing its states based on the specified intervals.
Write a program that simulates the control of a production machine in real time.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the input-output stream library
#include <thread> // Include the thread library to control time intervals
#include <chrono> // Include the chrono library for time manipulation
using namespace std;
// Function to simulate the production machine control system
void productionMachineControl(int idleDuration, int runningDuration, int stoppedDuration) {
while (true) { // Infinite loop to simulate continuous machine control
// Simulate Idle State
cout << "Machine is IDLE - Not in operation" << endl;
this_thread::sleep_for(chrono::seconds(idleDuration)); // Wait for the specified idle duration
// Simulate Running State
cout << "Machine is RUNNING - Production in progress" << endl;
this_thread::sleep_for(chrono::seconds(runningDuration)); // Wait for the specified running duration
// Simulate Stopped State
cout << "Machine is STOPPED - Production halted" << endl;
this_thread::sleep_for(chrono::seconds(stoppedDuration)); // Wait for the specified stopped duration
}
}
int main() {
int idleDuration, runningDuration, stoppedDuration;
// Ask the user to input the time durations for each machine state
cout << "Enter the duration for IDLE state in seconds: ";
cin >> idleDuration;
cout << "Enter the duration for RUNNING state in seconds: ";
cin >> runningDuration;
cout << "Enter the duration for STOPPED state in seconds: ";
cin >> stoppedDuration;
// Start the production machine control system with the user-defined durations
productionMachineControl(idleDuration, runningDuration, stoppedDuration);
return 0;
}
Output
Enter the duration for IDLE state in seconds: 3
Enter the duration for RUNNING state in seconds: 5
Enter the duration for STOPPED state in seconds: 2
Machine is IDLE - Not in operation
Machine is RUNNING - Production in progress
Machine is STOPPED - Production halted
...