Exemple #1
0
void Engine_run()
{
	double timestart = glfwGetTime();	
	int fps = 0;	

	time = Timer_getTime();		
	
	while(!done)
	{
		Timer_update();
		time = Timer_getTime();	
	
		Input_handleInput(time);					
			
		World_refresh();
			
		Logic_update(time);	
			
		Physics_update(theworld);
		
		Physics_detectCollisions(theworld);
			
		Logic_handleCollisions(theworld);	
		
		Graphics_renderScene(theworld);
		
		Graphics_refresh();
		
		fps++;
		if (glfwGetTime() - timestart >= 1.0)
		{
			fprintf(stderr, "Fps: %i, Objects: %u, Collision checks: %i, Script calls: %i\n", fps, World_getObjectCount(), Physics_getCheckCounter(), Script_getCallCounter());
			
			Physics_resetCheckCounter();
			Script_resetCallCounter();
			fps = 0;
			timestart = glfwGetTime();
		}
		
		glfwSleep(0.033);
	}
	
	WorldObject_destroy(theworld);
}
/*
** ===================================================================
**     Method      :  SensorHandler 
**     Description :  Checkea si hay un nuevo valor del AD, 
**                    y si lo hay lo procesa.
** ===================================================================
*/
void SenRpm_procesar(void* _self){
  struct SensorRpm *_s=(struct SensorRpm *)_self;
 
    int filtro = _s->conf->iFiltro;
    ulong valProv=0;
    float val_f=0;
    ulong microseg=0;
    uint pulsos=0;
    uint mult=0;
    uchar err;
 
   if(_s->procesar==TRUE){
    _s->procesar=FALSE;  
    
    microseg=CapturadorPT2_getMicroSegundos(_s->capturador);  //(_s->capturador->ultimoproceso.microsegundos);//
    pulsos=CapturadorPT2_getPulsos(_s->capturador); //_s->capturador->ultimoproceso.pulsos;//
   
    
    mult= 600 * pow10(_s->conf->iDecimales);   //600 6000 o 60000
    if(microseg!=0){
      
      valProv=(ulong)pulsos*mult;
      val_f=valProv/(((float)_s->conf->pulsosPorVuelta)*microseg/100000);
      val_f=(val_f*SenRpm_getAjusteGanancia(_s))/(float)1000;
      valProv = val_f;
     
      if(valProv<65535){     
        valProv= filtrarL (valProv,filtro,10,200,&buffer);
        Cpu_DisableInt();
        
         _s->procVal=(long)valProv; 
         
         buffer=valProv;
        _s->bufferFiltro=valProv;
        _s->state=RPM_OK;
        _s->ContadorUf=0;
        Cpu_EnableInt();  
      
      } else{
        Cpu_DisableInt();
        _s->state=RPM_OF;
        Cpu_EnableInt();   
      }
    }else {
      if(_s->ContadorUf<RPM_MAX_UF_COUNT){
        _s->ContadorUf++;
        _s->state=RPM_OK;
        valProv=(ulong)100*mult/(_s->conf->pulsosPorVuelta*_s->ContadorUf*Timer_getTime(&_s->timerMuestreo)); //calculo cual seria el valor de rpm si hubiese entrado un solo pulso
        
        //Nuevo
        valProv= filtrarL (valProv,filtro,10,200,& _s->bufferFiltro);
        //Fin Nuevo
        if(_s->procVal>valProv)
          _s->procVal=valProv;   //redusco el valor de rpms
      }else{ 
        _s->procVal=filtrarL (0,filtro,10,200,& _s->bufferFiltro);
      }
    }
    
   setValCalculado(valProv,0); 
 }
}
Exemple #3
0
/**
  Start a timer.
  Specify if you'd like the timer to repeat and, if so, the interval at which 
  you'd like it to repeat.  If you have set up a handler with setHandler() then your 
  handler function will get called at the specified interval.  If the timer is already
  running, this will reset it.

  @param millis The number of milliseconds 
  @param repeat Whether or not to repeat - true by default.
  */
int Timer_start( Timer *timer, int millis, bool repeat, bool freeOnStop )
{
// debug
    if (millis < 0) {
        panic("Timer_start");
        return CONTROLLER_ERROR_ILLEGAL_PARAMETER_VALUE;
    }

    //timer->started = timeval;
    timer->started = Timer_tick_count();
    timer->value = millis;

    timer->timeCurrent = 0;
    timer->timeInitial = millis * TIMER_CYCLES_PER_MS;
    timer->repeat = repeat;
    timer->freeOnStop = freeOnStop;

    Timer *nextEntry = timer->next;
    timer->next = NULL;

    TRACE_TMR(">>Timer_start %x %d %d %d %d\r\n", timer, millis, repeat, freeOnStop, timer_manager.servicing);
    // this could be a lot smarter - for example, modifying the current period?
    if (sync && !timer_manager.servicing ) {
        Task_enterCritical();
        //xSemaphoreTake(timer_mutex, -1);
    }

    TIMER_DBG_PROCESSING(true);
    if ( !timer_manager.running )
    {
        //    Timer_SetActive( true );
        Timer_setTimeTarget( timer->timeInitial );
        Timer_enable();
    }  

/*
    // Calculate how long remaining
    int target = Timer_getTimeTarget();
    int timeCurrent = Timer_getTime();
    int remaining = target - timeCurrent;

    TRACE_TMR("t %d tc %d r %d\r\n", target, timeCurrent, remaining);
*/
    // Get the entry ready to roll
    timer->timeCurrent = timer->timeInitial;

    // Add entry
    Timer* first = timer_manager.first;
    timer_manager.first = timer;

    timer->next = first;


    // Are we actually servicing an interupt right now?
    if ( !timer_manager.servicing )
    {
        TRACE_TMR("srv 0\r\n");
        // No - so does the time requested by this new timer make the time need to come earlier?
        // first, unlink the timer 
        Timer *te = timer;
        while (te) {
            if (te->next && te->next == timer) {
                te->next = nextEntry;
                TRACE_TMR("Unlinking from the list %x\r\n", nextEntry);
                break;
            }
            te = te->next;
        }

        // Calculate how long remaining
        int target = Timer_getTimeTarget();
        int timeCurrent = Timer_getTime();
        int remaining = target - timeCurrent;

        TRACE_TMR("t %d tc %d r %d\r\n", target, timeCurrent, remaining);
        TRACE_TMR("%d < %d %d\r\n", timer->timeCurrent, remaining, TIMER_MARGIN);
        if ( timer->timeCurrent < ( remaining - TIMER_MARGIN ) )
        {
            // Damn it!  Reschedule the next callback
            Timer_setTimeTarget( target - ( remaining - timer->timeCurrent ));
            TRACE_TMR("%x rc %d cv %d\r\n", timer, timer_manager.tc->TC_RC, timer_manager.tc->TC_CV);
            // pretend that the existing time has been with us for the whole slice so that when the 
            // IRQ happens it credits the correct (reduced) time.
            timer->timeCurrent += timeCurrent;
        }
        else
        {
            // pretend that the existing time has been with us for the whole slice so that when the 
            // IRQ happens it credits the correct (reduced) time.
            timer->timeCurrent += timeCurrent;
            TRACE_TMR("sch %d\r\n", timer->timeCurrent);
        }
    }
    else
    {
        TRACE_TMR("srv 1\r\n");
        // Yep... we're servicing something right now

        // Make sure the previous pointer is OK.  This comes up if we were servicing the first item
        // and it subsequently wants to delete itself, it would need to alter the next pointer of the 
        // the new head... err... kind of a pain, this
        if ( timer_manager.previous == NULL )
            timer_manager.previous = timer;

        // Need to make sure that if this new time is the lowest yet, that the IRQ routine 
        // knows that.  Since we added this entry onto the beginning of the list, the IRQ
        // won't look at it again
        if ( timer_manager.nextTime == -1 || timer_manager.nextTime > timer->timeCurrent )
            timer_manager.nextTime = timer->timeCurrent;
    }

    TIMER_DBG_PROCESSING(false);
    if (sync && !timer_manager.servicing ) {
        Task_exitCritical();
        //xSemaphoreGive(timer_mutex);
    }

    TRACE_TMR("<<Timer_start %x\r\n", timer);
    return CONTROLLER_OK;
}
/*
** ===================================================================
**    Method      :   SenRpm_getTiempoMedicion 
**    Description :   Obtiene el periodo en que se toman las muestras
** ===================================================================
*/
uint SenRpm_getTiempoMuestreo(void* _self){
  struct SensorRpm *_s=(struct SensorRpm *)_self;
  return Timer_getTime(&_s->timerMuestreo);
}
ulong MTimer_getTime(struct MethodTimer* t){
  return Timer_getTime((t->_base));
}