【问题标题】:How to read value of timer in CAPL?如何在 CAPL 中读取计时器的值?
【发布时间】:2018-06-07 21:51:38
【问题描述】:
variables
{
    mstimer T1;
    long x,y;
}

on start
{
    setTimer(T1,1); /*Timer set to 1ms*/
    x=timeNow()/100000.0; /*Getting a time stamp when i start a timer*/
}

on timer T1
{
  if(response==0)         /*Check if response is sent or a function*/ has completed successfully*/ /*CONDITION*/
  {
    cancelTimer(T1);      /*Cancel timer if response is correct*/
    y=timeNow()/100000.0; /*Getting a timestamp when i stop a timer.*/
    write("Total time taken is %d",y-x);    /*Getting the time required to complete the condition (response==0)*/
  }
  else /*Setting timer again to check the condition*/
  {
    setTimer(T1,1);
    write("Timer started again");
  }
}

值 (y-x) 始终显示 1ms,因为计时器设置为 1ms,但如果条件在 0.7ms 处变为真,该怎么办。我想要条件变为真的确切时间。

我已经使用 timeNow 函数来获取时间戳。

【问题讨论】:

    标签: timer capl canoe


    【解决方案1】:

    我看不到任何可访问的值。在这里你正在做布尔检查。请澄清您的问题。

    【讨论】:

      【解决方案2】:

      使用 CAPL 函数 timeToElapse 可以读取计时器的剩余时间,当它到期时,将调用在 on timer V2G 中实现的事件过程。

      在帮助中,您可以找到有关 CANoe 中计时器方法的更多说明

      CAPL Functions » Classes » Timer, MsTimer
      

      关于您在原始问题中的更新。这个怎么样:

      variables{
        mstimer myTimer;
        long timePoint = 0;
        int somethinghappen = 0;
      }
      
      on start{
        long firstTimeDuration, period;
        firstTimeDuration = 1000; period = 100;
        setTimerCyclic(myTimer, firstTimeDuration, period); /*Timer is set cyclical*/
        timePoint = timeNow()/100000.0; /* 
          Remember the time on measurement start, 
          which is on start always 0, so what's the reason to do this here?
        */
        write("Start time %5.3f", timePoint);
      }
      
      on timer myTimer{
        if(somethinghappen != 0){
          //you need to cancel the timer only when it was set as cyclic, see setTimerCyclic() 
          cancelTimer(myTimer); /*Cancel timer if response arrived*/
          write("Total time taken is %5.3f", timeNow()/100000.0 - timePoint); 
        }else{
          //nothing todo at the moment
        }
      }
      
      on key 'b' {//boom
        somethinghappen = 1;
      }
      

      不要忘记在测量运行时按定义的键。

      【讨论】:

        【解决方案3】:
        /* Timer values depend on the value that u gave in settimer() API, This is one of the way you can know the value of timer*/
        Variable
        {
        mstimer t1;
        timer t2;
        }
        on Start
        {
        Settimer(t1,10);/*waits for 10 ms and then goes to timer t1*/
        }
        
        on timer t1
        {
        settimer(t2,10);/*waits for 10 s and then goes to timer t2, so the value of timer t1 = 10s*/
        }
        
        on timer t2
        {
        
        }
        
        /*This is what i understood from what you asked. For further clarification give some more explanation on what you are asking*/
        

        【讨论】:

          【解决方案4】:

          正如许多人已经指出的那样,我相信您错误地使用了on timer。 来自Vector知识库,

          您可以在 CAPL 中定义时间事件。当此事件发生时,即经过一段时间后,将调用关联的on timer 过程。

          从我在这里看到的情况来看,通过在 on timer 内执行布尔检查,您将始终在计时器结束时读取当前经过的时间。如果由于某种情况发生而要过早退出计时器,我会建议一个完整的解决方法。您是否尝试过设置系统变量?

          【讨论】:

            【解决方案5】:

            如果您需要测量小于 1ms 单位的时间,请使用 TimeNowNS() 函数(纳秒)。基本上用 TimeNow() 和 TimeNowNS() 替换您的代码,并将使用的变量调整为双倍,以便您记录的时间戳将正确匹配。

            【讨论】:

              猜你喜欢
              • 2022-06-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-06-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多