|
| 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/powertrain_sensor.hpp |
| 20 | + */ |
| 21 | + |
| 22 | +#pragma once |
| 23 | +#ifndef CLOE_COMPONENT_POWERTRAIN_SENSOR_HPP_ |
| 24 | +#define CLOE_COMPONENT_POWERTRAIN_SENSOR_HPP_ |
| 25 | + |
| 26 | +#include <cloe/component.hpp> // for Component, Json |
| 27 | + |
| 28 | +namespace cloe { |
| 29 | + |
| 30 | +class PowertrainSensor : public Component { |
| 31 | + public: |
| 32 | + using Component::Component; |
| 33 | + PowertrainSensor() : Component("powertrain_sensor") {} |
| 34 | + virtual ~PowertrainSensor() noexcept = default; |
| 35 | + |
| 36 | + /** |
| 37 | + * Return the position of the acceleration pedal with no unit. |
| 38 | + * |
| 39 | + * The range goes from 0 (unpressed) to 1 (fully pressed). |
| 40 | + */ |
| 41 | + virtual double pedal_position_acceleration() const = 0; |
| 42 | + |
| 43 | + /** |
| 44 | + * Return the gear transmission. |
| 45 | + * |
| 46 | + * The sign of this field is linked to the mode of the gear |
| 47 | + * - positive: driving forward (e.g. a value of 3 means the third gear in driving forward mode) |
| 48 | + * - 0: means that the gear lever is in neutral position |
| 49 | + * - negative: reverse mode (e.g. a value of -1 means the first gear in reverse mode) |
| 50 | + * - int max: means that the transmission is in parking position (can be accessed via std::numeric_limits<int>::max()) |
| 51 | + */ |
| 52 | + virtual int gear_transmission() const = 0; |
| 53 | + |
| 54 | + /** |
| 55 | + * Return sensor state as JSON. |
| 56 | + */ |
| 57 | + Json active_state() const override { |
| 58 | + return Json{ |
| 59 | + {"pedal_position_acceleration", pedal_position_acceleration()}, |
| 60 | + {"gear_transmission", gear_transmission()}, |
| 61 | + }; |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * NopPowertrainSensor is an example no-op implementation of PowertrainSensor. |
| 67 | + */ |
| 68 | +class NopPowertrainSensor : public PowertrainSensor { |
| 69 | + public: |
| 70 | + using PowertrainSensor::PowertrainSensor; |
| 71 | + NopPowertrainSensor() : PowertrainSensor("nop_powertrain_sensor") {} |
| 72 | + virtual ~NopPowertrainSensor() noexcept {}; |
| 73 | + |
| 74 | + double pedal_position_acceleration() const override { return pedal_position_acceleration_; } |
| 75 | + |
| 76 | + int gear_transmission() const override { return gear_transmission_; } |
| 77 | + |
| 78 | + void reset() override { |
| 79 | + pedal_position_acceleration_ = 0.0; |
| 80 | + gear_transmission_ = 0; |
| 81 | + } |
| 82 | + |
| 83 | + protected: |
| 84 | + double pedal_position_acceleration_{0.0}; |
| 85 | + int gear_transmission_{0}; |
| 86 | +}; |
| 87 | + |
| 88 | +} // namespace cloe |
| 89 | + |
| 90 | +#endif // CLOE_COMPONENT_POWERTRAIN_SENSOR_HPP_ |
0 commit comments