-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
run_fibonacci.cc
54 lines (43 loc) · 1.82 KB
/
run_fibonacci.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
#include <iostream>
#include <gflags/gflags.h>
#include "drake/examples/fibonacci/fibonacci_difference_equation.h"
#include "drake/systems/analysis/simulator.h"
#include "drake/systems/framework/diagram_builder.h"
#include "drake/systems/primitives/vector_log_sink.h"
DEFINE_int32(steps, 10, "Length of Fibonacci sequence to generate.");
namespace drake {
namespace examples {
namespace fibonacci {
namespace {
// Use Drake's hybrid Simulator to produce the Fibonacci sequence up to
// the step number supplied on the command line (default 10).
int main(int argc, char* argv[]) {
// Handle the command line "steps" argument.
gflags::SetUsageMessage("usage: run_fibonacci [--steps=n]");
gflags::ParseCommandLineFlags(&argc, &argv, true);
// Build a Diagram containing the Fibonacci system and a data logger that
// samples the Fibonacci output port exactly at the update times.
systems::DiagramBuilder<double> builder;
auto fibonacci = builder.AddSystem<FibonacciDifferenceEquation>();
auto logger = LogVectorOutput(fibonacci->GetOutputPort("Fn"), &builder,
FibonacciDifferenceEquation::kPeriod);
auto diagram = builder.Build();
// Create a Simulator and use it to advance time until t=steps*h.
systems::Simulator<double> simulator(*diagram);
simulator.AdvanceTo(FLAGS_steps * FibonacciDifferenceEquation::kPeriod);
// Print out the contents of the log.
const auto& log = logger->FindLog(simulator.get_context());
for (int n = 0; n < log.sample_times().size(); ++n) {
const double t = log.sample_times()[n];
std::cout << n << ": " << log.data()(0, n)
<< " (t=" << t << ")\n";
}
return 0;
}
} // namespace
} // namespace fibonacci
} // namespace examples
} // namespace drake
int main(int argc, char **argv) {
return drake::examples::fibonacci::main(argc, argv);
}