blob: 98fcd7bd110d258eac6fab3fd2e12554669fafc5 [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
Austin Schuh1bf8a212019-05-26 22:13:14 -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"
18#include "aos/seasocks/seasocks_logger.h"
John Park33858a32018-09-28 23:05:48 -070019#include "aos/time/time.h"
20#include "aos/util/phased_loop.h"
Philipp Schrader4bd29b12017-02-22 04:42:27 +000021#include "frc971/autonomous/auto.q.h"
Comran Morsheddaf69232016-04-20 22:25:37 -070022#include "y2016/control_loops/superstructure/superstructure.q.h"
23#include "y2016/queues/ball_detector.q.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -070024#include "y2016/vision/vision.q.h"
Comran Morsheddaf69232016-04-20 22:25:37 -070025
Austin Schuhf2a50ba2016-12-24 16:16:26 -080026namespace chrono = ::std::chrono;
27
Comran Morsheddaf69232016-04-20 22:25:37 -070028namespace y2016 {
29namespace dashboard {
30namespace big_indicator {
31constexpr int kBlack = 0;
32constexpr int kBallIntaked = 1;
33constexpr int kAiming = 2;
34constexpr int kLockedOn = 3;
35} // namespace big_indicator
36
37namespace superstructure_indicator {
38constexpr int kBlack = 0;
39constexpr int kNotZeroed = 1;
40constexpr int kEstopped = 2;
41} // namespace superstructure_indicator
42
43// Define the following if we want to use a local www directory and feed in
44// dummy data.
45//#define DASHBOARD_TESTING
46
47// Define the following if we want to read from the vision queue, which has
48// caused problems in the past when auto aiming that still need to be addressed.
49//#define DASHBOARD_READ_VISION_QUEUE
50
Austin Schuh1bf8a212019-05-26 22:13:14 -070051DataCollector::DataCollector(::aos::EventLoop *event_loop)
52 : vision_status_fetcher_(
53 event_loop->MakeFetcher<::y2016::vision::VisionStatus>(
54 ".y2016.vision.vision_status")),
Austin Schuh4b652c92019-05-27 13:22:27 -070055 ball_detector_fetcher_(
56 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
57 ".y2016.sensors.ball_detector")),
Austin Schuha250b2d2019-05-27 16:14:02 -070058 autonomous_mode_fetcher_(
59 event_loop->MakeFetcher<::frc971::autonomous::AutonomousMode>(
60 ".frc971.autonomous.auto_mode")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070061 cur_raw_data_("no data"),
Comran Morsheddaf69232016-04-20 22:25:37 -070062 sample_id_(0),
63 measure_index_(0),
Comran Morshed40a18002016-04-28 13:40:26 -070064 overflow_id_(1) {}
Comran Morsheddaf69232016-04-20 22:25:37 -070065
66void DataCollector::RunIteration() {
67 ::aos::MutexLocker locker(&mutex_);
68 measure_index_ = 0;
69
70// Add recorded data here. /////
71#ifdef DASHBOARD_TESTING
72 // The following feeds data into the webserver when we do not have a process
73 // feeding data to the queues.
74 // To test, we are sending three streams holding randomly generated numbers.
75 AddPoint("test", ::std::rand() % 4);
76 AddPoint("test2", ::std::rand() % 3);
77 AddPoint("test3", ::std::rand() % 3 - 1);
78 (void)big_indicator::kBlack;
79 (void)big_indicator::kBallIntaked;
80 (void)big_indicator::kAiming;
81 (void)big_indicator::kLockedOn;
82 (void)superstructure_indicator::kBlack;
83 (void)superstructure_indicator::kNotZeroed;
84 (void)superstructure_indicator::kEstopped;
85#else
86 int big_indicator = big_indicator::kBlack;
87 int superstructure_state_indicator = superstructure_indicator::kBlack;
88 // We should never have a -1 here, so this is an indicator that somethings
89 // gone wrong with reading the auto queue.
90 int auto_mode_indicator = -1;
91
Austin Schuha250b2d2019-05-27 16:14:02 -070092 autonomous_mode_fetcher_.Fetch();
Comran Morsheddaf69232016-04-20 22:25:37 -070093 ::y2016::control_loops::superstructure_queue.status.FetchLatest();
Austin Schuh4b652c92019-05-27 13:22:27 -070094 ball_detector_fetcher_.Fetch();
Austin Schuh1bf8a212019-05-26 22:13:14 -070095 vision_status_fetcher_.Fetch();
Comran Morsheddaf69232016-04-20 22:25:37 -070096
Comran Morshed38967332016-04-23 19:26:48 -070097// Caused glitching with auto-aim at NASA, so be cautious with this until
98// we find a good fix.
99#ifdef DASHBOARD_READ_VISION_QUEUE
Austin Schuh1bf8a212019-05-26 22:13:14 -0700100 if (vision_status_fetcher_.get() &&
101 (vision_status_fetcher_->left_image_valid ||
102 vision_status_fetcher_->right_image_valid)) {
Comran Morshed38967332016-04-23 19:26:48 -0700103 big_indicator = big_indicator::kAiming;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700104 if (::std::abs(vision_status_fetcher_->horizontal_angle) < 0.002) {
Comran Morshed38967332016-04-23 19:26:48 -0700105 big_indicator = big_indicator::kLockedOn;
106 }
107 }
108#else
109 (void)big_indicator::kAiming;
110 (void)big_indicator::kLockedOn;
111#endif
112
113 // Ball detector comes after vision because we want to prioritize that
114 // indication.
Austin Schuh4b652c92019-05-27 13:22:27 -0700115 if (ball_detector_fetcher_.get()) {
Comran Morsheddaf69232016-04-20 22:25:37 -0700116 // TODO(comran): Grab detected voltage from joystick_reader. Except this
117 // value may not change, so it may be worth it to keep it as it is right
118 // now.
Austin Schuh4b652c92019-05-27 13:22:27 -0700119 if (ball_detector_fetcher_->voltage > 2.5) {
Comran Morshed38967332016-04-23 19:26:48 -0700120 big_indicator = big_indicator::kBallIntaked;
Comran Morsheddaf69232016-04-20 22:25:37 -0700121 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700122 }
123
124 if (::y2016::control_loops::superstructure_queue.status.get()) {
125 if (!::y2016::control_loops::superstructure_queue.status->zeroed) {
126 superstructure_state_indicator = superstructure_indicator::kNotZeroed;
127 }
128 if (::y2016::control_loops::superstructure_queue.status->estopped) {
129 superstructure_state_indicator = superstructure_indicator::kEstopped;
130 }
131 }
132
Austin Schuha250b2d2019-05-27 16:14:02 -0700133 if (autonomous_mode_fetcher_.get()) {
134 auto_mode_indicator = autonomous_mode_fetcher_->mode;
Comran Morsheddaf69232016-04-20 22:25:37 -0700135 }
136
137 AddPoint("big indicator", big_indicator);
138 AddPoint("superstructure state indicator", superstructure_state_indicator);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700139 if (auto_mode_indicator != 15) {
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700140 AddPoint("auto mode indicator", auto_mode_indicator);
141 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700142#endif
143
144 // Get ready for next iteration. /////
145 sample_id_++;
146}
147
148void DataCollector::AddPoint(const ::std::string &name, double value) {
149 // Mutex should be locked when this method is called to synchronize packets.
150 CHECK(mutex_.OwnedBySelf());
151
152 size_t index = GetIndex(sample_id_);
153
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800154 ItemDatapoint datapoint{value, ::aos::monotonic_clock::now()};
Comran Morsheddaf69232016-04-20 22:25:37 -0700155 if (measure_index_ >= sample_items_.size()) {
156 // New item in our data table.
157 ::std::vector<ItemDatapoint> datapoints;
158 SampleItem item{name, datapoints};
159 sample_items_.emplace_back(item);
160 } else if (index >= sample_items_.at(measure_index_).datapoints.size()) {
161 // New data point for an already existing item.
162 sample_items_.at(measure_index_).datapoints.emplace_back(datapoint);
163 } else {
164 // Overwrite an already existing data point for an already existing item.
165 sample_items_.at(measure_index_).datapoints.at(index) = datapoint;
166 }
167
168 measure_index_++;
169}
170
171::std::string DataCollector::Fetch(int32_t from_sample) {
172 ::aos::MutexLocker locker(&mutex_);
173
174 ::std::stringstream message;
175 message.precision(10);
176
177 // Send out the names of each item when requested by the client.
178 // Example: *item_one_name,item_two_name,item_three_name
179 if (from_sample == 0) {
180 message << "*"; // Begin name packet.
181
182 // Add comma-separated list of names.
183 for (size_t cur_data_name = 0; cur_data_name < sample_items_.size();
184 cur_data_name++) {
185 if (cur_data_name > 0) {
186 message << ",";
187 }
188 message << sample_items_.at(cur_data_name).name;
189 }
190 return message.str();
191 }
192
193 // Send out one sample containing the data.
194 // Samples are split with dollar signs, info with percent signs, and
195 // measurements with commas.
196 // Example of data with two samples: $289%2803.13%10,67$290%2803.14%12,68
197
198 // Note that we are ignoring the from_sample being sent to keep up with the
199 // live data without worrying about client lag.
Comran Morshed40a18002016-04-28 13:40:26 -0700200 int32_t cur_sample = sample_id_;
Comran Morsheddaf69232016-04-20 22:25:37 -0700201 int32_t adjusted_index = GetIndex(cur_sample);
202 message << "$"; // Begin data packet.
203
204 // Make sure we are not out of range.
Comran Morshed40a18002016-04-28 13:40:26 -0700205 if (sample_items_.size() > 0) {
206 if (static_cast<size_t>(adjusted_index) <
207 sample_items_.at(0).datapoints.size()) {
208 message << cur_sample << "%"
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700209 << ::aos::time::DurationInSeconds(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800210 sample_items_.at(0)
211 .datapoints.at(adjusted_index)
212 .time.time_since_epoch())
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800213 << "%"; // Send time.
Comran Morshed40a18002016-04-28 13:40:26 -0700214 // Add comma-separated list of data points.
215 for (size_t cur_measure = 0; cur_measure < sample_items_.size();
216 cur_measure++) {
217 if (cur_measure > 0) {
218 message << ",";
219 }
220 message << sample_items_.at(cur_measure)
221 .datapoints.at(adjusted_index)
222 .value;
223 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700224 }
225 }
226
227 return message.str();
228}
229
230size_t DataCollector::GetIndex(size_t sample_id) {
231 return sample_id % overflow_id_;
232}
233
234void DataCollector::operator()() {
235 ::aos::SetCurrentThreadName("DashboardData");
236
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800237 ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(100),
238 chrono::seconds(0));
Comran Morsheddaf69232016-04-20 22:25:37 -0700239 while (run_) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800240 phased_loop.SleepUntilNext();
Comran Morsheddaf69232016-04-20 22:25:37 -0700241 RunIteration();
242 }
243}
244
Austin Schuh1bf8a212019-05-26 22:13:14 -0700245SocketHandler::SocketHandler(::aos::EventLoop *event_loop)
246 : data_collector_(event_loop),
247 data_collector_thread_(::std::ref(data_collector_)) {}
Comran Morsheddaf69232016-04-20 22:25:37 -0700248
249void SocketHandler::onConnect(seasocks::WebSocket *connection) {
250 connections_.insert(connection);
251 LOG(INFO, "Connected: %s : %s\n", connection->getRequestUri().c_str(),
252 seasocks::formatAddress(connection->getRemoteAddress()).c_str());
253}
254
255void SocketHandler::onData(seasocks::WebSocket *connection, const char *data) {
256 int32_t from_sample = atoi(data);
257
258 ::std::string send_data = data_collector_.Fetch(from_sample);
259 connection->send(send_data.c_str());
260}
261
262void SocketHandler::onDisconnect(seasocks::WebSocket *connection) {
263 connections_.erase(connection);
264 LOG(INFO, "Disconnected: %s : %s\n", connection->getRequestUri().c_str(),
265 seasocks::formatAddress(connection->getRemoteAddress()).c_str());
266}
267
268void SocketHandler::Quit() {
269 data_collector_.Quit();
270 data_collector_thread_.join();
271}
272
Comran Morsheddaf69232016-04-20 22:25:37 -0700273} // namespace dashboard
274} // namespace y2016
275
276int main(int, char *[]) {
Brian Silvermanacdabeb2019-03-23 14:04:36 -0700277 // Make sure to reference this to force the linker to include it.
278 findEmbeddedContent("");
279
Comran Morsheddaf69232016-04-20 22:25:37 -0700280 ::aos::InitNRT();
281
Austin Schuh1bf8a212019-05-26 22:13:14 -0700282 ::aos::ShmEventLoop event_loop;
283
Comran Morsheddaf69232016-04-20 22:25:37 -0700284 ::seasocks::Server server(::std::shared_ptr<seasocks::Logger>(
Austin Schuh86cd5722019-04-14 13:34:20 -0700285 new ::aos::seasocks::SeasocksLogger(::seasocks::Logger::Level::Info)));
Austin Schuh1bf8a212019-05-26 22:13:14 -0700286 ::y2016::dashboard::SocketHandler socket_handler(&event_loop);
Comran Morsheddaf69232016-04-20 22:25:37 -0700287
288 server.addWebSocketHandler(
289 "/ws",
290 ::std::shared_ptr<::y2016::dashboard::SocketHandler>(&socket_handler));
291#ifdef DASHBOARD_TESTING
292 server.serve("www", 1180);
293#else
294 // Absolute directory of www folder on the robot.
295 server.serve("/home/admin/robot_code/www", 1180);
296#endif
297
298 socket_handler.Quit();
299
300 ::aos::Cleanup();
301 return 0;
302}