Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Refactor trajectory #278

Merged
merged 31 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2a22346
Merge branch 'master' into feature/PointOnLink
dellaert Sep 16, 2021
177b0d2
Merge pull request #224 from borglab/feature/PointOnLink
dellaert Sep 16, 2021
f0f3ec2
Renamed old Phase to FootContactState
dellaert Sep 2, 2021
f7ea4f5
Added Kinematics routines for Trajectory [WIP]
dellaert Sep 2, 2021
a79be89
Prototyped how test would look
dellaert Sep 2, 2021
28680f1
Started refactor
dellaert Sep 2, 2021
9a5c0cb
refactoring resumed: methods moved from Phase to FootContactState, wh…
danbarla Sep 22, 2021
14747c9
refactor resumed: methods moved from WalkCycle to Trajectory includin…
danbarla Sep 22, 2021
bc7fcd5
small fix
danbarla Sep 22, 2021
ac2ab93
WalkCycle Construtor with FootContactState Vector, other fixes
danbarla Sep 25, 2021
14129a8
added documentation
danbarla Sep 26, 2021
f1a024e
rename to FootContactConstraintSpec
danbarla Sep 26, 2021
8021fae
added KInematicsPhase.cpp, KInematicsTrajectory adjusted accordingly
danbarla Sep 26, 2021
03c176f
fixed tests
danbarla Sep 27, 2021
fe9429f
small test fix
danbarla Sep 27, 2021
168cbed
fixed docs and small changes
danbarla Oct 2, 2021
1dce847
some renaming
danbarla Oct 2, 2021
752894b
contact_points_ member removed from Trajectory. Some functionality we…
danbarla Oct 25, 2021
3c1ecdc
Merge branch 'master' into feature/refactor_trajectory
danbarla Oct 25, 2021
dd89641
fixes after review
danbarla Oct 27, 2021
dfef9bd
added CostraintSpec Destructor
danbarla Oct 28, 2021
4c56618
fix cast in WalkCycle
danbarla Oct 28, 2021
30e125a
Merge branch 'master' into feature/refactor_trajectory
danbarla Oct 28, 2021
ee2829e
more changes after reviews
danbarla Oct 29, 2021
5c881b7
small changes
danbarla Oct 29, 2021
ab36212
prints to debug clang tests
danbarla Oct 29, 2021
8f99b24
Revert "prints to debug clang tests"
danbarla Oct 30, 2021
50429cb
Revert "small changes"
danbarla Oct 30, 2021
02672d7
Revert "more changes after reviews"
danbarla Oct 30, 2021
44381f3
back to "small changes", last reviewed commit
danbarla Nov 1, 2021
8a862e9
clang-9 issues on CI, commented out for now
danbarla Nov 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions examples/example_spider_walking/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ Trajectory getTrajectory(const Robot& robot, size_t repeat) {
links.insert(links.end(), even_links.begin(), even_links.end());

const Point3 contact_in_com(0, 0.19, 0);
Phase stationary(1, links, contact_in_com), odd(2, odd_links, contact_in_com),
even(2, even_links, contact_in_com);
auto stationary = boost::make_shared<FootContactConstraintSpec>(links, contact_in_com);
auto odd = boost::make_shared<FootContactConstraintSpec>(odd_links, contact_in_com);
auto even = boost::make_shared<FootContactConstraintSpec>(even_links, contact_in_com);

WalkCycle walk_cycle;
walk_cycle.addPhase(stationary);
walk_cycle.addPhase(even);
walk_cycle.addPhase(stationary);
walk_cycle.addPhase(odd);
FootContactVector states = {stationary, even, stationary, odd};
std::vector<size_t> phase_lengths = {1,2,1,2};

WalkCycle walk_cycle(states, phase_lengths);

return Trajectory(walk_cycle, repeat);
}
Expand Down Expand Up @@ -98,7 +98,7 @@ int main(int argc, char** argv) {
double ground_height = 1.0;
const Point3 step(0, 0.4, 0);
gtsam::NonlinearFactorGraph objectives =
trajectory.contactPointObjectives(robot, Isotropic::Sigma(3, 1e-7), step);
trajectory.contactPointObjectives(robot, Isotropic::Sigma(3, 1e-7), step, ground_height);

// Get final time step.
int K = trajectory.getEndTimeStep(trajectory.numPhases() - 1);
Expand Down
34 changes: 3 additions & 31 deletions gtdynamics.i
Original file line number Diff line number Diff line change
Expand Up @@ -835,26 +835,10 @@ class Interval {
// ContactPointGoals is defined in specializations

class Phase {
Phase(size_t num_time_steps);
Phase(size_t num_time_steps,
const std::vector<gtdynamics::PointOnLink> &point_on_links);
Phase(size_t num_time_steps,
const std::vector<gtdynamics::LinkSharedPtr> &links,
const gtsam::Point3 &contact_in_com);
const gtdynamics::PointOnLinks &contactPoints() const;
const gtsam::Point3 &contactPoint(const std::string &link_name) const;
Phase(size_t k_start, size_t k_end,
const boost::shared_ptr<gtdynamics::ConstraintSpec> &constraints);
int numTimeSteps() const;
void print(const string &s = "");
gtsam::NonlinearFactorGraph
contactPointObjectives(const gtdynamics::PointOnLinks &all_contact_points,
const gtsam::Point3 &step,
const gtsam::SharedNoiseModel &cost_model,
size_t k_start,
gtdynamics::ContactPointGoals &cp_goals) const;
std::map<string, gtsam::Point3>
updateContactPointGoals(const gtdynamics::PointOnLinks &all_contact_points,
const gtsam::Point3 &step,
gtdynamics::ContactPointGoals &cp_goals) const;
gtsam::Matrix jointMatrix(const gtdynamics::Robot &robot,
const gtsam::Values &results, size_t k = 0,
double dt) const;
Expand All @@ -863,21 +847,11 @@ class Phase {
#include <gtdynamics/utils/WalkCycle.h>
class WalkCycle {
WalkCycle();
WalkCycle(const std::vector<gtdynamics::Phase>& phases);
void addPhase(const gtdynamics::Phase& phase);
const gtdynamics::Phase& phase(size_t p);
const std::vector<gtdynamics::Phase>& phases() const;
size_t numPhases() const;
const gtdynamics::PointOnLinks& contactPoints() const;
void print(const string& s = "") const;
gtdynamics::ContactPointGoals
initContactPointGoal(const gtdynamics::Robot& robot,
double ground_height) const;
std::vector<string> swingLinks(size_t p) const;
gtsam::NonlinearFactorGraph
contactPointObjectives(const gtsam::Point3 &step,
const gtsam::SharedNoiseModel &cost_model, size_t k_start,
gtdynamics::ContactPointGoals @cp_goals) const;
std::vector<string> getPhaseSwingLinks(size_t p) const;
};

#include <gtdynamics/utils/Trajectory.h>
Expand All @@ -903,12 +877,10 @@ class Trajectory {
gtsam::Values multiPhaseInitialValues(const gtdynamics::Robot& robot,
double gaussian_noise, double dt) const;
std::vector<int> finalTimeSteps() const;
size_t phaseIndex(size_t p) const;
const Phase &phase(size_t p) const;
size_t getStartTimeStep(size_t p) const;
size_t getEndTimeStep(size_t p) const;
const gtdynamics::PointOnLinks &getPhaseContactLinks(size_t p) const;
std::vector<string> getPhaseSwingLinks(size_t p) const;
PointGoalFactor pointGoalFactor(const gtdynamics::Robot &robot,
const string &link_name,
const gtdynamics::PointOnLink &cp, size_t k,
Expand Down
3 changes: 2 additions & 1 deletion gtdynamics/kinematics/Kinematics.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ class Kinematics : public Optimizer {
* @param contact_goals1 goals for contact points for interval.k_end
* All results are return in values.
*/
gtsam::Values interpolate(const Interval& interval, const Robot& robot,
template <class CONTEXT>
gtsam::Values interpolate(const CONTEXT& context, const Robot& robot,
const ContactGoals& contact_goals1,
const ContactGoals& contact_goals2) const;
};
Expand Down
7 changes: 4 additions & 3 deletions gtdynamics/kinematics/KinematicsInterval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ Values Kinematics::inverse<Interval>(const Interval& interval,
return results;
}

Values Kinematics::interpolate(const Interval& interval, const Robot& robot,
const ContactGoals& contact_goals1,
const ContactGoals& contact_goals2) const {
template <>
Values Kinematics::interpolate<Interval>(const Interval& interval, const Robot& robot,
const ContactGoals& contact_goals1,
const ContactGoals& contact_goals2) const {
Values result;
const double dt = 1.0 / (interval.k_start - interval.k_end); // 5 6 7 8 9 [10
for (size_t k = interval.k_start; k <= interval.k_end; k++) {
Expand Down
83 changes: 83 additions & 0 deletions gtdynamics/kinematics/KinematicsPhase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* ----------------------------------------------------------------------------
* GTDynamics Copyright 2020, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* See LICENSE for the license information
* -------------------------------------------------------------------------- */

/**
* @file KinematicsPhase.cpp
* @brief Kinematics for a Phase with fixed contacts.
* @author: Dan Barladeanu, Frank Dellaert
*/

#include <gtdynamics/kinematics/Kinematics.h>
#include <gtdynamics/utils/Phase.h>
#include <gtsam/nonlinear/GaussNewtonOptimizer.h>
#include <gtsam/nonlinear/LevenbergMarquardtOptimizer.h>

namespace gtdynamics {

using gtsam::NonlinearFactorGraph;
using gtsam::Values;
using std::string;
using std::vector;

template <>
NonlinearFactorGraph Kinematics::graph<Phase>(const Phase& phase,
const Robot& robot) const {
NonlinearFactorGraph graph;
Interval interval = static_cast<Interval>(phase);
graph.add(this->graph(interval, robot));
return graph;
}

template <>
NonlinearFactorGraph Kinematics::pointGoalObjectives<Phase>(
const Phase& phase, const ContactGoals& contact_goals) const {
NonlinearFactorGraph graph;
Interval interval = static_cast<Interval>(phase);
graph.add(pointGoalObjectives(interval, contact_goals));
return graph;
}

template <>
NonlinearFactorGraph Kinematics::jointAngleObjectives<Phase>(
const Phase& phase, const Robot& robot) const {
NonlinearFactorGraph graph;
Interval interval = static_cast<Interval>(phase);
graph.add(jointAngleObjectives(interval, robot));
return graph;
}

template <>
Values Kinematics::initialValues<Phase>(const Phase& phase,
const Robot& robot,
double gaussian_noise) const {
Values values;
Interval interval = static_cast<Interval>(phase);
values.insert(initialValues(interval, robot, gaussian_noise));
return values;
}

template <>
Values Kinematics::inverse<Phase>(const Phase& phase,
const Robot& robot,
const ContactGoals& contact_goals) const {
Values results;
Interval interval = static_cast<Interval>(phase);
results.insert(inverse(interval, robot, contact_goals));
return results;
}

template <>
Values Kinematics::interpolate<Phase>(const Phase& phase, const Robot& robot,
const ContactGoals& contact_goals1,
const ContactGoals& contact_goals2) const {
Values result;
Interval interval = static_cast<Interval>(phase);
result = interpolate(interval, robot, contact_goals1, contact_goals2);
return result;
}

} // namespace gtdynamics
90 changes: 90 additions & 0 deletions gtdynamics/kinematics/KinematicsTrajectory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* ----------------------------------------------------------------------------
* GTDynamics Copyright 2020, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* See LICENSE for the license information
* -------------------------------------------------------------------------- */

/**
* @file KinematicsInterval.cpp
* @brief Kinematics for an trajectory with fixed contacts.
* @author: Frank Dellaert
*/

#include <gtdynamics/kinematics/Kinematics.h>
#include <gtdynamics/utils/Slice.h>
#include <gtdynamics/utils/Trajectory.h>
#include <gtsam/nonlinear/GaussNewtonOptimizer.h>
#include <gtsam/nonlinear/LevenbergMarquardtOptimizer.h>

namespace gtdynamics {

using gtsam::NonlinearFactorGraph;
using gtsam::Values;
using std::string;
using std::vector;

template <>
NonlinearFactorGraph Kinematics::graph<Trajectory>(const Trajectory& trajectory,
const Robot& robot) const {
NonlinearFactorGraph graph;
for (auto&& phase : trajectory.phases()) {
graph.add(this->graph(phase, robot));
}
return graph;
}

template <>
NonlinearFactorGraph Kinematics::pointGoalObjectives<Trajectory>(
const Trajectory& trajectory, const ContactGoals& contact_goals) const {
NonlinearFactorGraph graph;
for (auto&& phase : trajectory.phases()) {
graph.add(pointGoalObjectives(phase, contact_goals));
}
return graph;
}

template <>
NonlinearFactorGraph Kinematics::jointAngleObjectives<Trajectory>(
const Trajectory& trajectory, const Robot& robot) const {
NonlinearFactorGraph graph;
for (auto&& phase : trajectory.phases()) {
graph.add(jointAngleObjectives(phase, robot));
}
return graph;
}

template <>
Values Kinematics::initialValues<Trajectory>(const Trajectory& trajectory,
const Robot& robot,
double gaussian_noise) const {
Values values;
for (auto&& phase : trajectory.phases()) {
values.insert(initialValues(phase, robot, gaussian_noise));
}
return values;
}

template <>
Values Kinematics::inverse<Trajectory>(
const Trajectory& trajectory, const Robot& robot,
const ContactGoals& contact_goals) const {
Values results;
for (auto&& phase : trajectory.phases()) {
results.insert(inverse(phase, robot, contact_goals));
}
return results;
}

template<>
Values Kinematics::interpolate<Trajectory>(const Trajectory& trajectory, const Robot& robot,
const ContactGoals& contact_goals1,
const ContactGoals& contact_goals2) const {
Values results;
for (auto&& phase : trajectory.phases()) {
results.insert(interpolate(phase, robot, contact_goals1, contact_goals2));
}
return results;
}

} // namespace gtdynamics
32 changes: 32 additions & 0 deletions gtdynamics/utils/ConstraintSpec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* ----------------------------------------------------------------------------
* GTDynamics Copyright 2020, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* See LICENSE for the license information
* -------------------------------------------------------------------------- */

/**
* @file ConstraintSpec.h
* @brief Base class for constraint specification
* @author: Dan Barladeanu, Frank Dellaert
*/

#pragma once

namespace gtdynamics {

/**
* @class ConstraintSpec is a base class for constraint specification on phases.
* Derived Constraints could be walking, jumping, rocketry etc..
* Derived Class currently implemented: FootContactConstraintSpec - specific for walking.
*/

class ConstraintSpec {
public:
/// GTSAM-style print, pure virtual here
virtual void print(const std::string &s) const = 0;
};



} // namespace gtdynamics
Loading