-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbouncing_ball.cpp
More file actions
79 lines (61 loc) · 2.25 KB
/
Copy pathbouncing_ball.cpp
File metadata and controls
79 lines (61 loc) · 2.25 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <fmu4cpp/fmu_base.hpp>
using namespace fmu4cpp;
class BouncingBall : public fmu_base {
public:
explicit BouncingBall(const fmu_data &data)
: fmu_base(data) {
register_variable(
real(
"height", &height_)
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::CONTINUOUS)
.setInitial(initial_t::EXACT));
register_variable(
real(
"velocity", &velocity_)
.setCausality(causality_t::LOCAL)
.setVariability(variability_t::CONTINUOUS));
register_variable(
real(
"gravity", &gravity_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));
register_variable(
real(
"bounceFactor", &bounceFactor_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));
BouncingBall::reset();
}
bool do_step(double currentTime, double dt) override {
// Update velocity with gravity
velocity_ += gravity_ * dt;
// Update height with current velocity
height_ += velocity_ * dt;
// Check for bounce
if (height_ <= 0.0f) {
height_ = 0.0f; // Reset height to ground level
velocity_ = -velocity_ * bounceFactor_;// Reverse velocity and apply bounce factor
}
return true;
}
void reset() override {
height_ = 10;
velocity_ = 0;
gravity_ = -9.81f;
bounceFactor_ = 0.6f;
}
private:
double height_; // Current height of the ball
double velocity_; // Current velocity of the ball
double gravity_; // Acceleration due to gravity
double bounceFactor_;// Factor to reduce velocity on bounce
};
model_info fmu4cpp::get_model_info() {
model_info info;
info.modelName = "BouncingBall";
info.description = "A bouncing ball model";
info.modelIdentifier = FMU4CPP_MODEL_IDENTIFIER;
return info;
}
FMU4CPP_INSTANTIATE(BouncingBall);