Skip to content

Commit 134075a

Browse files
committed
tweaks to base class to support derived classes
1 parent 3047b4c commit 134075a

File tree

1 file changed

+78
-13
lines changed

1 file changed

+78
-13
lines changed

roboticstoolbox/mobile/drivers.py

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@Author: Peter Corke, original MATLAB code and Python version
44
@Author: Kristian Gibson, initial MATLAB port
55
"""
6+
67
from abc import ABC, abstractmethod
78
import warnings
89
from math import pi, sin, cos, tan, atan2
@@ -26,6 +27,57 @@ class VehicleDriverBase(ABC):
2627
:seealso: :class:`RandomPath`
2728
"""
2829

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+
2981
@abstractmethod
3082
def demand(self):
3183
"""
@@ -72,27 +124,40 @@ def vehicle(self, v):
72124
def __repr__(self):
73125
return str(self)
74126

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+
75151

76152
# ========================================================================= #
77153

78154

79155
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):
89157
"""
90158
Driving agent for random path
91159
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+
96161
:param dthresh: distance threshold, defaults to 0.05
97162
:type dthresh: float, optional
98163

0 commit comments

Comments
 (0)