MATLAB Answers

Issue with defining inputs in classdef and getting properties updated

2 views (last 30 days)
J AI
J AI on 10 Aug 2020
Commented: J AI on 11 Aug 2020
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!

  0 Comments

Sign in to comment.

Accepted Answer

per isakson
per isakson on 11 Aug 2020
Edited: per isakson on 11 Aug 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 Comments

J AI
J AI on 11 Aug 2020
This solves the problem of "assigning" values to the properties. Thanks!
Is there a way to do the same using:
motor = VirtualMotor();
motor = motor.input(5,10)
?
per isakson
per isakson on 11 Aug 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)

Sign in to comment.

More Answers (1)

Tommy
Tommy on 11 Aug 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 Comment

J AI
J AI on 11 Aug 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!

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!