-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
run_quadrotor_dynamics.cc
119 lines (98 loc) · 4.06 KB
/
run_quadrotor_dynamics.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/// @file
///
/// This demo sets up a passive Quadrotor plant in a world described by the
/// warehouse model. The robot simply passively falls to the floor within the
/// walls of the warehouse, falling from the initial_height command line
/// argument.
#include <gflags/gflags.h>
#include "drake/common/find_resource.h"
#include "drake/common/text_logging.h"
#include "drake/lcm/drake_lcm.h"
#include "drake/multibody/parsers/model_instance_id_table.h"
#include "drake/multibody/parsers/sdf_parser.h"
#include "drake/multibody/parsers/urdf_parser.h"
#include "drake/multibody/rigid_body_plant/drake_visualizer.h"
#include "drake/multibody/rigid_body_plant/rigid_body_plant.h"
#include "drake/multibody/rigid_body_tree_construction.h"
#include "drake/systems/analysis/runge_kutta2_integrator.h"
#include "drake/systems/analysis/simulator.h"
#include "drake/systems/framework/diagram.h"
#include "drake/systems/framework/diagram_builder.h"
#include "drake/systems/primitives/constant_vector_source.h"
namespace drake {
using multibody::joints::kFixed;
using multibody::joints::kRollPitchYaw;
using parsers::ModelInstanceIdTable;
using parsers::urdf::AddModelInstanceFromUrdfFileToWorld;
using parsers::sdf::AddModelInstancesFromSdfFile;
using systems::InputPortDescriptor;
namespace examples {
namespace quadrotor {
namespace {
DEFINE_double(duration, 0.5, "Total duration of simulation.");
DEFINE_double(initial_height, 0.051, "Initial height of the Quadrotor.");
template <typename T>
class Quadrotor : public systems::Diagram<T> {
public:
Quadrotor() {
this->set_name("Quadrotor");
auto tree = std::make_unique<RigidBodyTree<T>>();
ModelInstanceIdTable model_id_table = AddModelInstanceFromUrdfFileToWorld(
FindResourceOrThrow("drake/examples/quadrotor/quadrotor.urdf"),
kRollPitchYaw, tree.get());
const int quadrotor_id = model_id_table.at("quadrotor");
AddModelInstancesFromSdfFile(
FindResourceOrThrow("drake/examples/quadrotor/warehouse.sdf"),
kFixed, nullptr /* weld to frame */, tree.get());
drake::multibody::AddFlatTerrainToWorld(tree.get());
systems::DiagramBuilder<T> builder;
plant_ =
builder.template AddSystem<systems::RigidBodyPlant<T>>(std::move(tree));
plant_->set_name("plant");
// Verifies that the quadrotor has no actuators.
DRAKE_DEMAND(plant_->get_num_actuators() == 0);
DRAKE_DEMAND(plant_->get_num_actuators(quadrotor_id) == 0);
systems::DrakeVisualizer* publisher =
builder.template AddSystem<systems::DrakeVisualizer>(
plant_->get_rigid_body_tree(), &lcm_);
builder.Connect(plant_->get_output_port(0), publisher->get_input_port(0));
builder.BuildInto(this);
}
void SetDefaultState(const systems::Context<T>& context,
systems::State<T>* state) const override {
DRAKE_DEMAND(state != nullptr);
systems::Diagram<T>::SetDefaultState(context, state);
systems::State<T>& plant_state =
this->GetMutableSubsystemState(*plant_, state);
VectorX<T> x0(plant_->get_num_states());
x0.setZero();
/* x0 is the initial state where
* x0(0), x0(1), x0(2) are the quadrotor's x, y, z -states
* x0(3), x0(4), x0(5) are the quadrotor's Euler angles phi, theta, psi
*/
x0(2) = FLAGS_initial_height; // Sets arbitrary z-position.
plant_->set_state_vector(&plant_state, x0);
}
private:
systems::RigidBodyPlant<T>* plant_{};
lcm::DrakeLcm lcm_;
};
int do_main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
Quadrotor<double> model;
systems::Simulator<double> simulator(model);
// Same as the nominal step size, since we're using a fixed step integrator.
const double max_step_size = 1e-3;
simulator.reset_integrator<systems::RungeKutta2Integrator<double>>(
model, max_step_size, &simulator.get_mutable_context());
simulator.Initialize();
simulator.StepTo(FLAGS_duration);
return 0;
}
} // namespace
} // namespace quadrotor
} // namespace examples
} // namespace drake
int main(int argc, char* argv[]) {
return drake::examples::quadrotor::do_main(argc, argv);
}