SIM_fibonacci_time demonstrates how to schedule an automatic job. #1235

This commit is contained in:
John M. Penn 2022-02-28 15:39:32 -06:00
parent c8a29bea0d
commit 273ffcfd87
7 changed files with 152 additions and 0 deletions

View File

@ -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)

View File

@ -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 );
```

View File

@ -0,0 +1,6 @@
exec(open("Modified_data/realtime.py").read())
trick.stop(144);

View File

@ -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;

View File

@ -0,0 +1,2 @@
TRICK_CFLAGS += -Imodels
TRICK_CXXFLAGS += -Imodels

View File

@ -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

View File

@ -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 <math.h>
#include <iostream>
#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);
}