Skip to content

Commit 6a96c41

Browse files
authored
Merge pull request #525 from bark-simulator/safe_distance_label_lateral
Safe distance label lateral
2 parents 8d8ac97 + 77597fb commit 6a96c41

30 files changed

+464
-169
lines changed

bark/benchmark/tests/py_benchmark_runner_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_database_runner(self):
4949
safe_dist_params = ParameterServer(log_if_default=True)
5050
test_python_params = ParameterServer(log_if_default=True)
5151
evaluators = {"success" : "EvaluatorGoalReached", "collision" : "EvaluatorCollisionEgoAgent",
52-
"max_steps": "EvaluatorStepCount", "safe_dist_lon" : {"type" : "EvaluatorDynamicSafeDistLong", "params" : safe_dist_params},
52+
"max_steps": "EvaluatorStepCount", "safe_dist_lon" : {"type" : "EvaluatorDynamicSafeDist", "params" : safe_dist_params},
5353
"safe_dist_lat" : {"type" : "EvaluatorStaticSafeDist", "params" : safe_dist_params},
5454
"test_evaluator" : {"type" : "TestPythonEvaluator", "params" : test_python_params},
5555
"test_evaluator_serializable" : TestPythonEvaluatorSerializable()}
@@ -173,7 +173,7 @@ def test_database_runner_python_behavior(self):
173173
db = BenchmarkDatabase(database_root=local_release_filename)
174174
safe_dist_params = ParameterServer(log_if_default=True)
175175
evaluators = {"success" : "EvaluatorGoalReached", "collision" : "EvaluatorCollisionEgoAgent",
176-
"max_steps": "EvaluatorStepCount", "safe_dist_lon" : {"type" : "EvaluatorDynamicSafeDistLong", "params" : safe_dist_params},
176+
"max_steps": "EvaluatorStepCount", "safe_dist_lon" : {"type" : "EvaluatorDynamicSafeDist", "params" : safe_dist_params},
177177
"safe_dist_lat" : {"type" : "EvaluatorStaticSafeDist", "params" : safe_dist_params}}
178178
terminal_when = {"collision" :lambda x: x, "max_steps": lambda x : x>2, "safe_dist_lon" : lambda x: x }
179179
params = ParameterServer() # only for evaluated agents not passed to scenario!

bark/commons/tests/frenet_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,29 @@ TEST(calculate_state_diff, minus_pi2_minuspi4_oriented_no_overlap) {
221221
EXPECT_NEAR(diff.angle, B_PI_2*2/3, 0.001);
222222
}
223223

224+
TEST(calculate_state_diff, zero_minus_3pi4_oriented_no_overlap) {
225+
// some line with three points from x=1 to x=10, y=0
226+
FrenetState state1{2.0, 0.0, 3.0, 1.0, 0.0, 0.0, 0.0};
227+
FrenetState state2{11.0, -8.0, 1.0, 0.0, -3*B_PI/4, 0.0, 0.0};
228+
229+
double width1 = 2.0, width2 = 3.0, length1 = 5.0, length2 = 4.0;
230+
const auto shape1 = Polygon(
231+
Pose(0.5, width1/2, 0),
232+
{Point2d(0, 0), Point2d(0, width1), Point2d(length1, width1),
233+
Point2d(length1, 0), Point2d(0, 0)});
234+
const auto shape2 = Polygon(
235+
Pose(0.5, width2/2, 0),
236+
{Point2d(0, 0), Point2d(0, width2), Point2d(length2, width2),
237+
Point2d(length2, 0), Point2d(0, 0)});
238+
FrenetStateDifference diff(state1, shape1, state2, shape2);
239+
240+
EXPECT_NEAR(diff.lon, 11.0 - 2.0 - 4.5 + cos(3*B_PI/4)*3.5 - sin(3*B_PI/4)*1.5, 0.001);
241+
EXPECT_NEAR(diff.lat, - (8.0 -1.0 - sin(3*B_PI/4)*3.5 + cos(3*B_PI/4)*1.5), 0.001);
242+
EXPECT_NEAR(diff.vlat, -1.0, 0.001);
243+
EXPECT_NEAR(diff.vlon, -2.0, 0.001);
244+
EXPECT_NEAR(diff.angle, -3*B_PI/4, 0.001);
245+
}
246+
224247
TEST(calculate_state_diff, lateral_zero_oriented_no_overlap) {
225248
// some line with three points from x=1 to x=10, y=0
226249
FrenetState state1{1.0, 0.0, 3.0, 5.0, 0.0, 0.0, 0.0};

bark/commons/transformation/frenet_state.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ State FrenetStateToDynamicState(const FrenetState& frenet_state,
9292
* @param polygon
9393
* @return A struct containing rear_dist, front_dist, left_dist, side_dist of rotated shape
9494
*/
95-
auto ShapeExtensionAtTangentAngle(const double& tangent_angle, const Polygon& polygon) {
95+
ShapeExtension ShapeExtensionAtTangentAngle(const double& tangent_angle, const Polygon& polygon) {
9696
// Assumes equal extension to sides for polygon
9797
BARK_EXPECT_TRUE(std::abs(polygon.right_dist_ - polygon.left_dist_) < 0.01);
9898
const double polygon_side_extend = polygon.left_dist_;
@@ -111,8 +111,7 @@ auto ShapeExtensionAtTangentAngle(const double& tangent_angle, const Polygon& po
111111
} else {
112112
std::swap(left_dist, right_dist);
113113
}
114-
const struct{double front_dist; double rear_dist; double left_dist; double right_dist;}
115-
shape_extension{front_dist, rear_dist, left_dist, right_dist};
114+
ShapeExtension shape_extension{front_dist, rear_dist, left_dist, right_dist};
116115
return shape_extension;
117116
}
118117

@@ -140,10 +139,12 @@ FrenetStateDifference::FrenetStateDifference(const FrenetState& frenet_from, con
140139
if(from.lon <= to.lon) {
141140
double diff_lon = to.lon - shape_extend_at_tangent2.rear_dist -
142141
(from.lon + shape_extend_at_tangent1.front_dist);
142+
lon_zeroed = diff_lon <= 0;
143143
lon = diff_lon > 0 ? diff_lon : to.lon - from.lon;
144144
} else {
145145
double diff_lon = to.lon + shape_extend_at_tangent2.front_dist -
146146
(from.lon - shape_extend_at_tangent1.rear_dist);
147+
lon_zeroed = diff_lon >= 0;
147148
lon = diff_lon < 0 ? diff_lon : to.lon - from.lon;
148149
}
149150

@@ -154,11 +155,13 @@ FrenetStateDifference::FrenetStateDifference(const FrenetState& frenet_from, con
154155
double diff_lat = to.lat - shape_extend_at_tangent2.right_dist -
155156
(from.lat + shape_extend_at_tangent1.left_dist);
156157
// if shape consideration leads to negative distance use only pure lateral difference
158+
lat_zeroed = diff_lat <= 0;
157159
lat = diff_lat > 0 ? diff_lat : to.lat - from.lat;
158160
} else {
159161
// lateral difference is negative
160-
double diff_lat = to.lat - shape_extend_at_tangent1.right_dist -
161-
(from.lat + shape_extend_at_tangent2.left_dist);
162+
double diff_lat = to.lat + shape_extend_at_tangent1.right_dist -
163+
(from.lat - shape_extend_at_tangent2.right_dist);
164+
lat_zeroed = diff_lat >= 0;
162165
lat = diff_lat < 0 ? diff_lat : to.lat - from.lat;
163166
}
164167

bark/commons/transformation/frenet_state.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,22 @@ struct FrenetStateDifference : public FrenetState {
5151
// - positive lateral difference if frenet_state2 is in "left" of frenet_state1
5252
// - positive long and velocities diff if frenet state 2 is "faster" than frenet state1
5353
// - positive angle difference if frenet state2 is turned more left than frenet state 1
54-
FrenetStateDifference() : FrenetState(), from(), to() {}
54+
// - boolean flags lat_zeroed and lon_zeroed indicate if the lateral/longitudinal shape-based distance is zero
55+
// giving that the calculated distances actually represent a state-based distance
56+
FrenetStateDifference() : FrenetState(), from(), to(), lat_zeroed(false), lon_zeroed(false) {}
5557
FrenetStateDifference(const FrenetState& frenet_state1, const bark::geometry::Polygon& polygon1,
5658
const FrenetState& frenet_state2, const bark::geometry::Polygon& polygon2);
5759
FrenetState from;
5860
FrenetState to;
61+
bool lat_zeroed;
62+
bool lon_zeroed;
5963
};
6064

6165
bark::models::dynamic::State FrenetStateToDynamicState(
6266
const FrenetState& frenet_state, const bark::geometry::Line& path);
6367

64-
auto ShapeExtensionAtTangentAngle(const double& tangent_angle, const bark::geometry::Polygon& polygon);
68+
typedef struct{double front_dist; double rear_dist; double left_dist; double right_dist;} ShapeExtension;
69+
ShapeExtension ShapeExtensionAtTangentAngle(const double& tangent_angle, const bark::geometry::Polygon& polygon);
6570

6671
double LatAccStreetToVehicleCs(
6772
double acc_lat_street, double acc_lon, double delta_time,

bark/models/behavior/idm/idm_lane_tracking.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class BehaviorIDMLaneTracking : public BaseIDM {
2727
explicit BehaviorIDMLaneTracking(const commons::ParamsPtr& params) :
2828
BehaviorModel(params),
2929
BaseIDM(params),
30-
limit_steering_rate_(true) {
30+
limit_steering_rate_(params->GetBool("BehaviorIDMLaneTracking::LimitSteeringRate",
31+
"Bool if steering limited according to dynamics model", true)) {
3132
crosstrack_error_gain_ =
3233
params->GetReal("BehaviorIDMLaneTracking::CrosstrackErrorGain",
3334
"Tuning factor of stanley controller", 1.0);

bark/models/behavior/motion_primitives/primitives/primitive_const_acc_stay_lane.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class PrimitiveConstAccStayLane : public Primitive,
3232

3333
std::string GetName() const override;
3434

35+
double GetAcceleration() const { return acceleration_; }
36+
3537
LaneCorridorPtr SelectTargetCorridor(
3638
const ObservedWorld& observed_world,
3739
const AdjacentLaneCorridors& adjacent_corridors) override;

bark/models/behavior/rule_based/lane_change_behavior.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ BehaviorLaneChangeRuleBased::FrontRearAgents(
5050
const ObservedWorld& observed_world,
5151
const LaneCorridorPtr& lane_corr) const {
5252
AgentInformation front_info, rear_info;
53-
const auto& front_rear = observed_world.GetAgentFrontRear(lane_corr);
53+
double lateral_difference_threshold = 100; // do not restrict regarding lateral diff
54+
const auto& front_rear = observed_world.GetAgentFrontRearForId(observed_world.GetEgoAgentId(),
55+
lane_corr,
56+
lateral_difference_threshold,
57+
true); // require to be in this lane corridor
5458
const auto& ego_agent = observed_world.GetEgoAgent();
5559
if (front_rear.front.first) {
5660
// front info

bark/models/dynamic/single_track.hpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,39 @@ class SingleTrackModel : public DynamicModel {
8888
double GetLatAccelerationMax() const {
8989
return acceleration_limits_.lat_acc_max;
9090
}
91+
92+
/**
93+
* @brief Calculates maximum possible lateral evasive acceleration away from center line of a lane
94+
* assuming single track vehicle dynamics
95+
*
96+
* @param v velocity of vehicle in global coordinate system
97+
* @param theta orientation of vehicle in global coordinate system
98+
* @param tangent_angle angle of center line in global coordinate system, assumed to be constant over time
99+
* @param a_total_max maximum possible acceleration of vehicle into direction of orientation
100+
* @param on_left_of_center_line to calculate evasive lateral maximum acceleration, flag indicates on which side
101+
* of center line the vehicle is
102+
* @return double maximum possible lateral evasive acceleration
103+
*/
104+
double CalculateLatAccelerationMaxAtFrenetAngle(const double &v, const double& theta,
105+
const double& tangent_angle,
106+
const double a_total_max,
107+
bool on_left_of_center_line) const {
108+
const auto max_acc_lat_dyn = GetLatAccelerationMax();
109+
const double delta_max = std::atan2(max_acc_lat_dyn * wheel_base_, v * v);
110+
double delta_max_evasive;
111+
const double abs_angle_diff = std::abs(bark::geometry::SignedAngleDiff(theta, tangent_angle));
112+
if ((on_left_of_center_line && abs_angle_diff < bark::geometry::B_PI_2) ||
113+
(!on_left_of_center_line && abs_angle_diff > bark::geometry::B_PI_2)) {
114+
delta_max_evasive = delta_max;
115+
} else {
116+
delta_max_evasive = delta_max;
117+
}
118+
const double max_lat_acc_from_steering = v*v*sin(delta_max_evasive)/wheel_base_*cos(tangent_angle+theta);
119+
const double max_lat_acc_from_long_acc = std::abs(a_total_max)*(sin(tangent_angle-theta));
120+
const double max_lat_acc = max_lat_acc_from_long_acc-max_lat_acc_from_steering;
121+
return std::abs(max_lat_acc);
122+
}
123+
91124
double GetLonAccelerationMax(const State& x) const {
92125
return acceleration_limits_.lon_acc_max;
93126
}
@@ -158,7 +191,7 @@ inline double CalculateSteeringAngle(const SingleTrackModelPtr& model,
158191

159192
FrenetState f_state = FrenetState(state_front, ref_line);
160193
double vel = state(StateDefinition::VEL_POSITION);
161-
double delta = -NormToPI(f_state.angle) + atan2(-gain * f_state.lat, vel);
194+
double delta = +NormToPI(f_state.angle) + atan2(-gain * f_state.lat, vel);
162195

163196
VLOG(5) << "del=" << delta << ", fa=" << f_state.angle << ", na=" << NormToPI(f_state.angle) <<
164197
", g=" << gain << ", lat=" << f_state.lat << ", v=" << vel;

bark/python_wrapper/models/behavior.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ void python_behavior(py::module m) {
213213
py::class_<Primitive, PyPrimitive, PrimitivePtr>(m, "Primitive")
214214
.def(py::init<const bark::commons::ParamsPtr&>())
215215
.def("Plan", &Primitive::Plan)
216-
.def("IsPreConditionSatisfied", &Primitive::IsPreConditionSatisfied);
216+
.def("IsPreConditionSatisfied", &Primitive::IsPreConditionSatisfied)
217+
.def_property_readonly("name", &Primitive::GetName);
217218

218219
py::class_<BehaviorMPMacroActions, BehaviorModel,
219220
shared_ptr<BehaviorMPMacroActions>>(m, "BehaviorMPMacroActions")
@@ -292,13 +293,14 @@ void python_behavior(py::module m) {
292293
.def(py::init<const bark::commons::ParamsPtr&>())
293294
.def(py::pickle(
294295
[](const PrimitiveConstAccStayLane& b) {
295-
return py::make_tuple(ParamsToPython(b.Primitive::GetParams()));
296+
return py::make_tuple(ParamsToPython(b.Primitive::GetParams()),
297+
b.GetAcceleration());
296298
},
297299
[](py::tuple t) {
298-
if (t.size() != 1)
300+
if (t.size() != 2)
299301
throw std::runtime_error("Invalid behavior model state!");
300302
return new PrimitiveConstAccStayLane(
301-
PythonToParams(t[0].cast<py::tuple>()));
303+
PythonToParams(t[0].cast<py::tuple>()), t[1].cast<double>());
302304
}));
303305

304306
py::class_<PrimitiveConstAccChangeToLeft, Primitive,

bark/python_wrapper/polymorphic_conversion.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@
6868
#ifdef PLANNER_UCT
6969
#include "bark_mcts/models/behavior/behavior_uct_hypothesis.hpp"
7070
#include "bark_mcts/models/behavior/behavior_uct_risk_constraint.hpp"
71+
#include "bark_mcts/models/behavior/behavior_uct_nheuristic_risk_constraint.hpp"
7172
#include "bark_mcts/models/behavior/behavior_uct_cooperative.hpp"
7273
#include "bark_mcts/models/behavior/hypothesis/idm/hypothesis_idm.hpp"
7374
using bark::models::behavior::BehaviorHypothesisIDM;
7475
using bark::models::behavior::BehaviorUCTHypothesis;
7576
using bark::models::behavior::BehaviorUCTRiskConstraint;
7677
using bark::models::behavior::BehaviorUCTCooperative;
78+
using bark::models::behavior::BehaviorUCTNHeuristicRiskConstraint;
7779
#endif
7880

7981
#ifdef PLANNER_MIQP
@@ -177,6 +179,8 @@ py::tuple BehaviorModelToPython(BehaviorModelPtr behavior_model) {
177179
#ifdef PLANNER_UCT
178180
else if (typeid(*behavior_model) == typeid(BehaviorUCTCooperative)) {
179181
behavior_model_name = "BehaviorUCTCooperative";
182+
} else if (typeid(*behavior_model) == typeid(BehaviorUCTNHeuristicRiskConstraint)) {
183+
behavior_model_name = "BehaviorUCTNHeuristicRiskConstraint";
180184
} else if (typeid(*behavior_model) == typeid(BehaviorUCTRiskConstraint)) {
181185
behavior_model_name = "BehaviorUCTRiskConstraint";
182186
} else if (typeid(*behavior_model) == typeid(BehaviorUCTHypothesis)) {
@@ -256,6 +260,9 @@ BehaviorModelPtr PythonToBehaviorModel(py::tuple t) {
256260
else if (behavior_model_name.compare("BehaviorUCTCooperative") == 0) {
257261
return std::make_shared<BehaviorUCTCooperative>(
258262
t[0].cast<BehaviorUCTCooperative>());
263+
} else if (behavior_model_name.compare("BehaviorUCTNHeuristicRiskConstraint") == 0) {
264+
return std::make_shared<BehaviorUCTNHeuristicRiskConstraint>(
265+
t[0].cast<BehaviorUCTNHeuristicRiskConstraint>());
259266
} else if (behavior_model_name.compare("BehaviorUCTRiskConstraint") == 0) {
260267
return std::make_shared<BehaviorUCTRiskConstraint>(
261268
t[0].cast<BehaviorUCTRiskConstraint>());

0 commit comments

Comments
 (0)