MATLAB Answers

Index in position 1 is invalid PID Controller

15 views (last 30 days)
Tom Hart
Tom Hart on 6 Dec 2020
Edited: Stephan on 7 Dec 2020
Hi,
I think I'm having a syntax error for my tf, it's not working for some reason when I run it.
% set up motor with initial conditions and properties
motor.timeConstant = 5/60; % [min]
motor.gain = 0.2; % [rpm/V]
% transfer function for the motor
TFmotor_OL = tf([0 motor.gain],[motor.timeConstant 1]);
% set up PID
PID.Pgain = 4; % proportional gain
PID.Igain = 80; % internal gain
PID.Dgain = 1; % derivative gain
% create the controller
controller = pid(PID.Pgain, PID.Igain, PID.Dgain);
% connect the controller and motor
TFmotor_CL = feedback(controller*TFmotor_OL, 1);
% plot open-loop response
step(TFmotor_OL, 2)
% add time constant for derivative
tf = 0;
% plot closed-loop response
hold on % keep open-loop plot
step(TFmotor_CL, 2) % plot closed-loop response
legend('show') % show legend
I've also tried using this and it still doesn't work.
% transfer function for the motor
s = tf('s');
TFmotor_OL = motor.gain / (motor.timeConstant*s +1);
if you could help that would be great :)
  1 Comment
Tom Hart
Tom Hart on 7 Dec 2020
Update on this. If I close Matlab and run the script it works, but then when i try to run it a second time it wont work.
Anyone know why?

Sign in to comment.

Answers (1)

Stephan
Stephan on 7 Dec 2020
Edited: Stephan on 7 Dec 2020
When you run this the first time you first call the inbuilt function tf and then later you create a variable, which is named tf also:
TFmotor_OL = tf([0 motor.gain],[motor.timeConstant 1]);
.
.
.
tf = 0;
Since you not clear the workspace, the next time you run the function the call of tf with brackets will be interpreted as the indexed call of the variable tf, not the function that you want to call.
To avoid this you should rename your variable. It is never a good idea to name variables or scripts like inbuilt functions.

Community Treasure Hunt

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

Start Hunting!

Translated by