Initial commit of model/Thrustcurve that uses linear interpolation
This commit is contained in:
parent
0a2faccdf1
commit
8fb3f06664
@ -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})
|
||||
|
3
src/model/CMakeLists.txt
Normal file
3
src/model/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
add_library(model
|
||||
Thrustcurve.cpp)
|
||||
|
60
src/model/Thrustcurve.cpp
Normal file
60
src/model/Thrustcurve.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include "Thrustcurve.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
Thrustcurve::Thrustcurve(std::vector<std::pair<double, double>>& 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;
|
||||
|
||||
}
|
38
src/model/Thrustcurve.h
Normal file
38
src/model/Thrustcurve.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef MODEL_THRUSTCURVE_H
|
||||
#define MODEL_THRUSTCURVE_H
|
||||
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
|
||||
// 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<std::pair<double, double>>& 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<std::pair<double, double>> thrustCurve;
|
||||
double maxTime;
|
||||
};
|
||||
#endif // MODEL_THRUSTCURVE_H
|
Loading…
x
Reference in New Issue
Block a user