|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// BSD 2-Clause License |
| 3 | +// |
| 4 | +// Copyright (C) 2024, INRIA |
| 5 | +// Copyright note valid unless otherwise stated in individual files. |
| 6 | +// All rights reserved. |
| 7 | +/////////////////////////////////////////////////////////////////////////////// |
| 8 | + |
| 9 | +#include <pinocchio/bindings/python/utils/pickle-map.hpp> |
| 10 | +#include <pinocchio/fwd.hpp> |
| 11 | + |
| 12 | +#include <eigenpy/deprecation-policy.hpp> |
| 13 | +#include <eigenpy/eigenpy.hpp> |
| 14 | +#include <eigenpy/std-unique-ptr.hpp> |
| 15 | +#include <eigenpy/std-vector.hpp> |
| 16 | + |
| 17 | +#include "simple-mpc/arm-dynamics.hpp" |
| 18 | +#include "simple-mpc/arm-mpc.hpp" |
| 19 | +#include "simple-mpc/python.hpp" |
| 20 | + |
| 21 | +namespace simple_mpc |
| 22 | +{ |
| 23 | + namespace python |
| 24 | + { |
| 25 | + namespace bp = boost::python; |
| 26 | + using eigenpy::StdVectorPythonVisitor; |
| 27 | + |
| 28 | + ArmMPC * createArmMPC(const bp::dict & settings, std::shared_ptr<ArmDynamicsOCP> problem) |
| 29 | + { |
| 30 | + ArmMPCSettings conf; |
| 31 | + |
| 32 | + conf.TOL = bp::extract<double>(settings["TOL"]); |
| 33 | + conf.mu_init = bp::extract<double>(settings["mu_init"]); |
| 34 | + conf.max_iters = bp::extract<std::size_t>(settings["max_iters"]); |
| 35 | + conf.num_threads = bp::extract<std::size_t>(settings["num_threads"]); |
| 36 | + conf.timestep = bp::extract<double>(settings["timestep"]); |
| 37 | + |
| 38 | + return new ArmMPC{conf, problem}; |
| 39 | + } |
| 40 | + |
| 41 | + bp::dict getSettings(ArmMPC & self) |
| 42 | + { |
| 43 | + ArmMPCSettings & conf = self.settings_; |
| 44 | + bp::dict settings; |
| 45 | + settings["TOL"] = conf.TOL; |
| 46 | + settings["mu_init"] = conf.mu_init; |
| 47 | + settings["max_iters"] = conf.max_iters; |
| 48 | + settings["num_threads"] = conf.num_threads; |
| 49 | + settings["timestep"] = conf.timestep; |
| 50 | + |
| 51 | + return settings; |
| 52 | + } |
| 53 | + |
| 54 | + void exposeArmMPC() |
| 55 | + { |
| 56 | + using StageVec = std::vector<std::shared_ptr<StageModel>>; |
| 57 | + StdVectorPythonVisitor<StageVec, true>::expose( |
| 58 | + "StdVec_StageModel", eigenpy::details::overload_base_get_item_for_std_vector<StageVec>()); |
| 59 | + |
| 60 | + bp::class_<ArmMPC, boost::noncopyable>("ArmMPC", bp::no_init) |
| 61 | + .def("__init__", bp::make_constructor(&createArmMPC, bp::default_call_policies())) |
| 62 | + .def("getSettings", &getSettings) |
| 63 | + .def("generateReachHorizon", &ArmMPC::generateReachHorizon, bp::args("self", "reach_pose")) |
| 64 | + .def("iterate", &ArmMPC::iterate, bp::args("self", "x")) |
| 65 | + .def("setReferencePose", &ArmMPC::setReferencePose, bp::args("self", "t", "pose_ref")) |
| 66 | + .def("getReferencePose", &ArmMPC::getReferencePose, bp::args("self", "t")) |
| 67 | + .def_readwrite("x_reference", &ArmMPC::x_reference_) |
| 68 | + .def_readonly("ocp_handler", &ArmMPC::ocp_handler_) |
| 69 | + .def("switchToReach", &ArmMPC::switchToReach, ("self"_a, "reach_pose")) |
| 70 | + .def("switchToRest", &ArmMPC::switchToRest, "self"_a) |
| 71 | + .def("getStateDerivative", &ArmMPC::getStateDerivative, ("self"_a, "t")) |
| 72 | + .def( |
| 73 | + "getModelHandler", &ArmMPC::getModelHandler, "self"_a, bp::return_internal_reference<>(), |
| 74 | + "Get the robot model handler.") |
| 75 | + .def( |
| 76 | + "getDataHandler", &ArmMPC::getDataHandler, "self"_a, bp::return_internal_reference<>(), |
| 77 | + "Get the robot data handler.") |
| 78 | + .def( |
| 79 | + "getTrajOptProblem", &ArmMPC::getTrajOptProblem, "self"_a, bp::return_internal_reference<>(), |
| 80 | + "Get the trajectory optimal problem.") |
| 81 | + .def( |
| 82 | + "getReachHorizon", &ArmMPC::getReachHorizon, "self"_a, bp::return_internal_reference<>(), |
| 83 | + "Get the reach horizon.") |
| 84 | + .add_property("solver", bp::make_getter(&ArmMPC::solver_, eigenpy::ReturnInternalStdUniquePtr{})) |
| 85 | + .add_property("xs", &ArmMPC::xs_) |
| 86 | + .add_property("us", &ArmMPC::us_) |
| 87 | + .add_property("Ks", &ArmMPC::Ks_); |
| 88 | + } |
| 89 | + |
| 90 | + } // namespace python |
| 91 | +} // namespace simple_mpc |
0 commit comments