-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pendulum example and some other changes
- Loading branch information
Showing
9 changed files
with
144 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
|
||
add_library(bouncing_ball SHARED | ||
bouncing_ball.cpp | ||
"$<TARGET_OBJECTS:fmu4cpp>" | ||
"bouncing_ball.cpp" | ||
) | ||
target_include_directories(bouncing_ball PRIVATE "../../include") | ||
target_compile_definitions(bouncing_ball PRIVATE FMU4CPP_MODEL_IDENTIFIER="bouncing_ball") | ||
|
||
generateFMU("bouncing_ball") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
include("${PROJECT_SOURCE_DIR}/cmake/generate_fmu.cmake") | ||
|
||
add_subdirectory(BouncingBall) | ||
add_subdirectory(SimplePendulum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
add_library(simple_pendulum SHARED | ||
"simple_pendulum.cpp" | ||
) | ||
|
||
generateFMU(simple_pendulum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
#include <fmu4cpp/fmu_base.hpp> | ||
|
||
#include <numbers> | ||
|
||
using namespace fmu4cpp; | ||
|
||
|
||
class SimplePendulum : public fmu_base { | ||
public: | ||
SimplePendulum(const std::string &instanceName, const std::string &resources) | ||
: fmu_base(instanceName, resources) { | ||
|
||
register_variable(real("angle", | ||
[this] { return angle_; }) | ||
.setCausality(causality_t::OUTPUT) | ||
.setVariability(variability_t::CONTINUOUS)); | ||
|
||
register_variable(real("angularVelocity", | ||
[this] { return angularVelocity_; }) | ||
.setCausality(causality_t::LOCAL) | ||
.setVariability(variability_t::CONTINUOUS)); | ||
register_variable(real( | ||
"gravity", | ||
[this] { return gravity_; }, | ||
[this](double input) { gravity_ = input; }) | ||
.setCausality(causality_t::PARAMETER) | ||
.setVariability(variability_t::FIXED)); | ||
register_variable(real( | ||
"length", | ||
[this] { return length_; }, | ||
[this](double input) { length_ = input; }) | ||
.setCausality(causality_t::PARAMETER) | ||
.setVariability(variability_t::FIXED)); | ||
register_variable(real( | ||
"damping", | ||
[this] { return damping_; }, | ||
[this](double input) { damping_ = input; }) | ||
.setCausality(causality_t::PARAMETER) | ||
.setVariability(variability_t::FIXED)); | ||
|
||
SimplePendulum::reset(); | ||
} | ||
|
||
bool do_step(double currentTime, double dt) override { | ||
angularVelocity_ += (-gravity_ / length_) * sin(angle_) * dt - damping_ * angularVelocity_ * dt; | ||
angle_ += angularVelocity_ * dt; | ||
return true; | ||
} | ||
|
||
void reset() override { | ||
angle_ = std::numbers::pi / 4;// 45 degrees | ||
angularVelocity_ = 0; | ||
gravity_ = -9.81; | ||
length_ = 1.0; | ||
damping_ = 0.1; | ||
} | ||
|
||
private: | ||
double angle_; // Current angle of the pendulum | ||
double angularVelocity_;// Current angular velocity | ||
double gravity_; // Acceleration due to gravity | ||
double length_; // Length of the pendulum | ||
double damping_; // Damping factor | ||
}; | ||
|
||
|
||
model_info fmu4cpp::get_model_info() { | ||
model_info info; | ||
info.modelName = "SimplePendulum"; | ||
info.description = "A simple pendulum model"; | ||
info.modelIdentifier = FMU4CPP_MODEL_IDENTIFIER; | ||
return info; | ||
} | ||
|
||
std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::string &fmuResourceLocation) { | ||
return std::make_unique<SimplePendulum>(instanceName, fmuResourceLocation); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
include("${PROJECT_SOURCE_DIR}/cmake/generate_fmu.cmake") | ||
|
||
|
||
add_library(${modelIdentifier} SHARED | ||
model.cpp | ||
"$<TARGET_OBJECTS:fmu4cpp>") | ||
target_include_directories(${modelIdentifier} PRIVATE ${PROJECT_SOURCE_DIR}/export/include) | ||
target_compile_definitions(${modelIdentifier} PRIVATE FMU4CPP_MODEL_IDENTIFIER="${modelIdentifier}") | ||
"model.cpp" | ||
) | ||
|
||
# Generate FMU | ||
generateFMU("${modelIdentifier}") | ||
|
||
set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/model.txt") | ||
file(WRITE ${OUTPUT_FILE} "${modelIdentifier}") | ||
|
||
#generateFMU("model" "${modelIdentifier}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters