blob: a2ed68cd8708e1d0543ffd5828e96385dba50bfb [file] [log] [blame]
Stephan Pleines85b295c2024-02-04 17:50:26 -08001#include "aos/analysis/in_process_plotter.h"
James Kuszmaul48671362020-12-24 13:54:16 -08002
3#include "aos/configuration.h"
4
Stephan Pleinesf63bde82024-01-13 15:59:33 -08005namespace frc971::analysis {
James Kuszmaul48671362020-12-24 13:54:16 -08006
7namespace {
Austin Schuhaa3b0862022-07-15 14:38:41 -07008const char *kDataPath = "frc971/analysis/cpp_plot";
James Kuszmaul48671362020-12-24 13:54:16 -08009const char *kConfigPath = "frc971/analysis/plotter.json";
10} // namespace
11
12Plotter::Plotter()
13 : config_(aos::configuration::ReadConfig(kConfigPath)),
14 event_loop_factory_(&config_.message()),
15 event_loop_(event_loop_factory_.MakeEventLoop("plotter")),
16 plot_sender_(event_loop_->MakeSender<Plot>("/analysis")),
James Kuszmaulb67409b2022-06-20 16:25:03 -070017 web_proxy_(event_loop_.get(), event_loop_factory_.scheduler_epoll(),
18 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();
Austin Schuhea62f602022-07-18 16:53:04 -070022
23 color_wheel_.emplace_back(ColorWheelColor{.name = "red", .color = {1, 0, 0}});
24 color_wheel_.emplace_back(
25 ColorWheelColor{.name = "green", .color = {0, 1, 0}});
26 color_wheel_.emplace_back(
27 ColorWheelColor{.name = "purple", .color = {0.54, 0.3, 0.75}});
28 color_wheel_.emplace_back(
29 ColorWheelColor{.name = "blue", .color = {0, 0, 1}});
30 color_wheel_.emplace_back(
31 ColorWheelColor{.name = "yellow", .color = {1, 1, 0}});
32 color_wheel_.emplace_back(
33 ColorWheelColor{.name = "teal", .color = {0, 1, 1}});
34 color_wheel_.emplace_back(
35 ColorWheelColor{.name = "pink", .color = {1, 0, 1}});
36 color_wheel_.emplace_back(
37 ColorWheelColor{.name = "orange", .color = {1, 0.6, 0}});
38 color_wheel_.emplace_back(
39 ColorWheelColor{.name = "brown", .color = {0.6, 0.3, 0}});
40 color_wheel_.emplace_back(
41 ColorWheelColor{.name = "white", .color = {1, 1, 1}});
James Kuszmaul48671362020-12-24 13:54:16 -080042}
43
James Kuszmaulb67409b2022-06-20 16:25:03 -070044void Plotter::Spin() {
45 // Set non-infinite replay rate to avoid pegging a full CPU.
46 event_loop_factory_.SetRealtimeReplayRate(1.0);
47 event_loop_factory_.Run();
48}
James Kuszmaul48671362020-12-24 13:54:16 -080049
50void Plotter::Title(std::string_view title) {
51 title_ = builder_.fbb()->CreateString(title);
52}
53
54void Plotter::AddFigure(std::string_view title, double width, double height) {
55 MaybeFinishFigure();
56
57 if (!title.empty()) {
58 figure_title_ = builder_.fbb()->CreateString(title);
59 }
60
61 // For positioning, just stack figures vertically.
62 auto position_builder = builder_.MakeBuilder<Position>();
63 position_builder.add_top(next_top_);
64 position_builder.add_left(0);
65 position_builder.add_width(width);
66 position_builder.add_height(height);
67 position_ = position_builder.Finish();
68
69 next_top_ += height;
70}
71
72void Plotter::XLabel(std::string_view label) {
73 xlabel_ = builder_.fbb()->CreateString(label);
74}
75
76void Plotter::YLabel(std::string_view label) {
77 ylabel_ = builder_.fbb()->CreateString(label);
78}
79
80void Plotter::AddLine(const std::vector<double> &x,
Austin Schuhea62f602022-07-18 16:53:04 -070081 const std::vector<double> &y, LineOptions options) {
Austin Schuh0318e5e2023-02-20 17:39:34 -080082 CHECK_EQ(x.size(), y.size()) << ": " << options.label;
James Kuszmaul48671362020-12-24 13:54:16 -080083 CHECK(!position_.IsNull())
84 << "You must call AddFigure() before calling AddLine().";
85
86 flatbuffers::Offset<flatbuffers::String> label_offset;
Austin Schuhea62f602022-07-18 16:53:04 -070087 if (!options.label.empty()) {
88 label_offset = builder_.fbb()->CreateString(options.label);
James Kuszmaul48671362020-12-24 13:54:16 -080089 }
90
91 std::vector<Point> points;
92 for (size_t ii = 0; ii < x.size(); ++ii) {
93 points.emplace_back(x[ii], y[ii]);
94 }
milind1f1dca32021-07-03 13:50:07 -070095 const flatbuffers::Offset<flatbuffers::Vector<const Point *>> points_offset =
96 builder_.fbb()->CreateVectorOfStructs(points);
James Kuszmaul48671362020-12-24 13:54:16 -080097
Austin Schuhea62f602022-07-18 16:53:04 -070098 const Color *color;
99 if (options.color.empty()) {
100 color = &color_wheel_.at(color_wheel_position_).color;
101 color_wheel_position_ = (color_wheel_position_ + 1) % color_wheel_.size();
102 } else {
103 auto it = std::find_if(
104 color_wheel_.begin(), color_wheel_.end(),
105 [options_color = options.color](const ColorWheelColor &color) {
106 return color.name == options_color;
107 });
108 CHECK(it != color_wheel_.end()) << ": Failed to find " << options.color;
109 color = &(it->color);
110 }
James Kuszmaul48671362020-12-24 13:54:16 -0800111
James Kuszmaul19217a42022-06-17 10:54:29 -0700112 LineStyle::Builder style_builder = builder_.MakeBuilder<LineStyle>();
Austin Schuhea62f602022-07-18 16:53:04 -0700113 if (options.line_style.find('*') != options.line_style.npos) {
Austin Schuh69d0b732022-07-20 21:19:32 -0700114 style_builder.add_point_size(options.point_size);
James Kuszmaul19217a42022-06-17 10:54:29 -0700115 } else {
116 style_builder.add_point_size(0.0);
117 }
Austin Schuhea62f602022-07-18 16:53:04 -0700118 style_builder.add_draw_line(options.line_style.find('-') !=
119 options.line_style.npos);
James Kuszmaul19217a42022-06-17 10:54:29 -0700120 const flatbuffers::Offset<LineStyle> style_offset = style_builder.Finish();
121
James Kuszmaul48671362020-12-24 13:54:16 -0800122 auto line_builder = builder_.MakeBuilder<Line>();
123 line_builder.add_label(label_offset);
124 line_builder.add_points(points_offset);
125 line_builder.add_color(color);
James Kuszmaul19217a42022-06-17 10:54:29 -0700126 line_builder.add_style(style_offset);
James Kuszmaul48671362020-12-24 13:54:16 -0800127 lines_.push_back(line_builder.Finish());
128}
129
130void Plotter::MaybeFinishFigure() {
131 if (!lines_.empty()) {
132 const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Line>>>
133 lines_offset = builder_.fbb()->CreateVector(lines_);
134 auto figure_builder = builder_.MakeBuilder<Figure>();
135 figure_builder.add_title(figure_title_);
136 figure_builder.add_position(position_);
137 figure_builder.add_lines(lines_offset);
138 figure_builder.add_xlabel(xlabel_);
139 figure_builder.add_share_x_axis(share_x_axis_);
140 figure_builder.add_ylabel(ylabel_);
141 figures_.push_back(figure_builder.Finish());
142 }
143 lines_.clear();
144 figure_title_.o = 0;
145 xlabel_.o = 0;
146 ylabel_.o = 0;
147 position_.o = 0;
148 share_x_axis_ = false;
149 color_wheel_position_ = 0;
150}
151
152void Plotter::Publish() {
153 MaybeFinishFigure();
154 const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Figure>>>
155 figures_offset = builder_.fbb()->CreateVector(figures_);
156
157 auto plot_builder = builder_.MakeBuilder<Plot>();
158 plot_builder.add_title(title_);
159 plot_builder.add_figures(figures_offset);
160
Austin Schuhea62f602022-07-18 16:53:04 -0700161 CHECK_EQ(builder_.Send(plot_builder.Finish()), aos::RawSender::Error::kOk);
James Kuszmaul48671362020-12-24 13:54:16 -0800162
163 builder_ = plot_sender_.MakeBuilder();
164
165 title_.o = 0;
166 figures_.clear();
167 next_top_ = 0;
168}
169
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800170} // namespace frc971::analysis