$darkmode
actuator_component.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
25 #pragma once
26 
27 #include <memory> // for shared_ptr<>
28 
29 #include <cloe/component/latlong_actuator.hpp> // for LatLongActuator
30 #include <cloe/component/vehicle_state_model.hpp> // for VehicleStateModel
31 
32 #include "task_control.hpp" // for TaskControl
33 #include "vtd_logger.hpp" // for vtd_logger
34 
35 namespace vtd {
36 
38  public:
39  VtdVehicleControl() = default;
40  virtual ~VtdVehicleControl() = default;
41 
49  virtual void step_begin(const cloe::Sync& sync) = 0;
50 
55  virtual void step_end(const cloe::Sync&){};
56 
60  virtual bool update_vehicle_label() { return false; }
61 
66  return cloe::utility::ActuationLevel::None;
67  }
68 
69  virtual void reset(){};
70 
71  virtual cloe::Json to_json() const = 0;
72 
73  friend void to_json(cloe::Json& j, const VtdVehicleControl& vc) { j = vc.to_json(); }
74 };
75 
91  public:
92  VtdLatLongActuator(std::shared_ptr<TaskControl> tc, uint64_t id)
93  : LatLongActuator("vtd/lat_long_actuator"), task_control_(tc), vehicle_id_(id) {}
94  virtual ~VtdLatLongActuator() = default;
95 
107  bool update_vehicle_label() override { return this->has_level_change(); }
108 
110 
111  cloe::Duration process(const cloe::Sync& sync) override { return LatLongActuator::process(sync); }
112 
113  void reset() override {
114  old_level_.set_none();
115  LatLongActuator::reset();
116  task_control_->reset();
117  }
118 
119  void step_begin(const cloe::Sync&) override {
120  add_driver_control();
121  // Detect driver or controller takeover for lateral and/or longitudinal control
122  if (this->has_level_change()) {
123  vtd_logger()->info("VtdLatLongActuator: vehicle {} controller state: {}", id(),
124  level_.to_human_cstr());
125  }
126  }
127 
128  void step_end(const cloe::Sync&) override { this->old_level_ = this->level_; }
129 
130  cloe::Json to_json() const override { return dynamic_cast<const cloe::LatLongActuator&>(*this); }
131 
132  private:
133  bool has_level_change() { return old_level_ != this->level_; }
134 
135  void add_driver_control() {
136  DriverControl dc;
137  dc.player_id = vehicle_id_;
138 
139  if (is_acceleration()) {
140  dc.target_acceleration = *acceleration();
141  dc.validity_flags |= RDB_DRIVER_INPUT_VALIDITY_TGT_ACCEL;
142  }
143 
144  if (is_steering_angle()) {
145  dc.target_steering = *steering_angle();
146  dc.validity_flags |= RDB_DRIVER_INPUT_VALIDITY_TGT_STEERING;
147  }
148 
149  if (is_acceleration() || is_steering_angle()) {
150  dc.validity_flags |= RDB_DRIVER_INPUT_VALIDITY_ADD_ON;
151  task_control_->add_driver_control(dc);
152  }
153  }
154 
155  private:
156  std::shared_ptr<TaskControl> task_control_;
157  uint64_t vehicle_id_;
159 };
160 
174  public:
175  VtdExternalEgoModel(std::shared_ptr<TaskControl> tc, uint64_t id, const std::string& veh_name)
176  : VehicleStateModel("vtd/ego_state")
177  , task_control_(tc)
178  , vehicle_id_(id)
179  , vehicle_name_(veh_name) {}
180  virtual ~VtdExternalEgoModel() = default;
181 
189  void step_begin(const cloe::Sync& sync) override {
190  if (this->is_vehicle_state()) {
191  add_dyn_object_state();
192  } else if (sync.step() > 1) {
193  // During the first time step, external model has not yet been updated. Throw otherwise.
194  throw cloe::ModelError("VtdExternalEgoModel: vehicle state not set.");
195  }
196  }
197 
198  void reset() override {
199  VehicleStateModel::reset();
200  task_control_->reset();
201  }
202 
203  cloe::Json to_json() const override {
204  return dynamic_cast<const cloe::VehicleStateModel&>(*this);
205  }
206 
207  private:
208  void add_dyn_object_state() {
209  auto ego_state = this->vehicle_state();
210  assert(ego_state);
211  DynObjectState os;
212  assert(static_cast<uint64_t>(ego_state->id) == vehicle_id_);
213  os.base_id = ego_state->id;
214  os.base_type = cloe_vtd_obj_class_map.at(ego_state->classification);
215  os.base_name = vehicle_name_;
216  os.base_geo = rdb_geometry_from_object(ego_state.get());
217  os.base_pos = rdb_coord_from_object(ego_state.get());
218  os.ext_speed = rdb_coord_from_vector3d(ego_state->velocity, ego_state->angular_velocity);
219  os.ext_accel = rdb_coord_pos_from_vector3d(ego_state->acceleration);
220  // Add new ego state to task control message.
221  task_control_->add_dyn_object_state(os);
222  }
223 
224  private:
225  std::shared_ptr<TaskControl> task_control_;
226  uint64_t vehicle_id_;
227  std::string vehicle_name_;
228 };
229 
230 } // namespace vtd
Definition: latlong_actuator.hpp:37
virtual bool is_acceleration() const
Definition: latlong_actuator.hpp:57
virtual bool is_steering_angle() const
Definition: latlong_actuator.hpp:75
utility::ActuationLevel actuation_level() const
Definition: latlong_actuator.hpp:82
Definition: model.hpp:62
Definition: sync.hpp:34
virtual uint64_t step() const =0
Definition: vehicle_state_model.hpp:35
bool is_vehicle_state()
Definition: vehicle_state_model.hpp:63
const boost::optional< Object > & vehicle_state()
Definition: vehicle_state_model.hpp:51
Definition: actuation_level.hpp:48
Definition: actuator_component.hpp:173
void reset() override
Definition: actuator_component.hpp:198
void step_begin(const cloe::Sync &sync) override
Definition: actuator_component.hpp:189
Definition: actuator_component.hpp:90
void reset() override
Definition: actuator_component.hpp:113
bool update_vehicle_label() override
Definition: actuator_component.hpp:107
void step_begin(const cloe::Sync &) override
Definition: actuator_component.hpp:119
cloe::Duration process(const cloe::Sync &sync) override
Definition: actuator_component.hpp:111
cloe::utility::ActuationLevel get_actuation_level() override
Definition: actuator_component.hpp:109
void step_end(const cloe::Sync &) override
Definition: actuator_component.hpp:128
Definition: actuator_component.hpp:37
virtual void step_end(const cloe::Sync &)
Definition: actuator_component.hpp:55
virtual cloe::utility::ActuationLevel get_actuation_level()
Definition: actuator_component.hpp:65
virtual bool update_vehicle_label()
Definition: actuator_component.hpp:60
virtual void step_begin(const cloe::Sync &sync)=0
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36