| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh, | ||
| 5 | // Heriot-Watt University | ||
| 6 | // Copyright note valid unless otherwise stated in individual files. | ||
| 7 | // All rights reserved. | ||
| 8 | /////////////////////////////////////////////////////////////////////////////// | ||
| 9 | |||
| 10 | #include "crocoddyl/core/residuals/control.hpp" | ||
| 11 | |||
| 12 | namespace crocoddyl { | ||
| 13 | |||
| 14 | template <typename Scalar> | ||
| 15 | ✗ | ResidualModelControlTpl<Scalar>::ResidualModelControlTpl( | |
| 16 | std::shared_ptr<typename Base::StateAbstract> state, const VectorXs& uref) | ||
| 17 | ✗ | : Base(state, static_cast<std::size_t>(uref.size()), | |
| 18 | ✗ | static_cast<std::size_t>(uref.size()), false, false, true), | |
| 19 | ✗ | uref_(uref) { | |
| 20 | ✗ | if (nu_ == 0) { | |
| 21 | ✗ | throw_pretty("Invalid argument: " | |
| 22 | << "it seems to be an autonomous system, if so, don't add " | ||
| 23 | "this residual function"); | ||
| 24 | } | ||
| 25 | ✗ | } | |
| 26 | |||
| 27 | template <typename Scalar> | ||
| 28 | ✗ | ResidualModelControlTpl<Scalar>::ResidualModelControlTpl( | |
| 29 | std::shared_ptr<typename Base::StateAbstract> state, const std::size_t nu) | ||
| 30 | ✗ | : Base(state, nu, nu, false, false, true), uref_(VectorXs::Zero(nu)) { | |
| 31 | ✗ | if (nu_ == 0) { | |
| 32 | ✗ | throw_pretty("Invalid argument: " | |
| 33 | << "it seems to be an autonomous system, if so, don't add " | ||
| 34 | "this residual function"); | ||
| 35 | } | ||
| 36 | ✗ | } | |
| 37 | |||
| 38 | template <typename Scalar> | ||
| 39 | ✗ | ResidualModelControlTpl<Scalar>::ResidualModelControlTpl( | |
| 40 | std::shared_ptr<typename Base::StateAbstract> state) | ||
| 41 | : Base(state, state->get_nv(), state->get_nv(), false, false, true), | ||
| 42 | ✗ | uref_(VectorXs::Zero(state->get_nv())) {} | |
| 43 | |||
| 44 | template <typename Scalar> | ||
| 45 | ✗ | void ResidualModelControlTpl<Scalar>::calc( | |
| 46 | const std::shared_ptr<ResidualDataAbstract>& data, | ||
| 47 | const Eigen::Ref<const VectorXs>&, const Eigen::Ref<const VectorXs>& u) { | ||
| 48 | ✗ | if (static_cast<std::size_t>(u.size()) != nu_) { | |
| 49 | ✗ | throw_pretty( | |
| 50 | "Invalid argument: " << "u has wrong dimension (it should be " + | ||
| 51 | std::to_string(nu_) + ")"); | ||
| 52 | } | ||
| 53 | |||
| 54 | ✗ | data->r = u - uref_; | |
| 55 | ✗ | } | |
| 56 | |||
| 57 | template <typename Scalar> | ||
| 58 | ✗ | void ResidualModelControlTpl<Scalar>::calc( | |
| 59 | const std::shared_ptr<ResidualDataAbstract>& data, | ||
| 60 | const Eigen::Ref<const VectorXs>&) { | ||
| 61 | ✗ | data->r.setZero(); | |
| 62 | ✗ | } | |
| 63 | |||
| 64 | template <typename Scalar> | ||
| 65 | #ifndef NDEBUG | ||
| 66 | ✗ | void ResidualModelControlTpl<Scalar>::calcDiff( | |
| 67 | const std::shared_ptr<ResidualDataAbstract>& data, | ||
| 68 | const Eigen::Ref<const VectorXs>&, const Eigen::Ref<const VectorXs>&) { | ||
| 69 | #else | ||
| 70 | void ResidualModelControlTpl<Scalar>::calcDiff( | ||
| 71 | const std::shared_ptr<ResidualDataAbstract>&, | ||
| 72 | const Eigen::Ref<const VectorXs>&, const Eigen::Ref<const VectorXs>&) { | ||
| 73 | #endif | ||
| 74 | // The Jacobian has constant values which were set in createData. | ||
| 75 | ✗ | assert_pretty(MatrixXs(data->Ru).isApprox(MatrixXs::Identity(nu_, nu_)), | |
| 76 | "Ru has wrong value"); | ||
| 77 | ✗ | } | |
| 78 | |||
| 79 | template <typename Scalar> | ||
| 80 | std::shared_ptr<ResidualDataAbstractTpl<Scalar> > | ||
| 81 | ✗ | ResidualModelControlTpl<Scalar>::createData( | |
| 82 | DataCollectorAbstract* const _data) { | ||
| 83 | ✗ | std::shared_ptr<ResidualDataAbstract> data = | |
| 84 | std::allocate_shared<ResidualDataAbstract>( | ||
| 85 | ✗ | Eigen::aligned_allocator<ResidualDataAbstract>(), this, _data); | |
| 86 | ✗ | data->Ru.diagonal().fill((Scalar)1.); | |
| 87 | ✗ | return data; | |
| 88 | ✗ | } | |
| 89 | |||
| 90 | template <typename Scalar> | ||
| 91 | ✗ | void ResidualModelControlTpl<Scalar>::calcCostDiff( | |
| 92 | const std::shared_ptr<CostDataAbstract>& cdata, | ||
| 93 | const std::shared_ptr<ResidualDataAbstract>&, | ||
| 94 | const std::shared_ptr<ActivationDataAbstract>& adata, const bool) { | ||
| 95 | ✗ | cdata->Lu = adata->Ar; | |
| 96 | ✗ | cdata->Luu = adata->Arr; | |
| 97 | ✗ | } | |
| 98 | |||
| 99 | template <typename Scalar> | ||
| 100 | template <typename NewScalar> | ||
| 101 | ✗ | ResidualModelControlTpl<NewScalar> ResidualModelControlTpl<Scalar>::cast() | |
| 102 | const { | ||
| 103 | typedef ResidualModelControlTpl<NewScalar> ReturnType; | ||
| 104 | ✗ | ReturnType ret(state_->template cast<NewScalar>(), | |
| 105 | ✗ | uref_.template cast<NewScalar>()); | |
| 106 | ✗ | return ret; | |
| 107 | } | ||
| 108 | |||
| 109 | template <typename Scalar> | ||
| 110 | ✗ | void ResidualModelControlTpl<Scalar>::print(std::ostream& os) const { | |
| 111 | ✗ | os << "ResidualModelControl"; | |
| 112 | ✗ | } | |
| 113 | |||
| 114 | template <typename Scalar> | ||
| 115 | const typename MathBaseTpl<Scalar>::VectorXs& | ||
| 116 | ✗ | ResidualModelControlTpl<Scalar>::get_reference() const { | |
| 117 | ✗ | return uref_; | |
| 118 | } | ||
| 119 | |||
| 120 | template <typename Scalar> | ||
| 121 | ✗ | void ResidualModelControlTpl<Scalar>::set_reference(const VectorXs& reference) { | |
| 122 | ✗ | if (static_cast<std::size_t>(reference.size()) != nu_) { | |
| 123 | ✗ | throw_pretty("Invalid argument: " | |
| 124 | << "the control reference has wrong dimension (" | ||
| 125 | << reference.size() | ||
| 126 | << " provided - it should be " + std::to_string(nu_) + ")") | ||
| 127 | } | ||
| 128 | ✗ | uref_ = reference; | |
| 129 | ✗ | } | |
| 130 | |||
| 131 | } // namespace crocoddyl | ||
| 132 |