Skip to content
Snippets Groups Projects
traci-vehicle.h 8.93 KiB
Newer Older
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2017 Fraunhofer ESK
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Karsten Roscher <karsten.roscher@esk.fraunhofer.de>
 */
#ifndef TRACI_VEHICLE_H
#define TRACI_VEHICLE_H

#include <tuple>

#include <ns3/node.h>
#include <ns3/simple-ref-count.h>
#include <ns3/vector.h>
#include <ns3/assert.h>

#include "traci.h"
#include "traci-types.h"
#include "traci-cached-variable.h"

namespace ns3
{
namespace traci
{

/*!
 * @brief Class representing a single vehicle on the TraCI host.
 *
 * This class will cache all values to reduce the network traffic if
 * values are required multiple times at different places.
 */
class Vehicle : public SimpleRefCount<Vehicle>
{
public:

  /*!
   * @brief Create a new vehicle with the provided id.
   * @param traci	Link to the TraCI host
   * @param id		Id of the object on the TraCI host
   */
  Vehicle (Ptr<TraCi> traci, std::string id);

  /*!
   * @brief Get the id of the object
   * @return	Id of the object
   */
  std::string
  GetId () const;

  /*!
   * @brief Get the assigned traci instance
   * @return 	Traci instance used
   */
  Ptr<TraCi>
  GetTraCi () const;

  /*!
   * @brief Set the node this vehicle is attached to
   * @param node	Node the vehicle is attached to
   */
  void
  SetNode (Ptr<Node> node);

  /*!
   * @brief Get the node this vehicle is attached to (if any)
   * @return Pointer to the node this vehicle is attached to, null if not attached
   */
  Ptr<Node>
  GetNode () const;

  /*
   * Specific value getters
   */

  /*!
   * @brief Get the position of the object (center of front bumper)
   * @param forceReload		Force reload from the TraCI host if true
   * @return 	Position of the object
   */
  Vector
  GetPosition (bool forceReload = false) const;

  //! Get the 2D position of the object (center of front bumper)
  Vector2D
  GetPosition2d (bool forceReload = false) const;

  /*!
   * @brief Get the position of the vehicle center
   *
   * The center will be calculated in x/y dimensions, the z coordinate
   * is left untouched
   *
   * @param forceReload		Force reload from TraCI host if true
   * @return Position of the vehicle center
   */
  Vector
  GetCenterPosition (bool forceReload = false) const;

  //! Get length in meters
  double
  GetLength (bool forceReload = false) const;

  //! Get width in meters
  double
  GetWidth (bool forceReload = false) const;

  //! Get height in meters
  double
  GetHeight (bool forceReload = false) const;

  //! Get speed in m/s
  double
  GetSpeed (bool forceReload = false) const;

  //! Get max speed in m/s
  double
  GetMaxSpeed (bool forceReload = false) const;

  //! Get lateral speed in m/s
  double
  GetSpeedLateral (bool forceReload = false) const;

  //! Get max lateral speed in m/s
  double
  GetMaxSpeedLateral (bool forceReload = false) const;

  //! Get current heading
  Heading
  GetHeading (bool forceReload = false) const;

  //! Get velocity vector
  Vector
  GetVelocity (bool forceReload = false) const;

  //! Get current acceleration
  double
  GetAcceleration (bool forceReload = false) const;

  //! Get max acceleration
  double
  GetMaxAcceleration (bool forceReload = false) const;

  //! Get max deceleration
  double
  GetMaxDeceleration (bool forceReload = false) const;

  //! Get current lane idx
  int
  GetLaneIndex (bool forceReload = false) const;

  //! Get current road id
  std::string
  GetRoadID (bool forceReload = false) const;

  //! Get current route id
  std::string
  GetRouteID (bool forceReload = false) const;

  //! Get route, edge list
  std::vector<std::string>
  GetRoute (bool forceReload = false) const;

  Color
  GetColor (bool forceReload = false) const;

  /*
   * Specific value setters
   */

  void
  SetColor (const Color& color);

  /*
   * Generic get/set
   */

  /*!
   * @brief Get the value of a vehicle variable from the TraCI host.
   *
   * This is a fallback method if no specific Get* method is available.
   *
   * @param variable  Variable code for the request.
   * @tparam Expected return type.
   *
   * @return Requested variable value.
   *
   * @throw ...
   */
  template<typename ResultType>
  ResultType
  GetVariable (int variable);


  /*!
   * @brief           Set the value of a vehicle variable with the TraCI host.
   *
   * This is a fallback method if no specific Set* method is available.
   *
   * @param variable  Variable code for the request
   * @param value     Variable content for the request
   * @param type      Identifier of the data type (optional, if omitted default for template type is used)
   * @tparam          Type of the variable
   *
   * @throw ...
   */
  template<typename VariableType>
  void
  SetVariable (int variable, VariableType value);
  
  
  /*!
   * @brief change lane of the specified vehicle over given duration.
   *
   * Parameters:
   * - ID of the lange to change to
   * - duration for the change
   * - if the provided ID signals a relative lane chance
   */
  void
  changeLane (int laneID, double duration);
  
  /*!
   * @brief set speed of the specified vehicle.
   *
   * Parameters:
   * - new speed to set
   */
  void
  setSpeed (double speed);
  
  /*!
   * @brief set maximum lateral speed of the specified vehicle.
   *
   * Parameters:
   * - new max speed to set
   */
  void
  setMaxSpeedLateral (double max_speed);

  /*!
   * @brief gradually change the speed over given duration.
   *
   * Parameters:
   * - new speed to set
   * - duration for the change
   */
  void
  slowDown (double speed, double duration);
  
  /*!
   * @brief set new position for vehicle.
   *
   * Parameters:
   * - ID of the road
   * - ID of the lange to change to
   * - x coordinate
   * - y coordinate
   * - angle (if default value, then use the angle of the edge)
   */
  void
  moveToXY (std::string edgeID, int laneID, double x, double y, double angle=-1000000.0);
  
  /*!
   * @brief set route for vehicle.
   *
   * The first edge in the list has to be the one that the vehicle is at at the moment.
   *
   * Parameters:
   * - new edgeId list
   */
  void
  setRoute (std::vector<std::string> edgeIdList);
  
private:
  Ptr<TraCi> m_traci;	// link to the traci instance
  std::string m_id;	// id of the object

  Ptr<Node> m_node;	// node this vehicle is attached to

  // Cached variables

  CachedVariable<Position3D, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_POSITION3D> m_position;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_LENGTH, true> m_length;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_WIDTH, true> m_width;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_HEIGHT, true> m_height;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_SPEED> m_speed;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_SPEED_LAT> m_speedLateral;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_MAXSPEED> m_maxSpeed;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_MAXSPEED> m_maxSpeedLateral;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_ANGLE> m_heading;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_ACCELERATION> m_acceleration;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_ACCEL> m_maxAcceleration;
  CachedVariable<double, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_DECEL> m_maxDeceleration;
  CachedVariable<int, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_LANE_INDEX> m_laneIdx;
  CachedVariable<std::string, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_ROAD_ID> m_roadId;
  CachedVariable<std::string, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_ROUTE_ID> m_routeId;
  CachedVariable<std::vector<std::string>, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_EDGES> m_route;
  CachedVariable<Color, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::VAR_COLOR> m_color;
};

/*
 * Implementation of the template based methods
 */


template<typename ResultType>
ResultType
Vehicle::GetVariable (int variable)
{
  NS_ASSERT (m_traci);
  return m_traci->GetVariable<ResultType>(libsumo::CMD_GET_VEHICLE_VARIABLE, variable, m_id);
}

template<typename VariableType>
void
Vehicle::SetVariable (int variable, VariableType value)
{
  NS_ASSERT (m_traci);
  m_traci->SetVariable<VariableType>(libsumo::CMD_SET_VEHICLE_VARIABLE, variable, value, m_id);
}

}
}

#endif // TRACI_VEHICLE_H