Week - 4
1.)
Example Stateflow Logic
Here's how you might structure your states and transitions in Stateflow:
- States:
- Inactive:
- On Entry:
LED_power = 0;
- On Entry:
- Power On:
- Transition to Check Water Supply.
- Check Water Supply:
- If
water_supply == 1then transition to Soaking. - Else transition to Inactive and activate the water supply LED:
LED_water_supply = 1;
- If
- Soaking:
- On Entry:
duration = 200;
- On Entry:
- Washing:
- On Entry:
duration = 100;
- On Entry:
- Rinsing:
- On Entry:
duration = 20;
- On Entry:
- Drying:
- On Entry:
duration = 50;
- On Entry:
- Finished:
- On Entry:
LED_finished = 1;
- On Entry:
- Inactive:
Connecting Outputs to Simulink
- Connect Stateflow Outputs:
- Connect the LED outputs to Simulink blocks (e.g., Scope or Display) to visualize the states of the LEDs.
Finalize and Test the Model
Set Up Simulation Parameters:
- Go to Simulation > Model Configuration Parameters.
- Set the stop time to a value that exceeds the total duration of the processes (e.g., 500 seconds).
Run the Simulation:
- Click the Run button to simulate the washing machine logic.
- Check the outputs to ensure that the LEDs and transitions work as expected.
2.)
Steps to Create the Gear Shift Logic in Simulink
Open Simulink and Create a New Model:
- Launch MATLAB and open Simulink.
- Go to File > New > Model.
Add a Stateflow Chart:
- From the Simulink Library Browser, drag a Stateflow block into your model.
Open the Stateflow Chart:
- Double-click the Stateflow block to open the Stateflow editor.
Define States and Logic
Create States for Gear Shifting:
- You can represent the gear shifting logic using a single state since the gear is directly dependent on the input speed. You will use a conditional structure (a single chart) to determine the current gear based on the input speed.
Add Input and Output:
- Create an input variable
speedto take the speed input. - Create an output variable
gearto display the gear number.
- Create an input variable
Implement Gear Shifting Logic
- Add Gear Shifting Logic: In the Stateflow editor, define the logic using MATLAB-like syntax in the action of the state. Here’s how you can structure it:
plaintext// Transition logic based on speed if (speed >= 0 && speed <= 15) { gear = 1; } else if (speed >= 16 && speed <= 25) { gear = 2; } else if (speed >= 26 && speed <= 40) { gear = 3; } else if (speed >= 41 && speed <= 60) { gear = 4; } else if (speed > 61) { gear = 5; } else { gear = 0; // For any negative speed, gear could be 0 or indicate error. }
- Create the Output Display:
- Connect the output variable
gearto a Display block or a Scope in your Simulink model to visualize the current gear number.
- Connect the output variable
Add Speed Input
- Add a Constant or Slider Block for Speed Input:
- To provide the speed input during the simulation, you can use a Slider Gain block or a Constant block (for initial input) and connect it to the
speedinput of the Stateflow chart.
- To provide the speed input during the simulation, you can use a Slider Gain block or a Constant block (for initial input) and connect it to the
Finalize and Connect Components
- Connect All Components:
- Connect the Slider Gain or Constant block to the Stateflow block input (speed).
- Connect the Stateflow block output (gear) to a Display block to visualize the gear number.
Set Simulation Parameters
- Set Up Simulation Parameters:
- Go to Simulation > Model Configuration Parameters.
- Set the stop time for the simulation to a value that allows you to test gear shifting (e.g., 60 seconds).
Run the Simulation
- Run the Model:
- Click the Run button to start the simulation.
- Adjust the speed using the slider (or observe the output if using a constant) and watch the gear number change according to the speed input.
Saving the Model in Previous Version
Save the Model:
- Go to File > Save As.
- Choose a name for your model.
Convert to Previous Version:
- Go to File > Save As.
- Select Previous Version.
- Choose R2021a or any lower version.