generated from ut-issl/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from ut-issl/feature/add-force-generator
Add force generator
- Loading branch information
Showing
10 changed files
with
206 additions
and
3 deletions.
There are no files selected for viewing
Submodule s2e-core
updated
18 files
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ForceGenerator] | ||
// Standard deviation of force magnitude error [N] | ||
force_magnitude_standard_deviation_N = 0.01 | ||
|
||
// Standard deviation of force direction error [deg] | ||
force_direction_standard_deviation_deg = 1 | ||
|
||
[ComponentBase] | ||
// Prescaler with respect to the component update period | ||
prescaler = 1 |
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
100 changes: 100 additions & 0 deletions
100
s2e-ff/src/Components/IdealComponents/ForceGenerator.cpp
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,100 @@ | ||
#include "ForceGenerator.hpp" | ||
|
||
#include <cfloat> | ||
|
||
// Constructor | ||
ForceGenerator::ForceGenerator(const int prescaler, ClockGenerator* clock_gen, const double magnitude_error_standard_deviation_N, | ||
const double direction_error_standard_deviation_rad, const Dynamics* dynamics) | ||
: ComponentBase(prescaler, clock_gen), | ||
magnitude_noise_(0.0, magnitude_error_standard_deviation_N), | ||
direction_error_standard_deviation_rad_(direction_error_standard_deviation_rad), | ||
dynamics_(dynamics) { | ||
direction_noise_.set_param(0.0, 1.0); | ||
} | ||
|
||
ForceGenerator::~ForceGenerator() {} | ||
|
||
void ForceGenerator::MainRoutine(int count) { | ||
UNUSED(count); | ||
|
||
generated_force_b_N_ = ordered_force_b_N_; | ||
|
||
// Add noise | ||
double norm_ordered_force = norm(ordered_force_b_N_); | ||
if (norm_ordered_force > 0.0 + DBL_EPSILON) { | ||
// Add noise only when the force is generated | ||
libra::Vector<3> true_direction = normalize(generated_force_b_N_); | ||
libra::Quaternion error_quaternion = GenerateDirectionNoiseQuaternion(true_direction, direction_error_standard_deviation_rad_); | ||
libra::Vector<3> converted_direction = error_quaternion.frame_conv(generated_force_b_N_); | ||
double force_norm_with_error = norm_ordered_force + magnitude_noise_; | ||
generated_force_b_N_ = force_norm_with_error * converted_direction; | ||
} | ||
|
||
// Convert frame | ||
libra::Quaternion q_i2b = dynamics_->GetAttitude().GetQuaternion_i2b(); | ||
libra::Quaternion q_i2rtn = dynamics_->GetOrbit().CalcQuaternionI2LVLH(); | ||
generated_force_i_N_ = q_i2b.frame_conv_inv(generated_force_b_N_); | ||
generated_force_rtn_N_ = q_i2rtn.frame_conv(generated_force_i_N_); | ||
} | ||
|
||
void ForceGenerator::PowerOffRoutine() { | ||
generated_force_b_N_ *= 0.0; | ||
generated_force_i_N_ *= 0.0; | ||
generated_force_rtn_N_ *= 0.0; | ||
} | ||
|
||
void ForceGenerator::SetForce_i_N(const libra::Vector<3> force_i_N) { | ||
libra::Quaternion q_i2b = dynamics_->GetAttitude().GetQuaternion_i2b(); | ||
ordered_force_b_N_ = q_i2b.frame_conv(force_i_N); | ||
} | ||
|
||
void ForceGenerator::SetForce_rtn_N(const libra::Vector<3> force_rtn_N) { | ||
libra::Quaternion q_i2b = dynamics_->GetAttitude().GetQuaternion_i2b(); | ||
libra::Quaternion q_i2rtn = dynamics_->GetOrbit().CalcQuaternionI2LVLH(); | ||
|
||
libra::Vector<3> force_i_N = q_i2rtn.frame_conv_inv(force_rtn_N); | ||
ordered_force_b_N_ = q_i2b.frame_conv(force_i_N); | ||
} | ||
|
||
std::string ForceGenerator::GetLogHeader() const { | ||
std::string str_tmp = ""; | ||
|
||
std::string head = "IdealForceGenerator_"; | ||
str_tmp += WriteVector(head + "ordered_force", "b", "N", 3); | ||
str_tmp += WriteVector(head + "generated_force", "b", "N", 3); | ||
str_tmp += WriteVector(head + "generated_force", "i", "N", 3); | ||
str_tmp += WriteVector(head + "generated_force", "rtn", "N", 3); | ||
|
||
return str_tmp; | ||
} | ||
|
||
std::string ForceGenerator::GetLogValue() const { | ||
std::string str_tmp = ""; | ||
|
||
str_tmp += WriteVector(ordered_force_b_N_); | ||
str_tmp += WriteVector(generated_force_b_N_); | ||
str_tmp += WriteVector(generated_force_i_N_); | ||
str_tmp += WriteVector(generated_force_rtn_N_); | ||
|
||
return str_tmp; | ||
} | ||
|
||
libra::Quaternion ForceGenerator::GenerateDirectionNoiseQuaternion(libra::Vector<3> true_direction, const double error_standard_deviation_rad) { | ||
libra::Vector<3> random_direction; | ||
random_direction[0] = direction_noise_; | ||
random_direction[1] = direction_noise_; | ||
random_direction[2] = direction_noise_; | ||
random_direction = normalize(random_direction); | ||
|
||
libra::Vector<3> rotation_axis; | ||
rotation_axis = outer_product(true_direction, random_direction); | ||
double norm_rotation_axis = norm(rotation_axis); | ||
if (norm_rotation_axis < 0.0 + DBL_EPSILON) { | ||
// No rotation error if the randomized direction is parallel to the true direction | ||
rotation_axis = true_direction; | ||
} | ||
|
||
double error_angle_rad = direction_noise_ * error_standard_deviation_rad; | ||
libra::Quaternion error_quaternion(rotation_axis, error_angle_rad); | ||
return error_quaternion; | ||
} |
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,47 @@ | ||
#pragma once | ||
|
||
#include <Component/Abstract/ComponentBase.h> | ||
#include <Dynamics/Dynamics.h> | ||
#include <Interface/LogOutput/Logger.h> | ||
|
||
#include <Library/math/NormalRand.hpp> | ||
#include <Library/math/Vector.hpp> | ||
|
||
class ForceGenerator : public ComponentBase, public ILoggable { | ||
public: | ||
ForceGenerator(const int prescaler, ClockGenerator* clock_gen, const double magnitude_error_standard_deviation_N, | ||
const double direction_error_standard_deviation_rad, const Dynamics* dynamics); | ||
~ForceGenerator(); | ||
|
||
// ComponentBase override function | ||
void MainRoutine(int count); | ||
void PowerOffRoutine(); | ||
|
||
// ILogabble override function | ||
virtual std::string GetLogHeader() const; | ||
virtual std::string GetLogValue() const; | ||
|
||
// Getter | ||
inline const Vector<3> GetGeneratedForce_b_N() const { return generated_force_b_N_; }; | ||
inline const Vector<3> GetGeneratedForce_i_N() const { return generated_force_i_N_; }; | ||
inline const Vector<3> GetGeneratedForce_rtn_N() const { return generated_force_rtn_N_; }; | ||
|
||
// Setter | ||
inline void SetForce_b_N(const libra::Vector<3> force_b_N) { ordered_force_b_N_ = force_b_N; }; | ||
void SetForce_i_N(const libra::Vector<3> force_i_N); | ||
void SetForce_rtn_N(const libra::Vector<3> force_rtn_N); | ||
|
||
protected: | ||
libra::Vector<3> ordered_force_b_N_{0.0}; | ||
libra::Vector<3> generated_force_b_N_{0.0}; | ||
libra::Vector<3> generated_force_i_N_{0.0}; | ||
libra::Vector<3> generated_force_rtn_N_{0.0}; | ||
|
||
// Noise | ||
libra::NormalRand magnitude_noise_; | ||
libra::NormalRand direction_noise_; | ||
double direction_error_standard_deviation_rad_; | ||
libra::Quaternion GenerateDirectionNoiseQuaternion(libra::Vector<3> true_direction, const double error_standard_deviation_rad); | ||
|
||
const Dynamics* dynamics_; | ||
}; |
21 changes: 21 additions & 0 deletions
21
s2e-ff/src/Components/IdealComponents/InitializeForceGenerator.cpp
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,21 @@ | ||
#include "InitializeForceGenerator.hpp" | ||
|
||
#include <Interface/InitInput/IniAccess.h> | ||
|
||
ForceGenerator InitializeForceGenerator(ClockGenerator* clock_gen, const std::string file_name, const Dynamics* dynamics) { | ||
// General | ||
IniAccess ini_file(file_name); | ||
|
||
// CompoBase | ||
int prescaler = ini_file.ReadInt("ComponentBase", "prescaler"); | ||
if (prescaler <= 1) prescaler = 1; | ||
|
||
// ForceGenerator | ||
char section[30] = "ForceGenerator"; | ||
double force_magnitude_standard_deviation_N = ini_file.ReadDouble(section, "force_magnitude_standard_deviation_N"); | ||
double force_direction_standard_deviation_deg = ini_file.ReadDouble(section, "force_direction_standard_deviation_deg"); | ||
double force_direction_standard_deviation_rad = libra::deg_to_rad * force_direction_standard_deviation_deg; | ||
ForceGenerator force_generator(prescaler, clock_gen, force_magnitude_standard_deviation_N, force_direction_standard_deviation_rad, dynamics); | ||
|
||
return force_generator; | ||
} |
5 changes: 5 additions & 0 deletions
5
s2e-ff/src/Components/IdealComponents/InitializeForceGenerator.hpp
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,5 @@ | ||
#pragma once | ||
|
||
#include "ForceGenerator.hpp" | ||
|
||
ForceGenerator InitializeForceGenerator(ClockGenerator* clock_gen, const std::string file_name, const Dynamics* dynamics); |
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