Skip to content

Commit

Permalink
fix: sequence relative event condition (#456)
Browse files Browse the repository at this point in the history
* feat: add clone methods to EventCondition classes

* fix: sequence relative event condition update

* patch: things
  • Loading branch information
vishwa2710 authored Oct 31, 2024
1 parent 7e082b3 commit 32769f6
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using namespace pybind11;
using ostk::core::type::Real;
using ostk::core::type::Shared;
using ostk::core::type::String;
using ostk::core::type::Unique;

using ostk::mathematics::object::VectorXd;

Expand All @@ -35,6 +36,11 @@ class PyEventCondition : public EventCondition
{
PYBIND11_OVERRIDE_PURE_NAME(bool, EventCondition, "is_satisfied", isSatisfied, currentState, previousState);
}

EventCondition* clone() const override
{
PYBIND11_OVERRIDE_PURE_NAME(EventCondition*, EventCondition, "clone", clone);
}
};

inline void OpenSpaceToolkitAstrodynamicsPy_EventCondition(pybind11::module& aModule)
Expand Down
11 changes: 6 additions & 5 deletions bindings/python/test/test_event_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ def event_condition(
class MyEventCondition(EventCondition):
def is_satisfied(
self,
current_state_vector,
current_time,
previous_state_vector,
previous_time,
current_state,
previous_state,
):
return current_state_vector[0] > 0.0 and previous_state_vector[0] < 0.0
return (
current_state.get_position().get_coordinates()[2] > 0.0
and previous_state.get_position().get_coordinates()[2] < 0.0
)

return MyEventCondition(name, evaluator, target)

Expand Down
7 changes: 7 additions & 0 deletions include/OpenSpaceToolkit/Astrodynamics/EventCondition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <OpenSpaceToolkit/Core/Type/Real.hpp>
#include <OpenSpaceToolkit/Core/Type/String.hpp>
#include <OpenSpaceToolkit/Core/Type/Unique.hpp>

#include <OpenSpaceToolkit/Physics/Unit/Derived/Angle.hpp>
#include <OpenSpaceToolkit/Physics/Unit/Length.hpp>
Expand All @@ -18,6 +19,7 @@ namespace astrodynamics

using ostk::core::type::Real;
using ostk::core::type::String;
using ostk::core::type::Unique;

using ostk::physics::unit::Angle;
using ostk::physics::unit::Length;
Expand Down Expand Up @@ -118,6 +120,11 @@ class EventCondition
/// printing
virtual void print(std::ostream& anOutputStream, bool displayDecorator = true) const;

/// @brief Create a copy of this EventCondition
///
/// @return Pointer to the cloned EventCondition
virtual EventCondition* clone() const = 0;

/// @brief Check if the Event Condition is satisfied based on current state and previous
/// state/time
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ class AngularCondition : public EventCondition
/// @return Boolean value indicating if the Event Condition is met
virtual bool isSatisfied(const State& currentState, const State& previousState) const override;

/// @brief Create a copy of this AngularCondition
///
/// @return Pointer to the cloned EventCondition
virtual AngularCondition* clone() const override;

/// @brief Create an angular condition that is satisfied when the angle is within a range
///
/// @param aName A string representing the name of the Angular Event Condition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class LogicalCondition : public EventCondition
/// the printing process.
virtual void print(std::ostream& anOutputStream, bool displayDecorator = true) const override;

/// @brief Create a copy of this LogicalCondition.
///
/// @return Pointer to the cloned EventCondition
virtual LogicalCondition* clone() const override;

private:
typedef std::function<
bool(const Array<Shared<EventCondition>>&, const State& currentState, const State& previousState)>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class RealCondition : public EventCondition
/// @return Boolean value indicating if the Event Condition is met
virtual bool isSatisfied(const State& currentState, const State& previousState) const override;

/// @brief clone the Real Condition
///
/// @return Pointer to the cloned Real Condition
virtual RealCondition* clone() const override;

/// @brief Convert criterion to string
///
/// @param aCriterion An enum representing the criterion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ bool AngularCondition::isSatisfied(const State& currentState, const State& previ
return comparator_(evaluator_(currentState), evaluator_(previousState), (target_.value + target_.valueOffset));
}

AngularCondition* AngularCondition::clone() const
{
return new AngularCondition(*this);
}

AngularCondition AngularCondition::WithinRange(
const String& aName, const std::function<Real(const State&)>& anEvaluator, const Pair<Angle, Angle>& aTargetRange
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ void LogicalCondition::print(std::ostream& anOutputStream, bool displayDecorator
displayDecorator ? ostk::core::utils::Print::Footer(anOutputStream) : void();
}

LogicalCondition* LogicalCondition::clone() const
{
return new LogicalCondition(*this);
}

LogicalCondition::evaluationSignature LogicalCondition::GenerateEvaluator(const LogicalCondition::Type& aType)
{
switch (aType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ bool RealCondition::isSatisfied(const State& currentState, const State& previous
return comparator_(evaluate(currentState), evaluate(previousState));
}

RealCondition* RealCondition::clone() const
{
return new RealCondition(*this);
}

String RealCondition::StringFromCriterion(const Criterion& aCriterion)
{
switch (aCriterion)
Expand Down
9 changes: 8 additions & 1 deletion src/OpenSpaceToolkit/Astrodynamics/Trajectory/Sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>

#include <OpenSpaceToolkit/Core/Type/Unique.hpp>

#include <OpenSpaceToolkit/Physics/Time/Duration.hpp>

#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Sequence.hpp>
Expand All @@ -15,6 +17,7 @@ namespace astrodynamics
namespace trajectory
{

using ostk::core::type::Unique;
using ostk::physics::time::Duration;

Sequence::Solution::Solution(const Array<Segment::Solution>& aSegmentSolutionArray, const bool& anExecutionIsComplete)
Expand Down Expand Up @@ -322,6 +325,9 @@ Sequence::Solution Sequence::solveToCondition(

Duration propagationDuration = Duration::Zero();

const Unique<EventCondition> sequenceCondition(anEventCondition.clone());
sequenceCondition->updateTarget(initialState);

while (!eventConditionIsSatisfied && propagationDuration <= aMaximumPropagationDuration)
{
for (const Segment& segment : segments_)
Expand Down Expand Up @@ -350,7 +356,8 @@ Sequence::Solution Sequence::solveToCondition(
return {segmentSolutions, false};
}

eventConditionIsSatisfied = anEventCondition.isSatisfied(segmentSolution.states.accessLast(), initialState);
eventConditionIsSatisfied =
sequenceCondition->isSatisfied(segmentSolution.states.accessLast(), initialState);

// Terminate Sequence successfully if a provided event condition was satisfied
if (eventConditionIsSatisfied)
Expand Down
3 changes: 3 additions & 0 deletions test/OpenSpaceToolkit/Astrodynamics/EventCondition.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using ostk::core::type::Real;
using ostk::core::type::String;
using ostk::core::type::Unique;

using ostk::physics::coordinate::Frame;
using ostk::physics::coordinate::Position;
Expand All @@ -37,6 +38,8 @@ class TestCondition : public EventCondition
{
}

MOCK_METHOD(EventCondition*, clone, (), (const, override));

MOCK_METHOD(bool, isSatisfied, (const State& currentState, const State& previousState), (const, override));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <OpenSpaceToolkit/Core/Type/Real.hpp>
#include <OpenSpaceToolkit/Core/Type/Shared.hpp>
#include <OpenSpaceToolkit/Core/Type/String.hpp>
#include <OpenSpaceToolkit/Core/Type/Unique.hpp>

#include <OpenSpaceToolkit/Mathematics/Object/Vector.hpp>

Expand All @@ -23,6 +24,7 @@ using ostk::core::container::Pair;
using ostk::core::type::Real;
using ostk::core::type::Shared;
using ostk::core::type::String;
using ostk::core::type::Unique;

using ostk::mathematics::object::VectorXd;

Expand Down Expand Up @@ -309,6 +311,11 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_EventCondition_AngularCondition, isSatisfi
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_EventCondition_AngularCondition, Clone)
{
EXPECT_NO_THROW({ Unique<AngularCondition> clonedCondition(defaultCondition_.clone()); });
}

TEST_F(OpenSpaceToolkit_Astrodynamics_EventCondition_AngularCondition, StringFromCriterion)
{
EXPECT_TRUE(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <OpenSpaceToolkit/Core/Type/Real.hpp>
#include <OpenSpaceToolkit/Core/Type/Shared.hpp>
#include <OpenSpaceToolkit/Core/Type/String.hpp>
#include <OpenSpaceToolkit/Core/Type/Unique.hpp>

#include <OpenSpaceToolkit/Mathematics/Object/Vector.hpp>

Expand All @@ -24,6 +25,7 @@ using ostk::core::container::Array;
using ostk::core::type::Real;
using ostk::core::type::Shared;
using ostk::core::type::String;
using ostk::core::type::Unique;

using ostk::mathematics::object::VectorXd;

Expand Down Expand Up @@ -225,3 +227,8 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_EventCondition_LogicalCondition, Or)
EXPECT_FALSE(logicalCondition.isSatisfied(defaultState_, defaultState_));
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_EventCondition_LogicalCondition, Clone)
{
EXPECT_NO_THROW({ Unique<LogicalCondition> clonedCondition(defaultLogicalCondition_.clone()); });
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <OpenSpaceToolkit/Core/Type/Real.hpp>
#include <OpenSpaceToolkit/Core/Type/Shared.hpp>
#include <OpenSpaceToolkit/Core/Type/String.hpp>
#include <OpenSpaceToolkit/Core/Type/Unique.hpp>

#include <OpenSpaceToolkit/Mathematics/Object/Vector.hpp>

Expand All @@ -22,6 +23,7 @@ using ostk::core::container::Array;
using ostk::core::type::Real;
using ostk::core::type::Shared;
using ostk::core::type::String;
using ostk::core::type::Unique;

using ostk::mathematics::object::VectorXd;

Expand Down Expand Up @@ -214,6 +216,11 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_EventCondition_RealCondition, DurationCond
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_EventCondition_RealCondition, Clone)
{
EXPECT_NO_THROW({ Unique<RealCondition> clonedCondition(defaultCondition_.clone()); });
}

TEST_F(OpenSpaceToolkit_Astrodynamics_EventCondition_RealCondition, StringFromCriterion)
{
{
Expand Down
27 changes: 27 additions & 0 deletions test/OpenSpaceToolkit/Astrodynamics/Trajectory/Sequence.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,33 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_Trajectory_Sequence, SolveToCondition)

EXPECT_FALSE(solution.executionIsComplete);
}

// sequence completion due to duration condition (Regression test)
{
const Segment segment = Segment::Coast(
"Coast",
std::make_shared<RealCondition>(
RealCondition::DurationCondition(RealCondition::Criterion::StrictlyPositive, Duration::Seconds(10.0))
),
defaultDynamics_,
defaultNumericalSolver_
);

const Sequence sequence = {
{segment},
defaultNumericalSolver_,
defaultDynamics_,
defaultMaximumPropagationDuration_,
};

const RealCondition eventCondition =
RealCondition::DurationCondition(RealCondition::Criterion::StrictlyPositive, Duration::Minutes(1.0));

const Sequence::Solution solution = sequence.solveToCondition(defaultState_, eventCondition);

EXPECT_TRUE(solution.executionIsComplete);
EXPECT_EQ(solution.segmentSolutions.getSize(), 6);
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Trajectory_Sequence, Solve_2)
Expand Down

0 comments on commit 32769f6

Please sign in to comment.