3
3
@Author: Peter Corke, original MATLAB code and Python version
4
4
@Author: Kristian Gibson, initial MATLAB port
5
5
"""
6
+
6
7
from abc import ABC , abstractmethod
7
8
import warnings
8
9
from math import pi , sin , cos , tan , atan2
@@ -26,6 +27,57 @@ class VehicleDriverBase(ABC):
26
27
:seealso: :class:`RandomPath`
27
28
"""
28
29
30
+ def __init__ (
31
+ self ,
32
+ workspace = None ,
33
+ speed = 1 ,
34
+ headinggain = 0.3 ,
35
+ goalmarkerstyle = None ,
36
+ verbose = False ,
37
+ ):
38
+ """
39
+ _summary_
40
+
41
+ :param workspace: dimension of workspace, see :func:`spatialmath.base.exand_dims`
42
+ :type workspace: scalar, array_like(2), array_like(4)
43
+ :param speed: forward speed, defaults to 1
44
+ :type speed: float, optional
45
+ :param headinggain: _description_, defaults to 0.3
46
+ :type headinggain: float, optional
47
+ :param verbose: _description_, defaults to False
48
+ :type verbose: bool, optional
49
+
50
+ The workspace of the robot is a rectangular region defined by `workspace`
51
+ that is specified by (see ``plotvol2``):
52
+
53
+ ============== ======= =======
54
+ ``workspace`` x-range y-range
55
+ ============== ======= =======
56
+ A (scalar) -A:A -A:A
57
+ [A, B] A:B A:B
58
+ [A, B, C, D] A:B C:D
59
+ ============== ======= =======
60
+ """
61
+ if hasattr (workspace , "workspace" ):
62
+ # workspace can be defined by an object with a workspace attribute
63
+ self ._workspace = base .expand_dims (workspace .workspace )
64
+ else :
65
+ self ._workspace = base .expand_dims (workspace )
66
+
67
+ self ._speed = speed
68
+ self ._headinggain = headinggain
69
+ self ._verbose = verbose
70
+
71
+ if goalmarkerstyle is None :
72
+ self ._goal_marker_style = {
73
+ "marker" : "D" ,
74
+ "markersize" : 6 ,
75
+ "color" : "r" ,
76
+ "linestyle" : "None" ,
77
+ }
78
+ else :
79
+ self ._goal_marker_style = goalmarkerstyle
80
+
29
81
@abstractmethod
30
82
def demand (self ):
31
83
"""
@@ -72,27 +124,40 @@ def vehicle(self, v):
72
124
def __repr__ (self ):
73
125
return str (self )
74
126
127
+ @property
128
+ def workspace (self ):
129
+ """
130
+ Size of robot driving workspace
131
+
132
+ :return: workspace bounds [xmin, xmax, ymin, ymax]
133
+ :rtype: ndarray(4)
134
+
135
+ Returns the bounds of the workspace as specified by constructor
136
+ option ``workspace``
137
+ """
138
+ return self ._workspace
139
+
140
+ def driveto (self , goal ):
141
+
142
+ goal_heading = atan2 (goal [1 ] - self ._veh ._x [1 ], goal [0 ] - self ._veh ._x [0 ])
143
+ delta_heading = base .angdiff (goal_heading , self ._veh ._x [2 ])
144
+ print (
145
+ f"t={ self ._veh ._t :.1f} , pos=({ self ._veh ._x [0 ]:.1f} , { self ._veh ._x [1 ]:.1f} ), " ,
146
+ f"goal_heading={ goal_heading * 180 / pi :.1f} , delta_heading={ delta_heading * 180 / pi :.1f} " ,
147
+ )
148
+
149
+ return np .r_ [self ._speed , self ._headinggain * delta_heading ]
150
+
75
151
76
152
# ========================================================================= #
77
153
78
154
79
155
class RandomPath (VehicleDriverBase ):
80
- def __init__ (
81
- self ,
82
- workspace ,
83
- speed = 1 ,
84
- dthresh = 0.05 ,
85
- seed = 0 ,
86
- headinggain = 0.3 ,
87
- goalmarkerstyle = None ,
88
- ):
156
+ def __init__ (self , dthresh = 0.05 , seed = 0 , ** kwargs ):
89
157
"""
90
158
Driving agent for random path
91
159
92
- :param workspace: dimension of workspace, see :func:`spatialmath.base.exand_dims`
93
- :type workspace: scalar, array_like(2), array_like(4)
94
- :param speed: forward speed, defaults to 1
95
- :type speed: float, optional
160
+
96
161
:param dthresh: distance threshold, defaults to 0.05
97
162
:type dthresh: float, optional
98
163
0 commit comments