Skip to content

Commit 157e999

Browse files
tobifalkcassava
authored andcommitted
models: Add vehicle state model component
1 parent 9e738c4 commit 157e999

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2022 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+
*/
18+
/**
19+
* \file cloe/component/vehicle_state_model.hpp
20+
*/
21+
22+
#pragma once
23+
#ifndef CLOE_COMPONENT_VEHICLE_STATE_MODEL_HPP_
24+
#define CLOE_COMPONENT_VEHICLE_STATE_MODEL_HPP_
25+
26+
#include <functional> // for function
27+
28+
#include <boost/optional.hpp> // for optional<>
29+
30+
#include <fable/json/with_boost.hpp> // for to_json
31+
#include <cloe/component.hpp> // for Component, Json
32+
#include <cloe/component/object.hpp> // for Object
33+
34+
namespace cloe {
35+
36+
class VehicleStateModel : public Component {
37+
public:
38+
using Component::Component;
39+
VehicleStateModel() : Component("vehicle_state") {}
40+
virtual ~VehicleStateModel() noexcept = default;
41+
42+
/**
43+
* Set the ego vehicle state corresponding to the end of the current step.
44+
*/
45+
virtual void set_vehicle_state(const Object& obj) { vehicle_state_ = obj; }
46+
47+
/**
48+
* Get the ego vehicle state at the end of the current step. Note that
49+
* vehicle_state() may invoke set_vehicle_state() by calling
50+
* vehicle_state_callback_().
51+
*/
52+
const boost::optional<Object>& vehicle_state() {
53+
if (vehicle_state_callback_) {
54+
vehicle_state_callback_();
55+
}
56+
return vehicle_state_;
57+
}
58+
59+
/**
60+
* Return true if set_vehicle_state was called for the current step. Note that
61+
* is_vehicle_state() may invoke set_vehicle_state() by calling
62+
* vehicle_state_callback_().
63+
*/
64+
bool is_vehicle_state() {
65+
if (vehicle_state_callback_) {
66+
vehicle_state_callback_();
67+
}
68+
return static_cast<bool>(vehicle_state_);
69+
}
70+
71+
/**
72+
* Register callback function that will invoke set_vehicle_state().
73+
*/
74+
void register_vehicle_state_callback(const std::function<void(void)>& c) {
75+
vehicle_state_callback_ = c;
76+
}
77+
78+
/**
79+
* Write the JSON representation of the vehicle state into j.
80+
*
81+
* Currently, the API is unstable, because we don't have access to any real
82+
* data.
83+
*/
84+
Json active_state() const override {
85+
return Json{
86+
{"vehicle_state", vehicle_state_},
87+
};
88+
}
89+
90+
Duration process(const Sync& sync) override {
91+
auto t = Component::process(sync);
92+
vehicle_state_.reset();
93+
return t;
94+
}
95+
96+
void reset() override {
97+
Component::reset();
98+
vehicle_state_.reset();
99+
}
100+
101+
protected:
102+
/**
103+
* Vehicle state determined by a vehicle dynamics model.
104+
*
105+
* Contains object pose, velocity, acceleration, angular_velocity in world coordinates.
106+
*/
107+
boost::optional<Object> vehicle_state_;
108+
109+
/**
110+
* Callback function that is invoked when access to the ego vehicle target
111+
* state is requested (by calling is_vehicle_state() or vehicle_state()).
112+
*
113+
* The main use case for the callback is to update the ego vehicle state using
114+
* an actuator and/or vehicle dynamics model external of the simulator. Then,
115+
* the callback could implement the following:
116+
* - Update the external model with the latest simulator state
117+
* - Trigger the time stepping of the external model
118+
* - Invoke set_vehicle_state() to update LatLongActuator
119+
*
120+
* Note that the callback function must ensure that repeated invocation within
121+
* the same time step does not lead to unintended behavior.
122+
*/
123+
std::function<void(void)> vehicle_state_callback_;
124+
};
125+
126+
} // namespace cloe
127+
128+
#endif // CLOE_COMPONENT_VEHICLE_STATE_MODEL_HPP_

0 commit comments

Comments
 (0)