WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
SimTableScheduleModel.h
/******************************************************************************
* Copyright (c) ATTX INC 2025. All Rights Reserved.
*
* This software and associated documentation (the "Software") are the 
* proprietary and confidential information of ATTX INC. The Software is 
* furnished under a license agreement between ATTX and the user organization 
* and may be used or copied only in accordance with the terms of the agreement.
* Refer to 'license/attx_license.adoc' for standard license terms.
*
* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION 
* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or 
* transmitted in any form or by any means, for any purpose, without the express 
* written permission of ATTX INC.
******************************************************************************/
/*
Simulated table schedule model header file
 
Author: Alex Reynolds
*/
/*
Metadata for MS GUI:
imdata = {"exclude" : True}
*/

#ifndef MODELS_SUPPORT_SIM_TABLE_SCHEDULE_MODEL_H
#define MODELS_SUPPORT_SIM_TABLE_SCHEDULE_MODEL_H

#include <chrono>

#include "simulation/Model.h"
#include "simulation/NormalRandom.hpp"
#include "schedules/TableScheduler.hpp"

namespace warptwin {

    /**
     * @brief       Model of simulated table schedule for flight software emulation
     * 
     * This model is a simulated version of the table scheduler for running flight
     * software with appropriate timing in WarpTwin. It internally maintains a
     * table schedule model, which it steps in sequence internally within every
     * simulation step. 
     * 
     * To schedule apps, the table schedule model should have its registerApp
     * called (as with the real table scheduler).
     * 
     * @author Alex Reynolds <alex.reynolds@attx.tech>
    */
    MODEL(SimTableScheduleModel)
    public:
        // Model params
        //         NAME                     TYPE                    DEFAULT VALUE
        START_PARAMS
            /** The rate at which the table schedule in the model runs. If faster
             *  than the simulation rate, this model will subsample and step 
             *  the schedule in sequence. If slower, the model will return until
             *  it is ready to run.
             *  Valid choices are: 1000, 200, 100, 50, 10*/
            SIGNAL(table_schedule_rate,     int,                    1000)

            /** Flag to set the table scheduler to measure timing. Setting this to true
             *  triggers the table scheduler model to output a timing report in the 
             *  sim output directory/table_timing.json */
            SIGNAL(measure_timing,          int,                    false)
        END_PARAMS

        // Model inputs
        //         NAME                     TYPE                    DEFAULT VALUE
        START_INPUTS

        END_INPUTS

        // Model outputs
        //         NAME                     TYPE                    DEFAULT VALUE
        START_OUTPUTS

        END_OUTPUTS

        /// @brief   Function to register apps with the table scheduler.
        /// @param app The app to register
        /// @param slot The slot to register the app to
        /// @return Error code corresponding to success/failure
        virtual int16 registerApp(warpos::App& app, int16 slot);

        int16 activate() override;
        int16 deactivate() override;
    protected:
        int16 start() override;
        int16 execute() override; 

        /// @brief Execute schedule step given current step time
        /// @param step_time The current step time to execute
        /// @return Error code out of schedule step
        int16 _executeScheduleStep(const clockwerk::Time& step_time);

        // Vector of app registries to store until startup
        std::vector<std::pair<warpos::App*, int16>> _registries;

        /// Pointers to table schedulers. These are initialized based on the parameterized
        /// table schedule rate value and only one is allocated
        warpos::TableScheduler<10>* _ts_10 = nullptr;
        warpos::TableScheduler<50>* _ts_50 = nullptr;
        warpos::TableScheduler<100>* _ts_100 = nullptr;
        warpos::TableScheduler<200>* _ts_200 = nullptr;
        warpos::TableScheduler<1000>* _ts_1000 = nullptr;

        // Internal variables to track window start/stop
        clockwerk::Time _time_per_slot;
        clockwerk::Time _step_end_sys_time;
        clockwerk::Time _next_step_start;

        // Variables to track timing
        int _total_slots = 0;
        std::vector<std::vector<double>> _timing_values_micros;     ///< The number of microseconds per app to run
        std::vector<int> _schedule_mapping;                         ///< Map schedule indices to timing vector
        std::vector<warpos::App*> _app_record;                       ///< Record of mapped apps
        std::vector<std::string> _names;
        int16 _last_sch_step = 0;
        bool _first_run = true;
        std::chrono::time_point<std::chrono::high_resolution_clock> _start_time;
        std::chrono::time_point<std::chrono::high_resolution_clock> _end_time;
    };
}

#endif