LCOV - code coverage report
Current view: top level - impl/test/test_support - TestSupport.hpp (source / functions) Hit Total Coverage
Test: ad_rss Lines: 34 34 100.0 %
Date: 2021-08-31 11:34:43 Functions: 8 8 100.0 %
Branches: 2 4 50.0 %

           Branch data     Line data    Source code
       1                 :            : // ----------------- BEGIN LICENSE BLOCK ---------------------------------
       2                 :            : //
       3                 :            : // Copyright (C) 2018-2020 Intel Corporation
       4                 :            : //
       5                 :            : // SPDX-License-Identifier: LGPL-2.1-only
       6                 :            : //
       7                 :            : // ----------------- END LICENSE BLOCK -----------------------------------
       8                 :            : #pragma once
       9                 :            : 
      10                 :            : #include <cmath>
      11                 :            : #include <gtest/gtest.h>
      12                 :            : #include <limits>
      13                 :            : #include "RssTestParameters.hpp"
      14                 :            : #include "ad/physics/Operation.hpp"
      15                 :            : #include "ad/rss/situation/Physics.hpp"
      16                 :            : #include "ad/rss/situation/RelativePosition.hpp"
      17                 :            : #include "ad/rss/situation/Situation.hpp"
      18                 :            : #include "ad/rss/situation/VehicleState.hpp"
      19                 :            : #include "ad/rss/state/ProperResponse.hpp"
      20                 :            : #include "ad/rss/state/RssState.hpp"
      21                 :            : #include "ad/rss/unstructured/Geometry.hpp"
      22                 :            : #include "ad/rss/world/Object.hpp"
      23                 :            : #include "ad/rss/world/RoadSegment.hpp"
      24                 :            : 
      25                 :            : namespace ad {
      26                 :            : namespace rss {
      27                 :            : 
      28                 :            : // make the code more readable
      29                 :            : using physics::Acceleration;
      30                 :            : using physics::Distance;
      31                 :            : using physics::Duration;
      32                 :            : using physics::ParametricValue;
      33                 :            : using physics::Speed;
      34                 :            : 
      35                 :            : const double cDoubleNear(0.01);
      36                 :            : 
      37                 :            : #define ARRAYLEN(a) (sizeof(a) / sizeof(a[0]))
      38                 :            : 
      39                 :            : /**
      40                 :            :  * @brief resets the RSS state to it's safe state
      41                 :            :  *
      42                 :            :  * @param[in/out] state the longitudinal RSS state to be reset
      43                 :            :  */
      44                 :            : void resetRssState(state::LongitudinalRssState &state);
      45                 :            : 
      46                 :            : /**
      47                 :            :  * @brief resets the RSS state to it's safe state
      48                 :            :  *
      49                 :            :  * @param[in/out] state the lateral RSS state to be reset
      50                 :            :  */
      51                 :            : void resetRssState(state::LateralRssState &state);
      52                 :            : 
      53                 :            : /**
      54                 :            :  * @brief resets the RSS state to it's safe state
      55                 :            :  *
      56                 :            :  * @param[in/out] state the unstructured RSS state to be reset
      57                 :            :  */
      58                 :            : void resetRssState(state::UnstructuredSceneRssState &state);
      59                 :            : 
      60                 :            : /**
      61                 :            :  * @brief resets the RSS state within the RssState to it's safe state
      62                 :            :  *
      63                 :            :  * @param[in/out] rssState the response state to be reset
      64                 :            :  * @param[in] situationId the situation id to be set within the RssState
      65                 :            :  * @param[in] objectId the object id to be set within the RssState
      66                 :            :  * @param[in] situationType the situation type to be set within the RssState
      67                 :            :  */
      68                 :            : void resetRssState(state::RssState &rssState,
      69                 :            :                    situation::SituationId const situationId,
      70                 :            :                    world::ObjectId const objectId,
      71                 :            :                    situation::SituationType const situationType);
      72                 :            : 
      73                 :            : /**
      74                 :            :  * @brief resets the RSS state of the proper response to it's safe state
      75                 :            :  *
      76                 :            :  * @param[in/out] properResponse the the proper response to be reset
      77                 :            :  */
      78                 :            : void resetRssState(state::ProperResponse &properResponse);
      79                 :            : 
      80                 :            : /**
      81                 :            :  * @brief sets the RSS state to a specific unsafe state
      82                 :            :  *
      83                 :            :  * @param[in/out] state the lateral/longitudinal RSS state to be set
      84                 :            :  * @param[in] literal the response literal of the respective RSS state to be set
      85                 :            :  */
      86                 :            : template <typename RSSState, typename RSSStateLiteral>
      87                 :         34 : void setRssStateUnsafe(RSSState &state, RSSStateLiteral const literal)
      88                 :            : {
      89                 :         34 :   state.response = literal;
      90                 :         34 :   state.isSafe = false;
      91                 :         34 : }
      92                 :            : 
      93                 :            : /**
      94                 :            :  * @brief converts km/h to m/sec
      95                 :            :  *
      96                 :            :  * @param[in] speed the speed provided in km/h
      97                 :            :  *
      98                 :            :  * @returns the speed as physics::Speed
      99                 :            :  */
     100                 :      51454 : inline physics::Speed kmhToMeterPerSec(double const speed)
     101                 :            : {
     102                 :      51454 :   return Speed(speed / 3.6);
     103                 :            : }
     104                 :            : 
     105                 :            : /**
     106                 :            :  * @brief convert an object into an ego vehicle object
     107                 :            :  *
     108                 :            :  * @param[in] iObject the object to be taken as basis
     109                 :            :  *
     110                 :            :  * @returns a copy of the object with object type EgoVehicle
     111                 :            :  */
     112                 :        847 : inline world::Object objectAsEgo(world::Object const &iObject)
     113                 :            : {
     114                 :        847 :   world::Object object(iObject);
     115                 :        847 :   object.objectType = world::ObjectType::EgoVehicle;
     116                 :        847 :   return object;
     117                 :            : }
     118                 :            : 
     119                 :            : /**
     120                 :            :  * @return RssDynamics used within tests
     121                 :            :  *
     122                 :            :  */
     123                 :            : world::RssDynamics getObjectRssDynamics();
     124                 :            : 
     125                 :            : /**
     126                 :            :  * @return RssDynamics used within tests
     127                 :            :  *
     128                 :            :  */
     129                 :            : world::RssDynamics getEgoRssDynamics();
     130                 :            : 
     131                 :            : /**
     132                 :            :  * @brief create an object
     133                 :            :  *
     134                 :            :  * @param[in] lonVelocity the longitudinal velocity to be applied
     135                 :            :  * @param[in] latVelocity the lateral velocity to be applied
     136                 :            :  *
     137                 :            :  * @returns an object with applied lon/lat velocities
     138                 :            :  */
     139                 :            : world::Object createObject(double const lonVelocity, double const latVelocity);
     140                 :            : 
     141                 :            : /**
     142                 :            :  * @brief create a vehicle state
     143                 :            :  *
     144                 :            :  * @param[in] objectType type of object
     145                 :            :  * @param[in] lonVelocity the longitudinal velocity to be applied
     146                 :            :  * @param[in] latVelocity the lateral velocity to be applied
     147                 :            :  *
     148                 :            :  * @returns a vehicle state with applied lon/lat velocities
     149                 :            :  */
     150                 :            : situation::VehicleState
     151                 :            : createVehicleState(world::ObjectType const objectType, double const lonVelocity, double const latVelocity);
     152                 :            : 
     153                 :            : /**
     154                 :            :  * @brief create an object state
     155                 :            :  *
     156                 :            :  * @param[in] lonVelocity the longitudinal velocity to be applied
     157                 :            :  * @param[in] latVelocity the lateral velocity to be applied
     158                 :            :  *
     159                 :            :  * @returns an object state with applied lon/lat velocities
     160                 :            :  */
     161                 :            : world::ObjectState createObjectState(double const lonVelocity, double const latVelocity);
     162                 :            : 
     163                 :            : /**
     164                 :            :  * @brief create a vehicle state for longitudianl motion
     165                 :            :  *
     166                 :            :  * lateral velocity becomes 0.
     167                 :            :  *
     168                 :            :  * @param[in] lonVelocity the longitudinal velocity to be applied
     169                 :            :  *
     170                 :            :  * @returns a vehicle state with applied lon velocity
     171                 :            :  */
     172                 :        153 : inline situation::VehicleState createVehicleStateForLongitudinalMotion(double const lonVelocity)
     173                 :            : {
     174                 :        153 :   return createVehicleState(world::ObjectType::OtherVehicle, lonVelocity, 0.);
     175                 :            : }
     176                 :            : 
     177                 :            : /**
     178                 :            :  * @brief create a vehicle state for longitudianl motion
     179                 :            :  *
     180                 :            :  * longitudinal velocity becomes 0.
     181                 :            :  *
     182                 :            :  * @param[in] latVelocity the lateral velocity to be applied
     183                 :            :  *
     184                 :            :  * @returns a vehicle state with applied lat velocity
     185                 :            :  */
     186                 :         46 : inline situation::VehicleState createVehicleStateForLateralMotion(double const latVelocity)
     187                 :            : {
     188                 :         46 :   return createVehicleState(world::ObjectType::OtherVehicle, 0., latVelocity);
     189                 :            : }
     190                 :            : 
     191                 :            : /**
     192                 :            :  * @brief create a relative longitudinal position object
     193                 :            :  *
     194                 :            :  * @param[in] position the longitudinal relative position to be applied
     195                 :            :  * @param[in] distance the distance to be applied
     196                 :            :  *
     197                 :            :  * @returns a relative longitudinal position object
     198                 :            :  */
     199                 :            : situation::RelativePosition createRelativeLongitudinalPosition(situation::LongitudinalRelativePosition const &position,
     200                 :            :                                                                Distance const &distance = Distance(0.));
     201                 :            : 
     202                 :            : /**
     203                 :            :  * @brief create a relative lateral position object
     204                 :            :  *
     205                 :            :  * @param[in] position the lateral relative position to be applied
     206                 :            :  * @param[in] distance the distance to be applied
     207                 :            :  *
     208                 :            :  * @returns a relative lateral position object
     209                 :            :  */
     210                 :            : situation::RelativePosition createRelativeLateralPosition(situation::LateralRelativePosition const &position,
     211                 :            :                                                           Distance const &distance = Distance(0.));
     212                 :            : 
     213                 :            : /**
     214                 :            :  * @brief calculate the longitudinal stopping distance
     215                 :            :  *
     216                 :            :  * @param[in] objectSpeed the objectSpeed used for the calculation
     217                 :            :  * @param[in] objectSpeed the object maximum speed used for the calculation
     218                 :            :  * @param[in] acceleration the acceleration to be applied for acceleration within response time
     219                 :            :  * @param[in] deceleration the deceleration to be applied for braking
     220                 :            :  * @param[in] responseTime the responseTime to be applied before braking
     221                 :            :  *
     222                 :            :  * @returns the distance required to stop when applying accelMax during the responseTime and then brake to the full stop
     223                 :            :  */
     224                 :            : Distance calculateLongitudinalStoppingDistance(Speed const &objectSpeed,
     225                 :            :                                                Speed const &objectMaxSpeedOnAcceleration,
     226                 :            :                                                Acceleration const &acceleration,
     227                 :            :                                                Acceleration const &deceleration,
     228                 :            :                                                Duration const &responseTime);
     229                 :            : 
     230                 :            : /**
     231                 :            :  * @brief calculate the longitudinal min safe distance in following >>> leading configuration
     232                 :            :  *
     233                 :            :  * @param[in] followingObjectSpeed the object following at back: velocity
     234                 :            :  * @param[in] followingObjectRssDynamics the object following at back: RSS dynamics
     235                 :            :  * @param[in] leadingObjectSpeed the object leading in front: velocity
     236                 :            :  * @param[in] leadingObjectRssDynamics the object leading in front: RSS dynamics
     237                 :            :  *
     238                 :            :  * @returns the minimum safe distance required that the following object does not crash into the leading object
     239                 :            :  *  in case the leading object performs a brake with brakeMax and following object performs a stated braking pattern
     240                 :            :  *  with (see calculateLongitudinalStoppingDistance) with brakeMin
     241                 :            :  */
     242                 :            : Distance calculateLongitudinalMinSafeDistance(physics::Speed const &followingObjectSpeed,
     243                 :            :                                               world::RssDynamics const &followingObjectRssDynamics,
     244                 :            :                                               physics::Speed const &leadingObjectSpeed,
     245                 :            :                                               world::RssDynamics const &leadingObjectRssDynamics);
     246                 :            : 
     247                 :            : /**
     248                 :            :  * @brief calculate the longitudinal min safe distance in opposite direction configuration with
     249                 :            :  *   one object in correct lane and the other in the wrong
     250                 :            :  *
     251                 :            :  * @param[in] objectInCorrectLaneSpeed the object driving in the correct lane direction: velocity
     252                 :            :  * @param[in] objectInCorrectLaneRssDynamics the object driving in the correct lane direction: RSS dynamics
     253                 :            :  * @param[in] objectNotInCorrectLaneSpeed the object driving in the wrong lane: velocity
     254                 :            :  * @param[in] objectNotInCorrectLaneRssDynamics the object driving in the wrong lane: RSS dynamics
     255                 :            :  *
     256                 :            :  * @returns the minimum safe distance required that both objects are still able to break and not crash into each other.
     257                 :            :  *  The object in correct lane performs a stated braking pattern (see calculateLongitudinalStoppingDistance) with
     258                 :            :  * brakeMinCorrect;
     259                 :            :  *  the object in wrong lane performs a stated braking pattern with brakeMin.
     260                 :            :  */
     261                 :            : Distance
     262                 :            : calculateLongitudinalMinSafeDistanceOppositeDirection(physics::Speed const &objectInCorrectLaneSpeed,
     263                 :            :                                                       world::RssDynamics const &objectInCorrectLaneRssDynamics,
     264                 :            :                                                       physics::Speed const &objectNotInCorrectLaneSpeed,
     265                 :            :                                                       world::RssDynamics const &objectNotInCorrectLaneRssDynamics);
     266                 :            : 
     267                 :            : /**
     268                 :            :  * @brief calculate the lateral min safe distance of two objects
     269                 :            :  *
     270                 :            :  * @param[in] leftObjectSpeed the object driving on the left side: velocity
     271                 :            :  * @param[in] leftObjectRssDynamics the object driving on the left side: RSS dynamics
     272                 :            :  * @param[in] rightObjectSpeed the object driving on the right side: velocity
     273                 :            :  * @param[in] rightObjectRssDynamics the object driving on the right side: RSS dynamics
     274                 :            :  *
     275                 :            :  * @returns the minimum safe distance required that both objects are still able to break and not crash into each other.
     276                 :            :  *  Both objects perform a stated braking pattern with brakeMin in lateral direction.
     277                 :            :  */
     278                 :            : Distance calculateLateralMinSafeDistance(physics::Speed const &leftObjectSpeed,
     279                 :            :                                          world::RssDynamics const &leftObjectRssDynamics,
     280                 :            :                                          physics::Speed const &rightObjectSpeed,
     281                 :            :                                          world::RssDynamics const &rightObjectRssDynamics);
     282                 :            : 
     283                 :            : /**
     284                 :            :  * @brief update the vehicle state and unstructured scene state info
     285                 :            :  *
     286                 :            :  * @param[in] backLeft position of the vehicle
     287                 :            :  * @param[in] positiveDirection direction selection
     288                 :            :  * @param[out] stateInfo unstructured state info
     289                 :            :  * @param[out] vehicleState resulting vehicle state
     290                 :            :  */
     291                 :            : void getUnstructuredVehicle(unstructured::Point const &centerPoint,
     292                 :            :                             bool positiveDirection,
     293                 :            :                             state::UnstructuredSceneStateInformation &stateInfo,
     294                 :            :                             situation::VehicleState &vehicleState);
     295                 :            : 
     296                 :            : /**
     297                 :            :  * @brief class providing constants for longitudinal/lateral RSS states
     298                 :            :  *
     299                 :            :  * The respective states can be augmented with expected situation specific ResponseInformation data (see
     300                 :            :  * stateWithInformation() calls).
     301                 :            :  */
     302                 :            : class TestSupport
     303                 :            : {
     304                 :            : public:
     305                 :            :   /**
     306                 :            :    * @brief constructor
     307                 :            :    */
     308                 :            :   TestSupport();
     309                 :            : 
     310                 :            :   /**
     311                 :            :    * @brief augment a lateral RSS state with situation specific ResponseInformation data
     312                 :            :    *
     313                 :            :    * @param[in] lateralState the lateral RSS state to be augmented by ResponseInformation
     314                 :            :    * @param[in] situation the situation used as basis for calculation of expected ResponseInformation data
     315                 :            :    *
     316                 :            :    * @returns the augmented RSS state is returned
     317                 :            :    */
     318                 :            :   static state::LateralRssState stateWithInformation(state::LateralRssState const &lateralState,
     319                 :            :                                                      situation::Situation const &situation);
     320                 :            : 
     321                 :            :   /**
     322                 :            :    * @brief augment a longitudinal RSS state with situation specific ResponseInformation data
     323                 :            :    *
     324                 :            :    * @param[in] longitudinalState the longitudinal RSS state to be augmented by ResponseInformation
     325                 :            :    * @param[in] situation the situation used as basis for calculation of expected ResponseInformation data
     326                 :            :    *
     327                 :            :    * @returns the augmented RSS state is returned
     328                 :            :    */
     329                 :            :   static state::LongitudinalRssState stateWithInformation(state::LongitudinalRssState const &longitudinalState,
     330                 :            :                                                           situation::Situation const &situation);
     331                 :            : 
     332                 :            :   state::LongitudinalRssState cLongitudinalSafe;
     333                 :            :   state::LongitudinalRssState cLongitudinalNone;
     334                 :            :   state::LongitudinalRssState cLongitudinalBrakeMin;
     335                 :            :   state::LongitudinalRssState cLongitudinalBrakeMinCorrect;
     336                 :            :   state::LateralRssState cLateralSafe;
     337                 :            :   state::LateralRssState cLateralNone;
     338                 :            :   state::LateralRssState cLateralBrakeMin;
     339                 :            : };
     340                 :            : 
     341                 :            : extern const TestSupport cTestSupport;
     342                 :            : 
     343                 :          1 : inline world::RoadSegment longitudinalNoDifferenceRoadSegment()
     344                 :            : {
     345                 :          1 :   world::RoadSegment roadSegment;
     346                 :          1 :   world::LaneSegment laneSegment;
     347                 :            : 
     348                 :          1 :   laneSegment.id = 1;
     349                 :          1 :   laneSegment.length.minimum = Distance(10);
     350                 :          1 :   laneSegment.length.maximum = Distance(10);
     351                 :            : 
     352                 :          1 :   laneSegment.width.minimum = Distance(5);
     353                 :          1 :   laneSegment.width.maximum = Distance(5);
     354                 :            : 
     355         [ +  - ]:          1 :   roadSegment.push_back(laneSegment);
     356                 :          2 :   return roadSegment;
     357                 :            : }
     358                 :            : 
     359                 :          1 : inline world::RoadSegment longitudinalDifferenceRoadSegment()
     360                 :            : {
     361                 :          1 :   world::RoadSegment roadSegment;
     362                 :          1 :   world::LaneSegment laneSegment;
     363                 :            : 
     364                 :          1 :   laneSegment.id = 1;
     365                 :          1 :   laneSegment.length.minimum = Distance(5);
     366                 :          1 :   laneSegment.length.maximum = Distance(10);
     367                 :            : 
     368                 :          1 :   laneSegment.width.minimum = Distance(5);
     369                 :          1 :   laneSegment.width.maximum = Distance(5);
     370         [ +  - ]:          1 :   roadSegment.push_back(laneSegment);
     371                 :          2 :   return roadSegment;
     372                 :            : }
     373                 :            : 
     374                 :            : } // namespace rss
     375                 :            : } // namespace ad

Generated by: LCOV version 1.14