-
Notifications
You must be signed in to change notification settings - Fork 0
/
coppeliaScene.m
88 lines (61 loc) · 2.19 KB
/
coppeliaScene.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function coppeliaScene(block)
setup(block);
%%
function setup(block)
% Register number of ports
block.NumInputPorts = 2;
block.NumOutputPorts = 1;
% Override input port properties
for i = 1:2
block.InputPort(i).Dimensions = 1;
block.InputPort(i).DatatypeID = 0; % double
block.InputPort(i).Complexity = 'Real';
block.InputPort(i).DirectFeedthrough = true;
block.InputPort(i).SamplingMode = 'Sample';
end
% Override output port properties
block.OutputPort(1).Dimensions = 3;
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(1).SamplingMode = 'Sample';
% Register parameters
block.NumDialogPrms = 0;
% Register sample times
block.SampleTimes = [0.05 0];
block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('InitializeConditions', @InitializeConditions);
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Terminate', @Terminate); % Required
%%
function Start(block)
obj.client = RemoteAPIClient();
obj.sim = obj.client.getObject('sim');
obj.client.setStepping(true);
obj.robot = obj.sim.getObject('/PioneerP3DX');
obj.leftMotor = obj.sim.getObject('/PioneerP3DX/leftMotor');
obj.rightMotor = obj.sim.getObject('/PioneerP3DX/rightMotor');
assignin('base','obj',obj);
set_param(block.BlockHandle, 'UserData', obj);
%%
function InitializeConditions(block)
obj = get_param(block.BlockHandle, 'UserData');
obj.sim.startSimulation();
%%
function Outputs(block)
obj = get_param(block.BlockHandle, 'UserData');
u = block.InputPort(1).Data;
omega = block.InputPort(2).Data;
wr = (2*u + 0.331*omega)/0.195;
wl = (2*u - 0.331*omega)/0.195;
obj.sim.setJointTargetVelocity(obj.rightMotor,wr);
obj.sim.setJointTargetVelocity(obj.leftMotor,wl);
obj.client.step();
pos = obj.sim.getObjectPosition(obj.robot,-1);
ori = obj.sim.getObjectOrientation(obj.robot,-1);
[x,y,~] = pos{:}; [~,~,phi] = ori{:}; pose = [x,y,phi];
block.OutputPort(1).Data = double(pose);
%%
function Terminate(block)
obj = get_param(block.blockHandle, 'UserData');
obj.sim.stopSimulation();