Challenge

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;
    • Power On:
      • Transition to Check Water Supply.
    • Check Water Supply:
      • If water_supply == 1 then transition to Soaking.
      • Else transition to Inactive and activate the water supply LED: LED_water_supply = 1;
    • Soaking:
      • On Entry: duration = 200;
    • Washing:
      • On Entry: duration = 100;
    • Rinsing:
      • On Entry: duration = 20;
    • Drying:
      • On Entry: duration = 50;
    • Finished:
      • On Entry: LED_finished = 1;

Connecting Outputs to Simulink

  1. 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

  1. 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).
  2. 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

  1. Open Simulink and Create a New Model:

    • Launch MATLAB and open Simulink.
    • Go to File > New > Model.
  2. Add a Stateflow Chart:

    • From the Simulink Library Browser, drag a Stateflow block into your model.
  3. Open the Stateflow Chart:

    • Double-click the Stateflow block to open the Stateflow editor.

Define States and Logic

  1. 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.
  2. Add Input and Output:

    • Create an input variable speed to take the speed input.
    • Create an output variable gear to display the gear number.

Implement Gear Shifting Logic

  1. 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. }

  1. Create the Output Display:
    • Connect the output variable gear to a Display block or a Scope in your Simulink model to visualize the current gear number.

Add Speed Input

  1. 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 speed input of the Stateflow chart.

Finalize and Connect Components

  1. 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

  1. 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

  1. 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

  1. Save the Model:

    • Go to File > Save As.
    • Choose a name for your model.
  2. Convert to Previous Version:

    • Go to File > Save As.
    • Select Previous Version.
    • Choose R2021a or any lower version.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.