Exemplo da API Micro Aimsun Next 9¶
Exemplo de Controle (Mudando os tempos de fase)¶
Este exemplo mostra como usar as funções da API Aimsun Next para mudar os tempos de um plano de controle.
É baseado na rede do tutorial: Edição - Rede finalizada. A API com as configurações atuais mudará os tempos do plano de controle "Controle AM" para o nó 1541. Note que o tipo do cruzamento de controle deve ser alterado de 'Fixo' para 'Externo'
Versão C++¶
#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("CARREGAR");
return 0;
}
int AAPIInit()
{
AKIPrintString(" Inicializar");
ANGConnEnableVehiclesInBatch(true);
return 0;
}
int AAPISimulationReady()
{
AKIPrintString(" AAPISimulationReady");
return 0;
}
int AAPIManage(double time, double timeSta, double timTrans, double acicle)
{
//AKIPrintString(" Gerenciar");
return 0;
}
int AAPIPostManage(double time, double timeSta, double timTrans, double acicle)
{
//AKIPrintString(" Pós Gerenciar");
int currentPhase = ECIGetCurrentPhase(nodeId);
if (currentPhase == phaseToChange && previousPhase != phaseToChange) //Mudança para a fase alvo acaba de acontecer
{
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(" Finalizar");
return 0;
}
int AAPIUnLoad()
{
AKIPrintString("DESCARREGAR");
return 0;
}
int AAPIPreRouteChoiceCalculation(double time, double timeSta)
{
AKIPrintString(" Cálculo da Escolha de Pré-Rota");
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("Um Pedestre entrou na rede");
return 0;
}
int AAPIExitPedestrian(int idPedestrian, int destinationCentroid)
{
AKIPrintString("Um Pedestre saiu da rede");
return 0;
}
Versão Python¶
from AAPI import *
NODE_ID = 1541
PHASE_TO_CHANGE = 2 #Índice da fase que iremos mudar
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 ) #Tirar controle do 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