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

조회 수: 18 (최근 30일)
Farzad Torabi
Farzad Torabi 2022년 11월 25일
댓글: Matt J 2022년 11월 28일
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.
  댓글 수: 2
Farzad Torabi
Farzad Torabi 2022년 11월 28일
Thank you Soooooo much! it workeddddd!!!
Matt J
Matt J 2022년 11월 28일
Thank you Soooooo much! it workeddddd!!!
If so, please Accept-click the answer.

댓글을 달려면 로그인하십시오.

추가 답변 (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