Skip to content

Aimsun Next Micro API Example 9

Example of Control (Bus Priority on a junction)

This example shows how to use Aimsun Next API functions to change the timings of a the control plan.

It is based on the tutorial network: Editing - Finished network. The API with the current settings will change the timings of control plan "Control AM" for node 1541. Note that the type of the control junction must be switched from 'Fixed' to 'External'

C++ Version

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

int nodeId = 1541;
int controlPlanId = 1630;
int phaseToChange = 2;
double newTime = 16.0;
int previousPhase = -1;
double timeLastChange = -100.0;

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)
{
    //AKIPrintString("\tPostManage");
    int currentPhase = ECIGetCurrentPhase(nodeId);
    if (currentPhase == phaseToChange && previousPhase != phaseToChange) //Change to target phase just happened
    {
        int disableReport = ECIDisableEvents(nodeId);
        int timeChangeReport = ECIChangeTimingPhase(nodeId, phaseToChange, newTime, timeSta);
        timeLastChange = time;
    }
    if (time == timeLastChange + newTime)
    {
        int enableEventsReport = ECIEnableEventsActivatingPhase(nodeId, phaseToChange + 1, 0.0, time);
        timeLastChange = -100.0;
    }
    previousPhase = currentPhase;
    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 *

NODE_ID = 1541
PHASE_TO_CHANGE = 2 #Index of the phase we will be changing
NEW_TIME = 16
previousPhase = None
timeLastChange = -100.0

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):
    global previousPhase, timeLastChange
    # AKIPrintString( "AAPIPostManage" )
    currentPhase = ECIGetCurrentPhase( NODE_ID )
    if currentPhase == PHASE_TO_CHANGE and previousPhase != PHASE_TO_CHANGE:
        ECIDisableEvents( NODE_ID ) #Take control from Aimsun Next
        timeChangeReport = ECIChangeTimingPhase( NODE_ID, PHASE_TO_CHANGE, NEW_TIME, timeSta )
        timeLastChange = time
    if time == timeLastChange + NEW_TIME:
        ECIEnableEventsActivatingPhase(NODE_ID, PHASE_TO_CHANGE +1, 0.0, time)
        timeLastChange = -100.0
    previousPhase = currentPhase
return 0

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

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

def AAPIPreRouteChoiceCalculation(time, timeSta):
return 0

def AAPIVehicleStartParking (idveh, idsection, time):
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