forked from bobsayshilol/engine-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamshaft.cpp
More file actions
59 lines (47 loc) · 1.45 KB
/
camshaft.cpp
File metadata and controls
59 lines (47 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "../include/camshaft.h"
#include "../include/crankshaft.h"
#include "../include/constants.h"
#include "../include/units.h"
#include <cmath>
#include <assert.h>
Camshaft::Camshaft() {
m_crankshaft = nullptr;
m_lobeAngles = nullptr;
m_lobeProfile = nullptr;
m_lobes = 0;
m_advance = 0;
m_baseRadius = 0;
}
Camshaft::~Camshaft() {
assert(m_lobeAngles == nullptr);
}
void Camshaft::initialize(const Parameters ¶ms) {
m_lobeAngles = new double[params.Lobes];
memset(m_lobeAngles, 0, sizeof(double) * params.Lobes);
m_lobes = params.Lobes;
m_crankshaft = params.Crankshaft;
m_lobeProfile = params.LobeProfile;
m_advance = params.Advance;
m_baseRadius = params.BaseRadius;
}
void Camshaft::destroy() {
delete[] m_lobeAngles;
m_lobeAngles = nullptr;
m_lobes = 0;
}
double Camshaft::valveLift(int lobe) const {
return sampleLobe(getAngle() + m_lobeAngles[lobe]);
}
double Camshaft::sampleLobe(double theta) const {
double clampedTheta = std::fmod(theta, 2 * constants::pi);
if (clampedTheta < 0) clampedTheta += 2 * constants::pi;
if (clampedTheta >= constants::pi) clampedTheta -= 2 * constants::pi;
return m_lobeProfile->sampleTriangle(clampedTheta);
}
double Camshaft::getAngle() const {
const double angle =
std::fmod((m_crankshaft->getAngle() + m_advance) * 0.5, 2 * constants::pi);
return (angle < 0)
? angle + 2 * constants::pi
: angle;
}