-
Notifications
You must be signed in to change notification settings - Fork 4
/
TebPlanner.m
executable file
·974 lines (768 loc) · 31.7 KB
/
TebPlanner.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% TebPlanner.m %
% ============ %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Repository: %
% https://github.com/TUC-ProAut/ros_teb_planner %
% %
% Chair of Automation Technology, Technische Universität Chemnitz %
% https://www.tu-chemnitz.de/etit/proaut %
% %
% Authors: %
% Bhakti Danve, Peter Weissig %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% New BSD License %
% %
% Copyright (c) 2019-2021 TU Chemnitz %
% All rights reserved. %
% %
% Redistribution and use in source and binary forms, with or without %
% modification, are permitted provided that the following conditions are met: %
% * Redistributions of source code must retain the above copyright notice, %
% this list of conditions and the following disclaimer. %
% * Redistributions in binary form must reproduce the above copyright %
% notice, this list of conditions and the following disclaimer in the %
% documentation and/or other materials provided with the distribution. %
% * Neither the name of the copyright holder nor the names of its %
% contributors may be used to endorse or promote products derived from %
% this software without specific prior written permission. %
% %
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED %
% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR %
% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR %
% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, %
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, %
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; %
% OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, %
% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR %
% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF %
% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Interface-class to access ROS TEB-Planner
classdef TebPlanner < handle
properties
maxTimeOut = 60; % maximum time for planning in seconds
end
properties (SetAccess = private)
startPose struct; % [x,y, theta]
goalPose struct; % [x,y, theta]
initialPlan struct; % n*[x,y, theta]
startVelocity struct; % [vx,vy, omega]
circularObstacles struct; % n*[x,y, radius, vx,vy]
polylineObstacles struct; % n*[points=m*[x,y] , vx,vy]
polygonObstacles struct; % n*[points=m*[x,y] , vx,vy]
waypoints struct; % n*[x,y]
hasNewResponse = false; % flag for new received msg
% will be set on response (service or topic)
% will be reset on access (e.g. full feedback, poses, ...)
storeNextTopicResponse = false; % this flag can have different types
% 'never' ... do never store next response
% false ... do not store next response
% true ... store next response only
% 2,3,... ... store next 2,3,... responses only
% 'always' ... store all received responses
end
properties (Access = private)
latestMsg = []; % latest received ros-msg
end
% constructor & destructor
methods
function obj = TebPlanner()
%Construct an instance of this class
% initialize start & goal pose
obj.setStartPose();
obj.setGoalPose(2, 0);
obj.setStartVelocity();
% initialize empty structs to show fields
obj.clearInitialPlan();
obj.clearWaypoints();
obj.clearObstacles();
end
end
% planning
methods
% do the planning
function result = plan(obj)
% init result and internal variables
result = false;
obj.latestMsg = [];
client = obj.getRosService(obj.maxTimeOut);
% fill message with content
request = rosmessage(client);
request.Request = obj.getRequestMsg();
% call the service
response = call(client,request,'Timeout', obj.maxTimeOut);
% check result
if (isempty(response))
return
end
% store response
obj.latestMsg = response.Respond;
obj.hasNewResponse = true;
result = true;
end
function result = plan_using_topics(obj)
% init result
result = false;
% enable reception of next message
obj.incTopicResponseCount()
% create publisher & the message
pub = obj.getRosPublisher();
% fill message with content
msg = obj.getRequestMsg();
% trigger planning
pub.send(msg);
% wait for response, if result is requested
if (nargout > 0)
result = obj.waitForNewResponse();
end
end
% do the re-planning
function result = replan(obj)
% init result and internal variables
result = false;
obj.latestMsg = [];
client = obj.getRosReplanService(obj.maxTimeOut);
% fill message with no content
request = rosmessage(client);
% call the service
response = call(client,request,'Timeout', obj.maxTimeOut);
% check result
if (isempty(response))
return
end
% store response
obj.latestMsg = response.Respond;
obj.hasNewResponse = true;
result = true;
end
function result = replan_using_topics(obj)
% init result
result = false;
% enable reception of next message
obj.incTopicResponseCount()
% create publisher & the message
pub = obj.getRosReplanPublisher();
% fill message with content
msg = rosmessage('std_msgs/Empty');
% trigger planning
pub.send(msg);
% wait for response, if result is requested
if (nargout > 0)
result = obj.waitForNewResponse();
end
end
% change response mode
function setTopicResponseMode(obj, mode)
% check parameters
if (nargin < 2)
mode = true; % one-time response
end
% check if variable is a number
if (isnumeric(mode))
if ((mode < -2) || (mode == 0))
mode = false;
elseif (mode == -2)
mode = 'never';
elseif (mode == -1)
mode = 'always';
elseif (mode == 1)
mode = true;
end
elseif (~islogical(mode) && ...
~strcmp(mode, 'always') && ~strcmp(mode, 'never'))
warning('unknown value(-type) for mode %s', mode)
return
end
% store current value
obj.storeNextTopicResponse = mode;
% init subscriber, if necessary
sub = obj.getRosSubscriber();
% check if response is enabled
if (~islogical(mode) || mode)
if (isempty(sub.NewMessageFcn))
sub.NewMessageFcn = @(src, msg) obj.rosCallback(src,msg);
end
else
if (~isempty(sub.NewMessageFcn))
sub.NewMessageFcn = [];
end
end
end
end
methods (Access = private)
function count = getTopicResponseCount(obj)
% init result
count = 0;
% get current value
value = obj.storeNextTopicResponse;
% check if variable is bool
if (islogical(value))
if (value)
count = 1; % one-time response
else
count = 0; % always off
end
return
end
% check if variable is equal to 'always' or 'never'
if (strcmp(value, 'never'))
count = -2; % never
return
end
if (strcmp(value, 'always'))
count = -1; % always on
return
end
% check if variable is a number
if (isnumeric(value))
count = value;
return
end
end
function incTopicResponseCount(obj)
% load current response count
count = obj.getTopicResponseCount();
% increment count
if (count >= 0)
count = count + 1;
end
% store current count (activates/deactivates the receiver)
obj.setTopicResponseMode(count)
end
function active = decTopicResponseCount(obj)
% init result
active = false;
% load current response count
count = obj.getTopicResponseCount();
% deactivate response & return, if disabled
if ((count == 0) || (count <= -2))
obj.setTopicResponseMode(false)
return
end
% decrement count
if (count > 0)
count = count - 1;
end
% store current count (activates/deactivates the receiver)
obj.setTopicResponseMode(count)
active = true;
end
function rosCallback(obj, ~, data)
if (obj.decTopicResponseCount())
% store message
obj.latestMsg = data;
obj.hasNewResponse = true;
end
end
end
% data required for TEB-Planning
methods
% Sets the starting pose for the teb-planner
% (theta is measured in radians)
function pose = setStartPose(obj, x, y, theta)
if (nargin < 4); theta = 0; end
if (nargin < 3); y = 0; end
if (nargin < 2); x = 0; end
pose = struct( ...
'x' , x(1), ...
'y' , y(1), ...
'theta', theta(1));
obj.startPose = pose;
end
% Gets the starting pose for the teb-planner
function out = getStartPose(obj)
out = obj.startPose;
end
% Sets the goal pose for the teb-planner
% (theta is measured in radians)
function pose = setGoalPose(obj, x, y, theta)
if (nargin < 4); theta = 0; end
pose = struct( ...
'x' , x(1), ...
'y' , y(1), ...
'theta', theta(1));
obj.goalPose = pose;
end
% Gets the goal pose for the teb-planner
function out = getGoalPose(obj)
out = obj.goalPose;
end
% Sets the start velocity for the teb-planner robot
% Function Parameters:
% vx - velocity component in x-direction
% vy - velocity component in y-direction
% omega - angular velocity
% (measured in radians per second)
function velocity = setStartVelocity(obj, vx, vy, omega)
if (nargin < 4); omega = 0; end
if (nargin < 3); vy = 0; end
if (nargin < 2); vx = 0; end
velocity = struct( ...
'vx' , vx(1), ...
'vy' , vy(1), ...
'omega', omega(1));
obj.startVelocity = velocity;
end
% Get start velocity of the robot
function out = getStartVelocity(obj)
out = obj.startVelocity;
end
% Add initial plan
% Function Parameters:
% poses - [x1,y1,theta1; x2,y2,theta2; ...; xn,yn,thetan]
function initial_plan = setInitialPlan(obj, poses)
if (size(poses, 2) < 3); poses(1,3) = 0; end
obj.clearInitialPlan();
initial_plan = obj.initialPlan;
for i = 1:size(poses, 1)
initial_plan(i).x = poses(i,1);
initial_plan(i).y = poses(i,2);
initial_plan(i).theta = poses(i,3);
end
obj.initialPlan = initial_plan;
end
% Get the initial plan added for the robot
function out = getInitialPlan(obj)
out = obj.initialPlan;
end
% Delete initial plan
function clearInitialPlan(obj)
obj.initialPlan = struct( ...
'x' , {}, ...
'y' , {}, ...
'theta', {});
end
% Adds circular obstacle
% Function Parameters:
% position - [x,y] position of the circular obstacle
% velocity - [vx,vy] components of obstacle velocity
% radius - radius of circular obstacle
function obstacle = addCircularObstacle(obj, ...
position, velocity, radius)
if (nargin < 4); radius = 0; end
if (nargin < 3); velocity = [0, 0]; end % vx, vy
if (length(velocity) < 2); velocity(2) = 0; end
if (length(position) < 2); position(2) = 0; end
obstacle = struct( ...
'x' , position(1), ...
'y' , position(2), ...
'radius' , radius(1), ...
'vx' , velocity(1), ...
'vy' , velocity(2));
if (isempty(obj.circularObstacles))
obj.circularObstacles = obstacle;
else
obj.circularObstacles(end + 1) = obstacle;
end
end
% Gets the circular obstacles present in the scenario
function out = getCircularObstacles(obj)
out = obj.circularObstacles;
end
% Deletes all circular obstacles
function clearCircularObstacles(obj)
obj.circularObstacles = struct( ...
'x' , {}, ...
'y' , {}, ...
'radius' , {}, ...
'vx' , {}, ...
'vy' , {});
end
% Adds polyline obstacles;
% Function Parameters:
% points - should contain min. two 2-D points
% for generating polyline
% e.g. [x1,y1; x2,y2; x3,y3]
% velocity - [vx,vy] components of obstacle velocity
function obstacle = addPolylineObstacle(obj, points, velocity)
if (nargin < 3); velocity = [0,0]; end
if (length(velocity) < 2); velocity(2) = 0; end
assert(size(points,1) >= 2, ...
'PolylineObstacle need at least 2 points');
obstacle = struct( ...
'points', struct('x', {}, 'y', {}), ...
'vx' , velocity(1), ...
'vy' , velocity(2));
for i = 1:size(points, 1)
obstacle.points(i).x = points(i,1);
obstacle.points(i).y = points(i,2);
end
if (isempty(obj.polylineObstacles))
obj.polylineObstacles = obstacle;
else
obj.polylineObstacles(end + 1) = obstacle;
end
end
% Gets the polyline obstacles present in the scenario
function out = getPolylineObstacles(obj)
out = obj.polylineObstacles;
end
% Deletes all polyline obstacles
function clearPolylineObstacles(obj)
obj.polylineObstacles = struct( ...
'points', {}, ...
'vx' , {}, ...
'vy' , {});
end
% Adds polygon obstacles;
% Function Parameters:
% points - should contain min. three 2-D points
% for generating a polygon
% e.g. [x1,y1; x2,y2; x3,y3; x4,y4]
% velocity - [vx,vy] components of obstacle velocity
function obstacle = addPolygonObstacle(obj,points,velocity)
if (nargin < 3); velocity = [0,0]; end
if (length(velocity) < 2); velocity(2) = 0; end
assert(size(points,1) >= 3, ...
'PolygonObstacle need at least 3 points');
obstacle = struct( ...
'points', struct('x', {}, 'y', {}), ...
'vx' , velocity(1), ...
'vy' , velocity(2));
for i = 1:size(points, 1)
obstacle.points(i).x = points(i,1);
obstacle.points(i).y = points(i,2);
end
if (isempty(obj.polygonObstacles))
obj.polygonObstacles = obstacle;
else
obj.polygonObstacles(end + 1) = obstacle;
end
end
% Gets the polygon obstacles present in the scenario
function out = getPolygonObstacles(obj)
out = obj.polygonObstacles;
end
% Deletes all polygon obstacles
function clearPolygonObstacles(obj)
obj.polygonObstacles = struct( ...
'points', {}, ...
'vx' , {}, ...
'vy' , {});
end
% Deletes all obstacles
function clearObstacles(obj)
obj.clearCircularObstacles();
obj.clearPolylineObstacles();
obj.clearPolygonObstacles();
end
% Adds one way-point to the trajectory
function addWaypoint(obj, x, y)
way_point = struct(...
'x', x(1), ...
'y', y(1));
if (isempty(obj.waypoints))
obj.waypoints = way_point;
else
obj.waypoints(end + 1) = way_point;
end
end
% Gets all way-points added to generate trajectory
function out = getWaypoints(obj)
out = obj.waypoints;
end
% Delete all way-points
function clearWaypoints(obj)
obj.waypoints = struct(...
'x', {}, ...
'y', {});
end
end
% access ros messages
methods
% get request msg
function out = getRequestMsg(obj)
% create request message
topic_type = 'teb_planner_pa_msgs/Request';
msg = rosmessage(topic_type);
% fill starting pose
msg.Start.Position.X = obj.startPose.x;
msg.Start.Position.Y = obj.startPose.y;
temp = eul2quat([obj.startPose.theta, 0, 0]);
msg.Start.Orientation.W = temp(1);
msg.Start.Orientation.X = temp(2);
msg.Start.Orientation.Y = temp(3);
msg.Start.Orientation.Z = temp(4);
% fill goal pose
msg.Goal.Position.X = obj.goalPose.x;
msg.Goal.Position.Y = obj.goalPose.y;
temp = eul2quat([obj.goalPose.theta, 0, 0]);
msg.Goal.Orientation.W = temp(1);
msg.Goal.Orientation.X = temp(2);
msg.Goal.Orientation.Y = temp(3);
msg.Goal.Orientation.Z = temp(4);
if (~isempty(obj.initialPlan))
% fill initial plan
for i = 1:length(obj.initialPlan)
pose_stamped = rosmessage('geometry_msgs/PoseStamped');
% set frame id
pose_stamped.Header.FrameId = 'odom';
% set point
pose_stamped.Pose.Position.X = obj.initialPlan(i).x;
pose_stamped.Pose.Position.Y = obj.initialPlan(i).y;
% set Orientation
temp = eul2quat([obj.initialPlan(i).theta, 0, 0]);
pose_stamped.Pose.Orientation.W = temp(1);
pose_stamped.Pose.Orientation.X = temp(2);
pose_stamped.Pose.Orientation.Y = temp(3);
pose_stamped.Pose.Orientation.Z = temp(4);
msg.InitialPlan.Poses(i) = pose_stamped;
end
end
% set start velocity
msg.StartVel.Linear.X = obj.startVelocity.vx;
msg.StartVel.Linear.Y = obj.startVelocity.vy;
msg.StartVel.Angular.Z = obj.startVelocity.omega;
% Creating point or circular obstacles
for i = 1:length(obj.circularObstacles)
obst = rosmessage('costmap_converter/ObstacleMsg');
obst.Radius = obj.circularObstacles(i).radius;
% add single point
point = rosmessage('geometry_msgs/Point32');
point.X = obj.circularObstacles(i).x;
point.Y = obj.circularObstacles(i).y;
obst.Polygon.Points = point;
% add velocity
vel = rosmessage('geometry_msgs/TwistWithCovariance');
vel.Twist.Linear.X = obj.circularObstacles(i).vx;
vel.Twist.Linear.Y = obj.circularObstacles(i).vy;
obst.Velocities = vel;
% add obst to message
msg.Obstacles.Obstacles(end + 1) = obst;
end
% Creating polyline obstacles
for i = 1:length(obj.polylineObstacles)
% add dimensions
for j = 1:length(obj.polylineObstacles(i).points) - 1
polyl_obst = rosmessage('costmap_converter/ObstacleMsg');
% point 1
point1 = rosmessage('geometry_msgs/Point32');
point1.X = obj.polylineObstacles(i).points(j).x;
point1.Y = obj.polylineObstacles(i).points(j).y;
% point 2
point2 = rosmessage('geometry_msgs/Point32');
point2.X = obj.polylineObstacles(i).points(j+1).x;
point2.Y = obj.polylineObstacles(i).points(j+1).y;
% add points
polyl_obst.Polygon.Points = point1;
polyl_obst.Polygon.Points(2) = point2;
% add message
msg.Obstacles.Obstacles(end + 1) = polyl_obst;
end
end
% Creating polygon obstacles
for i = 1:length(obj.polygonObstacles)
polygon_obst = rosmessage('costmap_converter/ObstacleMsg');
% add dimensions
for j = 1:length(obj.polygonObstacles(i).points)
point(j) = rosmessage('geometry_msgs/Point32');
point(j).X = obj.polygonObstacles(i).points(j).x;
point(j).Y = obj.polygonObstacles(i).points(j).y;
polygon_obst.Polygon.Points(j) = point(j);
end
% add velocity
vel = rosmessage('geometry_msgs/TwistWithCovariance');
vel.Twist.Linear.X = obj.polygonObstacles(i).vx;
vel.Twist.Linear.Y = obj.polygonObstacles(i).vy;
polygon_obst.Velocities = vel;
msg.Obstacles.Obstacles(end + 1) = polygon_obst;
end
% Add waypoint
for i = 1:length(obj.waypoints)
pose_stamped = rosmessage('geometry_msgs/PoseStamped');
pose_stamped.Pose.Position.X = obj.waypoints(i).x;
pose_stamped.Pose.Position.Y = obj.waypoints(i).y;
msg.Waypoints.Poses(end + 1)= pose_stamped;
end
% return message
out = msg;
end
% wait for response msg
function result = waitForNewResponse(obj)
% init result
result = false;
% otherwise, wait for response
start_time = datetime();
while true
if (obj.hasNewResponse)
result = true;
return
end
count = obj.getTopicResponseCount();
if ((count <= -2) || (count == 0))
result = false;
return
end
if (seconds(datetime() - start_time) >= obj.maxTimeOut)
warning('timeout using TEB-Planner');
return
end
drawnow('limitrate');
pause(0.01);
end
end
% get response msg
function out = getResultMsg(obj)
% reset internal flag, because latest msg will be accessed
obj.hasNewResponse = false;
% return latest message
out = obj.latestMsg;
end
% get content of latest response msg(Poses)
function out = getResultPoses(obj)
% load latest message
msg = obj.getResultMsg();
% check if latest message is not empty
if (isempty(msg))
fprintf(['Can''t access poses - ', ...
'latest message is empty :-(']);
return
end
% load poses
p = msg.Poses.Poses;
poses = zeros(length(p), 3);
if (~isempty(p))
for i = 1:length(p)
poses(i,1) = p(i).Position.X;
poses(i,2) = p(i).Position.Y;
temp = p(i).Orientation;
temp = [temp.W, temp.X, temp.Y, temp.Z];
poses(i,3) = quat2angle(temp);
end
end
out = poses;
end
% get content of latest response msg(Feedback)
function [out, all] = getResultFeedback(obj, msg)
% init result
out = [];
all = [];
if ((nargin < 2) || isempty(msg))
% load latest message
msg = obj.getResultMsg();
% check if latest message is not empty
if (isempty(msg))
fprintf(['Can''t access feedback - ', ...
'latest message is empty :-(']);
return
end
end
ts = msg.Feedback.Trajectories;
ts_curr_index = 1 + ...
msg.Feedback.SelectedTrajectoryIdx;
all = cell(length(ts), 1);
out = [];
for ts_ind = 1:length(ts)
t = ts(ts_ind).Trajectory;
poses = zeros(length(t), 4);
for i = 1:length(t)
poses(i,1) = t(i).Pose.Position.X;
poses(i,2) = t(i).Pose.Position.Y;
temp = t(i).Pose.Orientation;
temp = [temp.W, temp.X, temp.Y, temp.Z];
poses(i,3) = quat2angle(temp);
poses(i,4) = t(i).TimeFromStart.seconds;
end
all{ts_ind} = poses;
if (ts_ind == ts_curr_index)
out = poses;
end
end
end
end
% ros-interface
methods (Static)
function out = getRosNode()
persistent rosnode;
if (isempty(rosnode))
node_name = 'rosnode_for_matlab';
fprintf(['Creating ROS-node "', node_name, ...
'" for teb-planner\n']);
rosnode = robotics.ros.Node(node_name);
end
out = rosnode;
end
function out = getRosService(timeout)
persistent client;
if (isempty(client))
service_name='/teb_planner_node_pa/plan';
%service_type='teb_planner_pa_msgs/Plan';
rosnode = TebPlanner.getRosNode();
fprintf(['Creating ROS-service client for "', ...
service_name, '"\n']);
if (nargin < 1)
client = robotics.ros.ServiceClient(rosnode, ...
service_name);
else
client = robotics.ros.ServiceClient(rosnode, ...
service_name, 'Timeout', timeout);
end
end
out = client;
end
function out = getRosReplanService(timeout)
persistent replan_client;
if (isempty(replan_client))
service_name='/teb_planner_node_pa/replan';
rosnode = TebPlanner.getRosNode();
fprintf(['Creating ROS-service client for "', ...
service_name, '"\n']);
if (nargin < 1)
replan_client = robotics.ros.ServiceClient(rosnode, ...
service_name);
else
replan_client = robotics.ros.ServiceClient(rosnode, ...
service_name, 'Timeout', timeout);
end
end
out = replan_client;
end
function out = getRosPublisher()
persistent pub;
if (isempty(pub))
topic_name='/teb_planner_node_pa/request';
topic_type='teb_planner_pa_msgs/Request';
rosnode = TebPlanner.getRosNode();
fprintf(['Creating ROS-publisher for "', topic_name, ...
'"\n']);
pub = robotics.ros.Publisher(rosnode, ...
topic_name, topic_type);
end
out = pub;
end
function out = getRosSubscriber()
persistent sub;
if (isempty(sub))
topic_name='/teb_planner_node_pa/respond';
topic_type='teb_planner_pa_msgs/Respond';
rosnode = TebPlanner.getRosNode();
fprintf(['Creating ROS-subscriber for "', topic_name, ...
'"\n']);
sub = robotics.ros.Subscriber(rosnode, ...
topic_name, topic_type);
end
out = sub;
end
function out = getRosReplanPublisher()
persistent pub;
if (isempty(pub))
topic_name='/teb_planner_node_pa/request_replan';
topic_type='std_msgs/Empty';
rosnode = TebPlanner.getRosNode();
fprintf(['Creating ROS-publisher for "', topic_name, ...
'"\n']);
pub = robotics.ros.Publisher(rosnode, ...
topic_name, topic_type);
end
out = pub;
end
end
end