Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/accumulated dv inertial frame #513

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/Support/bskReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Version |release|
- Implemented new syntax for variable logging. See :ref:`bskPrinciples-6`.
- Basilisk minimum Python version is now formally 3.8.x (checked by build files). Previously, it was indicated to be
3.7.x yet in practice it was 3.8.x.
- Added a ``TotalAccumDV_CN_N`` field in :ref:`SCStatesMsgPayload` that saves the total accumulated velocity of the
spacecraft's center of mass in the inertial frame.

.. warning::

Expand Down
1 change: 1 addition & 0 deletions src/architecture/msgPayloadDefC/SCStatesMsgPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct {
double omegaDot_BN_B[3]; //!< r/s/s Current angular acceleration
double TotalAccumDVBdy[3]; //!< m/s Accumulated DV of center of mass in body frame coordinates
double TotalAccumDV_BN_B[3]; //!< m/s Accumulated DV of body frame in body frame coordinates
double TotalAccumDV_CN_N[3]; //!< m/s Accumulated DV of center of mass in inertial frame coordinates
double nonConservativeAccelpntB_B[3];//!< m/s/s Current Spacecraft non-conservative body frame accel
uint64_t MRPSwitchCount; //!< -- Number of times that MRPs have switched
}SCStatesMsgPayload;
Expand Down
79 changes: 77 additions & 2 deletions src/simulation/dynamics/spacecraft/_UnitTest/test_spacecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import numpy
import pytest
from random import random

filename = inspect.getframeinfo(inspect.currentframe()).filename
path = os.path.dirname(os.path.abspath(filename))
Expand Down Expand Up @@ -54,12 +55,13 @@ def addTimeColumn(time, data):
, "SCPointBVsPointC"
, "scOptionalRef"
, "scAccumDV"
, "scAccumDVExtForce"
])
def test_spacecraftAllTest(show_plots, function):
"""Module Unit Test"""
if function == "scOptionalRef":
[testResults, testMessage] = eval(function + '(show_plots, 1e-3)')
elif function == "scAccumDV":
elif function == "scAccumDV" or function == "scAccumDVExtForce":
[testResults, testMessage] = eval(function + '()')
else:
[testResults, testMessage] = eval(function + '(show_plots)')
Expand Down Expand Up @@ -1073,9 +1075,11 @@ def scAccumDV():

dataAccumDV_CN_B = dataLog.TotalAccumDVBdy
dataAccumDV_BN_B = dataLog.TotalAccumDV_BN_B
dataAccumDV_CN_N = dataLog.TotalAccumDV_CN_N

accuracy = 1e-10
truth_dataAccumDV_CN_B = [0.0, 0.0, 0.0]
truth_dataAccumDV_CN_N = [0.0, 0.0, 0.0]
schaubh marked this conversation as resolved.
Show resolved Hide resolved
v_r = numpy.cross(numpy.array(scObject.hub.omega_BN_BInit).T, -numpy.array(scObject.hub.r_BcB_B).T)[0]
truth_dataAccumDV_BN_B = numpy.zeros(3)
for i in range(len(dataLog.times())-1):
Expand All @@ -1090,16 +1094,87 @@ def scAccumDV():
testFailCount += 1
testMessages.append("FAILED: Spacecraft Point B Accumulated DV test failed pos unit test")

if not unitTestSupport.isArrayEqual(dataAccumDV_CN_N[i+1],truth_dataAccumDV_CN_N,3,accuracy):
joaogvcarneiro marked this conversation as resolved.
Show resolved Hide resolved
testFailCount += 1
testMessages.append("FAILED: Spacecraft Point C Accumulated DV in inertial frame test failed pos unit test")

if testFailCount == 0:
print("PASSED: Spacecraft Accumulated DV tests with offset CoM")

return [testFailCount, ''.join(testMessages)]


def scAccumDVExtForce():
"""Module Unit Test"""
# The __tracebackhide__ setting influences pytest showing of tracebacks:
# the mrp_steering_tracking() function will not be shown unless the
# --fulltrace command line option is specified.
__tracebackhide__ = True

testFailCount = 0 # zero unit test result counter
testMessages = [] # create empty list to store test log messages

scObject = spacecraft.Spacecraft()
scObject.ModelTag = "spacecraftBody"

unitTaskName = "unitTask" # arbitrary name (don't change)
unitProcessName = "TestProcess" # arbitrary name (don't change)

# Create a sim module as an empty container
unitTestSim = SimulationBaseClass.SimBaseClass()

# Create test thread
timeStep = 0.1
testProcessRate = macros.sec2nano(timeStep) # update process rate update time
testProc = unitTestSim.CreateNewProcess(unitProcessName)
testProc.addTask(unitTestSim.CreateNewTask(unitTaskName, testProcessRate))

# Add test module to runtime call list
unitTestSim.AddModelToTask(unitTaskName, scObject)

# Add external force and torque
extFTObject = extForceTorque.ExtForceTorque()
extFTObject.ModelTag = "externalDisturbance"
extFTObject.extTorquePntB_B = [[0], [0], [0]]
extForce = numpy.array([random() for _ in range(3)])
extFTObject.extForce_B = [[item] for item in extForce]
scObject.addDynamicEffector(extFTObject)
unitTestSim.AddModelToTask(unitTaskName, extFTObject)

# Define initial conditions of the spacecraft
scObject.hub.mHub = 100

dataLog = scObject.scStateOutMsg.recorder()
unitTestSim.AddModelToTask(unitTaskName, dataLog)

unitTestSim.InitializeSimulation()
stopTime = 0.5
unitTestSim.ConfigureStopTime(macros.sec2nano(stopTime))
unitTestSim.ExecuteSimulation()

dataAccumDV_CN_N = dataLog.TotalAccumDV_CN_N
timeArraySec = dataLog.times() * macros.NANO2SEC

accuracy = 1e-10
for i in range(len(dataLog.times())):
truth_dataAccumDV_CN_N = extForce * timeArraySec[i] / scObject.hub.mHub
if not unitTestSupport.isArrayEqual(dataAccumDV_CN_N[i], truth_dataAccumDV_CN_N, 3, accuracy):
testFailCount += 1
testMessages.append("FAILED: Spacecraft Point C Accumulated DV with external force test failed unit test")

if testFailCount == 0:
print("PASSED: Spacecraft Accumulated DV tests with offset CoM")

return [testFailCount, ''.join(testMessages)]


if __name__ == "__main__":
# scAttRef(True, 1e-3)
# SCTranslation(True)
# SCTransAndRotation(True)
# SCRotation(True)
# SCTransBOE(True)
# SCPointBVsPointC(True)
scOptionalRef(True, 0.001)
# scOptionalRef(True, 0.001)
# scAccumDV()
scAccumDVExtForce()
5 changes: 5 additions & 0 deletions src/simulation/dynamics/spacecraft/spacecraft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Spacecraft::Spacecraft()
this->simTimePrevious = 0;
this->dvAccum_CN_B.setZero();
this->dvAccum_BN_B.setZero();
this->dvAccum_CN_N.setZero();

// - Set integrator as RK4 by default
this->integrator = new svIntegratorRK4(this);
Expand Down Expand Up @@ -101,6 +102,7 @@ void Spacecraft::writeOutputStateMessages(uint64_t clockTime)
eigenMatrixXd2CArray(this->dvAccum_CN_B, stateOut.TotalAccumDVBdy);
stateOut.MRPSwitchCount = this->hub.MRPSwitchCount;
eigenMatrixXd2CArray(this->dvAccum_BN_B, stateOut.TotalAccumDV_BN_B);
eigenMatrixXd2CArray(this->dvAccum_CN_N, stateOut.TotalAccumDV_CN_N);
eigenVector3d2CArray(this->nonConservativeAccelpntB_B, stateOut.nonConservativeAccelpntB_B);
eigenVector3d2CArray(this->omegaDot_BN_B, stateOut.omegaDot_BN_B);
this->scStateOutMsg.write(&stateOut, this->moduleID, clockTime);
Expand Down Expand Up @@ -487,6 +489,9 @@ void Spacecraft::postIntegration(double integrateToThisTime) {
this->dvAccum_BN_B += newDcm_NB.transpose()*(newV_BN_N -
this->hubGravVelocity->getState());

// - Find the accumulated DV of the center of mass in the inertial frame
this->dvAccum_CN_N += newV_CN_N - this->BcGravVelocity->getState();

// - non-conservative acceleration of the body frame in the body frame
this->nonConservativeAccelpntB_B = (newDcm_NB.transpose()*(newV_BN_N -
this->hubGravVelocity->getState()))/this->timeStep;
Expand Down
1 change: 1 addition & 0 deletions src/simulation/dynamics/spacecraft/spacecraft.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Spacecraft : public DynamicObject{

Eigen::Vector3d dvAccum_CN_B; //!< [m/s] Accumulated delta-v of center of mass relative to inertial frame in body frame coordinates
Eigen::Vector3d dvAccum_BN_B; //!< [m/s] accumulated delta-v of body frame relative to inertial frame in body frame coordinates
Eigen::Vector3d dvAccum_CN_N; //!< [m/s] accumulated delta-v of center of mass relative to inertial frame in inertial frame coordinates
Eigen::Vector3d nonConservativeAccelpntB_B;//!< [m/s/s] Current spacecraft body acceleration in the B frame
Eigen::Vector3d omegaDot_BN_B; //!< [rad/s/s] angular acceleration of body wrt to N in body frame
Eigen::Vector3d totOrbAngMomPntN_N; //!< [kg m^2/s] Total orbital angular momentum about N in N frame compenents
Expand Down
Loading