Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 1 | #ifndef Y2016_DASHBOARD_DASHBOARD_H_ |
| 2 | #define Y2016_DASHBOARD_DASHBOARD_H_ |
| 3 | |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 4 | #include <atomic> |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 5 | #include <iostream> |
| 6 | #include <memory> |
| 7 | #include <sstream> |
| 8 | #include <string> |
| 9 | #include <thread> |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "aos/events/event_loop.h" |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 13 | #include "aos/stl_mutex/stl_mutex.h" |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 14 | #include "aos/time/time.h" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 15 | #include "frc971/autonomous/auto_mode_generated.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 16 | #include "seasocks/PageHandler.h" |
| 17 | #include "seasocks/PrintfLogger.h" |
| 18 | #include "seasocks/StringUtil.h" |
| 19 | #include "seasocks/WebSocket.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | #include "y2016/control_loops/superstructure/superstructure_status_generated.h" |
| 21 | #include "y2016/queues/ball_detector_generated.h" |
| 22 | #include "y2016/vision/vision_generated.h" |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 23 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 24 | namespace y2016::dashboard { |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 25 | |
| 26 | // Dashboard is a webserver that opens a socket and stream data from the robot |
| 27 | // to the client. It is divided between the DataCollector, which polls |
| 28 | // RunIteration to determine what to send to the client, and an instance of a |
| 29 | // Seasocks server, which initiates a webserver on a port and opens a socket |
| 30 | // for streaming data. |
| 31 | |
| 32 | // It is an adaption of http_status, which was a 2015 project |
| 33 | // that plotted live position data from the robot queues on a webpage for |
| 34 | // debugging. |
| 35 | |
| 36 | class DataCollector { |
| 37 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 38 | DataCollector(::aos::EventLoop *event_loop); |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 39 | void RunIteration(); |
| 40 | |
| 41 | // Store a datapoint. In this case, we are reading data points to determine |
| 42 | // what color to display on the webpage indicators. Traditionally, this would |
| 43 | // be used to plot live data on a graph on the page. |
| 44 | void AddPoint(const ::std::string &name, double value); |
| 45 | |
| 46 | // Method called by the websocket to get a JSON-packaged string containing, |
| 47 | // at most, a constant number of samples, starting at from_sample. |
| 48 | ::std::string Fetch(int32_t from_sample); |
| 49 | |
| 50 | void operator()(); |
| 51 | void Quit() { run_ = false; } |
| 52 | |
| 53 | private: |
| 54 | // Returns a wrapped index based on the overflow size. |
| 55 | size_t GetIndex(size_t sample_id); |
| 56 | |
| 57 | struct ItemDatapoint { |
| 58 | double value; |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 59 | ::aos::monotonic_clock::time_point time; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct SampleItem { |
| 63 | ::std::string name; |
| 64 | ::std::vector<ItemDatapoint> datapoints; |
| 65 | }; |
| 66 | |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 67 | ::aos::EventLoop *event_loop_; |
| 68 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 69 | ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_; |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 70 | ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 71 | ::aos::Fetcher<::frc971::autonomous::AutonomousMode> autonomous_mode_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 72 | ::aos::Fetcher<::y2016::control_loops::superstructure::Status> |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 73 | superstructure_status_fetcher_; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 74 | |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 75 | // Storage vector that is written and overwritten with data in a FIFO fashion. |
| 76 | ::std::vector<SampleItem> sample_items_; |
| 77 | |
| 78 | ::std::string cur_raw_data_; |
| 79 | int32_t sample_id_; // Last sample id used. |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 80 | size_t measure_index_; // Last measure index used. |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 81 | const int32_t overflow_id_; // Vector wrapping size. |
| 82 | |
| 83 | ::std::atomic<bool> run_{true}; |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 84 | aos::stl_mutex mutex_; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | class SocketHandler : public seasocks::WebSocket::Handler { |
| 88 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 89 | SocketHandler(::aos::EventLoop *event_loop); |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 90 | void onConnect(seasocks::WebSocket *connection) override; |
| 91 | void onData(seasocks::WebSocket *connection, const char *data) override; |
| 92 | void onDisconnect(seasocks::WebSocket *connection) override; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 93 | void Quit(); |
| 94 | |
| 95 | private: |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 96 | ::std::set<seasocks::WebSocket *> connections_; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 97 | DataCollector data_collector_; |
| 98 | ::std::thread data_collector_thread_; |
| 99 | }; |
| 100 | |
| 101 | class SeasocksLogger : public seasocks::PrintfLogger { |
| 102 | public: |
| 103 | SeasocksLogger(Level min_level_to_log); |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 104 | void log(Level level, const char *message) override; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 107 | } // namespace y2016::dashboard |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 108 | |
| 109 | #endif // Y2016_DASHBOARD_DASHBOARD_H_ |