|
1 |
| -/* MIT License |
2 |
| - * |
3 |
| - * Copyright (c) 2023 - 2024 Andreas Merkle <web@blue-andi.de> |
4 |
| - * |
5 |
| - * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 |
| - * of this software and associated documentation files (the "Software"), to deal |
7 |
| - * in the Software without restriction, including without limitation the rights |
8 |
| - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9 |
| - * copies of the Software, and to permit persons to whom the Software is |
10 |
| - * furnished to do so, subject to the following conditions: |
11 |
| - * |
12 |
| - * The above copyright notice and this permission notice shall be included in all |
13 |
| - * copies or substantial portions of the Software. |
14 |
| - * |
15 |
| - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 |
| - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 |
| - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18 |
| - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 |
| - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 |
| - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 |
| - * SOFTWARE. |
22 |
| - */ |
23 |
| - |
24 |
| -/******************************************************************************* |
25 |
| - DESCRIPTION |
26 |
| -*******************************************************************************/ |
27 |
| -/** |
28 |
| - * @brief Concrete Longitudinal Controller. |
29 |
| - * @author Gabryel Reyes <gabryelrdiaz@gmail.com> |
30 |
| - */ |
31 |
| - |
32 |
| -/****************************************************************************** |
33 |
| - * Includes |
34 |
| - *****************************************************************************/ |
35 |
| - |
36 |
| -#include "LongitudinalController.h" |
37 |
| -#include "PlatoonUtils.h" |
38 |
| -#include <Arduino.h> |
39 |
| - |
40 |
| -/****************************************************************************** |
41 |
| - * Compiler Switches |
42 |
| - *****************************************************************************/ |
43 |
| - |
44 |
| -/****************************************************************************** |
45 |
| - * Macros |
46 |
| - *****************************************************************************/ |
47 |
| - |
48 |
| -/****************************************************************************** |
49 |
| - * Types and classes |
50 |
| - *****************************************************************************/ |
51 |
| - |
52 |
| -/****************************************************************************** |
53 |
| - * Prototypes |
54 |
| - *****************************************************************************/ |
55 |
| - |
56 |
| -/****************************************************************************** |
57 |
| - * Local Variables |
58 |
| - *****************************************************************************/ |
59 |
| - |
60 |
| -/****************************************************************************** |
61 |
| - * Public Methods |
62 |
| - *****************************************************************************/ |
63 |
| - |
64 |
| -LongitudinalController::LongitudinalController() : ILongitudinalController() |
65 |
| -{ |
66 |
| -} |
67 |
| - |
68 |
| -LongitudinalController::~LongitudinalController() |
69 |
| -{ |
70 |
| -} |
71 |
| - |
72 |
| -bool LongitudinalController::calculateLongitudinalMovement(const Waypoint& currentWaypoint, |
73 |
| - const Waypoint& targetWaypoint, int16_t& centerSpeedSetpoint) |
74 |
| -{ |
75 |
| - bool isSuccessful = true; |
76 |
| - int32_t distance = PlatoonUtils::calculateAbsoluteDistance(targetWaypoint, currentWaypoint); |
77 |
| - |
78 |
| - if (0 == distance) |
79 |
| - { |
80 |
| - /* Target reached. */ |
81 |
| - centerSpeedSetpoint = 0; |
82 |
| - } |
83 |
| - else |
84 |
| - { |
85 |
| - /* Calculate speed using ramp factor. */ |
86 |
| - centerSpeedSetpoint = constrain(((distance * RAMP_FACTOR) + OFFSET_SPEED), -MAX_MOTOR_SPEED, MAX_MOTOR_SPEED); |
87 |
| - } |
88 |
| - |
89 |
| - return isSuccessful; |
90 |
| -} |
91 |
| - |
92 |
| -/****************************************************************************** |
93 |
| - * Protected Methods |
94 |
| - *****************************************************************************/ |
95 |
| - |
96 |
| -/****************************************************************************** |
97 |
| - * Private Methods |
98 |
| - *****************************************************************************/ |
99 |
| - |
100 |
| -/****************************************************************************** |
101 |
| - * External Functions |
102 |
| - *****************************************************************************/ |
103 |
| - |
104 |
| -/****************************************************************************** |
105 |
| - * Local Functions |
106 |
| - *****************************************************************************/ |
| 1 | +/* MIT License |
| 2 | + * |
| 3 | + * Copyright (c) 2023 - 2024 Andreas Merkle <web@blue-andi.de> |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +/******************************************************************************* |
| 25 | + DESCRIPTION |
| 26 | +*******************************************************************************/ |
| 27 | +/** |
| 28 | + * @brief Driving state. |
| 29 | + * @author Gabryel Reyes <gabryelrdiaz@gmail.com> |
| 30 | + */ |
| 31 | + |
| 32 | +/****************************************************************************** |
| 33 | + * Includes |
| 34 | + *****************************************************************************/ |
| 35 | + |
| 36 | +#include "DrivingState.h" |
| 37 | + |
| 38 | +/****************************************************************************** |
| 39 | + * Compiler Switches |
| 40 | + *****************************************************************************/ |
| 41 | + |
| 42 | +/****************************************************************************** |
| 43 | + * Macros |
| 44 | + *****************************************************************************/ |
| 45 | + |
| 46 | +/****************************************************************************** |
| 47 | + * Types and classes |
| 48 | + *****************************************************************************/ |
| 49 | + |
| 50 | +/****************************************************************************** |
| 51 | + * Prototypes |
| 52 | + *****************************************************************************/ |
| 53 | + |
| 54 | +/****************************************************************************** |
| 55 | + * Local Variables |
| 56 | + *****************************************************************************/ |
| 57 | + |
| 58 | +/****************************************************************************** |
| 59 | + * Public Methods |
| 60 | + *****************************************************************************/ |
| 61 | + |
| 62 | +void DrivingState::entry() |
| 63 | +{ |
| 64 | + m_isActive = true; |
| 65 | +} |
| 66 | + |
| 67 | +void DrivingState::process(StateMachine& sm) |
| 68 | +{ |
| 69 | + /* Check if the state is active. */ |
| 70 | + if (false == m_isActive) |
| 71 | + { |
| 72 | + m_topMotorSpeed = 0; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + /* Check limits. */ |
| 77 | + |
| 78 | + /* Check follower feedback. Calculate platoon length and react accordingly */ |
| 79 | + |
| 80 | + /* Calculate top motor speed. */ |
| 81 | + m_topMotorSpeed = m_maxMotorSpeed; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void DrivingState::exit() |
| 86 | +{ |
| 87 | + m_isActive = false; |
| 88 | +} |
| 89 | + |
| 90 | +void DrivingState::setMaxMotorSpeed(int16_t maxSpeed) |
| 91 | +{ |
| 92 | + m_maxMotorSpeed = maxSpeed; |
| 93 | +} |
| 94 | + |
| 95 | +bool DrivingState::getTopMotorSpeed(int16_t& topMotorSpeed) const |
| 96 | +{ |
| 97 | + topMotorSpeed = m_topMotorSpeed; |
| 98 | + |
| 99 | + /* Only valid if the state is active. */ |
| 100 | + return m_isActive; |
| 101 | +} |
| 102 | + |
| 103 | +void DrivingState::setVehicleData(const VehicleData& vehicleData) |
| 104 | +{ |
| 105 | + m_vehicleData = vehicleData; |
| 106 | +} |
| 107 | + |
| 108 | +void DrivingState::setLastFollowerFeedback(const VehicleData& feedback) |
| 109 | +{ |
| 110 | + m_followerFeedback = feedback; |
| 111 | +} |
| 112 | + |
| 113 | +/****************************************************************************** |
| 114 | + * Protected Methods |
| 115 | + *****************************************************************************/ |
| 116 | + |
| 117 | +/****************************************************************************** |
| 118 | + * Private Methods |
| 119 | + *****************************************************************************/ |
| 120 | + |
| 121 | +/****************************************************************************** |
| 122 | + * External Functions |
| 123 | + *****************************************************************************/ |
| 124 | + |
| 125 | +/****************************************************************************** |
| 126 | + * Local Functions |
| 127 | + *****************************************************************************/ |
0 commit comments