From 8f8b54b440a261f267fb15bffd9f7662fabad83a Mon Sep 17 00:00:00 2001 From: Travis Hunter Date: Sun, 12 Feb 2023 19:24:30 -0700 Subject: [PATCH] Initial commit of model/Thrustcurve that uses linear interpolation --- CMakeLists.txt | 1 + src/model/CMakeLists.txt | 3 ++ src/model/Thrustcurve.cpp | 60 +++++++++++++++++++++++++++++++++++++++ src/model/Thrustcurve.h | 38 +++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 src/model/CMakeLists.txt create mode 100644 src/model/Thrustcurve.cpp create mode 100644 src/model/Thrustcurve.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 82212ea..1a14268 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ enable_testing() add_subdirectory(src/gui) add_subdirectory(src/sim) add_subdirectory(src/Utils) +add_subdirectory(src/model) #find_package(wxWidgets REQUIRED gl core base) #include(${wxWidgets_USE_FILE}) diff --git a/src/model/CMakeLists.txt b/src/model/CMakeLists.txt new file mode 100644 index 0000000..d523a3e --- /dev/null +++ b/src/model/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(model + Thrustcurve.cpp) + diff --git a/src/model/Thrustcurve.cpp b/src/model/Thrustcurve.cpp new file mode 100644 index 0000000..47bc640 --- /dev/null +++ b/src/model/Thrustcurve.cpp @@ -0,0 +1,60 @@ +#include "Thrustcurve.h" + +#include + +Thrustcurve::Thrustcurve(std::vector>& tc) + : thrustCurve(tc), + maxTime(0.0) +{ + maxTime = std::max_element(thrustCurve.begin(), + thrustCurve.end(), + [](const auto& a, const auto& b) + { + return a.first < a.second; + })->first; +} + +Thrustcurve::Thrustcurve() +{ + thrustCurve.emplace_back(0.0, 0.0); + maxTime = 0.0; +} + +Thrustcurve::~Thrustcurve() +{} + +double Thrustcurve::getThrust(double t) +{ + if(t < 0.0 || t > maxTime) + { + return 0.0; + } + + // Find the right interval + auto i = thrustCurve.cbegin(); + while(i->first <= t) + { + // If t is equal to a data point that we have, then just return + // the thrust we know. Otherwise it fell between two points and we + // will interpolate + if(i->first == t) + { + return i->second; + } + else + { + i++; + } + } + // linearly interpolate the thrust and return + // tStart and tEnd are the start time and the end time + // of the current interval. thrustStart is the thrust at + // the start of the interval. + double tStart = std::prev(i)->first; + double thrustStart = std::prev(i)->second; + double tEnd = i->first; + double slope = (i->second - std::prev(i)->second) / + (i->first - std::prev(i)->first); + return thrustStart + (t - tStart) * slope; + +} \ No newline at end of file diff --git a/src/model/Thrustcurve.h b/src/model/Thrustcurve.h new file mode 100644 index 0000000..b78047d --- /dev/null +++ b/src/model/Thrustcurve.h @@ -0,0 +1,38 @@ +#ifndef MODEL_THRUSTCURVE_H +#define MODEL_THRUSTCURVE_H + +#include +#include + +// No namespace. Maybe should be, but this is a model rocket program +// so model is sort of implied? Or I'm just making excuses for being lazy + +class Thrustcurve +{ +public: + /** + * Constructor takes a vector of pairs. The first item a timestamp, + * the second the thrust in newtons. + */ + Thrustcurve(std::vector>& tc); + /** + * Default constructor. Will create an empty thrustcurve, always returning 0.0 + * for all requested times. + */ + Thrustcurve(); + ~Thrustcurve(); + + /** + * Assuming that the thrust is one dimensional. Seems reasonable, but just + * documenting that for the record. For timesteps between known points the thrust + * is interpolated linearly + * @param t The time in seconds. For t > burntime or < 0, this will return 0.0 + * @return Thrust in Newtons + */ + double getThrust(double t); + +private: + std::vector> thrustCurve; + double maxTime; +}; +#endif // MODEL_THRUSTCURVE_H \ No newline at end of file