Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 1 | #include "y2016/dashboard/dashboard.h" |
| 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
| 4 | #include <complex> |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 5 | #include <iostream> |
| 6 | #include <sstream> |
| 7 | #include <string> |
| 8 | #include <thread> |
| 9 | #include <vector> |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 10 | |
Brian Silverman | acdabeb | 2019-03-23 14:04:36 -0700 | [diff] [blame] | 11 | #include "internal/Embedded.h" |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 12 | #include "seasocks/Server.h" |
| 13 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 14 | #include "aos/events/shm-event-loop.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 15 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 16 | #include "aos/logging/logging.h" |
Austin Schuh | 86cd572 | 2019-04-14 13:34:20 -0700 | [diff] [blame] | 17 | #include "aos/mutex/mutex.h" |
| 18 | #include "aos/seasocks/seasocks_logger.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 19 | #include "aos/time/time.h" |
| 20 | #include "aos/util/phased_loop.h" |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 21 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 22 | #include "frc971/autonomous/auto.q.h" |
| 23 | |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 24 | #include "y2016/control_loops/superstructure/superstructure.q.h" |
| 25 | #include "y2016/queues/ball_detector.q.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 26 | #include "y2016/vision/vision.q.h" |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 27 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 28 | namespace chrono = ::std::chrono; |
| 29 | |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 30 | namespace y2016 { |
| 31 | namespace dashboard { |
| 32 | namespace big_indicator { |
| 33 | constexpr int kBlack = 0; |
| 34 | constexpr int kBallIntaked = 1; |
| 35 | constexpr int kAiming = 2; |
| 36 | constexpr int kLockedOn = 3; |
| 37 | } // namespace big_indicator |
| 38 | |
| 39 | namespace superstructure_indicator { |
| 40 | constexpr int kBlack = 0; |
| 41 | constexpr int kNotZeroed = 1; |
| 42 | constexpr int kEstopped = 2; |
| 43 | } // namespace superstructure_indicator |
| 44 | |
| 45 | // Define the following if we want to use a local www directory and feed in |
| 46 | // dummy data. |
| 47 | //#define DASHBOARD_TESTING |
| 48 | |
| 49 | // Define the following if we want to read from the vision queue, which has |
| 50 | // caused problems in the past when auto aiming that still need to be addressed. |
| 51 | //#define DASHBOARD_READ_VISION_QUEUE |
| 52 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 53 | DataCollector::DataCollector(::aos::EventLoop *event_loop) |
| 54 | : vision_status_fetcher_( |
| 55 | event_loop->MakeFetcher<::y2016::vision::VisionStatus>( |
| 56 | ".y2016.vision.vision_status")), |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 57 | ball_detector_fetcher_( |
| 58 | event_loop->MakeFetcher<::y2016::sensors::BallDetector>( |
| 59 | ".y2016.sensors.ball_detector")), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 60 | cur_raw_data_("no data"), |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 61 | sample_id_(0), |
| 62 | measure_index_(0), |
Comran Morshed | 40a1800 | 2016-04-28 13:40:26 -0700 | [diff] [blame] | 63 | overflow_id_(1) {} |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 64 | |
| 65 | void DataCollector::RunIteration() { |
| 66 | ::aos::MutexLocker locker(&mutex_); |
| 67 | measure_index_ = 0; |
| 68 | |
| 69 | // Add recorded data here. ///// |
| 70 | #ifdef DASHBOARD_TESTING |
| 71 | // The following feeds data into the webserver when we do not have a process |
| 72 | // feeding data to the queues. |
| 73 | // To test, we are sending three streams holding randomly generated numbers. |
| 74 | AddPoint("test", ::std::rand() % 4); |
| 75 | AddPoint("test2", ::std::rand() % 3); |
| 76 | AddPoint("test3", ::std::rand() % 3 - 1); |
| 77 | (void)big_indicator::kBlack; |
| 78 | (void)big_indicator::kBallIntaked; |
| 79 | (void)big_indicator::kAiming; |
| 80 | (void)big_indicator::kLockedOn; |
| 81 | (void)superstructure_indicator::kBlack; |
| 82 | (void)superstructure_indicator::kNotZeroed; |
| 83 | (void)superstructure_indicator::kEstopped; |
| 84 | #else |
| 85 | int big_indicator = big_indicator::kBlack; |
| 86 | int superstructure_state_indicator = superstructure_indicator::kBlack; |
| 87 | // We should never have a -1 here, so this is an indicator that somethings |
| 88 | // gone wrong with reading the auto queue. |
| 89 | int auto_mode_indicator = -1; |
| 90 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 91 | ::frc971::autonomous::auto_mode.FetchLatest(); |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 92 | ::y2016::control_loops::superstructure_queue.status.FetchLatest(); |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 93 | ball_detector_fetcher_.Fetch(); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 94 | vision_status_fetcher_.Fetch(); |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 95 | |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 96 | // Caused glitching with auto-aim at NASA, so be cautious with this until |
| 97 | // we find a good fix. |
| 98 | #ifdef DASHBOARD_READ_VISION_QUEUE |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 99 | if (vision_status_fetcher_.get() && |
| 100 | (vision_status_fetcher_->left_image_valid || |
| 101 | vision_status_fetcher_->right_image_valid)) { |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 102 | big_indicator = big_indicator::kAiming; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 103 | if (::std::abs(vision_status_fetcher_->horizontal_angle) < 0.002) { |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 104 | big_indicator = big_indicator::kLockedOn; |
| 105 | } |
| 106 | } |
| 107 | #else |
| 108 | (void)big_indicator::kAiming; |
| 109 | (void)big_indicator::kLockedOn; |
| 110 | #endif |
| 111 | |
| 112 | // Ball detector comes after vision because we want to prioritize that |
| 113 | // indication. |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 114 | if (ball_detector_fetcher_.get()) { |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 115 | // TODO(comran): Grab detected voltage from joystick_reader. Except this |
| 116 | // value may not change, so it may be worth it to keep it as it is right |
| 117 | // now. |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 118 | if (ball_detector_fetcher_->voltage > 2.5) { |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 119 | big_indicator = big_indicator::kBallIntaked; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 120 | } |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (::y2016::control_loops::superstructure_queue.status.get()) { |
| 124 | if (!::y2016::control_loops::superstructure_queue.status->zeroed) { |
| 125 | superstructure_state_indicator = superstructure_indicator::kNotZeroed; |
| 126 | } |
| 127 | if (::y2016::control_loops::superstructure_queue.status->estopped) { |
| 128 | superstructure_state_indicator = superstructure_indicator::kEstopped; |
| 129 | } |
| 130 | } |
| 131 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 132 | if (::frc971::autonomous::auto_mode.get()) { |
| 133 | auto_mode_indicator = ::frc971::autonomous::auto_mode->mode; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | AddPoint("big indicator", big_indicator); |
| 137 | AddPoint("superstructure state indicator", superstructure_state_indicator); |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 138 | if (auto_mode_indicator != 15) { |
Campbell Crowley | 9ed61a5 | 2016-11-05 17:13:07 -0700 | [diff] [blame] | 139 | AddPoint("auto mode indicator", auto_mode_indicator); |
| 140 | } |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 141 | #endif |
| 142 | |
| 143 | // Get ready for next iteration. ///// |
| 144 | sample_id_++; |
| 145 | } |
| 146 | |
| 147 | void DataCollector::AddPoint(const ::std::string &name, double value) { |
| 148 | // Mutex should be locked when this method is called to synchronize packets. |
| 149 | CHECK(mutex_.OwnedBySelf()); |
| 150 | |
| 151 | size_t index = GetIndex(sample_id_); |
| 152 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 153 | ItemDatapoint datapoint{value, ::aos::monotonic_clock::now()}; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 154 | if (measure_index_ >= sample_items_.size()) { |
| 155 | // New item in our data table. |
| 156 | ::std::vector<ItemDatapoint> datapoints; |
| 157 | SampleItem item{name, datapoints}; |
| 158 | sample_items_.emplace_back(item); |
| 159 | } else if (index >= sample_items_.at(measure_index_).datapoints.size()) { |
| 160 | // New data point for an already existing item. |
| 161 | sample_items_.at(measure_index_).datapoints.emplace_back(datapoint); |
| 162 | } else { |
| 163 | // Overwrite an already existing data point for an already existing item. |
| 164 | sample_items_.at(measure_index_).datapoints.at(index) = datapoint; |
| 165 | } |
| 166 | |
| 167 | measure_index_++; |
| 168 | } |
| 169 | |
| 170 | ::std::string DataCollector::Fetch(int32_t from_sample) { |
| 171 | ::aos::MutexLocker locker(&mutex_); |
| 172 | |
| 173 | ::std::stringstream message; |
| 174 | message.precision(10); |
| 175 | |
| 176 | // Send out the names of each item when requested by the client. |
| 177 | // Example: *item_one_name,item_two_name,item_three_name |
| 178 | if (from_sample == 0) { |
| 179 | message << "*"; // Begin name packet. |
| 180 | |
| 181 | // Add comma-separated list of names. |
| 182 | for (size_t cur_data_name = 0; cur_data_name < sample_items_.size(); |
| 183 | cur_data_name++) { |
| 184 | if (cur_data_name > 0) { |
| 185 | message << ","; |
| 186 | } |
| 187 | message << sample_items_.at(cur_data_name).name; |
| 188 | } |
| 189 | return message.str(); |
| 190 | } |
| 191 | |
| 192 | // Send out one sample containing the data. |
| 193 | // Samples are split with dollar signs, info with percent signs, and |
| 194 | // measurements with commas. |
| 195 | // Example of data with two samples: $289%2803.13%10,67$290%2803.14%12,68 |
| 196 | |
| 197 | // Note that we are ignoring the from_sample being sent to keep up with the |
| 198 | // live data without worrying about client lag. |
Comran Morshed | 40a1800 | 2016-04-28 13:40:26 -0700 | [diff] [blame] | 199 | int32_t cur_sample = sample_id_; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 200 | int32_t adjusted_index = GetIndex(cur_sample); |
| 201 | message << "$"; // Begin data packet. |
| 202 | |
| 203 | // Make sure we are not out of range. |
Comran Morshed | 40a1800 | 2016-04-28 13:40:26 -0700 | [diff] [blame] | 204 | if (sample_items_.size() > 0) { |
| 205 | if (static_cast<size_t>(adjusted_index) < |
| 206 | sample_items_.at(0).datapoints.size()) { |
| 207 | message << cur_sample << "%" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 208 | << ::aos::time::DurationInSeconds( |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 209 | sample_items_.at(0) |
| 210 | .datapoints.at(adjusted_index) |
| 211 | .time.time_since_epoch()) |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 212 | << "%"; // Send time. |
Comran Morshed | 40a1800 | 2016-04-28 13:40:26 -0700 | [diff] [blame] | 213 | // Add comma-separated list of data points. |
| 214 | for (size_t cur_measure = 0; cur_measure < sample_items_.size(); |
| 215 | cur_measure++) { |
| 216 | if (cur_measure > 0) { |
| 217 | message << ","; |
| 218 | } |
| 219 | message << sample_items_.at(cur_measure) |
| 220 | .datapoints.at(adjusted_index) |
| 221 | .value; |
| 222 | } |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | return message.str(); |
| 227 | } |
| 228 | |
| 229 | size_t DataCollector::GetIndex(size_t sample_id) { |
| 230 | return sample_id % overflow_id_; |
| 231 | } |
| 232 | |
| 233 | void DataCollector::operator()() { |
| 234 | ::aos::SetCurrentThreadName("DashboardData"); |
| 235 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 236 | ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(100), |
| 237 | chrono::seconds(0)); |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 238 | while (run_) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 239 | phased_loop.SleepUntilNext(); |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 240 | RunIteration(); |
| 241 | } |
| 242 | } |
| 243 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 244 | SocketHandler::SocketHandler(::aos::EventLoop *event_loop) |
| 245 | : data_collector_(event_loop), |
| 246 | data_collector_thread_(::std::ref(data_collector_)) {} |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 247 | |
| 248 | void SocketHandler::onConnect(seasocks::WebSocket *connection) { |
| 249 | connections_.insert(connection); |
| 250 | LOG(INFO, "Connected: %s : %s\n", connection->getRequestUri().c_str(), |
| 251 | seasocks::formatAddress(connection->getRemoteAddress()).c_str()); |
| 252 | } |
| 253 | |
| 254 | void SocketHandler::onData(seasocks::WebSocket *connection, const char *data) { |
| 255 | int32_t from_sample = atoi(data); |
| 256 | |
| 257 | ::std::string send_data = data_collector_.Fetch(from_sample); |
| 258 | connection->send(send_data.c_str()); |
| 259 | } |
| 260 | |
| 261 | void SocketHandler::onDisconnect(seasocks::WebSocket *connection) { |
| 262 | connections_.erase(connection); |
| 263 | LOG(INFO, "Disconnected: %s : %s\n", connection->getRequestUri().c_str(), |
| 264 | seasocks::formatAddress(connection->getRemoteAddress()).c_str()); |
| 265 | } |
| 266 | |
| 267 | void SocketHandler::Quit() { |
| 268 | data_collector_.Quit(); |
| 269 | data_collector_thread_.join(); |
| 270 | } |
| 271 | |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 272 | } // namespace dashboard |
| 273 | } // namespace y2016 |
| 274 | |
| 275 | int main(int, char *[]) { |
Brian Silverman | acdabeb | 2019-03-23 14:04:36 -0700 | [diff] [blame] | 276 | // Make sure to reference this to force the linker to include it. |
| 277 | findEmbeddedContent(""); |
| 278 | |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 279 | ::aos::InitNRT(); |
| 280 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 281 | ::aos::ShmEventLoop event_loop; |
| 282 | |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 283 | ::seasocks::Server server(::std::shared_ptr<seasocks::Logger>( |
Austin Schuh | 86cd572 | 2019-04-14 13:34:20 -0700 | [diff] [blame] | 284 | new ::aos::seasocks::SeasocksLogger(::seasocks::Logger::Level::Info))); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 285 | ::y2016::dashboard::SocketHandler socket_handler(&event_loop); |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 286 | |
| 287 | server.addWebSocketHandler( |
| 288 | "/ws", |
| 289 | ::std::shared_ptr<::y2016::dashboard::SocketHandler>(&socket_handler)); |
| 290 | #ifdef DASHBOARD_TESTING |
| 291 | server.serve("www", 1180); |
| 292 | #else |
| 293 | // Absolute directory of www folder on the robot. |
| 294 | server.serve("/home/admin/robot_code/www", 1180); |
| 295 | #endif |
| 296 | |
| 297 | socket_handler.Quit(); |
| 298 | |
| 299 | ::aos::Cleanup(); |
| 300 | return 0; |
| 301 | } |