How to make a method a global variable in a class?

13 次查看(过去 30 天)
Hi All
I have to define an object, that calls a method, to connect Matlab to another software. When I define it in Matlab, I do it under one function but I have to use it also under another function in that class. but I get the error that dot indexing is not allowed.
classdef VrepConnectorZMQ
properties(Access = public)
sim; %Similar to fd
client; %Used for server connection and server requests
robot_joints = [] %List of joint handles
%Integration step used for simulation
joint_pos=[];
end
methods(Access = public)
function obj = VrepConnectorZMQ()
addpath("C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\programming\zmqRemoteApi\clients\matlab")
javaaddpath('C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\programming\zmqRemoteApi\clients\matlab\jeromq.jar')
% addpath vrep_lib/; %Adding the APIs to the path
client = RemoteAPIClient();
client.setStepping(true);
obj.sim = client.getObject('sim'); %RemoteAPI object
obj.sim.startSimulation()
for i = 1:7
obj.robot_joints(i) = obj.sim.getObject(strcat('/LBR_iiwa_14_R820_joint',int2str(i)));
end
for i = 1:7
obj.joint_pos(i) = obj.sim.getJointTargetPosition(obj.robot_joints(i));
end
% When simulation is not running, ZMQ message handling could be a bit
% slow, since the idle loop runs at 8 Hz by default. So let's make
% sure that the idle loop runs at full speed for this program:
% defaultIdleFps = sim.getInt32Param(sim.intparam_idle_fps);
% sim.setInt32Param(sim.intparam_idle_fps, 0);
end
and the other function under this class is :
function ApplyControl(obj, u,steptime)
startTime = obj.sim.getSimulationTime();
t = startTime;
while t<steptime
obj.client.step();
for i = 1:7
obj.sim.setJointTargetVelocity(obj.robot_joints(i), u(i));
end
t = obj.sim.getSimulationTime();
% for i = 1:(delta_t/obj.step_time_vrep) %Number of integrations in delta_t
% obj.sim.simxSynchronousTrigger(obj.clientID); %Triggering the integration
% % To overcome delay in values according to (Remote API modus operandi) document
% end
% obj.sim.simxGetPingTime(obj.clientID); %Synchronizing
end
I can not use obj.client.step() in the above function, unless I make sim and client global. How do I do that?

采纳的回答

chrisw23
chrisw23 2022-11-28
You can't access 'obj.client' in function 'ApplyControl' unless you save it to your declared property.
--> change your constructor to obj.client = RemoteAPIClient() as you do with your sim property
It's not recommended to use the same name for properties an local variables.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by