From 273ffcfd87fc98c50b05a4e3b53cab625d35e417 Mon Sep 17 00:00:00 2001 From: "John M. Penn" Date: Mon, 28 Feb 2022 15:39:32 -0600 Subject: [PATCH] SIM_fibonacci_time demonstrates how to schedule an automatic job. #1235 --- .../Modified_data/realtime.py | 11 ++++ trick_sims/SIM_fibonacci_time/README.md | 30 ++++++++++ .../SIM_fibonacci_time/RUN_test/input.py | 6 ++ trick_sims/SIM_fibonacci_time/S_define | 21 +++++++ trick_sims/SIM_fibonacci_time/S_overrides.mk | 2 + .../fibonacci_time/include/Fibonacci_time.hh | 23 ++++++++ .../fibonacci_time/src/Fibonacci_time.cpp | 59 +++++++++++++++++++ 7 files changed, 152 insertions(+) create mode 100644 trick_sims/SIM_fibonacci_time/Modified_data/realtime.py create mode 100644 trick_sims/SIM_fibonacci_time/README.md create mode 100644 trick_sims/SIM_fibonacci_time/RUN_test/input.py create mode 100644 trick_sims/SIM_fibonacci_time/S_define create mode 100644 trick_sims/SIM_fibonacci_time/S_overrides.mk create mode 100644 trick_sims/SIM_fibonacci_time/models/fibonacci_time/include/Fibonacci_time.hh create mode 100644 trick_sims/SIM_fibonacci_time/models/fibonacci_time/src/Fibonacci_time.cpp diff --git a/trick_sims/SIM_fibonacci_time/Modified_data/realtime.py b/trick_sims/SIM_fibonacci_time/Modified_data/realtime.py new file mode 100644 index 00000000..e8ce8380 --- /dev/null +++ b/trick_sims/SIM_fibonacci_time/Modified_data/realtime.py @@ -0,0 +1,11 @@ + +trick.frame_log_on() +trick.real_time_enable() +trick.exec_set_software_frame(0.1) +trick.itimer_enable() + +trick.exec_set_enable_freeze(True) +trick.exec_set_freeze_command(True) + +simControlPanel = trick.SimControlPanel() +trick.add_external_application(simControlPanel) diff --git a/trick_sims/SIM_fibonacci_time/README.md b/trick_sims/SIM_fibonacci_time/README.md new file mode 100644 index 00000000..e4899db5 --- /dev/null +++ b/trick_sims/SIM_fibonacci_time/README.md @@ -0,0 +1,30 @@ +# ```SIM_fibonacci_time``` + +This simulation is simply a demonstration of "automatic" job scheduling. + +In this particular simulation, we run a "scheduled" job every second, +and an "automatic" job when the elapsed time is equal to a Fibonacci number of seconds. + +"automatic" jobs run once, at t=0. There after, they must reschedule themselves. + +This is accomplished using the following two functions: + +``` +Trick::JobData * exec_get_curr_job(); +``` +``` +JobData::int set_next_call_time(long long time_tics); +``` +The first function retrieves the ```JobData``` of the job that is currently being executed, i.e., our "automatic" job. The second function allows us to set the time when the job will be called again. Notice that we have to specify the simulation time in time-tics. + +To get the current simulation time in time-tics we use: + +``` +long long exec_get_time_tics( void ); +``` + +And, to get the number of time tics per second, so we can convert between simulation time in seconds and time-tics, we use the following awkwardly name function: + +``` +int exec_get_time_tic_value( void ); +``` diff --git a/trick_sims/SIM_fibonacci_time/RUN_test/input.py b/trick_sims/SIM_fibonacci_time/RUN_test/input.py new file mode 100644 index 00000000..9913e6e8 --- /dev/null +++ b/trick_sims/SIM_fibonacci_time/RUN_test/input.py @@ -0,0 +1,6 @@ +exec(open("Modified_data/realtime.py").read()) + +trick.stop(144); + + + diff --git a/trick_sims/SIM_fibonacci_time/S_define b/trick_sims/SIM_fibonacci_time/S_define new file mode 100644 index 00000000..d9770b22 --- /dev/null +++ b/trick_sims/SIM_fibonacci_time/S_define @@ -0,0 +1,21 @@ +/************************************************************ +PURPOSE: + ( Simulate a satellite orbiting the Earth. ) +LIBRARY DEPENDENCIES: + ((satellite/src/Fibonacci_time.cpp)) +*************************************************************/ +#include "sim_objects/default_trick_sys.sm" +##include "fibonacci_time/include/Fibonacci_time.hh" +class Fibonacci_timeSimObject : public Trick::SimObject { + public: + Fibonacci_time fibonacci_time; + + Fibonacci_timeSimObject() { + ("default_data") fibonacci_time.default_data() ; + ("initialization") fibonacci_time.state_init() ; + (1.0, "scheduled") fibonacci_time.scheduled_job_1() ; + ("automatic") fibonacci_time.automatic_job_1() ; + } +}; + +Fibonacci_timeSimObject dyn; diff --git a/trick_sims/SIM_fibonacci_time/S_overrides.mk b/trick_sims/SIM_fibonacci_time/S_overrides.mk new file mode 100644 index 00000000..e1f6cccd --- /dev/null +++ b/trick_sims/SIM_fibonacci_time/S_overrides.mk @@ -0,0 +1,2 @@ +TRICK_CFLAGS += -Imodels +TRICK_CXXFLAGS += -Imodels diff --git a/trick_sims/SIM_fibonacci_time/models/fibonacci_time/include/Fibonacci_time.hh b/trick_sims/SIM_fibonacci_time/models/fibonacci_time/include/Fibonacci_time.hh new file mode 100644 index 00000000..bb5d698b --- /dev/null +++ b/trick_sims/SIM_fibonacci_time/models/fibonacci_time/include/Fibonacci_time.hh @@ -0,0 +1,23 @@ +/************************************************************************* +PURPOSE: (Example of "automatic" job, i.e., a self scheduling job.) +LIBRARY DEPENDENCIES: + ((fibonacci_time/src/Fibonacci_time.o)) +**************************************************************************/ +#ifndef _fibonacci_time_hh_ +#define _fibonacci_time_hh_ + +class Fibonacci_time { + + public: + int N; + int tics_per_second; + + int default_data(); + int state_init(); + int scheduled_job_1(); + int automatic_job_1(); +}; +#endif + + + diff --git a/trick_sims/SIM_fibonacci_time/models/fibonacci_time/src/Fibonacci_time.cpp b/trick_sims/SIM_fibonacci_time/models/fibonacci_time/src/Fibonacci_time.cpp new file mode 100644 index 00000000..f76dcdb0 --- /dev/null +++ b/trick_sims/SIM_fibonacci_time/models/fibonacci_time/src/Fibonacci_time.cpp @@ -0,0 +1,59 @@ +/********************************* TRICK HEADER ******************************* +PURPOSE: ( Example of an "automatic" (self-scheduling) job. ) +LIBRARY DEPENDENCY: + ((Fibonacci_time.o)) +*******************************************************************************/ +#include "../include/Fibonacci_time.hh" +#include +#include + +#include "trick/exec_proto.hh" +#include "trick/exec_proto.h" + +const char * ISO_6429_Restore_Default = "\x1b[00m"; +const char * ISO_6429_Red_Foreground = "\x1b[31m"; +const char * ISO_6429_Green_Foreground = "\x1b[32m"; + +int Fibonacci(int n) { + if (n<=1) { + return n; + } else { + return Fibonacci(n-1) + Fibonacci(n-2) ; + } +} + +int Fibonacci_time::default_data() { + N = 1; + tics_per_second = exec_get_time_tic_value(); + return (0); +} + +int Fibonacci_time::state_init() { + return (0); +} + +int Fibonacci_time::scheduled_job_1() { + + double time_in_seconds = (double)exec_get_time_tics() / tics_per_second; + std::cout << ISO_6429_Green_Foreground + << "Scheduled Job Time = " << time_in_seconds + << ISO_6429_Restore_Default << std::endl; + + return(0); +} + +int Fibonacci_time::automatic_job_1() { + + double time_in_seconds = (double)exec_get_time_tics() / tics_per_second; + std::cout << ISO_6429_Red_Foreground + << "Automatic Job Time = " << time_in_seconds + << ISO_6429_Restore_Default << std::endl; + + N++; + double next_time = Fibonacci(N); + + Trick::JobData * thisJob = exec_get_curr_job() ; + thisJob->set_next_call_time(next_time * tics_per_second); + + return(0); +}