/*@!Encoding:1252*/
includes
{
  #include "include\PDU-NM-ASR_Eth.cin"
  #include "include\VectorConceptCar_Grid.cin"
}

variables
{
  const double kMaxDistance = 200.0;
}

on sysvar sysvar::ConceptCar::Vehicle::Position
{
  if (UdpNm_GetState() > 1)
  {
    struct VCC_Object foundObjects[1];
    int               foundObjectsCount;
    int               i;
    
    //
    // Find nearest object for each segment
    //
    
    // Segment 0: left
    foundObjectsCount = VCC_FindNearestObjects( @sysvarMember::ConceptCar::Vehicle::Position.RelativeX, 
                                                @sysvarMember::ConceptCar::Vehicle::Position.RelativeY,
                                                @sysvarMember::ConceptCar::Vehicle::Position.Heading,
                                                -25, -15, kMaxDistance, foundObjects, elcount(foundObjects) );
    
    if (foundObjectsCount > 0)
    {
      $ObstacleDetection::Segment[0].Distance   = foundObjects[i].distance;
      $ObstacleDetection::Segment[0].Reliability = 100;
    }
    else
    {
      $ObstacleDetection::Segment[0].Distance.raw = 0xFFFFFFFF;
      $ObstacleDetection::Segment[0].Reliability   = 0;
    }
    
    // Segment 1: half-left
    foundObjectsCount = VCC_FindNearestObjects( @sysvarMember::ConceptCar::Vehicle::Position.RelativeX, 
                                                @sysvarMember::ConceptCar::Vehicle::Position.RelativeY,
                                                @sysvarMember::ConceptCar::Vehicle::Position.Heading,
                                                -15, -5, kMaxDistance, foundObjects, elcount(foundObjects) );
    
    if (foundObjectsCount > 0)
    {
      $ObstacleDetection::Segment[1].Distance   = foundObjects[i].distance;
      $ObstacleDetection::Segment[1].Reliability = 100;
    }
    else
    {
      $ObstacleDetection::Segment[1].Distance.raw = 0xFFFFFFFF;
      $ObstacleDetection::Segment[1].Reliability   = 0;
    }
    
    // Segment 2: center
    foundObjectsCount = VCC_FindNearestObjects( @sysvarMember::ConceptCar::Vehicle::Position.RelativeX, 
                                                @sysvarMember::ConceptCar::Vehicle::Position.RelativeY,
                                                @sysvarMember::ConceptCar::Vehicle::Position.Heading,
                                                -5, 5, kMaxDistance, foundObjects, elcount(foundObjects) );
    
    if (foundObjectsCount > 0)
    {
      $ObstacleDetection::Segment[2].Distance   = foundObjects[i].distance;
      $ObstacleDetection::Segment[2].Reliability = 100;
    }
    else
    {
      $ObstacleDetection::Segment[2].Distance.raw = 0xFFFFFFFF;
      $ObstacleDetection::Segment[2].Reliability   = 0;
    }
    
    // Segment 3: half-right
    foundObjectsCount = VCC_FindNearestObjects( @sysvarMember::ConceptCar::Vehicle::Position.RelativeX, 
                                                @sysvarMember::ConceptCar::Vehicle::Position.RelativeY,
                                                @sysvarMember::ConceptCar::Vehicle::Position.Heading,
                                                5, 15, kMaxDistance, foundObjects, elcount(foundObjects) );
    
    if (foundObjectsCount > 0)
    {
      $ObstacleDetection::Segment[3].Distance   = foundObjects[i].distance;
      $ObstacleDetection::Segment[3].Reliability = 100;
    }
    else
    {
      $ObstacleDetection::Segment[3].Distance.raw = 0xFFFFFFFF;
      $ObstacleDetection::Segment[3].Reliability   = 0;
    }
    
    // Segment 4: right
    foundObjectsCount = VCC_FindNearestObjects( @sysvarMember::ConceptCar::Vehicle::Position.RelativeX, 
                                                @sysvarMember::ConceptCar::Vehicle::Position.RelativeY,
                                                @sysvarMember::ConceptCar::Vehicle::Position.Heading,
                                                15, 25, kMaxDistance, foundObjects, elcount(foundObjects) );
    
    if (foundObjectsCount > 0)
    {
      $ObstacleDetection::Segment[4].Distance   = foundObjects[i].distance;
      $ObstacleDetection::Segment[4].Reliability = 100;
    }
    else
    {
      $ObstacleDetection::Segment[4].Distance.raw = 0xFFFFFFFF;
      $ObstacleDetection::Segment[4].Reliability   = 0;
    }
    
  }
  else
  {
    $ObstacleDetection::Segment[0].Distance.raw = 0xFFFFFFFF;
    $ObstacleDetection::Segment[0].Reliability   = 0;
    $ObstacleDetection::Segment[1].Distance.raw = 0xFFFFFFFF;
    $ObstacleDetection::Segment[1].Reliability   = 0;
    $ObstacleDetection::Segment[2].Distance.raw = 0xFFFFFFFF;
    $ObstacleDetection::Segment[2].Reliability   = 0;
    $ObstacleDetection::Segment[3].Distance.raw = 0xFFFFFFFF;
    $ObstacleDetection::Segment[3].Reliability   = 0;
    $ObstacleDetection::Segment[4].Distance.raw = 0xFFFFFFFF;
    $ObstacleDetection::Segment[4].Reliability   = 0;
  }
}