blob: af6019cec061c4fcbf1270ca6de75ef4d8b936a4 [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")),
Austin Schuh4b652c92019-05-27 13:22:27 -070057 ball_detector_fetcher_(
58 event_loop->MakeFetcher<::y2016::sensors::BallDetector>(
59 ".y2016.sensors.ball_detector")),
Austin Schuh1bf8a212019-05-26 22:13:14 -070060 cur_raw_data_("no data"),
Comran Morsheddaf69232016-04-20 22:25:37 -070061 sample_id_(0),
62 measure_index_(0),
Comran Morshed40a18002016-04-28 13:40:26 -070063 overflow_id_(1) {}
Comran Morsheddaf69232016-04-20 22:25:37 -070064
65void 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 Schrader4bd29b12017-02-22 04:42:27 +000091 ::frc971::autonomous::auto_mode.FetchLatest();
Comran Morsheddaf69232016-04-20 22:25:37 -070092 ::y2016::control_loops::superstructure_queue.status.FetchLatest();
Austin Schuh4b652c92019-05-27 13:22:27 -070093 ball_detector_fetcher_.Fetch();
Austin Schuh1bf8a212019-05-26 22:13:14 -070094 vision_status_fetcher_.Fetch();
Comran Morsheddaf69232016-04-20 22:25:37 -070095
Comran Morshed38967332016-04-23 19:26:48 -070096// 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 Schuh1bf8a212019-05-26 22:13:14 -070099 if (vision_status_fetcher_.get() &&
100 (vision_status_fetcher_->left_image_valid ||
101 vision_status_fetcher_->right_image_valid)) {
Comran Morshed38967332016-04-23 19:26:48 -0700102 big_indicator = big_indicator::kAiming;
Austin Schuh1bf8a212019-05-26 22:13:14 -0700103 if (::std::abs(vision_status_fetcher_->horizontal_angle) < 0.002) {
Comran Morshed38967332016-04-23 19:26:48 -0700104 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 Schuh4b652c92019-05-27 13:22:27 -0700114 if (ball_detector_fetcher_.get()) {
Comran Morsheddaf69232016-04-20 22:25:37 -0700115 // 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 Schuh4b652c92019-05-27 13:22:27 -0700118 if (ball_detector_fetcher_->voltage > 2.5) {
Comran Morshed38967332016-04-23 19:26:48 -0700119 big_indicator = big_indicator::kBallIntaked;
Comran Morsheddaf69232016-04-20 22:25:37 -0700120 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700121 }
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 Schrader4bd29b12017-02-22 04:42:27 +0000132 if (::frc971::autonomous::auto_mode.get()) {
133 auto_mode_indicator = ::frc971::autonomous::auto_mode->mode;
Comran Morsheddaf69232016-04-20 22:25:37 -0700134 }
135
136 AddPoint("big indicator", big_indicator);
137 AddPoint("superstructure state indicator", superstructure_state_indicator);
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700138 if (auto_mode_indicator != 15) {
Campbell Crowley9ed61a52016-11-05 17:13:07 -0700139 AddPoint("auto mode indicator", auto_mode_indicator);
140 }
Comran Morsheddaf69232016-04-20 22:25:37 -0700141#endif
142
143 // Get ready for next iteration. /////
144 sample_id_++;
145}
146
147void 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 Schuhf2a50ba2016-12-24 16:16:26 -0800153 ItemDatapoint datapoint{value, ::aos::monotonic_clock::now()};
Comran Morsheddaf69232016-04-20 22:25:37 -0700154 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 Morshed40a18002016-04-28 13:40:26 -0700199 int32_t cur_sample = sample_id_;
Comran Morsheddaf69232016-04-20 22:25:37 -0700200 int32_t adjusted_index = GetIndex(cur_sample);
201 message << "$"; // Begin data packet.
202
203 // Make sure we are not out of range.
Comran Morshed40a18002016-04-28 13:40:26 -0700204 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 Kuszmaul651fc3f2019-05-15 21:14:25 -0700208 << ::aos::time::DurationInSeconds(
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800209 sample_items_.at(0)
210 .datapoints.at(adjusted_index)
211 .time.time_since_epoch())
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800212 << "%"; // Send time.
Comran Morshed40a18002016-04-28 13:40:26 -0700213 // 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 Morsheddaf69232016-04-20 22:25:37 -0700223 }
224 }
225
226 return message.str();
227}
228
229size_t DataCollector::GetIndex(size_t sample_id) {
230 return sample_id % overflow_id_;
231}
232
233void DataCollector::operator()() {
234 ::aos::SetCurrentThreadName("DashboardData");
235
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800236 ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(100),
237 chrono::seconds(0));
Comran Morsheddaf69232016-04-20 22:25:37 -0700238 while (run_) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800239 phased_loop.SleepUntilNext();
Comran Morsheddaf69232016-04-20 22:25:37 -0700240 RunIteration();
241 }
242}
243
Austin Schuh1bf8a212019-05-26 22:13:14 -0700244SocketHandler::SocketHandler(::aos::EventLoop *event_loop)
245 : data_collector_(event_loop),
246 data_collector_thread_(::std::ref(data_collector_)) {}
Comran Morsheddaf69232016-04-20 22:25:37 -0700247
248void 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
254void 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
261void 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
267void SocketHandler::Quit() {
268 data_collector_.Quit();
269 data_collector_thread_.join();
270}
271
Comran Morsheddaf69232016-04-20 22:25:37 -0700272} // namespace dashboard
273} // namespace y2016
274
275int main(int, char *[]) {
Brian Silvermanacdabeb2019-03-23 14:04:36 -0700276 // Make sure to reference this to force the linker to include it.
277 findEmbeddedContent("");
278
Comran Morsheddaf69232016-04-20 22:25:37 -0700279 ::aos::InitNRT();
280
Austin Schuh1bf8a212019-05-26 22:13:14 -0700281 ::aos::ShmEventLoop event_loop;
282
Comran Morsheddaf69232016-04-20 22:25:37 -0700283 ::seasocks::Server server(::std::shared_ptr<seasocks::Logger>(
Austin Schuh86cd5722019-04-14 13:34:20 -0700284 new ::aos::seasocks::SeasocksLogger(::seasocks::Logger::Level::Info)));
Austin Schuh1bf8a212019-05-26 22:13:14 -0700285 ::y2016::dashboard::SocketHandler socket_handler(&event_loop);
Comran Morsheddaf69232016-04-20 22:25:37 -0700286
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}