blob: cd52b1c2dbc7969325311ae93e943e1ea610a568 [file] [log] [blame]
James Kuszmaul48671362020-12-24 13:54:16 -08001#include "frc971/analysis/in_process_plotter.h"
2
3#include "aos/configuration.h"
4
5namespace frc971 {
6namespace analysis {
7
8namespace {
9const char *kDataPath = "frc971/analysis";
10const char *kConfigPath = "frc971/analysis/plotter.json";
11} // namespace
12
13Plotter::Plotter()
14 : config_(aos::configuration::ReadConfig(kConfigPath)),
15 event_loop_factory_(&config_.message()),
16 event_loop_(event_loop_factory_.MakeEventLoop("plotter")),
17 plot_sender_(event_loop_->MakeSender<Plot>("/analysis")),
James Kuszmaul1a29c082022-02-03 14:02:47 -080018 web_proxy_(event_loop_.get(), aos::web_proxy::StoreHistory::kYes, -1),
James Kuszmaul48671362020-12-24 13:54:16 -080019 builder_(plot_sender_.MakeBuilder()) {
20 web_proxy_.SetDataPath(kDataPath);
21 event_loop_->SkipTimingReport();
22 color_wheel_.push_back(Color(1, 0, 0));
23 color_wheel_.push_back(Color(0, 1, 0));
24 color_wheel_.push_back(Color(0, 0, 1));
25 color_wheel_.push_back(Color(1, 1, 0));
26 color_wheel_.push_back(Color(0, 1, 1));
27 color_wheel_.push_back(Color(1, 0, 1));
Austin Schuhc2e9c502021-11-25 21:23:24 -080028 color_wheel_.push_back(Color(1, 0.6, 0));
29 color_wheel_.push_back(Color(0.6, 0.3, 0));
30 color_wheel_.push_back(Color(1, 1, 1));
James Kuszmaul48671362020-12-24 13:54:16 -080031}
32
33void Plotter::Spin() { event_loop_factory_.Run(); }
34
35void Plotter::Title(std::string_view title) {
36 title_ = builder_.fbb()->CreateString(title);
37}
38
39void Plotter::AddFigure(std::string_view title, double width, double height) {
40 MaybeFinishFigure();
41
42 if (!title.empty()) {
43 figure_title_ = builder_.fbb()->CreateString(title);
44 }
45
46 // For positioning, just stack figures vertically.
47 auto position_builder = builder_.MakeBuilder<Position>();
48 position_builder.add_top(next_top_);
49 position_builder.add_left(0);
50 position_builder.add_width(width);
51 position_builder.add_height(height);
52 position_ = position_builder.Finish();
53
54 next_top_ += height;
55}
56
57void Plotter::XLabel(std::string_view label) {
58 xlabel_ = builder_.fbb()->CreateString(label);
59}
60
61void Plotter::YLabel(std::string_view label) {
62 ylabel_ = builder_.fbb()->CreateString(label);
63}
64
65void Plotter::AddLine(const std::vector<double> &x,
66 const std::vector<double> &y, std::string_view label) {
67 CHECK_EQ(x.size(), y.size());
68 CHECK(!position_.IsNull())
69 << "You must call AddFigure() before calling AddLine().";
70
71 flatbuffers::Offset<flatbuffers::String> label_offset;
72 if (!label.empty()) {
73 label_offset = builder_.fbb()->CreateString(label);
74 }
75
76 std::vector<Point> points;
77 for (size_t ii = 0; ii < x.size(); ++ii) {
78 points.emplace_back(x[ii], y[ii]);
79 }
milind1f1dca32021-07-03 13:50:07 -070080 const flatbuffers::Offset<flatbuffers::Vector<const Point *>> points_offset =
81 builder_.fbb()->CreateVectorOfStructs(points);
James Kuszmaul48671362020-12-24 13:54:16 -080082
83 const Color *color = &color_wheel_.at(color_wheel_position_);
84 color_wheel_position_ = (color_wheel_position_ + 1) % color_wheel_.size();
85
86 auto line_builder = builder_.MakeBuilder<Line>();
87 line_builder.add_label(label_offset);
88 line_builder.add_points(points_offset);
89 line_builder.add_color(color);
90 lines_.push_back(line_builder.Finish());
91}
92
93void Plotter::MaybeFinishFigure() {
94 if (!lines_.empty()) {
95 const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Line>>>
96 lines_offset = builder_.fbb()->CreateVector(lines_);
97 auto figure_builder = builder_.MakeBuilder<Figure>();
98 figure_builder.add_title(figure_title_);
99 figure_builder.add_position(position_);
100 figure_builder.add_lines(lines_offset);
101 figure_builder.add_xlabel(xlabel_);
102 figure_builder.add_share_x_axis(share_x_axis_);
103 figure_builder.add_ylabel(ylabel_);
104 figures_.push_back(figure_builder.Finish());
105 }
106 lines_.clear();
107 figure_title_.o = 0;
108 xlabel_.o = 0;
109 ylabel_.o = 0;
110 position_.o = 0;
111 share_x_axis_ = false;
112 color_wheel_position_ = 0;
113}
114
115void Plotter::Publish() {
116 MaybeFinishFigure();
117 const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Figure>>>
118 figures_offset = builder_.fbb()->CreateVector(figures_);
119
120 auto plot_builder = builder_.MakeBuilder<Plot>();
121 plot_builder.add_title(title_);
122 plot_builder.add_figures(figures_offset);
123
milind1f1dca32021-07-03 13:50:07 -0700124 CHECK_EQ(builder_.Send(plot_builder.Finish()),
125 aos::RawSender::Error::kOk);
James Kuszmaul48671362020-12-24 13:54:16 -0800126
127 builder_ = plot_sender_.MakeBuilder();
128
129 title_.o = 0;
130 figures_.clear();
131 next_top_ = 0;
132}
133
134} // namespace analysis
135} // namespace frc971