blob: d105a1030c799e329d88574dc5b66bb4101b913a [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"
Comran Morsheddaf69232016-04-20 22:25:37 -070021
Philipp Schrader4bd29b12017-02-22 04:42:27 +000022#include "frc971/autonomous/auto.q.h"
23
Comran Morsheddaf69232016-04-20 22:25:37 -070024#include "y2016/control_loops/superstructure/superstructure.q.h"
25#include "y2016/queues/ball_detector.q.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -070026#include "y2016/vision/vision.q.h"
Comran Morsheddaf69232016-04-20 22:25:37 -070027
Austin Schuhf2a50ba2016-12-24 16:16:26 -080028namespace chrono = ::std::chrono;
29
Comran Morsheddaf69232016-04-20 22:25:37 -070030namespace y2016 {
31namespace dashboard {
32namespace big_indicator {
33constexpr int kBlack = 0;
34constexpr int kBallIntaked = 1;
35constexpr int kAiming = 2;
36constexpr int kLockedOn = 3;
37} // namespace big_indicator
38
39namespace superstructure_indicator {
40constexpr int kBlack = 0;
41constexpr int kNotZeroed = 1;
42constexpr 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 Schuh1bf8a212019-05-26 22:13:14 -070053DataCollector::DataCollector(::aos::EventLoop *event_loop)
54 : vision_status_fetcher_(
55 event_loop->MakeFetcher<::y2016::vision::VisionStatus>(
56 ".y2016.vision.vision_status")),
57 cur_raw_data_("no data"),
Comran Morsheddaf69232016-04-20 22:25:37 -070058 sample_id_(0),
59 measure_index_(0),
Comran Morshed40a18002016-04-28 13:40:26 -070060 overflow_id_(1) {}
Comran Morsheddaf69232016-04-20 22:25:37 -070061
62void DataCollector::RunIteration() {
63 ::aos::MutexLocker locker(&mutex_);
64 measure_index_ = 0;
65
66// Add recorded data here. /////
67#ifdef DASHBOARD_TESTING
68 // The following feeds data into the webserver when we do not have a process
69 // feeding data to the queues.
70 // To test, we are sending three streams holding randomly generated numbers.
71 AddPoint("test", ::std::rand() % 4);
72 AddPoint("test2", ::std::rand() % 3);
73 AddPoint("test3", ::std::rand() % 3 - 1);
74 (void)big_indicator::kBlack;
75 (void)big_indicator::kBallIntaked;
76 (void)big_indicator::kAiming;
77 (void)big_indicator::kLockedOn;
78 (void)superstructure_indicator::kBlack;
79 (void)superstructure_indicator::kNotZeroed;
80 (void)superstructure_indicator::kEstopped;
81#else
82 int big_indicator = big_indicator::kBlack;
83 int superstructure_state_indicator = superstructure_indicator::kBlack;
84 // We should never have a -1 here, so this is an indicator that somethings
85 // gone wrong with reading the auto queue.
86 int auto_mode_indicator = -1;
87
Philipp Schrader4bd29b12017-02-22 04:42:27 +000088 ::frc971::autonomous::auto_mode.FetchLatest();
Comran Morsheddaf69232016-04-20 22:25:37 -070089 ::y2016::control_loops::superstructure_queue.status.FetchLatest();
90 ::y2016::sensors::ball_detector.FetchLatest();
Austin Schuh1bf8a212019-05-26 22:13:14 -070091 vision_status_fetcher_.Fetch();
Comran Morsheddaf69232016-04-20 22:25:37 -070092
Comran Morshed38967332016-04-23 19:26:48 -070093// Caused glitching with auto-aim at NASA, so be cautious with this until
94// we find a good fix.
95#ifdef DASHBOARD_READ_VISION_QUEUE
Austin Schuh1bf8a212019-05-26 22:13:14 -070096 if (vision_status_fetcher_.get() &&
97 (vision_status_fetcher_->left_image_valid ||
98 vision_status_fetcher_->right_image_valid)) {
Comran Morshed38967332016-04-23 19:26:48 -070099 big_indicator = big_indicator::kAiming;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700100 if (::std::abs(vision_status_fetcher_->horizontal_angle) < 0.002) {
Comran Morshed38967332016-04-23 19:26:48 -0700101 big_indicator = big_indicator::kLockedOn;
102 }
103 }
104#else
105 (void)big_indicator::kAiming;
106 (void)big_indicator::kLockedOn;
107#endif
108
109 // Ball detector comes after vision because we want to prioritize that
110 // indication.
Comran Morsheddaf69232016-04-20 22:25:37 -0700111 if (::y2016::sensors::ball_detector.get()) {
112 // TODO(comran): Grab detected voltage from joystick_reader. Except this
113 // value may not change, so it may be worth it to keep it as it is right
114 // now.
Comran Morshed38967332016-04-23 19:26:48 -0700115 if (::y2016::sensors::ball_detector->voltage > 2.5) {
116 big_indicator = big_indicator::kBallIntaked;
Comran Morsheddaf69232016-04-20 22:25:37 -0700117 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700118 }
119
120 if (::y2016::control_loops::superstructure_queue.status.get()) {
121 if (!::y2016::control_loops::superstructure_queue.status->zeroed) {
122 superstructure_state_indicator = superstructure_indicator::kNotZeroed;
123 }
124 if (::y2016::control_loops::superstructure_queue.status->estopped) {
125 superstructure_state_indicator = superstructure_indicator::kEstopped;
126 }
127 }
128
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000129 if (::frc971::autonomous::auto_mode.get()) {
130 auto_mode_indicator = ::frc971::autonomous::auto_mode->mode;
Comran Morsheddaf69232016-04-20 22:25:37 -0700131 }
132
133 AddPoint("big indicator", big_indicator);
134 AddPoint("superstructure state indicator", superstructure_state_indicator);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700135 if (auto_mode_indicator != 15) {
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700136 AddPoint("auto mode indicator", auto_mode_indicator);
137 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700138#endif
139
140 // Get ready for next iteration. /////
141 sample_id_++;
142}
143
144void DataCollector::AddPoint(const ::std::string &name, double value) {
145 // Mutex should be locked when this method is called to synchronize packets.
146 CHECK(mutex_.OwnedBySelf());
147
148 size_t index = GetIndex(sample_id_);
149
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800150 ItemDatapoint datapoint{value, ::aos::monotonic_clock::now()};
Comran Morsheddaf69232016-04-20 22:25:37 -0700151 if (measure_index_ >= sample_items_.size()) {
152 // New item in our data table.
153 ::std::vector<ItemDatapoint> datapoints;
154 SampleItem item{name, datapoints};
155 sample_items_.emplace_back(item);
156 } else if (index >= sample_items_.at(measure_index_).datapoints.size()) {
157 // New data point for an already existing item.
158 sample_items_.at(measure_index_).datapoints.emplace_back(datapoint);
159 } else {
160 // Overwrite an already existing data point for an already existing item.
161 sample_items_.at(measure_index_).datapoints.at(index) = datapoint;
162 }
163
164 measure_index_++;
165}
166
167::std::string DataCollector::Fetch(int32_t from_sample) {
168 ::aos::MutexLocker locker(&mutex_);
169
170 ::std::stringstream message;
171 message.precision(10);
172
173 // Send out the names of each item when requested by the client.
174 // Example: *item_one_name,item_two_name,item_three_name
175 if (from_sample == 0) {
176 message << "*"; // Begin name packet.
177
178 // Add comma-separated list of names.
179 for (size_t cur_data_name = 0; cur_data_name < sample_items_.size();
180 cur_data_name++) {
181 if (cur_data_name > 0) {
182 message << ",";
183 }
184 message << sample_items_.at(cur_data_name).name;
185 }
186 return message.str();
187 }
188
189 // Send out one sample containing the data.
190 // Samples are split with dollar signs, info with percent signs, and
191 // measurements with commas.
192 // Example of data with two samples: $289%2803.13%10,67$290%2803.14%12,68
193
194 // Note that we are ignoring the from_sample being sent to keep up with the
195 // live data without worrying about client lag.
Comran Morshed40a18002016-04-28 13:40:26 -0700196 int32_t cur_sample = sample_id_;
Comran Morsheddaf69232016-04-20 22:25:37 -0700197 int32_t adjusted_index = GetIndex(cur_sample);
198 message << "$"; // Begin data packet.
199
200 // Make sure we are not out of range.
Comran Morshed40a18002016-04-28 13:40:26 -0700201 if (sample_items_.size() > 0) {
202 if (static_cast<size_t>(adjusted_index) <
203 sample_items_.at(0).datapoints.size()) {
204 message << cur_sample << "%"
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700205 << ::aos::time::DurationInSeconds(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800206 sample_items_.at(0)
207 .datapoints.at(adjusted_index)
208 .time.time_since_epoch())
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800209 << "%"; // Send time.
Comran Morshed40a18002016-04-28 13:40:26 -0700210 // Add comma-separated list of data points.
211 for (size_t cur_measure = 0; cur_measure < sample_items_.size();
212 cur_measure++) {
213 if (cur_measure > 0) {
214 message << ",";
215 }
216 message << sample_items_.at(cur_measure)
217 .datapoints.at(adjusted_index)
218 .value;
219 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700220 }
221 }
222
223 return message.str();
224}
225
226size_t DataCollector::GetIndex(size_t sample_id) {
227 return sample_id % overflow_id_;
228}
229
230void DataCollector::operator()() {
231 ::aos::SetCurrentThreadName("DashboardData");
232
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800233 ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(100),
234 chrono::seconds(0));
Comran Morsheddaf69232016-04-20 22:25:37 -0700235 while (run_) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800236 phased_loop.SleepUntilNext();
Comran Morsheddaf69232016-04-20 22:25:37 -0700237 RunIteration();
238 }
239}
240
Austin Schuh1bf8a212019-05-26 22:13:14 -0700241SocketHandler::SocketHandler(::aos::EventLoop *event_loop)
242 : data_collector_(event_loop),
243 data_collector_thread_(::std::ref(data_collector_)) {}
Comran Morsheddaf69232016-04-20 22:25:37 -0700244
245void SocketHandler::onConnect(seasocks::WebSocket *connection) {
246 connections_.insert(connection);
247 LOG(INFO, "Connected: %s : %s\n", connection->getRequestUri().c_str(),
248 seasocks::formatAddress(connection->getRemoteAddress()).c_str());
249}
250
251void SocketHandler::onData(seasocks::WebSocket *connection, const char *data) {
252 int32_t from_sample = atoi(data);
253
254 ::std::string send_data = data_collector_.Fetch(from_sample);
255 connection->send(send_data.c_str());
256}
257
258void SocketHandler::onDisconnect(seasocks::WebSocket *connection) {
259 connections_.erase(connection);
260 LOG(INFO, "Disconnected: %s : %s\n", connection->getRequestUri().c_str(),
261 seasocks::formatAddress(connection->getRemoteAddress()).c_str());
262}
263
264void SocketHandler::Quit() {
265 data_collector_.Quit();
266 data_collector_thread_.join();
267}
268
Comran Morsheddaf69232016-04-20 22:25:37 -0700269} // namespace dashboard
270} // namespace y2016
271
272int main(int, char *[]) {
Brian Silvermanacdabeb2019-03-23 14:04:36 -0700273 // Make sure to reference this to force the linker to include it.
274 findEmbeddedContent("");
275
Comran Morsheddaf69232016-04-20 22:25:37 -0700276 ::aos::InitNRT();
277
Austin Schuh1bf8a212019-05-26 22:13:14 -0700278 ::aos::ShmEventLoop event_loop;
279
Comran Morsheddaf69232016-04-20 22:25:37 -0700280 ::seasocks::Server server(::std::shared_ptr<seasocks::Logger>(
Austin Schuh86cd5722019-04-14 13:34:20 -0700281 new ::aos::seasocks::SeasocksLogger(::seasocks::Logger::Level::Info)));
Austin Schuh1bf8a212019-05-26 22:13:14 -0700282 ::y2016::dashboard::SocketHandler socket_handler(&event_loop);
Comran Morsheddaf69232016-04-20 22:25:37 -0700283
284 server.addWebSocketHandler(
285 "/ws",
286 ::std::shared_ptr<::y2016::dashboard::SocketHandler>(&socket_handler));
287#ifdef DASHBOARD_TESTING
288 server.serve("www", 1180);
289#else
290 // Absolute directory of www folder on the robot.
291 server.serve("/home/admin/robot_code/www", 1180);
292#endif
293
294 socket_handler.Quit();
295
296 ::aos::Cleanup();
297 return 0;
298}