Simulink add vector as input to function block

146 views (last 30 days)
Kelvin Pagels on 18 Apr 2016
Commented: Stefan Raab on 19 Apr 2016
I have been looking for some time and I havent been able to find a solution to my problem yet.
Let me try to explain my problem and hopefully you guys can give me some help on how to solve this.
So I have a function:
function opTheta = funcDronessim(i,x_drone,y_drone,theta_gs,theta_d)
Which takes a position x_drone,y_drone and two angles theta_gs,theta_d as input.
I want to make this in simulink and not as a matlab script.
In a matlab script I would have written:
x_drone = 1:1:100; % The position X of the DRON
y_drone = 50*ones(1,length(x_drone)); % The position Y of the DRONE
for i = 1:1:100
function opTheta = funcDronessim(i,x_drone(i),y_drone(i),theta_gs,theta_d)
theta_d = -pi + opTheta;
theta_gs = opTheta;
end
And this way I can update the theta_d and theta_gs which is the only ones I want to update right now.
My question is: How can I do this in Simulink?
What did I try? I tried to add a MATLAB function block and put the function funcDronessim in there.
A screenshot of the simulink is here: simulink block
Now my problem comes, since I have no idea how to make a "for i:1:100" in simulink and make the following code in simulink:
x_drone = 1:1:100; % The position X of the DRON
y_drone = 50*ones(1,length(x_drone)); % The position Y of the DRONE
I tried to use a constant block and add 1:1:100 in it, but it doesnt give what I expected. Then I tried to make a vector (x_drone = 1:1:100) in a matlab script and use "from workspace" and in simulink "to workspace". But I think I dont really understand how this works, because it is making a really big matrix and not the vector I am expecting.
Do you have any idea of how this can be solved? I am totally lost and I have been using so many hours on this problem now and I assume its a really simple fix.
0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Answers (1)

Stefan Raab on 18 Apr 2016
Hello,
at first I think that the syntax of your MATLAB code is wrong. It seems that you want to define a function in a for-loop which is not possible. I assume that you just want to call this function. In this case you need to remove the "function" before opTheta. Also your MATLAB code does not define any initial values for "theta_d" and "theta_gs" which would cause an error in the first iteration.
The problem with your Simulink model is, that you are assigning a vector with dimension (1,100) to x_drone and Simulink does not "loop" through this vector but uses it as a constant with the assigned values. Therefore the MATLAB function is called with this whole vector rather than just one element out of this as you want. You could use a "Simulink/Ports & Subsystems/For Iterator Subsystem", which makes an amount of iteration for each timestep. This amount can be specified by you. In the following pictures I built the architecture as I think should work:
And inside the subsystem:
But note that this subsystem is called "N" times in each timestep, so actually you would only need one simulation time step. As there are no continuos states inside this model so far, you could use a fixed-time step solver and set the timestep equal to the end time of the simulation (so that there is only one timestep simulated). Otherwise you can also double click the "For Iterator" block and set "States when starting" to "reset". Then the output of the "For-Iterator-Subsystem" should be equal in each timestep and the index of the elements of the resulting vector are the results from the function call at the corresponding iterations.
Note that you can specify the initial conditions for theta_d and theta_gs in the corresponding "Unit Delay" blocks.
I hope this might help you.
Kind regards, Stefan
4 CommentsShow 2 older commentsHide 2 older comments
Kelvin Pagels on 19 Apr 2016
The idea is that the function
funcDronessim(i,x_drone(i),y_drone(i),theta_gs,theta_d)
Takes a moving drones position and the angle of an antenna on the ground and on the drone. The angle (antenna) on the drone/ground station is the ones we want to control.
So the antennas are two motors and we control the motors with a pid controller and the output of those controllers goes into the function.
Does it make sense?
So the motors are controlled by two pid controllers and they go into the function. The feedback from the function to the controllers are the opTheta (optimal angle).
Stefan Raab on 19 Apr 2016
Okay, first of all the vectors for x_drone and y_drone are then no vectors but scalar signals which might change over time. You would have to specify a source or a progress to these signals as you desire. For example with a constant block or a ramp block or anything else. Secondly, how I see it, is that you would need to remodel the MATLAB function in Simulink. Right now I only see these two desired angles going into this function, but there is no input with which the pid could control the motor.
At this point I would suggest to look at some tutorials on how to simulate dynamic systems in Simulink and how to build control structures with PIDs. There are loads of tutorials, here is a link:
Kind regards, Stefan

Sign in to comment.

Categories

Find more on Tuning, Analysis, and Validation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!