@Override public void run() { while (run) { updateSensors(); long lastCycleTime = System.currentTimeMillis(); CIBehavior current = activeBehavior; try { if (current != null) { current.step(behaviorTimestep++); for (CIBehavior b : alwaysActiveBehaviors) b.step(timestep); if (current.getTerminateBehavior()) { stopActiveBehavior(); } } } catch (Exception e) { e.printStackTrace(); } ioManager.setMotorSpeeds(leftSpeed, rightSpeed); if (broadcastHandler != null) broadcastHandler.update(timestep); long timeToSleep = CYCLE_TIME - (System.currentTimeMillis() - lastCycleTime); if (timeToSleep > 0) { try { Thread.sleep(timeToSleep); } catch (InterruptedException e) { } } timestep++; } }