|
| 1 | +// Copyright 2026 TIER IV, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "autoware/mpc_lateral_controller/steering_predictor.hpp" |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include <cmath> |
| 20 | + |
| 21 | +namespace |
| 22 | +{ |
| 23 | +using autoware::motion::control::mpc_lateral_controller::SteeringPredictor; |
| 24 | + |
| 25 | +constexpr double steer_tau = 0.1; |
| 26 | +constexpr double steer_delay = 0.0; |
| 27 | +/// One cycle of the runs below. The answer of a cycle does not yet hold the command given |
| 28 | +/// in that same cycle, so the cycle is short against the time constant to keep that delay |
| 29 | +/// inside the tolerance the tests allow. |
| 30 | +constexpr double cycle = 0.001; |
| 31 | + |
| 32 | +/// The angle the steering is asked to hold throughout each test below. |
| 33 | +constexpr double commanded_angle = 0.2; |
| 34 | + |
| 35 | +rclcpp::Time at(const double seconds) |
| 36 | +{ |
| 37 | + return rclcpp::Time(static_cast<int64_t>(seconds * 1e9), RCL_ROS_TIME); |
| 38 | +} |
| 39 | + |
| 40 | +/// Issue the same command every cycle until the given time, and report the angle the |
| 41 | +/// predictor arrives at. |
| 42 | +/// |
| 43 | +/// The predictor answers with the response of a first order lag to the commands it has |
| 44 | +/// been given, so a run that holds one command is a step response. A command is stored |
| 45 | +/// before the prediction of the same cycle is read, which is the order the controller |
| 46 | +/// uses. |
| 47 | +double hold_command_until(SteeringPredictor & predictor, const double seconds) |
| 48 | +{ |
| 49 | + double predicted = 0.0; |
| 50 | + for (double time = 0.0; time <= seconds + 0.5 * cycle; time += cycle) { |
| 51 | + predictor.storeSteerCmd(commanded_angle, at(time)); |
| 52 | + predicted = predictor.calcSteerPrediction(at(time)); |
| 53 | + } |
| 54 | + return predicted; |
| 55 | +} |
| 56 | +} // namespace |
| 57 | + |
| 58 | +/// A first order lag reaches 63.2 per cent of the command after one time constant. |
| 59 | +TEST(SteeringPredictorTest, ReachesSixtyThreePerCentAfterOneTimeConstant) |
| 60 | +{ |
| 61 | + SteeringPredictor predictor(steer_tau, steer_delay); |
| 62 | + |
| 63 | + const double predicted = hold_command_until(predictor, steer_tau); |
| 64 | + |
| 65 | + EXPECT_NEAR(predicted, commanded_angle * (1.0 - std::exp(-1.0)), 0.01 * commanded_angle); |
| 66 | +} |
| 67 | + |
| 68 | +/// The same lag reaches 95 per cent after three time constants. |
| 69 | +TEST(SteeringPredictorTest, ReachesNinetyFivePerCentAfterThreeTimeConstants) |
| 70 | +{ |
| 71 | + SteeringPredictor predictor(steer_tau, steer_delay); |
| 72 | + |
| 73 | + const double predicted = hold_command_until(predictor, 3.0 * steer_tau); |
| 74 | + |
| 75 | + EXPECT_NEAR(predicted, commanded_angle * (1.0 - std::exp(-3.0)), 0.01 * commanded_angle); |
| 76 | +} |
0 commit comments