--Cyclic scheduler with a watchdog:
with Ada.Calendar;
with Ada.Text_IO, Ada.Float_Text_IO;
with Ada.Numerics.Float_Random;
use Ada.Calendar;
use Ada.Text_IO, Ada.Float_Text_IO;
use Ada.Numerics.Float_Random;
-- add packages to use random number generator
procedure cyclic_wd is
Message: constant String := "Cyclic scheduler with watchdog";
-- change/add your declarations here
d: Duration := 1.0;
Start_Time: Time := Clock;
Start_Test: Time;
i: Integer := 0;
d2: Duration := 0.5;
Rounded_Time: Float := 0.0;
d3: Duration := 0.0;
-- Random Number Generator
G: Generator;
Rand_Num: Float;
procedure f1 is
Message: constant String := "f1 executing, time is now";
begin
Put(Message);
Put_Line(Duration'Image(Clock - Start_Time));
end f1;
procedure f2 is
Message: constant String := "f2 executing, time is now";
begin
Put(Message);
Put_Line(Duration'Image(Clock - Start_Time));
end f2;
procedure f3 is
Message: constant String := "f3 executing, time is now";
begin
Reset(G);
Rand_Num := Random(G);
delay Duration(Rand_Num);
Put(Message);
Put_Line(Duration'Image(Clock - Start_Time));
-- add a random delay here
-- Put("Random Time Float Delay is");
--Put_Line(Float'Image(Rand_Num));
delay Duration(Rand_Num);
end f3;
task Watchdog is
-- add your task entries for communication
entry deadline;
end Watchdog;
task body Watchdog is
begin
loop
-- add your task code inside this loop
Rounded_Time := Float'Ceiling(Float(Clock - Start_Time));
d3 := Duration(Rounded_Time - Float(Clock - Start_Time));
select
accept deadline;
or
delay Duration(Clock - Start_Time) + 0.5;
-- Put_Line(Duration'Image(Clock - Start_Time));
Put_Line("The deadline has passed.");
end select;
delay d3;
end loop;
end Watchdog;
begin
loop
-- change/add your code inside this loop
if i mod 2 = 0 then
f1;
f2;
Watchdog.deadline;
f3;
i := i + 1;
delay d2;
else
f1;
f2;
i := i + 1;
delay d;
end if;
end loop;
end cyclic_wd;