comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: Using "delay until" in real-time
Date: Wed, 13 Dec 2000 05:35:53 GMT
Date: 2000-12-13T05:35:53+00:00	[thread overview]
Message-ID: <ZUDZ5.22751$M5.895038@news1.frmt1.sfba.home.com> (raw)
In-Reply-To: yec3dfttmnd.fsf@king.cts.com

>It's more likely to be fixed-point.  If you're using
>Gnat, Ada.Real_Time.Time and Ada.Real_Time.Time_Span are both derived
>from Duration, which is a 64-bit fixed-point type with a Small of
>exactly 1.0e-9 (one nanosecond).
  (A win for mathematical oversimplification over engineering, ;)

Clearly you need to do your arithmetic in a system that correctly
represents a single tick.  Integers come to mind.  How about

  Iteration_Count : Natural := 0;
  ...
  delay until Start_Time + Ada.Real_Time.Time_Span(Iteration_Count)/60;

If Ada.Real_Time.Time_Span(Iteration_Count) is going to get too large
before your program is done, then you need something that isn't so big
it overflows, but will count ticks without roundoff.  A fixed point
value with suitable range and a 'small of 1/60 will do that.  Then
you need a way to convert this accurate time to an Ada.Real_Time.Time
How about:

with Ada.Real_Time;
package Accurate_Time is

  type Time_Span is delta 1.0/60 range 0.0 .. long enough
  for Time_Span'small use 1.0/60;

  type Time is private;

  function Clock return Time;

  function "+"(Left : Time; Right : Time_Span) return Time;

  function To_Real_Time(T : in Time) return Ada.Real_Time.Time;

private

  type Time is record
    Start_SC : Ada.Real_Time.Seconds_Count;
    Start_TS : Ada.Real_Time.Time_Span;
    Since : Time_Span;
  end record;

end Accurate_Time;

package body Accurate_Time is
  use Ada.Real_Time;

  function Clock return Time is
    Result : Time;
    Now : Ada.Real_Time.Time := Ada.Real_Time.Clock;
  begin
    Ada.Real_Time.Split(Now, Result.Start_SC, Result.Start_TS);
    Result.Since := 0.0;
    return Result;
  end Clock;

  function "+"(Left : Time; Right : Time_Span) return Time is
    Result : Time := Left;
  begin
    Result.Since := Result.Since + Right;
    return Result;
  end "+";

  function To_Real_Time(T : in Time) return Ada.Real_Time.Time is
    Whole_Seconds_Since : Ada.Real_Time.Seconds_Count;
    Fractional_Seconds_Since : Time_Span;
    SC : Ada.Real_Time.Seconds_Count;
    TS : Ada.Real_Time.Time_Span;
  begin
    Whole_Seconds_Since := Ada.Real_Time.Seconds_Count(T.Since);
    if Time_Span(Whole_Seconds_Since) > T.Since then
      Whole_Seconds_Since := Whole_Seconds_Since-1;
    end if;
    Fractional_Seconds_Since := T.Since - Time_Span(Whole_Seconds_Since);
    SC := T.Start_SC + Whole_Seconds_Since;
    TS := T.Start_TS
          + Ada.Real_Time.To_Time_Span(Duration(Fractional_Seconds_Since));
    return Ada.Real_Time.Time_Of(SC, TS);
  end To_Real_Time;

end Accurate_Time;

>However, its quite possible that the real-time clock hardware on my
>platform (PC) uses something like nannoseconds, and the OS call just
>approximates that.
  If it uses the usual 8253 descendant, that clock hardware ticks
at 1,193,182 Hz, (1/3 of the NTSC TV color subcarrier crystal
frequency) which is 0.838 mics/tick.



  parent reply	other threads:[~2000-12-13  5:35 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-12-12 16:27 Using "delay until" in real-time Ted Dennison
2000-12-12 18:01 ` Mike Silva
2000-12-12 19:57   ` Ted Dennison
2000-12-12 23:02     ` Mike Silva
2000-12-12 23:49       ` Ted Dennison
2000-12-18  6:26     ` Ray Blaak
2000-12-12 20:00 ` Ken Garlington
2000-12-12 20:40   ` Ted Dennison
2000-12-13  4:02     ` Ken Garlington
2000-12-13 14:29       ` Ted Dennison
2000-12-13 16:53     ` Larry Hazel
2000-12-13 17:41       ` Ted Dennison
2000-12-12 20:22 ` Keith Thompson
2000-12-12 20:54   ` Ted Dennison
2000-12-13  5:35   ` tmoran [this message]
2000-12-12 20:23 ` David C. Hoos, Sr.
2000-12-12 21:58   ` Ted Dennison
2000-12-12 23:18   ` Jeff Carter
2000-12-12 21:18 ` JP Thornley
2000-12-12 22:31   ` Ted Dennison
2000-12-13  8:02     ` Brian Orpin
2000-12-13 17:32     ` JP Thornley
2000-12-12 23:09 ` Ted Dennison
2000-12-13  7:43 ` Brian Orpin
2000-12-15  0:27 ` Frank
2000-12-19  7:50 ` Martin Gangkofer
2000-12-20  3:32   ` Ted Dennison
2000-12-20  5:29     ` tmoran
2000-12-20  7:59     ` Martin Gangkofer
2000-12-20  9:15       ` java servlets JF Harrison
2000-12-20 12:50     ` Using "delay until" in real-time Marin David Condic
2000-12-21  0:08     ` Alejandro R. Mosteo
2000-12-20  3:17 ` Ted Dennison
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox