James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame^] | 1 | #ifndef FRC971_ANALYSIS_IN_PROCESS_PLOTTER_H_ |
| 2 | #define FRC971_ANALYSIS_IN_PROCESS_PLOTTER_H_ |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | #include "aos/events/simulated_event_loop.h" |
| 7 | #include "aos/network/web_proxy.h" |
| 8 | #include "frc971/analysis/plot_data_generated.h" |
| 9 | |
| 10 | namespace frc971 { |
| 11 | namespace analysis { |
| 12 | |
| 13 | // This class wraps the WebProxy class to provide a convenient C++ interface to |
| 14 | // dynamically generate plots. |
| 15 | // Currently, the main useful interface that this provides is a matplotlib-like |
| 16 | // interface--see in_process_plotter_demo.cc for sample usage. It doesn't |
| 17 | // precisely follow matplotlib's conventions, but the basic style does mimic |
| 18 | // matplotlib. Future iterations may ditch this in favor of a more modern |
| 19 | // interface where we actually return handles for plots and lines and the such. |
| 20 | // |
| 21 | // Note that currently the port for the seb server is hard-coded to 8080, so |
| 22 | // only one instance of the Plotter can be present at once. |
| 23 | // |
| 24 | // You must call Spin() for the web server to actually do anything helpful. |
| 25 | class Plotter { |
| 26 | public: |
| 27 | Plotter(); |
| 28 | |
| 29 | // matplotlib-like interface |
| 30 | // The basic pattern is: |
| 31 | // 1) Call Figure() |
| 32 | // 2) Setup the lines, labels, etc. for the figure. |
| 33 | // 3) Repeat 1-2 however many times. |
| 34 | // 4) Call Publish(). |
| 35 | // 5) Repeat 1-5 however many times. |
| 36 | // |
| 37 | // Publish() actually pushes the figures that you setup to the web-page, |
| 38 | // either with an autogenerated title or the title set by Title(). All state |
| 39 | // is cleared (or should be cleared) by the call to Publish(). |
| 40 | |
| 41 | // Sets the title for the current set of plots; if you |
| 42 | void Title(std::string_view title); |
| 43 | void AddFigure(std::string_view title = "", double width = 900, |
| 44 | double height = 400); |
| 45 | void AddLine(const std::vector<double> &x, const std::vector<double> &y, |
| 46 | std::string_view label = ""); |
| 47 | void ShareXAxis(bool share) { share_x_axis_ = share; } |
| 48 | void XLabel(std::string_view label); |
| 49 | void YLabel(std::string_view label); |
| 50 | void Publish(); |
| 51 | |
| 52 | void Spin(); |
| 53 | private: |
| 54 | void MaybeFinishFigure(); |
| 55 | |
| 56 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 57 | aos::SimulatedEventLoopFactory event_loop_factory_; |
| 58 | std::unique_ptr<aos::EventLoop> event_loop_; |
| 59 | aos::Sender<Plot> plot_sender_; |
| 60 | aos::web_proxy::WebProxy web_proxy_; |
| 61 | |
| 62 | aos::Sender<Plot>::Builder builder_; |
| 63 | flatbuffers::Offset<flatbuffers::String> title_; |
| 64 | flatbuffers::Offset<flatbuffers::String> figure_title_; |
| 65 | flatbuffers::Offset<flatbuffers::String> xlabel_; |
| 66 | flatbuffers::Offset<flatbuffers::String> ylabel_; |
| 67 | bool share_x_axis_ = false; |
| 68 | float next_top_ = 0; |
| 69 | flatbuffers::Offset<Position> position_; |
| 70 | std::vector<flatbuffers::Offset<Figure>> figures_; |
| 71 | std::vector<flatbuffers::Offset<Line>> lines_; |
| 72 | |
| 73 | size_t color_wheel_position_ = 0; |
| 74 | std::vector<Color> color_wheel_; |
| 75 | }; |
| 76 | |
| 77 | } // namespace analysis |
| 78 | } // namespace frc971 |
| 79 | #endif // FRC971_ANALYSIS_IN_PROCESS_PLOTTER_H_ |