Skip to content

Aimsun Next Micro API Example 8

Example of Control (Bus Priority on a junction)

This example shows how to use Aimsun Next API function to manage the control plan so that buses approaching a junction have green light when they reach the junction. Once the bus leaves the junction, the control plan is restored to the state when the bus call was detected.

C++ Version

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

double previousPhaseIndex, previousPhaseTime;

// Procedures could be modified by the user
int AAPILoad()
{   
    return 0;
}

int AAPIInit()
{   
    ANGConnEnableVehiclesInBatch(true);
    previousPhaseIndex = -1;
    previousPhaseTime = -1; 
    return 0;
}
int AAPISimulationReady()
{
    return 0;
}
int AAPIManage(double time, double timeSta, double timeTrans, double acycle)
{
    return 0;
}

int AAPIPostManage(double time, double timeSta, double timeTrans, double acycle)
{ 
    int busPhase = 3;
    int intersection = 203;
    int busCallDetector = 383;
    int busExitDetector = 378;
    //get bus internal position
    int busVehiclePosition = AKIVehGetVehTypeInternalPosition( 9 );

    int currentPhase = ECIGetCurrentPhase( intersection );
    //check bus presence over busCallDetector 
    if( AKIDetGetCounterCyclebyId( busCallDetector, busVehiclePosition ) > 0 && currentPhase != busPhase && previousPhaseIndex == -1){
        AKIPrintString( "bus detected");            
        #change the control to bus phase
        previousPhaseIndex = currentPhase;
        previousPhaseTime = time - ECIGetStartingTimePhase( intersection );
        ECIChangeDirectPhase( intersection, busPhase, timeSta, time, acycle, 0 );
    }   
    //check bus presence over busExitDetector
    if( AKIDetGetCounterCyclebyId( busExitDetector, busVehiclePosition ) > 0 ){     
        //go back to previous phase
        if( previousPhaseIndex > 0 ){
            AKIPrintString( "go back to previous state");               
            ECIChangeDirectPhase( intersection, previousPhaseIndex, timeSta, time, acycle, previousPhaseTime);
            previousPhaseIndex = -1;
            previousPhaseTime = -1; 
        }
    }
    return 0;
}

int AAPIFinish()
{
    return 0;
}

int AAPIUnLoad()
{
    return 0;
}

Python Version

from AAPI import *

def AAPILoad(): 
    return 0

def AAPIInit(): 
    return 0

def AAPISimulationReady():
    return 0

def AAPIManage(time, timeSta, timeTrans, acycle):   
    return 0

def AAPIPostManage(time, timeSta, timeTrans, acycle):
    global previousPhaseIndex
    global previousPhaseTime

    busPhase = 3
    intersection = 203
    busCallDetector = 383
    busExitDetector = 378
    #get bus internal position
    busVehiclePosition = AKIVehGetVehTypeInternalPosition( 9 )

    currentPhase = ECIGetCurrentPhase( intersection )
    #check bus presence over busCallDetector 
    if AKIDetGetCounterCyclebyId( busCallDetector, busVehiclePosition ) > 0 and currentPhase != busPhase and previousPhaseIndex == -1:
        print "bus detected"                    
        #change the control to bus phase
        previousPhaseIndex = currentPhase
        previousPhaseTime = time - ECIGetStartingTimePhase( intersection )
        ECIChangeDirectPhase( intersection, busPhase, timeSta, time, acycle, 0 )

    #check bus presence over busExitDetector
    if AKIDetGetCounterCyclebyId( busExitDetector, busVehiclePosition ) > 0:        
        #go back to previous phase
        if previousPhaseIndex > 0:
            print "go back to previous state"                   
            ECIChangeDirectPhase( intersection, previousPhaseIndex, timeSta, time, acycle, previousPhaseTime)
            previousPhaseIndex = -1
            previousPhaseTime = -1


    return 0

def AAPIFinish():   
    return 0

def AAPIUnLoad():   
    return 0

previousPhaseIndex = -1
previousPhaseTime = -1