Untitled
unknown
c_cpp
a year ago
4.0 kB
4
Indexable
#include <iostream> #include <unordered_map> #include <string> // Forward declaration of State class class State; // Mealy Machine class class MealyMachine { private: State* currentState; public: // Constructor MealyMachine(State* initialState) : currentState(initialState) {} // Method to process inputs void processInputs(bool button, bool dco_s, bool go_down, bool go_up); // Getters for outputs bool getDco() const; bool getFan() const; bool getMotor() const; bool getMotorDir() const; }; // Define a class for state class State { public: std::string name; std::unordered_map<std::string, std::pair<State*, std::string>> transitions; // Mapping from inputs to next state and outputs // Constructor State(const std::string& name) : name(name) {} }; // Method definition for MealyMachine::processInputs void MealyMachine::processInputs(bool button, bool dco_s, bool go_down, bool go_up) { std::string input = std::to_string(button) + std::to_string(dco_s) + std::to_string(go_down) + std::to_string(go_up); // Check if the input has a transition defined for the current state if (currentState->transitions.find(input) != currentState->transitions.end()) { auto transition = currentState->transitions[input]; currentState = transition.first; std::cout << "Transition to state: " << currentState->name << std::endl; } else { std::cout << "No transition defined for inputs in state '" << currentState->name << "'" << std::endl; } } // Method definition for MealyMachine::getDco bool MealyMachine::getDco() const { // Get output associated with the current state return currentState->transitions.at("000").second == "1"; } // Method definition for MealyMachine::getFan bool MealyMachine::getFan() const { // Get output associated with the current state return currentState->transitions.at("000").second == "1"; } // Method definition for MealyMachine::getMotor bool MealyMachine::getMotor() const { // Get output associated with the current state return currentState->transitions.at("000").second == "1"; } // Method definition for MealyMachine::getMotorDir bool MealyMachine::getMotorDir() const { // Get output associated with the current state return currentState->transitions.at("000").second == "1"; } int main() { // Define states State* groundState = new State("Ground"); State* startingUpState = new State("StartingUp"); State* goingUpState = new State("GoingUp"); State* topState = new State("Top"); State* startingDownState = new State("StartingDown"); State* goingDownState = new State("GoingDown"); // Define transitions and outputs for each state groundState->transitions["1000"] = {startingUpState, "0000"}; startingUpState->transitions["1100"] = {goingUpState, "1100"}; goingUpState->transitions["0110"] = {topState, "0110"}; topState->transitions["0001"] = {startingDownState, "0000"}; startingDownState->transitions["1100"] = {goingDownState, "1100"}; goingDownState->transitions["1010"] = {groundState, "1010"}; // Create Mealy machine MealyMachine liftController(groundState); // Process inputs and get outputs liftController.processInputs(true, false, false, false); // button = true, dco_s = false, go_down = false, go_up = false std::cout << "DCO: " << (liftController.getDco() ? "1" : "0") << std::endl; std::cout << "Fan: " << (liftController.getFan() ? "1" : "0") << std::endl; std::cout << "Motor: " << (liftController.getMotor() ? "1" : "0") << std::endl; std::cout << "Motor Direction: " << (liftController.getMotorDir() ? "1" : "0") << std::endl; // Cleanup delete groundState; delete startingUpState; delete goingUpState; delete topState; delete startingDownState; delete goingDownState; return 0; }
Editor is loading...
Leave a Comment