MIMPC++
A fast typesafe Mixed Integer MPC C++ library
Simulation.hpp
1 //
2 // Created by fstark on 06.08.23.
3 //
4 
5 #pragma once
6 
7 #include <cnpy.h>
8 
9 #include <type_traits>
10 #include <concepts>
11 #include "System.hpp"
12 #include "MPC.hpp"
13 
14 #include "drake/systems/framework/diagram_builder.h"
15 #include "drake/common/drake_assert.h"
16 #include "drake/geometry/scene_graph.h"
17 #include "drake/multibody/parsing/parser.h"
18 #include "drake/multibody/plant/multibody_plant.h"
19 #include "drake/multibody/plant/externally_applied_spatial_force.h"
20 #include "drake/multibody/tree/prismatic_joint.h"
21 #include "drake/multibody/tree/revolute_joint.h"
22 #include "drake/multibody/plant/propeller.h"
23 #include "drake/multibody/plant/externally_applied_spatial_force_multiplexer.h"
24 #include "drake/systems/analysis/simulator.h"
25 #include "drake/systems/controllers/pid_controller.h"
26 #include "drake/systems/primitives/constant_vector_source.h"
27 #include "drake/systems/primitives/demultiplexer.h"
28 #include "drake/systems/primitives/matrix_gain.h"
29 #include "drake/systems/framework/diagram_builder.h"
30 #include "drake/visualization/visualization_config_functions.h"
31 #include "drake/systems/primitives/discrete_time_delay.h"
32 #include "drake/systems/primitives/vector_log_sink.h"
33 #include "MPCLeafSystem.hpp"
34 #include "VizForces.hpp"
35 
36 namespace mimpc::simulation {
37  template<class MPCType>
38  class Simulation {
39  using StateVec = MPCType::SYSTEM_TYPE::StateVec;
40  private:
41  MPCType::SYSTEM_TYPE::StateVec init_state_;
42  MPCType::SYSTEM_TYPE::StateVec target_state_;
43  MPCType::SYSTEM_TYPE::StateVec target_threshold_;
44 
45  MPCType::SYSTEM_TYPE::StateVec state_constraints_lb_;
46  MPCType::SYSTEM_TYPE::StateVec state_constraints_ub_;
47 
48  MPCType &mpc_;
49 
50  // drake
51  drake::systems::DiagramBuilder<double> *builder_;
52  drake::multibody::MultibodyPlant<double> *plant_;
53  drake::geometry::SceneGraph<double> *scene_graph_;
54 
55  drake::multibody::ExternallyAppliedSpatialForceMultiplexer<double> *forces_mux_;
56  drake::systems::Demultiplexer<double> *input_demux_;
57  drake::systems::DiscreteTimeDelay<double> *controller_delay_;
58 
59  drake::systems::MatrixGain<double> *state_output_matrix_;
60  MPCLeafSystem<MPCType> *mpc_leaf_system_;
61 
62  std::unique_ptr<drake::systems::Diagram<double>> diagram_;
63 
64 
65  // viz
66  std::shared_ptr<drake::geometry::Meshcat> meshcat_;
67  VizForces *forces_viz_;
68 
69  // logger
70  drake::systems::VectorLogSink<double> *state_logger_;
71  drake::systems::VectorLogSink<double> *input_logger_;
72 
73  drake::systems::Simulator<double> *simulator_;
74 
75  // TODO: check the coordinates here!
76  Eigen::Matrix<double, 7, 8> matrix_;
77 
78 
79  public:
80  Simulation(double delay_to_simulate, const std::string &plant_urdf_file, MPCType &mpc,
81  const StateVec &initState, const StateVec &targetState,
82  const StateVec &targetThreshold, const StateVec &stateConstraintsLb,
83  const StateVec &stateConstraintsUb, bool withViz, double binary_force);
84 
85  bool simulateToTarget(float time_out_seconds);
86 
87  int simulate(float for_seconds);
88 
89  void saveData(const std::string &filename);
90  };
91 };
92 
93 #include "Simulation.tpp"
Abstract class to represent system dynamics.
Definition: MPCLeafSystem.hpp:9
Definition: Simulation.hpp:38
Definition: VizForces.hpp:21