Issue with defining inputs in classdef and getting properties updated

7 visualizaciones (últimos 30 días)
I am defining a simple DC motor (the technical background is not necessary to understand my question) using the class file attached. I have followed this simple instruction to create this class file and it works. However, currently I have to define the inputs as I call the class file like this:
motor = VirtualMotor(5,10) % input 5V for 10 seconds (with 0.01 time step defined inside the class file)
motor =
VirtualMotor with properties:
voltage: 5
current: []
angle: []
velocity: []
time: 10
and then
[position,velocity,current] = motor.input;
to get my outputs. However, this does not result change in the properties of VirtualMotor, even though I was hoping to get this after I run the above line:
>> motor =
VirtualMotor with properties:
voltage: [1001x1 double] % 0 to 10 seconds with 0.01 second time step and thus 1001
current: [1001x1 double]
angle: [1001x1 double]
velocity: [1001x1 double]
time: [1001x1 double]
I want to achieve the same results with the following calls (while having my result stored in the motor properties as mentioned above):
>> motor = VirtualMotor();
>> [position,velocity,current] = motor.input(5,10);
Your help is highly appreciated as I am totally lost for now. Thanks in advance!

Respuesta aceptada

per isakson
per isakson el 11 de Ag. de 2020
Editada: per isakson el 11 de Ag. de 2020
Two things:
VirtualMotor is a value class. Is that your intention? See Comparison of Handle and Value Classes
You don't want to return these columns to the caller, you want to assign the values to properties. I modified the following statements of the method, input
function self = input(self)
self.angle = y(:,1); % awgn(y(:,1),0.01);
self.velocity = y(:,2); % awgn(y(:,2),0.1);
self.current = y(:,3); % awgn(y(:,3),1e-6);
Now
%%
motor = VirtualMotor(5,10);
motor = motor.input
outputs
motor =
VirtualMotor with properties:
voltage: 5
current: [1001×1 double]
angle: [1001×1 double]
velocity: [1001×1 double]
time: 10
  3 comentarios
per isakson
per isakson el 11 de Ag. de 2020
Change three lines of the method, input
function self = input(self, T, V )
t = 0:0.01:T;
volt = V*ones(1,length(t));
and possibly remove the constructor (or its input arguments)

Iniciar sesión para comentar.

Más respuestas (1)

Tommy
Tommy el 11 de Ag. de 2020
For your second line:
[position,velocity,current] = motor.input(5,10);
If you want input() to accept values for the voltage and time, you can define it like so:
function [angle,velocity,current] = input(self, voltage, time)
J = 2e-05; % moment of inertia of the rotor kg.m^2/s^2
b = 1e-06; % motor viscous friction constant N.m.s
K = 0.034; % motor torque constant N.m/Amp
R = 8.4; % Ohms
L = 0.018; % Henrys
A = [0 1 0;
0 -b/J K/J;
0 -K/L -R/L];
B = [0;
0;
1/L];
C = eye(3);
D = zeros(3,1);
sys = ss(A,B,C,D);
t = 0:0.01:time;
volt = voltage*ones(1,length(t));
y = lsim(sys,volt,t);
angle = y(:,1); % awgn(y(:,1),0.01);
velocity = y(:,2); % awgn(y(:,2),0.1);
current = y(:,3); % awgn(y(:,3),1e-6);
% Update the object's properties:
self.voltage = voltage;
self.time = time;
self.angle = angle;
self.velocity = velocity;
self.current = current;
end
However, note that MATLAB classes are value classes by default. I believe, to have properties of your motor object updated by a call to input(), then the function input() needs to return self, and you need to store that output into motor (thereby replacing the old motor object with the new, updated object). Something like this:
function [angle,velocity,current,self] = input(self, voltage, time)
...
end
and then
[position,velocity,current,motor] = motor.input(5,10);
Alternatively, you could work with a handle class:
classdef VirtualMotor < handle
...
end
Then, when you pass your object into a function like input(), it's properties will be updated with no requirement to explicitly pass the object back out of the function into the calling workspace.
  1 comentario
J AI
J AI el 11 de Ag. de 2020
Thanks a lot for your effort. I wish there was a way to accept two answers, but unfortunately only one can be accepted. The other answer is more precise and thus I accepted that. But hey, it's about helping the community right? so thanks once again for your brilliant effort!

Iniciar sesión para comentar.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by