Skip to content

Commit 67691d1

Browse files
committed
(refactor) Adds type hints, typo fix and formatting
1 parent a147d0f commit 67691d1

File tree

23 files changed

+179
-154
lines changed

23 files changed

+179
-154
lines changed

src/kompass_core/control/_base_.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import abstractmethod
2-
from typing import Optional, List
2+
from typing import Optional, List, Union
33
import numpy as np
44
import kompass_cpp
55
from ..models import RobotState
@@ -95,7 +95,7 @@ def logging_info(self) -> str:
9595

9696
@property
9797
@abstractmethod
98-
def linear_x_control(self):
98+
def linear_x_control(self) -> Union[List[float], np.ndarray]:
9999
"""
100100
Getter of the last linear forward velocity control computed by the controller
101101
@@ -106,7 +106,7 @@ def linear_x_control(self):
106106

107107
@property
108108
@abstractmethod
109-
def linear_y_control(self):
109+
def linear_y_control(self) -> Union[List[float], np.ndarray]:
110110
"""
111111
Getter the last linear velocity lateral control computed by the controller
112112
@@ -117,7 +117,7 @@ def linear_y_control(self):
117117

118118
@property
119119
@abstractmethod
120-
def angular_control(self):
120+
def angular_control(self) -> Union[List[float], np.ndarray]:
121121
"""
122122
Getter of the last angular velocity control computed by the controller
123123
@@ -255,7 +255,7 @@ def tracked_state(self) -> Optional[RobotState]:
255255

256256
@property
257257
@abstractmethod
258-
def linear_x_control(self) -> List[float]:
258+
def linear_x_control(self) -> Union[List[float], np.ndarray]:
259259
"""
260260
Getter of the last linear forward velocity control computed by the controller
261261
@@ -266,7 +266,7 @@ def linear_x_control(self) -> List[float]:
266266

267267
@property
268268
@abstractmethod
269-
def linear_y_control(self) -> List[float]:
269+
def linear_y_control(self) -> Union[List[float], np.ndarray]:
270270
"""
271271
Getter the last linear velocity lateral control computed by the controller
272272
@@ -277,7 +277,7 @@ def linear_y_control(self) -> List[float]:
277277

278278
@property
279279
@abstractmethod
280-
def angular_control(self) -> List[float]:
280+
def angular_control(self) -> Union[List[float], np.ndarray]:
281281
"""
282282
Getter of the last angular velocity control computed by the controller
283283
@@ -298,7 +298,7 @@ def distance_error(self) -> float:
298298
return target.crosstrack_error
299299

300300
@property
301-
def orientation_error(self) -> float:
301+
def orientation_error(self) -> Union[float, np.ndarray]:
302302
"""
303303
Getter of the path tracking orientation error (rad)
304304

src/kompass_core/control/dvz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def interpolated_path(self) -> kompass_cpp.types.Path:
130130
return self.__reference_cmd_generator.interpolated_path()
131131

132132
@property
133-
def tracked_state(self) -> RobotState:
133+
def tracked_state(self) -> Optional[RobotState]:
134134
"""
135135
Tracked state on the path
136136

src/kompass_core/control/dwa.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Optional
2+
from typing import Optional, Union, List
33
import numpy as np
44
from attrs import Factory, define, field
55
from ..datatypes.laserscan import LaserScanData
@@ -386,7 +386,7 @@ def result_cost(self) -> Optional[float]:
386386
return None
387387

388388
@property
389-
def linear_x_control(self) -> np.ndarray:
389+
def linear_x_control(self) -> Union[List[float], np.ndarray]:
390390
"""
391391
Getter of the last linear forward velocity control computed by the controller
392392
@@ -398,7 +398,7 @@ def linear_x_control(self) -> np.ndarray:
398398
return [0.0]
399399

400400
@property
401-
def linear_y_control(self) -> np.ndarray:
401+
def linear_y_control(self) -> Union[List[float], np.ndarray]:
402402
"""
403403
Getter the last linear velocity lateral control computed by the controller
404404
@@ -410,7 +410,7 @@ def linear_y_control(self) -> np.ndarray:
410410
return [0.0]
411411

412412
@property
413-
def angular_control(self) -> np.ndarray:
413+
def angular_control(self) -> Union[List[float], np.ndarray]:
414414
"""
415415
Getter of the last angular velocity control computed by the controller
416416

src/kompass_core/control/rgbd_follower.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
TrajectoryVelocities2D,
1313
TrajectoryPath,
1414
)
15-
from typing import Optional, List
15+
from typing import Optional, List, Union
1616
import numpy as np
1717
import logging
1818
from ._base_ import ControllerTemplate
@@ -521,7 +521,7 @@ def result_cost(self) -> Optional[float]:
521521
return None
522522

523523
@property
524-
def linear_x_control(self) -> np.ndarray:
524+
def linear_x_control(self) -> Union[List[float], np.ndarray]:
525525
"""
526526
Getter of the last linear forward velocity control computed by the controller
527527
@@ -533,7 +533,7 @@ def linear_x_control(self) -> np.ndarray:
533533
return [0.0]
534534

535535
@property
536-
def linear_y_control(self) -> np.ndarray:
536+
def linear_y_control(self) -> Union[List[float], np.ndarray]:
537537
"""
538538
Getter the last linear velocity lateral control computed by the controller
539539
@@ -545,7 +545,7 @@ def linear_y_control(self) -> np.ndarray:
545545
return [0.0]
546546

547547
@property
548-
def angular_control(self) -> np.ndarray:
548+
def angular_control(self) -> Union[List[float], np.ndarray]:
549549
"""
550550
Getter of the last angular velocity control computed by the controller
551551

src/kompass_core/performance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, List
1+
from typing import Any, List, Optional
22

33
import numpy as np
44
from .datatypes.obstacles import ObstaclesData
@@ -18,7 +18,7 @@ class MotionResult:
1818
def __init__(self) -> None:
1919
self.time = []
2020
self.steer_cmds: List[float] = []
21-
self.robot_path: PathSample = None
21+
self.robot_path: Optional[PathSample] = None
2222
self.speed_cmd: List[float] = []
2323
self.ori_error: List[float] = []
2424
self.lat_error: List[float] = []
@@ -93,7 +93,7 @@ def vis_result(
9393
)
9494

9595
# Plot test path
96-
visualization.plt_path_points_List(test, color="red", ax=ax3)
96+
visualization.plt_path_points_list(test, color="red", ax=ax3)
9797

9898
# Plot robot initial state
9999
robot_footprint.plt_robot(

src/kompass_core/utils/emergency_stop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, List
1+
from typing import Optional
22
from logging import Logger
33
from ..models import (
44
Robot,

src/kompass_cpp/bindings/bindings_planning.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace py = nanobind;
88
using namespace Kompass;
99

10-
1110
// Utils submodule
1211
void bindings_planning(py::module_ &m) {
1312
auto m_planning = m.def_submodule("planning", "KOMPASS CPP Planning Module");
@@ -24,7 +23,7 @@ void bindings_planning(py::module_ &m) {
2423
py::arg("goal_x"), py::arg("goal_y"), py::arg("goal_yaw"),
2524
py::arg("map_3d"))
2625
.def("solve", &Planning::OMPL2DGeometricPlanner::solve,
27-
py::arg("planning_timeout") = 1.0)
26+
py::arg("planning_timeout") = 1.0)
2827
.def("get_solution", &Planning::OMPL2DGeometricPlanner::getPath)
2928
.def("set_space_bounds_from_map",
3029
&Planning::OMPL2DGeometricPlanner::setSpaceBoundsFromMap,

src/kompass_cpp/kompass_cpp/include/controllers/dwa.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class DWA : public Follower {
7272
const int maxNumThreads = 1);
7373

7474
/**
75-
* @brief Reset the resolution of the robot's local map, this is equivalent to the box size representing each obstacle after conversion
75+
* @brief Reset the resolution of the robot's local map, this is equivalent to
76+
* the box size representing each obstacle after conversion
7677
*
7778
* @param octreeRes
7879
*/
@@ -111,7 +112,7 @@ class DWA : public Follower {
111112

112113
template <typename T>
113114
Controller::Result computeVelocityCommand(const Velocity2D &global_vel,
114-
const T &scan_points){
115+
const T &scan_points) {
115116
TrajSearchResult searchRes = findBestPath(global_vel, scan_points);
116117
Controller::Result finalResult;
117118
if (searchRes.isTrajFound) {
@@ -123,18 +124,18 @@ class DWA : public Follower {
123124
finalResult.status = Controller::Result::Status::NO_COMMAND_POSSIBLE;
124125
}
125126
return finalResult;
126-
};
127+
};
127128

128129
template <typename T>
129130
TrajSearchResult computeVelocityCommandsSet(const Velocity2D &global_vel,
130-
const T &scan_points){
131+
const T &scan_points) {
131132
TrajSearchResult searchRes = findBestPath(global_vel, scan_points);
132133
// Update latest velocity command
133134
if (searchRes.isTrajFound) {
134135
latest_velocity_command_ = searchRes.trajectory.velocities.getFront();
135136
}
136137
return searchRes;
137-
};
138+
};
138139

139140
std::tuple<MatrixXfR, MatrixXfR> getDebuggingSamples() const;
140141

@@ -175,7 +176,7 @@ class DWA : public Follower {
175176
*/
176177
template <typename T>
177178
TrajSearchResult findBestPath(const Velocity2D &global_vel,
178-
const T &scan_points){
179+
const T &scan_points) {
179180
// Throw an error if the global path is not set
180181
if (!currentPath) {
181182
throw std::invalid_argument("Pointer to global path is NULL. Cannot use "
@@ -185,11 +186,15 @@ class DWA : public Follower {
185186
// find closest segment to use in cost computation
186187
determineTarget();
187188

188-
if(rotate_in_place and currentTrackedTarget_->heading_error > goal_orientation_tolerance * 10.0){
189+
if (rotate_in_place and currentTrackedTarget_->heading_error >
190+
goal_orientation_tolerance * 10.0) {
189191
// If the robot is rotating in place and the heading error is large, we
190192
// do not need to sample trajectories
191193
LOG_DEBUG("Rotating In Place ...");
192-
auto trajectory = trajSampler->generateSingleSampleFromVel(Velocity2D(0.0, 0.0, - currentTrackedTarget_->heading_error * ctrlimitsParams.omegaParams.maxOmega / M_PI));
194+
auto trajectory = trajSampler->generateSingleSampleFromVel(
195+
Velocity2D(0.0, 0.0,
196+
-currentTrackedTarget_->heading_error *
197+
ctrlimitsParams.omegaParams.maxOmega / M_PI));
193198
return TrajSearchResult{trajectory, true, 0.0};
194199
}
195200

@@ -208,7 +213,7 @@ class DWA : public Follower {
208213
// Evaluate the samples and get the sample with the minimum cost
209214
return trajCostEvaluator->getMinTrajectoryCost(samples_, currentPath.get(),
210215
trackedRefPathSegment);
211-
};
216+
};
212217

213218
private:
214219
double max_forward_distance_ = 0.0;

src/kompass_cpp/kompass_cpp/include/controllers/vision_dwa.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class VisionDWA : public DWA, public RGBFollower {
2626
public:
2727
VisionDWAConfig() : RGBFollower::RGBFollowerConfig() {
2828
addParameter(
29-
"control_horizon",
30-
Parameter(2, 1, 1000, "Number of steps for applying the control"));
29+
"control_horizon",
30+
Parameter(2, 1, 1000, "Number of steps for applying the control"));
3131
addParameter(
3232
"prediction_horizon",
3333
Parameter(10, 1, 1000, "Number of steps for future prediction"));
@@ -219,8 +219,8 @@ class VisionDWA : public DWA, public RGBFollower {
219219
const Bbox2D &target_box_2d, const float yaw = 0.0);
220220

221221
Eigen::Vector2f getErrors() const {
222-
return Eigen::Vector2f(dist_error_, orientation_error_);
223-
}
222+
return Eigen::Vector2f(dist_error_, orientation_error_);
223+
}
224224

225225
private:
226226
VisionDWAConfig config_;
@@ -247,7 +247,8 @@ class VisionDWA : public DWA, public RGBFollower {
247247
* @param tracking_pose
248248
* @return Velocity2D
249249
*/
250-
Velocity2D getPureTrackingCtrl(const TrackedPose2D &tracking_pose, const bool update_global_error = false);
250+
Velocity2D getPureTrackingCtrl(const TrackedPose2D &tracking_pose,
251+
const bool update_global_error = false);
251252

252253
/**
253254
* @brief Get the Tracking Control Result based on object tracking and DWA

0 commit comments

Comments
 (0)