Skip to content

Commit dd7ec17

Browse files
tobifalkcassava
authored andcommitted
basic: Add option for setting driver request
This can be useful if the basic controller is temprarily disabled and the simulator does not handle the driver requests internally. A typical use case is a simulation with an external vehicle dynamics model connected to Cloe.
1 parent e2c724f commit dd7ec17

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

engine/tests/test_engine_json_schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@
122122
},
123123
"type": "object"
124124
},
125+
"driver_request": {
126+
"description": "component providing driver request",
127+
"type": "string"
128+
},
125129
"lka": {
126130
"additionalProperties": false,
127131
"description": "LKA configuration",

plugins/basic/src/basic.cpp

+35-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <boost/optional.hpp> // for optional<>
3333

34+
#include <cloe/component/driver_request.hpp> // for DriverRequest
3435
#include <cloe/component/latlong_actuator.hpp> // for LatLongActuator
3536
#include <cloe/component/object_sensor.hpp> // for ObjectSensor
3637
#include <cloe/component/utility/ego_sensor_canon.hpp> // for EgoSensor, EgoSensorCanon
@@ -81,6 +82,20 @@ const std::vector<std::pair<std::string, Algorithm>> ALGORITHMS{
8182

8283
} // namespace distance
8384

85+
double get_driver_request_acceleration(std::shared_ptr<DriverRequest> driver) {
86+
if (!driver->has_acceleration()) {
87+
throw cloe::Error("basic controller: {} has no acceleration data.", driver->name());
88+
}
89+
return *driver->acceleration();
90+
}
91+
92+
double get_driver_request_steering_angle(std::shared_ptr<DriverRequest> driver) {
93+
if (!driver->has_steering_angle()) {
94+
throw cloe::Error("basic controller: {} has no steering_angle data.", driver->name());
95+
}
96+
return *driver->steering_angle();
97+
}
98+
8499
struct AdaptiveCruiseControl {
85100
AccConfiguration config;
86101
std::shared_ptr<Vehicle> vehicle{nullptr};
@@ -194,12 +209,16 @@ struct AdaptiveCruiseControl {
194209
/**
195210
* FIXME(ben): The HMI should not be manipulated while we are in this part.
196211
*/
197-
void control(Vehicle& v, const Sync& sync) {
212+
void control(Vehicle& v, const Sync& sync, const std::string& driver_request) {
198213
assert(distance_algorithm < distance::ALGORITHMS.size());
199214

200215
if (!enabled || !active) {
201216
// When not enabled, the function is disabled except for the HMI,
202217
// which is controlled separately.
218+
if (!driver_request.empty()) {
219+
double acc = get_driver_request_acceleration(v.get<DriverRequest>(driver_request));
220+
v.get<LatLongActuator>(config.latlong_actuator)->set_acceleration(acc);
221+
}
203222
return;
204223
}
205224

@@ -275,8 +294,12 @@ struct LaneKeepingAssistant {
275294
public:
276295
explicit LaneKeepingAssistant(const LkaConfiguration& c) : config(c) {}
277296

278-
void control(Vehicle& v, const Sync&) {
297+
void control(Vehicle& v, const Sync&, const std::string& driver_request) {
279298
if (!config.enabled) {
299+
if (!driver_request.empty()) {
300+
double rad = get_driver_request_steering_angle(v.get<DriverRequest>(driver_request));
301+
v.get<LatLongActuator>(config.latlong_actuator)->set_steering_angle(rad);
302+
}
280303
return;
281304
}
282305

@@ -305,8 +328,12 @@ struct AutoEmergencyBraking {
305328
public:
306329
explicit AutoEmergencyBraking(const AebConfiguration& c) : config(c) {}
307330

308-
void control(Vehicle& v, const Sync&) {
331+
void control(Vehicle& v, const Sync&, const std::string& driver_request) {
309332
if (!config.enabled) {
333+
if (!driver_request.empty()) {
334+
double acc = get_driver_request_acceleration(v.get<DriverRequest>(driver_request));
335+
v.get<LatLongActuator>(config.latlong_actuator)->set_acceleration(acc);
336+
}
310337
return;
311338
}
312339
auto world_sensor = v.get<ObjectSensor>(config.world_sensor);
@@ -352,7 +379,7 @@ struct AutoEmergencyBraking {
352379
class BasicController : public Controller {
353380
public:
354381
BasicController(const std::string& name, const BasicConfiguration& c)
355-
: Controller(name), acc_(c.acc), aeb_(c.aeb), lka_(c.lka) {
382+
: Controller(name), acc_(c.acc), aeb_(c.aeb), lka_(c.lka), driver_request_(c.driver_request) {
356383
// Define the HMI of the basic controller:
357384
namespace contact = utility::contact;
358385
acc_.add_hmi(hmi_);
@@ -397,9 +424,9 @@ class BasicController : public Controller {
397424
assert(veh_ != nullptr);
398425

399426
hmi_.update(sync.time());
400-
acc_.control(*veh_, sync);
401-
lka_.control(*veh_, sync);
402-
aeb_.control(*veh_, sync);
427+
acc_.control(*veh_, sync, driver_request_);
428+
lka_.control(*veh_, sync, driver_request_);
429+
aeb_.control(*veh_, sync, driver_request_);
403430

404431
return sync.time();
405432
}
@@ -416,6 +443,7 @@ class BasicController : public Controller {
416443
AdaptiveCruiseControl acc_;
417444
AutoEmergencyBraking aeb_;
418445
LaneKeepingAssistant lka_;
446+
std::string driver_request_;
419447
utility::ContactMap<Duration> hmi_;
420448
};
421449

plugins/basic/src/basic.hpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ struct BasicConfiguration : public Confable {
150150
AccConfiguration acc;
151151
AebConfiguration aeb;
152152
LkaConfiguration lka;
153+
std::string driver_request;
153154

154155
void to_json(Json& j) const override {
155156
j = Json{
@@ -160,11 +161,14 @@ struct BasicConfiguration : public Confable {
160161
}
161162

162163
CONFABLE_SCHEMA(BasicConfiguration) {
164+
// clang-format off
163165
return Schema{
164-
{"acc", Schema(&acc, "ACC configuration")},
165-
{"aeb", Schema(&aeb, "AEB configuration")},
166-
{"lka", Schema(&lka, "LKA configuration")},
166+
{"acc", Schema(&acc, "ACC configuration")},
167+
{"aeb", Schema(&aeb, "AEB configuration")},
168+
{"lka", Schema(&lka, "LKA configuration")},
169+
{"driver_request", Schema(&driver_request, "component providing driver request")},
167170
};
171+
// clang-format on
168172
}
169173
};
170174

0 commit comments

Comments
 (0)