WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
SimpleThrusterModel.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.
******************************************************************************/
/*
Simple thruster model header file

Author: Alex Reynolds
*/
/*
Metadata for MS GUI:
imdata = {"displayname" : "Simple Thruster",
          "exclude" : False,
          "category" : "Actuators"
}
aliases = {"control_body" : "Spacecraft",
           "thruster_location__body" : "Position",
           "thruster_pointing__body" : "Pointing Vector",
           "thrust_bias" : "Bias",
           "thrust_noise_std" : "Noise",
           "g" : "EXCLUDE",
           "seed_value" : "EXCLUDE",
           "mdot" : "Mass Flow Rate",
           "Isp" : "Specific Impulse",
           "applied_thrust" : "EXCLUDE",
           "applied_force__body" : "Applied Force",
}
*/

#ifndef MODELS_ACTUATORS_SIMPLE_THRUSTER_MODEL_H
#define MODELS_ACTUATORS_SIMPLE_THRUSTER_MODEL_H

#include "simulation/Model.h"
#include "core/CartesianVector.hpp"
#include "frames/Body.h"
#include "frames/Node.h"
#include "models/support/BiasNoiseModel.h"
#include "constants/planetdefaults.h"

namespace warptwin {

    const double ON_STATE = 1e-15;   // Mdot threshold below which thruster is considered "off"

    enum cmds_e {
        OFF,
        ON
    };

    /**
     * @brief   Simple thruster model
     * 
     * This is a simple model of a thruster which applies a thrust force according
     * to mass flow and the specific impulse of the thruster. Force is applied to 
     * a node attached to the referenced body frame. The force applied by the 
     * thruster will produce a force and moment to the body as determined by the 
     * WarpTwin dynamics.
     * 
     * Noise is applied as a gaussian noise which is updated every time the thruster
     * command toggles from off to on.
     * 
     * @author Alex Reynolds <alex.reynolds@attx.tech>
    */
    MODEL(SimpleThrusterModel)
    public:
        // Model params
        //         NAME                     TYPE                    DEFAULT VALUE
        START_PARAMS
            /** The body which will be controlled by this model. The thruster force will be applied to a
             *  node aligned to this body, and WarpTwin will translate to force and torque at the body */
            SIGNAL(control_body,            Body*,                 nullptr)
            /** The thruster location relative to the provided body in the body frame*/
            SIGNAL(thruster_location__body, CartesianVector3,      CartesianVector3({0.0, 0.0, 0.0}))
            /** The thruster pointing vector in the body frame. This vector is the PLUME DIRECTION, so
             *  the actual force applied by the thruster is in the opposite direction of this vector. */
            SIGNAL(thruster_pointing__body, CartesianVector3,      CartesianVector3({0.0, 0.0, 0.0}))
            /** The bias in the thruster which is applied to every firing. */
            SIGNAL(thrust_bias,             double,                 0.0)
            /** The standard deviation of thruster noise which is applied firing to firing. A new noise
             *  draw is taken every time command toggles off to on. */
            SIGNAL(thrust_noise_std,        double,                 0.0)
            /** The Earth gravity in the units system of choice for the thruster (used in Isp calculation). Default is Metric.*/
            SIGNAL(g,                       double,                 warpos::earth_wgs84.mean_gravity)
            /** Value to seed the internal RNG for this model. */
            SIGNAL(seed_value,              int,                    0)
        END_PARAMS

        // Model inputs
        //         NAME                     TYPE                    DEFAULT VALUE
        START_INPUTS 
            /** The mass flow rate through the thruster at the current time. */
            SIGNAL(mdot,                    double,                 0.0)
            /** The specific impulse of the thruster at the current time. */
            SIGNAL(Isp,                     double,                 0.0)
        END_INPUTS

        // Model outputs
        //         NAME                     TYPE                    DEFAULT VALUE
        START_OUTPUTS
            /** The thrust applied by the thruster at its location on the spacecraft body */
            SIGNAL(applied_thrust,          double,                  0.0)
            /** The force applied by the thruster as a vector value in the body frame.
             *  This is an informational output. Force is automatically applied to the node. */
            SIGNAL(applied_force__body,     CartesianVector3,      CartesianVector3({0.0, 0.0, 0.0}))
        END_OUTPUTS

        // Getters for handles to respective frames
        // TODO: Fix this to not be needed because these can all be public. Until then it's here
        clockwerk::DataIO<Node*> thrusterNode = clockwerk::DataIO<Node*>(this, "thrusterNode", &_thruster_node);
    protected:
        int16 start() override;
        int16 execute() override; 

        /// @brief The bias and noise model for thrust output.
        BiasNoiseModel _thruster_bias_noise;

        /// @brief The node at which thrust is applied.
        Node _thruster_node;

        /// @brief Our nominal thrust calculation from Isp and the like.
        double _nominal_thrust = 0.0;

        /// @brief Value to hold perturbation to thrust
        double _thrust_perturbation = 0.0;

        /// @brief Value to hold total thrust calculation
        double _total_thrust = 0.0;

        /// @brief Value to hold last thrust command
        int _cmd_state = OFF;
        int _prev_cmd = OFF;
    };

}

#endif