Search code examples
matlabsimulationsimulinkrobotics

Dynamically configure my simulation (matlab-simulink)


I want to create 3 different controllers for the same system. I want to simulate them all in the same environment, and run each simulation with a different controller, or with 2 of the controllers and change mid simulation.

What i want to achieve is the following (without necessarily the switch case block), where some logic would replace the constant block:

enter image description here

I tried using a switch case block, however, my simulation fails because the sample time of the switch case block is different from the sample times in other components (derivatives for continuous PID, discrete PID, filters for continuous PID).

How can configure my simulation dynamically to use different controllers ( differenct blocks)?


Solution

  • What i ended up doing is the following:

    1: I used a multiport switch. The switch doesn't rely on sampling time, so there are no conflicts between PID sampling times and controller switching.

    2: I have a timeseries signal to activate the controller i want. The multiport switch accepts a integer signal, and it fails otherwise. To achieve that, the timeseries signal is constructed with the following method:

    Select_Controller = [0, 2;Tchange - Tsampling 2;Tchange 1  ];;
    

    and then this signal is passed from a ZOH block with sampling rate Tsampling. Thus the signal is always an integer value.

    This is the simulink diagram: enter image description here

    which results on those signals: enter image description here

    The command was:

    Select_Controller = [0, 2;(3 - 1e-3) 2;3 1  ];;