-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAction.m
More file actions
executable file
·115 lines (100 loc) · 4.02 KB
/
Copy pathAction.m
File metadata and controls
executable file
·115 lines (100 loc) · 4.02 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
classdef (Abstract) Action < handle
%Action Provides a blueprint to define new actions for Turtlebot3.
% Action class automatically open a new ROSTalker to communicate with
% Turtlebot and already implements control-loop definition and
% management.
% Final user has just to write algorithms to perform intended tasks
% overriding few methods.
properties(SetAccess = private, GetAccess = private)
model %Turtletbot3 object. Refere to a TBBurger or TBWafflePi object.
actionTimer %Timer performing control loop methods.
end
properties(SetAccess = private, GetAccess = protected)
talker %Instance of ROSTalker to communicate with Turtlebot.
end
properties(SetAccess = protected, GetAccess = protected)
loopRate %Execution rate of control loop, in Hz.
meanPeriod %Estimed execution period in seconds.
end
methods(Sealed, Access = protected)
function controlLoop(obj)
%controlLoop Check whether loop condition persists or not. If
% true, a new action execution is performed. Otherwise, control
% loop is broken.
if obj.loop()
obj.execute();
else
obj.stopAction();
end
%if ~isnan(obj.actionTimer.AveragePeriod)
%obj.meanPeriod = 0.9*obj.meanPeriod + 0.1*obj.actionTimer.AveragePeriod;
%end
if ~isnan(obj.actionTimer.AveragePeriod)
obj.meanPeriod = obj.actionTimer.AveragePeriod;
end
end
end
methods
function obj = Action(loopRate, model)
%Action Create a new Action instance. Initialize ROSTalker
%object and control-loop timer.
arguments
loopRate (1, 1) {mustBePositive, mustBeInteger} = 2;
model (1, 1) {mustBeA(model, "Turtlebot3")} = TBBurger();
end
obj.loopRate = loopRate;
obj.model = model;
obj.talker = obj.model.createTalker();
end
function preAction(obj)
%preAction Executes, eventually, preliminary operations
%before control-loop starts.
end
function postAction(obj)
%postAction Executes, eventually, post-processing operations.
end
end
methods(Sealed)
function startAction(obj)
%Use MATLAB timer. Not fully tested.
%startAction Fires loop timer.
obj.meanPeriod = 1/obj.loopRate;
obj.actionTimer = timer( ...
'TimerFcn', @(~, ~) obj.controlLoop(), ...
'ExecutionMode', 'fixedRate', ...
'Period', 1/obj.loopRate ...
);
obj.actionTimer.StartFcn = {@(~, ~) obj.preAction()};
obj.actionTimer.StopFcn = {@(~, ~) obj.postAction()};
obj.actionTimer.start();
end
function stopAction(obj)
%stopAction Invoked when loop condition is no more valid. Stops
%control-loop timer.
obj.actionTimer.stop();
tau = obj.actionTimer.AveragePeriod;
disp("Action stoppped.\nAverage period: ", string(tau), "'");
obj.actionTimer.delete();
end
function startWithRate(obj)
%Commonly used.
r = rosrate(obj.loopRate);
obj.meanPeriod = 1/obj.loopRate;
obj.preAction();
while obj.loop()
obj.execute();
waitfor(r)
%obj.meanPeriod = 0.9*obj.meanPeriod+0.1*r.LastPeriod;
stats = statistics(r);
obj.meanPeriod = stats.AveragePeriod;
end
disp('Averaged execution period: ')
disp(obj.meanPeriod)
obj.postAction();
end
end
methods(Abstract)
execute(obj);
check = loop(obj);
end
end