blob: 8aec0de2520d59d290d0e17243eb1b47caac7176 [file] [log] [blame]
Comran Morsheddaf69232016-04-20 22:25:37 -07001#include "y2016/dashboard/dashboard.h"
2
Austin Schuhf2a50ba2016-12-24 16:16:26 -08003#include <chrono>
4#include <complex>
Comran Morsheddaf69232016-04-20 22:25:37 -07005#include <iostream>
6#include <sstream>
7#include <string>
8#include <thread>
9#include <vector>
Comran Morsheddaf69232016-04-20 22:25:37 -070010
Brian Silvermanacdabeb2019-03-23 14:04:36 -070011#include "internal/Embedded.h"
Comran Morsheddaf69232016-04-20 22:25:37 -070012#include "seasocks/Server.h"
13
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "aos/events/shm_event_loop.h"
John Park398c74a2018-10-20 21:17:39 -070015#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070016#include "aos/logging/logging.h"
Austin Schuh86cd5722019-04-14 13:34:20 -070017#include "aos/mutex/mutex.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070018#include "aos/realtime.h"
Austin Schuh86cd5722019-04-14 13:34:20 -070019#include "aos/seasocks/seasocks_logger.h"
John Park33858a32018-09-28 23:05:48 -070020#include "aos/time/time.h"
21#include "aos/util/phased_loop.h"
Austin Schuhed5b26d2019-12-05 20:51:59 -080022#include "frc971/autonomous/auto_mode_generated.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070023#include "y2016/control_loops/superstructure/superstructure_status_generated.h"
24#include "y2016/queues/ball_detector_generated.h"
25#include "y2016/vision/vision_generated.h"
Comran Morsheddaf69232016-04-20 22:25:37 -070026
Austin Schuhf2a50ba2016-12-24 16:16:26 -080027namespace chrono = ::std::chrono;
28
Comran Morsheddaf69232016-04-20 22:25:37 -070029namespace y2016 {
30namespace dashboard {
31namespace big_indicator {
32constexpr int kBlack = 0;
33constexpr int kBallIntaked = 1;
34constexpr int kAiming = 2;
35constexpr int kLockedOn = 3;
36} // namespace big_indicator
37
38namespace superstructure_indicator {
39constexpr int kBlack = 0;
40constexpr int kNotZeroed = 1;
41constexpr int kEstopped = 2;
42} // namespace superstructure_indicator
43
44// Define the following if we want to use a local www directory and feed in
45// dummy data.
46//#define DASHBOARD_TESTING
47
48// Define the following if we want to read from the vision queue, which has
49// caused problems in the past when auto aiming that still need to be addressed.
50//#define DASHBOARD_READ_VISION_QUEUE
51
Austin Schuh1bf8a212019-05-26 22:13:14 -070052DataCollector::DataCollector(::aos::EventLoop *event_loop)
Austin Schuhd32b3622019-06-23 18:49:06 -070053 : event_loop_(event_loop),
54 vision_status_fetcher_(
Austin Schuh1bf8a212019-05-26 22:13:14 -070055 event_loop->MakeFetcher<::y2016::vision::VisionStatus>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070056 "/superstructure")),
Austin Schuh4b652c92019-05-27 13:22:27 -070057 ball_detector_fetcher_(
58 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070059 "/superstructure")),
Austin Schuha250b2d2019-05-27 16:14:02 -070060 autonomous_mode_fetcher_(
61 event_loop->MakeFetcher<::frc971::autonomous::AutonomousMode>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 "/aos")),
Austin Schuh9481d0d2019-06-29 21:56:17 -070063 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070064 event_loop
65 ->MakeFetcher<::y2016::control_loops::superstructure::Status>(
66 "/superstructure")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070067 cur_raw_data_("no data"),
Comran Morsheddaf69232016-04-20 22:25:37 -070068 sample_id_(0),
69 measure_index_(0),
Comran Morshed40a18002016-04-28 13:40:26 -070070 overflow_id_(1) {}
Comran Morsheddaf69232016-04-20 22:25:37 -070071
72void DataCollector::RunIteration() {
73 ::aos::MutexLocker locker(&mutex_);
74 measure_index_ = 0;
75
76// Add recorded data here. /////
77#ifdef DASHBOARD_TESTING
78 // The following feeds data into the webserver when we do not have a process
79 // feeding data to the queues.
80 // To test, we are sending three streams holding randomly generated numbers.
81 AddPoint("test", ::std::rand() % 4);
82 AddPoint("test2", ::std::rand() % 3);
83 AddPoint("test3", ::std::rand() % 3 - 1);
84 (void)big_indicator::kBlack;
85 (void)big_indicator::kBallIntaked;
86 (void)big_indicator::kAiming;
87 (void)big_indicator::kLockedOn;
88 (void)superstructure_indicator::kBlack;
89 (void)superstructure_indicator::kNotZeroed;
90 (void)superstructure_indicator::kEstopped;
91#else
92 int big_indicator = big_indicator::kBlack;
93 int superstructure_state_indicator = superstructure_indicator::kBlack;
94 // We should never have a -1 here, so this is an indicator that somethings
95 // gone wrong with reading the auto queue.
96 int auto_mode_indicator = -1;
97
Austin Schuha250b2d2019-05-27 16:14:02 -070098 autonomous_mode_fetcher_.Fetch();
Austin Schuh9481d0d2019-06-29 21:56:17 -070099 superstructure_status_fetcher_.Fetch();
Austin Schuh4b652c92019-05-27 13:22:27 -0700100 ball_detector_fetcher_.Fetch();
Austin Schuh1bf8a212019-05-26 22:13:14 -0700101 vision_status_fetcher_.Fetch();
Comran Morsheddaf69232016-04-20 22:25:37 -0700102
Comran Morshed38967332016-04-23 19:26:48 -0700103// Caused glitching with auto-aim at NASA, so be cautious with this until
104// we find a good fix.
105#ifdef DASHBOARD_READ_VISION_QUEUE
Austin Schuh1bf8a212019-05-26 22:13:14 -0700106 if (vision_status_fetcher_.get() &&
107 (vision_status_fetcher_->left_image_valid ||
108 vision_status_fetcher_->right_image_valid)) {
Comran Morshed38967332016-04-23 19:26:48 -0700109 big_indicator = big_indicator::kAiming;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700110 if (::std::abs(vision_status_fetcher_->horizontal_angle) < 0.002) {
Comran Morshed38967332016-04-23 19:26:48 -0700111 big_indicator = big_indicator::kLockedOn;
112 }
113 }
114#else
115 (void)big_indicator::kAiming;
116 (void)big_indicator::kLockedOn;
117#endif
118
119 // Ball detector comes after vision because we want to prioritize that
120 // indication.
Austin Schuh4b652c92019-05-27 13:22:27 -0700121 if (ball_detector_fetcher_.get()) {
Comran Morsheddaf69232016-04-20 22:25:37 -0700122 // TODO(comran): Grab detected voltage from joystick_reader. Except this
123 // value may not change, so it may be worth it to keep it as it is right
124 // now.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700125 if (ball_detector_fetcher_->voltage() > 2.5) {
Comran Morshed38967332016-04-23 19:26:48 -0700126 big_indicator = big_indicator::kBallIntaked;
Comran Morsheddaf69232016-04-20 22:25:37 -0700127 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700128 }
129
Austin Schuh9481d0d2019-06-29 21:56:17 -0700130 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131 if (!superstructure_status_fetcher_->zeroed()) {
Comran Morsheddaf69232016-04-20 22:25:37 -0700132 superstructure_state_indicator = superstructure_indicator::kNotZeroed;
133 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134 if (superstructure_status_fetcher_->estopped()) {
Comran Morsheddaf69232016-04-20 22:25:37 -0700135 superstructure_state_indicator = superstructure_indicator::kEstopped;
136 }
137 }
138
Austin Schuha250b2d2019-05-27 16:14:02 -0700139 if (autonomous_mode_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 auto_mode_indicator = autonomous_mode_fetcher_->mode();
Comran Morsheddaf69232016-04-20 22:25:37 -0700141 }
142
143 AddPoint("big indicator", big_indicator);
144 AddPoint("superstructure state indicator", superstructure_state_indicator);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700145 if (auto_mode_indicator != 15) {
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700146 AddPoint("auto mode indicator", auto_mode_indicator);
147 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700148#endif
149
150 // Get ready for next iteration. /////
151 sample_id_++;
152}
153
154void DataCollector::AddPoint(const ::std::string &name, double value) {
155 // Mutex should be locked when this method is called to synchronize packets.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700156 AOS_CHECK(mutex_.OwnedBySelf());
Comran Morsheddaf69232016-04-20 22:25:37 -0700157
158 size_t index = GetIndex(sample_id_);
159
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800160 ItemDatapoint datapoint{value, ::aos::monotonic_clock::now()};
Comran Morsheddaf69232016-04-20 22:25:37 -0700161 if (measure_index_ >= sample_items_.size()) {
162 // New item in our data table.
163 ::std::vector<ItemDatapoint> datapoints;
164 SampleItem item{name, datapoints};
165 sample_items_.emplace_back(item);
166 } else if (index >= sample_items_.at(measure_index_).datapoints.size()) {
167 // New data point for an already existing item.
168 sample_items_.at(measure_index_).datapoints.emplace_back(datapoint);
169 } else {
170 // Overwrite an already existing data point for an already existing item.
171 sample_items_.at(measure_index_).datapoints.at(index) = datapoint;
172 }
173
174 measure_index_++;
175}
176
177::std::string DataCollector::Fetch(int32_t from_sample) {
178 ::aos::MutexLocker locker(&mutex_);
179
180 ::std::stringstream message;
181 message.precision(10);
182
183 // Send out the names of each item when requested by the client.
184 // Example: *item_one_name,item_two_name,item_three_name
185 if (from_sample == 0) {
186 message << "*"; // Begin name packet.
187
188 // Add comma-separated list of names.
189 for (size_t cur_data_name = 0; cur_data_name < sample_items_.size();
190 cur_data_name++) {
191 if (cur_data_name > 0) {
192 message << ",";
193 }
194 message << sample_items_.at(cur_data_name).name;
195 }
196 return message.str();
197 }
198
199 // Send out one sample containing the data.
200 // Samples are split with dollar signs, info with percent signs, and
201 // measurements with commas.
202 // Example of data with two samples: $289%2803.13%10,67$290%2803.14%12,68
203
204 // Note that we are ignoring the from_sample being sent to keep up with the
205 // live data without worrying about client lag.
Comran Morshed40a18002016-04-28 13:40:26 -0700206 int32_t cur_sample = sample_id_;
Comran Morsheddaf69232016-04-20 22:25:37 -0700207 int32_t adjusted_index = GetIndex(cur_sample);
208 message << "$"; // Begin data packet.
209
210 // Make sure we are not out of range.
Comran Morshed40a18002016-04-28 13:40:26 -0700211 if (sample_items_.size() > 0) {
212 if (static_cast<size_t>(adjusted_index) <
213 sample_items_.at(0).datapoints.size()) {
214 message << cur_sample << "%"
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700215 << ::aos::time::DurationInSeconds(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800216 sample_items_.at(0)
217 .datapoints.at(adjusted_index)
218 .time.time_since_epoch())
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800219 << "%"; // Send time.
Comran Morshed40a18002016-04-28 13:40:26 -0700220 // Add comma-separated list of data points.
221 for (size_t cur_measure = 0; cur_measure < sample_items_.size();
222 cur_measure++) {
223 if (cur_measure > 0) {
224 message << ",";
225 }
226 message << sample_items_.at(cur_measure)
227 .datapoints.at(adjusted_index)
228 .value;
229 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700230 }
231 }
232
233 return message.str();
234}
235
236size_t DataCollector::GetIndex(size_t sample_id) {
237 return sample_id % overflow_id_;
238}
239
240void DataCollector::operator()() {
241 ::aos::SetCurrentThreadName("DashboardData");
242
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800243 ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(100),
Austin Schuhd32b3622019-06-23 18:49:06 -0700244 event_loop_->monotonic_now(),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800245 chrono::seconds(0));
Comran Morsheddaf69232016-04-20 22:25:37 -0700246 while (run_) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800247 phased_loop.SleepUntilNext();
Comran Morsheddaf69232016-04-20 22:25:37 -0700248 RunIteration();
249 }
250}
251
Austin Schuh1bf8a212019-05-26 22:13:14 -0700252SocketHandler::SocketHandler(::aos::EventLoop *event_loop)
253 : data_collector_(event_loop),
254 data_collector_thread_(::std::ref(data_collector_)) {}
Comran Morsheddaf69232016-04-20 22:25:37 -0700255
256void SocketHandler::onConnect(seasocks::WebSocket *connection) {
257 connections_.insert(connection);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700258 AOS_LOG(INFO, "Connected: %s : %s\n", connection->getRequestUri().c_str(),
259 seasocks::formatAddress(connection->getRemoteAddress()).c_str());
Comran Morsheddaf69232016-04-20 22:25:37 -0700260}
261
262void SocketHandler::onData(seasocks::WebSocket *connection, const char *data) {
263 int32_t from_sample = atoi(data);
264
265 ::std::string send_data = data_collector_.Fetch(from_sample);
266 connection->send(send_data.c_str());
267}
268
269void SocketHandler::onDisconnect(seasocks::WebSocket *connection) {
270 connections_.erase(connection);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700271 AOS_LOG(INFO, "Disconnected: %s : %s\n", connection->getRequestUri().c_str(),
272 seasocks::formatAddress(connection->getRemoteAddress()).c_str());
Comran Morsheddaf69232016-04-20 22:25:37 -0700273}
274
275void SocketHandler::Quit() {
276 data_collector_.Quit();
277 data_collector_thread_.join();
278}
279
Comran Morsheddaf69232016-04-20 22:25:37 -0700280} // namespace dashboard
281} // namespace y2016
282
283int main(int, char *[]) {
Brian Silvermanacdabeb2019-03-23 14:04:36 -0700284 // Make sure to reference this to force the linker to include it.
285 findEmbeddedContent("");
286
Comran Morsheddaf69232016-04-20 22:25:37 -0700287 ::aos::InitNRT();
288
Alex Perrycb7da4b2019-08-28 19:35:56 -0700289 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
290 aos::configuration::ReadConfig("config.json");
291
292 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh1bf8a212019-05-26 22:13:14 -0700293
Comran Morsheddaf69232016-04-20 22:25:37 -0700294 ::seasocks::Server server(::std::shared_ptr<seasocks::Logger>(
Austin Schuh86cd5722019-04-14 13:34:20 -0700295 new ::aos::seasocks::SeasocksLogger(::seasocks::Logger::Level::Info)));
Austin Schuh1bf8a212019-05-26 22:13:14 -0700296 ::y2016::dashboard::SocketHandler socket_handler(&event_loop);
Comran Morsheddaf69232016-04-20 22:25:37 -0700297
298 server.addWebSocketHandler(
299 "/ws",
300 ::std::shared_ptr<::y2016::dashboard::SocketHandler>(&socket_handler));
301#ifdef DASHBOARD_TESTING
302 server.serve("www", 1180);
303#else
304 // Absolute directory of www folder on the robot.
305 server.serve("/home/admin/robot_code/www", 1180);
306#endif
307
308 socket_handler.Quit();
309
310 ::aos::Cleanup();
311 return 0;
312}