Skip to content

Aimsun Next Micro API Example 10

Example of Detector information

This example shows how to use Aimsun Next API functions to read and print in the log information from detectors.

This API is based on the tutorial network: Editing - Finished network.

C++ Version

#include "AKIProxie.h"
#include "CIProxie.h"
#include "ANGConProxie.h"
#include "AAPI.h"
#include <stdio.h>

int detectorId = 1571;

int AAPILoad()
{
    AKIPrintString("LOAD");
    return 0;
}

int AAPIInit()
{
    AKIPrintString("\tInit");
    ANGConnEnableVehiclesInBatch(true);
    return 0;
}
int AAPISimulationReady()
{
    AKIPrintString("\tAAPISimulationReady");
    return 0;
}

int AAPIManage(double time, double timeSta, double timTrans, double acicle)
{
    //AKIPrintString("\tManage");
    return 0;
}

int AAPIPostManage(double time, double timeSta, double timTrans, double acicle)
{
    char astring[128];
    //AKIPrintString("\tPostManage");
    int instantDetection = AKIDetGetPresenceInstantDetectionbyId(detectorId, 0, time);
    double instantOccupiedTime = AKIDetGetTimeOccupiedInstantDetectionbyId(detectorId, 0, time);
    int vehiclesDetectedCount = AKIDetGetCounterCyclebyId(detectorId, 0);
    double instantSpeed = AKIDetGetSpeedInstantDetectionbyId(detectorId, 0, time);
    double instantDensity = AKIDetGetDensityInstantDetectionbyId(detectorId, 0, time);
    int counterAggregated = AKIDetGetCounterAggregatedbyId(detectorId, 0);
    double speedAggregated = AKIDetGetSpeedAggregatedbyId(detectorId, 0);
    double occupancyAggregated = AKIDetGetTimeOccupiedAggregatedbyId(detectorId, 0);
    int presenceAggregated = AKIDetGetPresenceAggregatedbyId(detectorId, 0);
    if (instantDetection > 0.0){
        sprintf(astring, "\t\t ------------------Detector %d INSTANT MEASURES------------------:", detectorId);
        AKIPrintString(astring);
        sprintf(astring,"Detection %d", instantDetection);
        AKIPrintString(astring);
        sprintf(astring, "Occupied time %g", instantOccupiedTime);
        AKIPrintString(astring);
        sprintf(astring, "Vehicles detected %d", vehiclesDetectedCount);
        AKIPrintString(astring);
        sprintf(astring, "Speed %f", instantSpeed);
        AKIPrintString(astring);
        sprintf(astring, "Density %f", instantDensity);
        AKIPrintString(astring);
        if (counterAggregated > 0){
            sprintf(astring, "Aggregated Count %d", counterAggregated);
            AKIPrintString(astring);
            sprintf(astring, "Aggregated Speed %f", speedAggregated);
            AKIPrintString(astring);
            sprintf(astring, "Aggregated Occupancy %f", occupancyAggregated);
            AKIPrintString(astring);
            sprintf(astring, "Aggregated Presence %d", presenceAggregated);
            AKIPrintString(astring);
        }
    }
    return 0;
}

int AAPIFinish()
{
    AKIPrintString("\tFinish");
    return 0;
}

int AAPIUnLoad()
{
    AKIPrintString("UNLOAD");
    return 0;
}

int AAPIPreRouteChoiceCalculation(double time, double timeSta)
{
    AKIPrintString("\tPreRouteChoice Calculation");
    return 0;
}

int AAPIVehicleStartParking(int idveh, int idsection, double time)
{
return 0;
}

int AAPIEnterVehicle(int idveh, int idsection)
{
    return 0;
}

int AAPIExitVehicle(int idveh, int idsection)
{
    return 0;
}

int AAPIEnterVehicleSection(int idveh, int idsection, double atime)
{
    return 0;
}

int AAPIExitVehicleSection(int idveh, int idsection, double time)
{
    return 0;
}

int AAPIEnterPedestrian(int idPedestrian, int originCentroid)
{
    AKIPrintString("A Pedestrian has entered the network");
    return 0;
}

int AAPIExitPedestrian(int idPedestrian, int destinationCentroid)
{
    AKIPrintString("A Pedestrian has exited the network");
    return 0;
}

Python Version

from AAPI import *

DETECTOR_IDS = [1571]

def AAPILoad():
    AKIPrintString( "AAPILoad" )
    return 0

def AAPIInit():
    AKIPrintString( "AAPIInit" )
    return 0
def AAPISimulationReady():
    AKIPrintString( "AAPISimulationReady" )
    return 0
def AAPIManage(time, timeSta, timeTrans, acycle):
    # AKIPrintString( "AAPIManage" )
    return 0

def AAPIPostManage(time, timeSta, timeTrans, acycle):
    for detectorId in DETECTOR_IDS:
        instantDetection = AKIDetGetPresenceInstantDetectionbyId( detectorId, 0, time)
        instantOccupiedTime = AKIDetGetTimeOccupiedInstantDetectionbyId( detectorId, 0, time )
        vehiclesDetectedCount = AKIDetGetCounterCyclebyId(detectorId, 0)
        instantSpeed = AKIDetGetSpeedInstantDetectionbyId( detectorId, 0, time)
        instantDensity =AKIDetGetDensityInstantDetectionbyId( detectorId, 0, time)
        counterAggregated = AKIDetGetCounterAggregatedbyId( detectorId, 0)
        speedAggregated = AKIDetGetSpeedAggregatedbyId( detectorId, 0)
        occupancyAggregated = AKIDetGetTimeOccupiedAggregatedbyId( detectorId, 0)
        presenceAggregated = AKIDetGetPresenceAggregatedbyId(detectorId, 0 )
        if instantDetection >0:
            AKIPrintString( "------------------ Detector " + str(detectorId) + " INSTANT MEASURES ------------------" )
            AKIPrintString( "detection %s"%instantDetection )
            AKIPrintString( "Occupied Time (Percentage) %s"%instantOccupiedTime )
            AKIPrintString( "%s Vehicles in the last cycle"%vehiclesDetectedCount )
            AKIPrintString( "Speed: " + str(instantSpeed) )
            AKIPrintString( "density: " + str(instantDensity) )
            if counterAggregated > 0: #Aggregated stats are only available after the first statistics interval
                AKIPrintString( "------------------ Detector " + str(detectorId) + " AGGREGATED MEASURES ------------------" )
                AKIPrintString( "Count: " + str( counterAggregated ) )
                AKIPrintString( "Speed: " + str( speedAggregated ) )
                AKIPrintString( "Occupancy: " + str( occupancyAggregated ) )
                AKIPrintString( "Presence: " + str( presenceAggregated ) )

    return 0

def AAPIFinish():
    AKIPrintString( "AAPIFinish" )
    return 0

def AAPIUnLoad():
    AKIPrintString( "AAPIUnLoad" )
    return 0

def AAPIPreRouteChoiceCalculation(time, timeSta):
    AKIPrintString( "AAPIPreRouteChoiceCalculation" )
    return 0

def AAPIEnterVehicle(idveh, idsection):
    return 0

def AAPIExitVehicle(idveh, idsection):
    return 0

def AAPIEnterPedestrian(idPedestrian, originCentroid):
    return 0

def AAPIExitPedestrian(idPedestrian, destinationCentroid):
    return 0

def AAPIEnterVehicleSection(idveh, idsection, atime):
    return 0

def AAPIExitVehicleSection(idveh, idsection, atime):
    return 0

def AAPIVehicleStartParking (idveh, idsection, time):
    return 0