blob: d01243fcb95079baca9ec6cf1bd09b139e82feb1 [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "aos/realtime.h"
Austin Schuh86cd5722019-04-14 13:34:20 -070018#include "aos/seasocks/seasocks_logger.h"
Brian Silverman1463c092020-10-30 17:28:24 -070019#include "aos/stl_mutex/stl_mutex.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_(
Brian Silverman1463c092020-10-30 17:28:24 -070055 event_loop->MakeFetcher<::y2016::vision::VisionStatus>("/vision")),
Austin Schuh4b652c92019-05-27 13:22:27 -070056 ball_detector_fetcher_(
57 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070058 "/superstructure")),
Austin Schuha250b2d2019-05-27 16:14:02 -070059 autonomous_mode_fetcher_(
60 event_loop->MakeFetcher<::frc971::autonomous::AutonomousMode>(
Tyler Chatow24b5db12020-01-06 21:16:56 -080061 "/autonomous")),
Austin Schuh9481d0d2019-06-29 21:56:17 -070062 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 event_loop
64 ->MakeFetcher<::y2016::control_loops::superstructure::Status>(
65 "/superstructure")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070066 cur_raw_data_("no data"),
Comran Morsheddaf69232016-04-20 22:25:37 -070067 sample_id_(0),
68 measure_index_(0),
Comran Morshed40a18002016-04-28 13:40:26 -070069 overflow_id_(1) {}
Comran Morsheddaf69232016-04-20 22:25:37 -070070
71void DataCollector::RunIteration() {
Brian Silverman1463c092020-10-30 17:28:24 -070072 std::unique_lock<aos::stl_mutex> locker(mutex_);
Comran Morsheddaf69232016-04-20 22:25:37 -070073 measure_index_ = 0;
74
75// Add recorded data here. /////
76#ifdef DASHBOARD_TESTING
77 // The following feeds data into the webserver when we do not have a process
78 // feeding data to the queues.
79 // To test, we are sending three streams holding randomly generated numbers.
80 AddPoint("test", ::std::rand() % 4);
81 AddPoint("test2", ::std::rand() % 3);
82 AddPoint("test3", ::std::rand() % 3 - 1);
83 (void)big_indicator::kBlack;
84 (void)big_indicator::kBallIntaked;
85 (void)big_indicator::kAiming;
86 (void)big_indicator::kLockedOn;
87 (void)superstructure_indicator::kBlack;
88 (void)superstructure_indicator::kNotZeroed;
89 (void)superstructure_indicator::kEstopped;
90#else
91 int big_indicator = big_indicator::kBlack;
92 int superstructure_state_indicator = superstructure_indicator::kBlack;
93 // We should never have a -1 here, so this is an indicator that somethings
94 // gone wrong with reading the auto queue.
95 int auto_mode_indicator = -1;
96
Austin Schuha250b2d2019-05-27 16:14:02 -070097 autonomous_mode_fetcher_.Fetch();
Austin Schuh9481d0d2019-06-29 21:56:17 -070098 superstructure_status_fetcher_.Fetch();
Austin Schuh4b652c92019-05-27 13:22:27 -070099 ball_detector_fetcher_.Fetch();
Austin Schuh1bf8a212019-05-26 22:13:14 -0700100 vision_status_fetcher_.Fetch();
Comran Morsheddaf69232016-04-20 22:25:37 -0700101
Comran Morshed38967332016-04-23 19:26:48 -0700102// Caused glitching with auto-aim at NASA, so be cautious with this until
103// we find a good fix.
104#ifdef DASHBOARD_READ_VISION_QUEUE
Austin Schuh1bf8a212019-05-26 22:13:14 -0700105 if (vision_status_fetcher_.get() &&
106 (vision_status_fetcher_->left_image_valid ||
107 vision_status_fetcher_->right_image_valid)) {
Comran Morshed38967332016-04-23 19:26:48 -0700108 big_indicator = big_indicator::kAiming;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700109 if (::std::abs(vision_status_fetcher_->horizontal_angle) < 0.002) {
Comran Morshed38967332016-04-23 19:26:48 -0700110 big_indicator = big_indicator::kLockedOn;
111 }
112 }
113#else
114 (void)big_indicator::kAiming;
115 (void)big_indicator::kLockedOn;
116#endif
117
118 // Ball detector comes after vision because we want to prioritize that
119 // indication.
Austin Schuh4b652c92019-05-27 13:22:27 -0700120 if (ball_detector_fetcher_.get()) {
Comran Morsheddaf69232016-04-20 22:25:37 -0700121 // TODO(comran): Grab detected voltage from joystick_reader. Except this
122 // value may not change, so it may be worth it to keep it as it is right
123 // now.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700124 if (ball_detector_fetcher_->voltage() > 2.5) {
Comran Morshed38967332016-04-23 19:26:48 -0700125 big_indicator = big_indicator::kBallIntaked;
Comran Morsheddaf69232016-04-20 22:25:37 -0700126 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700127 }
128
Austin Schuh9481d0d2019-06-29 21:56:17 -0700129 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 if (!superstructure_status_fetcher_->zeroed()) {
Comran Morsheddaf69232016-04-20 22:25:37 -0700131 superstructure_state_indicator = superstructure_indicator::kNotZeroed;
132 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700133 if (superstructure_status_fetcher_->estopped()) {
Comran Morsheddaf69232016-04-20 22:25:37 -0700134 superstructure_state_indicator = superstructure_indicator::kEstopped;
135 }
136 }
137
Austin Schuha250b2d2019-05-27 16:14:02 -0700138 if (autonomous_mode_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 auto_mode_indicator = autonomous_mode_fetcher_->mode();
Comran Morsheddaf69232016-04-20 22:25:37 -0700140 }
141
142 AddPoint("big indicator", big_indicator);
143 AddPoint("superstructure state indicator", superstructure_state_indicator);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700144 if (auto_mode_indicator != 15) {
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700145 AddPoint("auto mode indicator", auto_mode_indicator);
146 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700147#endif
148
149 // Get ready for next iteration. /////
150 sample_id_++;
151}
152
153void DataCollector::AddPoint(const ::std::string &name, double value) {
154 // Mutex should be locked when this method is called to synchronize packets.
Brian Silverman1463c092020-10-30 17:28:24 -0700155 AOS_CHECK(mutex_islocked(mutex_.native_handle()));
Comran Morsheddaf69232016-04-20 22:25:37 -0700156
157 size_t index = GetIndex(sample_id_);
158
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800159 ItemDatapoint datapoint{value, ::aos::monotonic_clock::now()};
Comran Morsheddaf69232016-04-20 22:25:37 -0700160 if (measure_index_ >= sample_items_.size()) {
161 // New item in our data table.
162 ::std::vector<ItemDatapoint> datapoints;
163 SampleItem item{name, datapoints};
164 sample_items_.emplace_back(item);
165 } else if (index >= sample_items_.at(measure_index_).datapoints.size()) {
166 // New data point for an already existing item.
167 sample_items_.at(measure_index_).datapoints.emplace_back(datapoint);
168 } else {
169 // Overwrite an already existing data point for an already existing item.
170 sample_items_.at(measure_index_).datapoints.at(index) = datapoint;
171 }
172
173 measure_index_++;
174}
175
176::std::string DataCollector::Fetch(int32_t from_sample) {
Brian Silverman1463c092020-10-30 17:28:24 -0700177 std::unique_lock<aos::stl_mutex> locker(mutex_);
Comran Morsheddaf69232016-04-20 22:25:37 -0700178
179 ::std::stringstream message;
180 message.precision(10);
181
182 // Send out the names of each item when requested by the client.
183 // Example: *item_one_name,item_two_name,item_three_name
184 if (from_sample == 0) {
185 message << "*"; // Begin name packet.
186
187 // Add comma-separated list of names.
188 for (size_t cur_data_name = 0; cur_data_name < sample_items_.size();
189 cur_data_name++) {
190 if (cur_data_name > 0) {
191 message << ",";
192 }
193 message << sample_items_.at(cur_data_name).name;
194 }
195 return message.str();
196 }
197
198 // Send out one sample containing the data.
199 // Samples are split with dollar signs, info with percent signs, and
200 // measurements with commas.
201 // Example of data with two samples: $289%2803.13%10,67$290%2803.14%12,68
202
203 // Note that we are ignoring the from_sample being sent to keep up with the
204 // live data without worrying about client lag.
Comran Morshed40a18002016-04-28 13:40:26 -0700205 int32_t cur_sample = sample_id_;
Comran Morsheddaf69232016-04-20 22:25:37 -0700206 int32_t adjusted_index = GetIndex(cur_sample);
207 message << "$"; // Begin data packet.
208
209 // Make sure we are not out of range.
Comran Morshed40a18002016-04-28 13:40:26 -0700210 if (sample_items_.size() > 0) {
211 if (static_cast<size_t>(adjusted_index) <
212 sample_items_.at(0).datapoints.size()) {
213 message << cur_sample << "%"
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700214 << ::aos::time::DurationInSeconds(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800215 sample_items_.at(0)
216 .datapoints.at(adjusted_index)
217 .time.time_since_epoch())
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800218 << "%"; // Send time.
Comran Morshed40a18002016-04-28 13:40:26 -0700219 // Add comma-separated list of data points.
220 for (size_t cur_measure = 0; cur_measure < sample_items_.size();
221 cur_measure++) {
222 if (cur_measure > 0) {
223 message << ",";
224 }
225 message << sample_items_.at(cur_measure)
226 .datapoints.at(adjusted_index)
227 .value;
228 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700229 }
230 }
231
232 return message.str();
233}
234
235size_t DataCollector::GetIndex(size_t sample_id) {
236 return sample_id % overflow_id_;
237}
238
239void DataCollector::operator()() {
240 ::aos::SetCurrentThreadName("DashboardData");
241
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800242 ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(100),
Austin Schuhd32b3622019-06-23 18:49:06 -0700243 event_loop_->monotonic_now(),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800244 chrono::seconds(0));
Comran Morsheddaf69232016-04-20 22:25:37 -0700245 while (run_) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800246 phased_loop.SleepUntilNext();
Comran Morsheddaf69232016-04-20 22:25:37 -0700247 RunIteration();
248 }
249}
250
Austin Schuh1bf8a212019-05-26 22:13:14 -0700251SocketHandler::SocketHandler(::aos::EventLoop *event_loop)
252 : data_collector_(event_loop),
253 data_collector_thread_(::std::ref(data_collector_)) {}
Comran Morsheddaf69232016-04-20 22:25:37 -0700254
255void SocketHandler::onConnect(seasocks::WebSocket *connection) {
256 connections_.insert(connection);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700257 AOS_LOG(INFO, "Connected: %s : %s\n", connection->getRequestUri().c_str(),
258 seasocks::formatAddress(connection->getRemoteAddress()).c_str());
Comran Morsheddaf69232016-04-20 22:25:37 -0700259}
260
261void SocketHandler::onData(seasocks::WebSocket *connection, const char *data) {
262 int32_t from_sample = atoi(data);
263
264 ::std::string send_data = data_collector_.Fetch(from_sample);
265 connection->send(send_data.c_str());
266}
267
268void SocketHandler::onDisconnect(seasocks::WebSocket *connection) {
269 connections_.erase(connection);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700270 AOS_LOG(INFO, "Disconnected: %s : %s\n", connection->getRequestUri().c_str(),
271 seasocks::formatAddress(connection->getRemoteAddress()).c_str());
Comran Morsheddaf69232016-04-20 22:25:37 -0700272}
273
274void SocketHandler::Quit() {
275 data_collector_.Quit();
276 data_collector_thread_.join();
277}
278
Comran Morsheddaf69232016-04-20 22:25:37 -0700279} // namespace dashboard
280} // namespace y2016
281
Austin Schuh094d09b2020-11-20 23:26:52 -0800282int main(int argc, char **argv) {
Brian Silvermanacdabeb2019-03-23 14:04:36 -0700283 // Make sure to reference this to force the linker to include it.
284 findEmbeddedContent("");
285
Austin Schuh094d09b2020-11-20 23:26:52 -0800286 ::aos::InitGoogle(&argc, &argv);
Comran Morsheddaf69232016-04-20 22:25:37 -0700287
Alex Perrycb7da4b2019-08-28 19:35:56 -0700288 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800289 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700290
291 ::aos::ShmEventLoop event_loop(&config.message());
Austin Schuh1bf8a212019-05-26 22:13:14 -0700292
Comran Morsheddaf69232016-04-20 22:25:37 -0700293 ::seasocks::Server server(::std::shared_ptr<seasocks::Logger>(
Austin Schuh86cd5722019-04-14 13:34:20 -0700294 new ::aos::seasocks::SeasocksLogger(::seasocks::Logger::Level::Info)));
Austin Schuh1bf8a212019-05-26 22:13:14 -0700295 ::y2016::dashboard::SocketHandler socket_handler(&event_loop);
Comran Morsheddaf69232016-04-20 22:25:37 -0700296
297 server.addWebSocketHandler(
298 "/ws",
299 ::std::shared_ptr<::y2016::dashboard::SocketHandler>(&socket_handler));
300#ifdef DASHBOARD_TESTING
301 server.serve("www", 1180);
302#else
303 // Absolute directory of www folder on the robot.
304 server.serve("/home/admin/robot_code/www", 1180);
305#endif
306
307 socket_handler.Quit();
308
Comran Morsheddaf69232016-04-20 22:25:37 -0700309 return 0;
310}