$darkmode
osi_sensor_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  */
23 #pragma once
24 
25 #include <memory> // for shared_ptr<>, unique_ptr<>
26 #include <string> // for string, to_string
27 
28 #include <Eigen/Geometry> // for Isometry3d, Vector3d
29 
30 #include <cloe/component/object.hpp> // for Object
31 #include <cloe/utility/geometry.hpp> // for quaternion_from_rpy
32 #include <cloe/utility/osi_message_handler.hpp> // for OsiMsgHandler
33 #include <cloe/utility/osi_transceiver.hpp> // for OsiTransceiver
34 
35 #include "vtd_conf.hpp" // for VtdSensorConfig
36 #include "vtd_sensor_data.hpp" // for VtdSensorData
37 
38 namespace vtd {
39 
47 class VtdOsiSensor : public cloe::utility::OsiMsgHandler, public VtdSensorData {
48  public:
49  virtual ~VtdOsiSensor() = default;
50 
51  VtdOsiSensor(std::unique_ptr<cloe::utility::OsiTransceiver>&& osi_transceiver, uint64_t owner_id)
52  : OsiMsgHandler(std::move(osi_transceiver), owner_id), VtdSensorData("osi_sensor") {
53  ego_object_ = std::make_shared<cloe::Object>(); // NOLINT
54  }
55 
56  void configure(const VtdSensorConfig& cfg);
57 
58  void step(const cloe::Sync& s) override {
59  VtdSensorData::clear_cache();
60  OsiMsgHandler::process_osi_msgs<osi3::SensorData>(s, restart_, sensor_data_time_);
61  if (abs(sensor_data_time_.count() - sensor_data_time_next_.count()) >=
62  s.step_width().count() / 100) {
63  // Sensor data time deviates from expected time by more than 1% of the time step.
64  throw cloe::ModelError(
65  "VtdOsiSensor: Sensor data at wrong timestamp. Expected: {}. Actual: {}.",
66  sensor_data_time_next_.count(), sensor_data_time_.count());
67  }
69  }
70 
71  void store_object(std::shared_ptr<cloe::Object> obj) override { world_objects_.push_back(obj); }
72 
73  void store_lane_boundary(const cloe::LaneBoundary& lb) override { lanes_[lb.id] = lb; }
74 
75  void store_ego_object(std::shared_ptr<cloe::Object> ego_obj) override { ego_object_ = ego_obj; }
76 
77  void store_sensor_meta_data(const Eigen::Vector3d& bbcenter_to_veh_origin,
78  const Eigen::Vector3d& ego_dimensions) override {
79  mount_ = osi_sensor_pose_;
80  Eigen::Vector3d translation = osi_sensor_pose_.translation();
81  // Correct for the difference in reference frame z-location.
82  translation(2) = translation(2) + (0.5 * ego_dimensions(2) + bbcenter_to_veh_origin(2));
83  mount_.translation() = translation;
84  }
85 
90  Eigen::Isometry3d get_static_mounting_position(const Eigen::Vector3d& bbcenter_to_veh_origin,
91  const Eigen::Vector3d& ego_dimensions) override {
92  // VTD2.2: rotation order: "dhDeg (z-axis), dpDeg (y*-axis) and drDeg
93  // (x**-axis). Each rotation is performed in the system resulting from the
94  // previous rotation."
95  // OSI3 rotation order: "yaw first (around the z-axis), pitch second (around
96  // the new y-axis) and roll third (around the new x-axis)"
97  Eigen::Quaterniond quaternion = cloe::utility::quaternion_from_rpy(
98  vtd_mnt_ori_drpy_(0), vtd_mnt_ori_drpy_(1), vtd_mnt_ori_drpy_(2));
99  Eigen::Vector3d translation = vtd_mnt_pos_dxyz_;
100  // Correct for the difference in reference frame z-location:
101  // VTD: ground level; OSI: rear axle center.
102  translation(2) = translation(2) - (0.5 * ego_dimensions(2) + bbcenter_to_veh_origin(2));
103  return cloe::utility::pose_from_rotation_translation(quaternion, translation);
104  }
105 
109  void set_mock_conf(std::shared_ptr<const cloe::utility::SensorMockConf> mock) override {
110  this->mock_ = mock;
111  }
112 
113  // As defined in `cloe/component.hpp`
114  void reset() override {
115  VtdSensorData::clear_cache();
116  this->set_reset_state();
117  }
118 
119  friend void to_json(cloe::Json& j, const VtdOsiSensor& s) {
120  to_json(j, static_cast<const VtdSensorData&>(s));
121  j = cloe::Json{
122  {"osi_connection", s.osi_comm_},
123  };
124  }
125 
126  protected:
128  Eigen::Vector3d vtd_mnt_pos_dxyz_; // [m]
129  Eigen::Vector3d vtd_mnt_ori_drpy_; // [rad]
130 };
131 
132 } // namespace vtd
Definition: lane_boundary.hpp:36
Definition: model.hpp:62
Definition: sync.hpp:34
virtual Duration time() const =0
virtual Duration step_width() const =0
Definition: osi_sensor_component.hpp:47
Eigen::Isometry3d get_static_mounting_position(const Eigen::Vector3d &bbcenter_to_veh_origin, const Eigen::Vector3d &ego_dimensions) override
Definition: osi_sensor_component.hpp:90
void set_mock_conf(std::shared_ptr< const cloe::utility::SensorMockConf > mock) override
Definition: osi_sensor_component.hpp:109
Eigen::Vector3d vtd_mnt_pos_dxyz_
Sensor mounting position obtained from config (in VTD vehicle coordinate frame).
Definition: osi_sensor_component.hpp:128
void reset() override
Definition: osi_sensor_component.hpp:114
void step(const cloe::Sync &s) override
Definition: osi_sensor_component.hpp:58
Definition: vtd_sensor_data.hpp:39
Eigen::Isometry3d mount_
Sensor mounting position and orientation.
Definition: vtd_sensor_data.hpp:124
std::shared_ptr< cloe::Object > ego_object_
ego object information from last processed frame.
Definition: vtd_sensor_data.hpp:133
cloe::Duration sensor_data_time_
Simulation time from last processed sensor message.
Definition: vtd_sensor_data.hpp:118
bool restart_
Indicates whether reset has been requested.
Definition: vtd_sensor_data.hpp:115
virtual void set_reset_state()
Definition: vtd_sensor_data.hpp:79
cloe::Duration sensor_data_time_next_
Expected simulation time for next sensor message.
Definition: vtd_sensor_data.hpp:121
cloe::Objects world_objects_
World objects from last processed frame.
Definition: vtd_sensor_data.hpp:130
cloe::LaneBoundaries lanes_
Lane id-to-boundary-map.
Definition: vtd_sensor_data.hpp:139