Guest User

Untitled

a guest
Jul 10th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. //
  2. // Copyright (C) 2006-2012 Christoph Sommer <[email protected]>
  3. //
  4. // Documentation for these modules is at http://veins.car2x.org/
  5. //
  6. // SPDX-License-Identifier: GPL-2.0-or-later
  7. //
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 2 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. //
  22.  
  23. #pragma once
  24.  
  25. #include <string>
  26. #include <fstream>
  27. #include <list>
  28. #include <stdexcept>
  29.  
  30. #include "veins/base/modules/BaseMobility.h"
  31. #include "veins/base/utils/FindModule.h"
  32. #include "veins/modules/mobility/traci/TraCIScenarioManager.h"
  33. #include "veins/modules/mobility/traci/TraCICommandInterface.h"
  34. #include "veins/modules/mobility/traci/VehicleSignal.h"
  35. #include "veins/base/utils/Heading.h"
  36.  
  37. namespace veins {
  38.  
  39. /**
  40. * @brief
  41. * Used in modules created by the TraCIScenarioManager.
  42. *
  43. * This module relies on the TraCIScenarioManager for state updates
  44. * and can not be used on its own.
  45. *
  46. * TraCI server implementations do not differentiate between the orientation and direction of a vehicle.
  47. * Thus, TraCIMobility::updatePosition sets the BaseMobility's orientation and direction to the same value.
  48. * Said value is equivalent to the heading of the vehicle.
  49. *
  50. * See the Veins website <a href="http://veins.car2x.org/"> for a tutorial, documentation, and publications </a>.
  51. *
  52. * @author Christoph Sommer, David Eckhoff, Luca Bedogni, Bastian Halmos, Stefan Joerer
  53. *
  54. * @see TraCIScenarioManager
  55. * @see TraCIScenarioManagerLaunchd
  56. *
  57. * @ingroup mobility
  58. */
  59. class VEINS_API TraCIMobility : public BaseMobility {
  60. public:
  61. class VEINS_API Statistics {
  62. public:
  63. double firstRoadNumber; /**< for statistics: number of first road we encountered (if road id can be expressed as a number) */
  64. simtime_t startTime; /**< for statistics: start time */
  65. simtime_t totalTime; /**< for statistics: total time travelled */
  66. simtime_t stopTime; /**< for statistics: stop time */
  67. double minSpeed; /**< for statistics: minimum value of currentSpeed */
  68. double maxSpeed; /**< for statistics: maximum value of currentSpeed */
  69. double totalDistance; /**< for statistics: total distance travelled */
  70. double totalCO2Emission; /**< for statistics: total CO2 emission */
  71.  
  72. void initialize();
  73. void watch(cSimpleModule& module);
  74. void recordScalars(cSimpleModule& module);
  75. };
  76.  
  77. static const simsignal_t collisionSignal;
  78. const static simsignal_t parkingStateChangedSignal;
  79.  
  80. TraCIMobility()
  81. : BaseMobility()
  82. , isPreInitialized(false)
  83. , manager(nullptr)
  84. , commandInterface(nullptr)
  85. , vehicleCommandInterface(nullptr)
  86. {
  87. }
  88. ~TraCIMobility() override
  89. {
  90. delete vehicleCommandInterface;
  91. }
  92. void initialize(int) override;
  93. void finish() override;
  94.  
  95. void handleSelfMsg(cMessage* msg) override;
  96. virtual void preInitialize(std::string external_id, const Coord& position, std::string road_id = "", double speed = -1, Heading heading = Heading::nan);
  97. virtual void nextPosition(const Coord& position, std::string road_id = "", double speed = -1, Heading heading = Heading::nan, VehicleSignalSet signals = {VehicleSignal::undefined});
  98. virtual void changePosition();
  99. virtual void changeParkingState(bool);
  100. virtual void collisionOccurred(bool newState);
  101. virtual void setExternalId(std::string external_id)
  102. {
  103. this->external_id = external_id;
  104. }
  105. virtual std::string getExternalId() const
  106. {
  107. if (external_id == "") throw cRuntimeError("TraCIMobility::getExternalId called with no external_id set yet");
  108. return external_id;
  109. }
  110. virtual double getHostPositionOffset() const
  111. {
  112. return hostPositionOffset;
  113. }
  114. virtual bool getParkingState() const
  115. {
  116. return isParking;
  117. }
  118. virtual std::string getRoadId() const
  119. {
  120. if (road_id == "") throw cRuntimeError("TraCIMobility::getRoadId called with no road_id set yet");
  121. return road_id;
  122. }
  123. virtual double getSpeed() const
  124. {
  125. if (speed == -1) throw cRuntimeError("TraCIMobility::getSpeed called with no speed set yet");
  126. return speed;
  127. }
  128. virtual VehicleSignalSet getSignals() const
  129. {
  130. if (signals.test(VehicleSignal::undefined)) throw cRuntimeError("TraCIMobility::getSignals called with no signals set yet");
  131. return signals;
  132. }
  133. /**
  134. * returns heading.
  135. */
  136. virtual Heading getHeading() const
  137. {
  138. if (heading.isNan()) throw cRuntimeError("TraCIMobility::getHeading called with no heading set yet");
  139. return heading;
  140. }
  141. virtual TraCIScenarioManager* getManager() const
  142. {
  143. if (!manager) manager = TraCIScenarioManagerAccess().get();
  144. return manager;
  145. }
  146. virtual TraCICommandInterface* getCommandInterface() const
  147. {
  148. if (!commandInterface) commandInterface = getManager()->getCommandInterface();
  149. return commandInterface;
  150. }
  151. virtual TraCICommandInterface::Vehicle* getVehicleCommandInterface() const
  152. {
  153. if (!vehicleCommandInterface) vehicleCommandInterface = new TraCICommandInterface::Vehicle(getCommandInterface()->vehicle(getExternalId()));
  154. return vehicleCommandInterface;
  155. }
  156.  
  157. /**
  158. * Returns the speed of the host (likely 0 if setHostSpeed==false)
  159. */
  160. Coord getHostSpeed() const
  161. {
  162. return BaseMobility::getCurrentSpeed();
  163. }
  164.  
  165. protected:
  166. int accidentCount; /**< number of accidents */
  167.  
  168. cOutVector currentPosXVec; /**< vector plotting posx */
  169. cOutVector currentPosYVec; /**< vector plotting posy */
  170. cOutVector currentSpeedVec; /**< vector plotting speed */
  171. cOutVector currentAccelerationVec; /**< vector plotting acceleration */
  172. cOutVector currentCO2EmissionVec; /**< vector plotting current CO2 emission */
  173.  
  174. Statistics statistics; /**< everything statistics-related */
  175.  
  176. bool isPreInitialized; /**< true if preInitialize() has been called immediately before initialize() */
  177.  
  178. std::string external_id; /**< updated by setExternalId() */
  179. double hostPositionOffset; /**< front offset for the antenna on this car */
  180. bool setHostSpeed; /**< whether to update the speed of the host (along with its position) */
  181.  
  182. simtime_t lastUpdate; /**< updated by nextPosition() */
  183. Coord roadPosition; /**< position of front bumper, updated by nextPosition() */
  184. std::string road_id; /**< updated by nextPosition() */
  185. double speed; /**< updated by nextPosition() */
  186. Heading heading; /**< updated by nextPosition() */
  187. VehicleSignalSet signals; /**<updated by nextPosition() */
  188.  
  189. cMessage* startAccidentMsg = nullptr;
  190. cMessage* stopAccidentMsg = nullptr;
  191. mutable TraCIScenarioManager* manager;
  192. mutable TraCICommandInterface* commandInterface;
  193. mutable TraCICommandInterface::Vehicle* vehicleCommandInterface;
  194. double last_speed;
  195.  
  196. bool isParking;
  197.  
  198. void fixIfHostGetsOutside() override; /**< called after each read to check for (and handle) invalid positions */
  199.  
  200. /**
  201. * Returns the amount of CO2 emissions in grams/second, calculated for an average Car
  202. * @param v speed in m/s
  203. * @param a acceleration in m/s^2
  204. * @returns emission in g/s
  205. */
  206. double calculateCO2emission(double v, double a) const;
  207.  
  208. /**
  209. * Calculates where the OMNeT++ module position of this car should be, given its front bumper position
  210. */
  211. Coord calculateHostPosition(const Coord& vehiclePos) const;
  212.  
  213. /**
  214. * Calling this method on pointers of type TraCIMobility is deprecated in favor of calling either getHostSpeed or getSpeed.
  215. */
  216. Coord getCurrentSpeed() const override
  217. {
  218. return BaseMobility::getCurrentSpeed();
  219. }
  220. };
  221.  
  222. class VEINS_API TraCIMobilityAccess {
  223. public:
  224. TraCIMobility* get(cModule* host)
  225. {
  226. TraCIMobility* traci = FindModule<TraCIMobility*>::findSubModule(host);
  227. ASSERT(traci);
  228. return traci;
  229. };
  230. };
  231.  
  232. } // namespace veins
  233.  
Advertisement
Add Comment
Please, Sign In to add comment