blob: b7d269bab37cecf86fa37b0e46cbfb9c24b99bb8 [file] [log] [blame]
James Kuszmaulf5f50dd2019-03-23 12:43:15 -07001#include <fcntl.h>
2
Philipp Schrader790cb542023-07-05 21:06:52 -07003#include "gflags/gflags.h"
4
James Kuszmaulf5f50dd2019-03-23 12:43:15 -07005#include "aos/init.h"
6#include "aos/logging/implementations.h"
7#include "aos/logging/replay.h"
8#include "aos/network/team_number.h"
9#include "aos/queue.h"
10#include "frc971/control_loops/drivetrain/drivetrain.q.h"
11#include "frc971/control_loops/drivetrain/localizer.q.h"
12#include "frc971/wpilib/imu.q.h"
James Kuszmaulf5f50dd2019-03-23 12:43:15 -070013#include "y2019/constants.h"
14#include "y2019/control_loops/drivetrain/drivetrain_base.h"
15#include "y2019/control_loops/drivetrain/event_loop_localizer.h"
16#if defined(SUPPORT_PLOT)
17#include "third_party/matplotlib-cpp/matplotlibcpp.h"
18#endif
19
20DEFINE_string(logfile, "", "Path to the logfile to replay.");
21// TODO(james): Figure out how to reliably source team number from logfile.
22DEFINE_int32(team, 971, "Team number to use for logfile replay.");
23DEFINE_int32(plot_duration, 30,
24 "Duration, in seconds, to plot after the start time.");
25DEFINE_int32(start_offset, 0,
26 "Time, in seconds, to start replay plot after the first enable.");
27
28namespace y2019 {
29namespace control_loops {
30namespace drivetrain {
31using ::y2019::constants::Field;
32
Philipp Schrader790cb542023-07-05 21:06:52 -070033typedef TypedLocalizer<constants::Values::kNumCameras, Field::kNumTargets,
34 Field::kNumObstacles,
35 EventLoopLocalizer::kMaxTargetsPerFrame, double>
36 TestLocalizer;
James Kuszmaulf5f50dd2019-03-23 12:43:15 -070037typedef typename TestLocalizer::Camera TestCamera;
38typedef typename TestCamera::Pose Pose;
39typedef typename TestCamera::LineSegment Obstacle;
40
41#if defined(SUPPORT_PLOT)
42// Plots a line from a vector of Pose's.
43void PlotPlotPts(const ::std::vector<Pose> &poses,
44 const ::std::map<::std::string, ::std::string> &kwargs) {
45 ::std::vector<double> x;
46 ::std::vector<double> y;
Philipp Schrader790cb542023-07-05 21:06:52 -070047 for (const Pose &p : poses) {
James Kuszmaulf5f50dd2019-03-23 12:43:15 -070048 x.push_back(p.abs_pos().x());
49 y.push_back(p.abs_pos().y());
50 }
51 matplotlibcpp::plot(x, y, kwargs);
52}
53#endif
54
55class LocalizerReplayer {
56 // This class is for replaying logfiles from the 2019 robot and seeing how the
57 // localizer performs withi any new changes versus how it performed during the
58 // real match. This starts running replay on the first enable and then will
59 // actually plot a subset of the run based on the --plot_duration and
60 // --start_offset flags.
61 public:
62 typedef ::frc971::control_loops::DrivetrainQueue::Goal DrivetrainGoal;
63 typedef ::frc971::control_loops::DrivetrainQueue::Position DrivetrainPosition;
64 typedef ::frc971::control_loops::DrivetrainQueue::Status DrivetrainStatus;
65 typedef ::frc971::control_loops::DrivetrainQueue::Output DrivetrainOutput;
66 typedef ::frc971::IMUValues IMUValues;
67 typedef ::aos::JoystickState JoystickState;
68
Austin Schuh9fe68f72019-08-10 19:32:03 -070069 LocalizerReplayer() : localizer_(&event_loop_, GetDrivetrainConfig()) {
James Kuszmaulf5f50dd2019-03-23 12:43:15 -070070 replayer_.AddDirectQueueSender<CameraFrame>(
71 "wpilib_interface.stripped.IMU", "camera_frames",
72 ".y2019.control_loops.drivetrain.camera_frames");
73
74 const char *drivetrain = "drivetrain.stripped";
75
76 replayer_.AddHandler<DrivetrainPosition>(
77 drivetrain, "position", [this](const DrivetrainPosition &msg) {
78 if (has_been_enabled_) {
79 localizer_.Update(last_last_U_ * battery_voltage_ / 12.0,
80 msg.sent_time, msg.left_encoder,
81 msg.right_encoder, latest_gyro_, 0.0);
82 if (start_time_ <= msg.sent_time &&
83 start_time_ + plot_duration_ >= msg.sent_time) {
84 t_pos_.push_back(
85 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
86 msg.sent_time.time_since_epoch())
87 .count());
88 new_x_.push_back(localizer_.x());
89 new_y_.push_back(localizer_.y());
90 new_theta_.push_back(localizer_.theta());
91 new_left_encoder_.push_back(localizer_.left_encoder());
92 new_right_encoder_.push_back(localizer_.right_encoder());
93 new_left_velocity_.push_back(localizer_.left_velocity());
94 new_right_velocity_.push_back(localizer_.right_velocity());
95
96 left_voltage_.push_back(last_last_U_(0, 0));
97 right_voltage_.push_back(last_last_U_(1, 0));
98 }
99 }
100 });
101
102 replayer_.AddHandler<DrivetrainGoal>(
103 drivetrain, "goal", [this](const DrivetrainGoal &msg) {
104 if (has_been_enabled_ && start_time_ <= msg.sent_time &&
105 (start_time_ + plot_duration_ >= msg.sent_time)) {
106 t_goal_.push_back(
107 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
108 msg.sent_time.time_since_epoch())
109 .count());
110 drivetrain_mode_.push_back(msg.controller_type);
111 throttle_.push_back(msg.throttle);
112 wheel_.push_back(msg.wheel);
113 }
114 });
115
116 replayer_.AddHandler<DrivetrainStatus>(
117 drivetrain, "status", [this](const DrivetrainStatus &msg) {
118 if (has_been_enabled_ && start_time_ <= msg.sent_time &&
119 (start_time_ + plot_duration_ >= msg.sent_time)) {
120 t_status_.push_back(
121 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
122 msg.sent_time.time_since_epoch())
123 .count());
124 original_x_.push_back(msg.x);
125 original_y_.push_back(msg.y);
126 original_theta_.push_back(msg.theta);
127 original_line_theta_.push_back(msg.line_follow_logging.rel_theta);
128 original_line_goal_theta_.push_back(
129 msg.line_follow_logging.goal_theta);
130 original_line_distance_.push_back(
131 msg.line_follow_logging.distance_to_target);
132 }
133 });
134
135 replayer_.AddHandler<DrivetrainOutput>(
136 drivetrain, "output", [this](const DrivetrainOutput &msg) {
137 last_last_U_ = last_U_;
138 last_U_ << msg.left_voltage, msg.right_voltage;
139 });
140
141 replayer_.AddHandler<frc971::control_loops::drivetrain::LocalizerControl>(
142 drivetrain, "localizer_control",
143 [this](const frc971::control_loops::drivetrain::LocalizerControl &msg) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700144 AOS_LOG_STRUCT(DEBUG, "localizer_control", msg);
James Kuszmaulf5f50dd2019-03-23 12:43:15 -0700145 localizer_.ResetPosition(msg.sent_time, msg.x, msg.y, msg.theta,
146 msg.theta_uncertainty,
147 !msg.keep_current_theta);
148 });
149
150 replayer_.AddHandler<JoystickState>(
151 "wpilib_interface.stripped.DSReader", "joystick_state",
152 [this](const JoystickState &msg) {
153 if (msg.enabled && !has_been_enabled_) {
154 has_been_enabled_ = true;
155 start_time_ =
156 msg.sent_time + ::std::chrono::seconds(FLAGS_start_offset);
157 }
158 });
159
160 replayer_.AddHandler<IMUValues>(
161 "wpilib_interface.stripped.IMU", "sending",
162 [this](const IMUValues &msg) {
163 latest_gyro_ = msg.gyro_z;
164 if (has_been_enabled_ && start_time_ <= msg.sent_time &&
165 (start_time_ + plot_duration_ >= msg.sent_time)) {
166 t_imu_.push_back(
167 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
168 msg.sent_time.time_since_epoch())
169 .count());
170 lat_accel_.push_back(msg.accelerometer_y);
171 long_accel_.push_back(msg.accelerometer_x);
172 z_accel_.push_back(msg.accelerometer_z);
173 }
174 });
175
176 replayer_.AddHandler<::aos::RobotState>(
177 "wpilib_interface.stripped.SensorReader", "robot_state",
178 [this](const ::aos::RobotState &msg) {
179 battery_voltage_ = msg.voltage_battery;
180 });
181 }
182
183 void ProcessFile(const char *filename) {
184 int fd;
185 if (strcmp(filename, "-") == 0) {
186 fd = STDIN_FILENO;
187 } else {
188 fd = open(filename, O_RDONLY);
189 }
190 if (fd == -1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700191 AOS_PLOG(FATAL, "couldn't open file '%s' for reading", filename);
James Kuszmaulf5f50dd2019-03-23 12:43:15 -0700192 }
193
194 replayer_.OpenFile(fd);
195 ::aos::RawQueue *queue = ::aos::logging::GetLoggingQueue();
196 while (!replayer_.ProcessMessage()) {
197 const ::aos::logging::LogMessage *const msg =
198 static_cast<const ::aos::logging::LogMessage *>(
199 queue->ReadMessage(::aos::RawQueue::kNonBlock));
200 if (msg != NULL) {
201 ::aos::logging::internal::PrintMessage(stdout, *msg);
202
203 queue->FreeMessage(msg);
204 }
205 continue;
206 }
207 replayer_.CloseCurrentFile();
208
Austin Schuhf257f3c2019-10-27 21:00:43 -0700209 AOS_PCHECK(close(fd));
James Kuszmaulf5f50dd2019-03-23 12:43:15 -0700210
211 Plot();
212 }
213
214 void Plot() {
215#if defined(SUPPORT_PLOT)
216 int start_idx = 0;
217 for (const float t : t_pos_) {
218 if (t < ::std::chrono::duration_cast<::std::chrono::duration<double>>(
219 start_time_.time_since_epoch())
220 .count() +
221 0.0) {
222 ++start_idx;
223 } else {
224 break;
225 }
226 }
227 ::std::vector<float> trunc_orig_x(original_x_.begin() + start_idx,
228 original_x_.end());
229 ::std::vector<float> trunc_orig_y(original_y_.begin() + start_idx,
230 original_y_.end());
231 ::std::vector<float> trunc_new_x(new_x_.begin() + start_idx, new_x_.end());
232 ::std::vector<float> trunc_new_y(new_y_.begin() + start_idx, new_y_.end());
233 matplotlibcpp::figure();
234 matplotlibcpp::plot(trunc_orig_x, trunc_orig_y,
235 {{"label", "original"}, {"marker", "o"}});
236 matplotlibcpp::plot(trunc_new_x, trunc_new_y,
237 {{"label", "new"}, {"marker", "o"}});
238 matplotlibcpp::xlim(-0.1, 19.0);
239 matplotlibcpp::ylim(-5.0, 5.0);
240 Field field;
241 for (const Target &target : field.targets()) {
242 PlotPlotPts(target.PlotPoints(), {{"c", "g"}});
243 }
244 for (const Obstacle &obstacle : field.obstacles()) {
245 PlotPlotPts(obstacle.PlotPoints(), {{"c", "k"}});
246 }
247 matplotlibcpp::grid(true);
248 matplotlibcpp::legend();
249
250 matplotlibcpp::figure();
251 matplotlibcpp::plot(t_status_, original_x_,
252 {{"label", "original x"}, {"marker", "o"}});
253 matplotlibcpp::plot(t_status_, original_y_,
254 {{"label", "original y"}, {"marker", "o"}});
255 matplotlibcpp::plot(t_status_, original_theta_,
256 {{"label", "original theta"}, {"marker", "o"}});
257 matplotlibcpp::plot(t_pos_, new_x_, {{"label", "new x"}, {"marker", "o"}});
258 matplotlibcpp::plot(t_pos_, new_y_, {{"label", "new y"}, {"marker", "o"}});
259 matplotlibcpp::plot(t_pos_, new_theta_,
260 {{"label", "new theta"}, {"marker", "o"}});
261 matplotlibcpp::grid(true);
262 matplotlibcpp::legend();
263
264 matplotlibcpp::figure();
265 matplotlibcpp::plot(t_pos_, left_voltage_,
266 {{"label", "left voltage"}, {"marker", "o"}});
267 matplotlibcpp::plot(t_pos_, right_voltage_,
268 {{"label", "right voltage"}, {"marker", "o"}});
269 matplotlibcpp::grid(true);
270 matplotlibcpp::legend();
271
272 matplotlibcpp::figure();
273 matplotlibcpp::plot(t_pos_, new_left_encoder_,
274 {{"label", "left encoder"}, {"marker", "o"}});
275 matplotlibcpp::plot(t_pos_, new_right_encoder_,
276 {{"label", "right encoder"}, {"marker", "o"}});
277 matplotlibcpp::plot(t_pos_, new_left_velocity_,
278 {{"label", "left velocity"}, {"marker", "o"}});
279 matplotlibcpp::plot(t_pos_, new_right_velocity_,
280 {{"label", "right velocity"}, {"marker", "o"}});
281 matplotlibcpp::grid(true);
282 matplotlibcpp::legend();
283
284 matplotlibcpp::figure();
285 matplotlibcpp::plot(t_goal_, drivetrain_mode_,
286 {{"label", "mode"}, {"marker", "o"}});
287 matplotlibcpp::plot(t_goal_, throttle_,
288 {{"label", "throttle"}, {"marker", "o"}});
289 matplotlibcpp::plot(t_goal_, wheel_, {{"label", "wheel"}, {"marker", "o"}});
290 matplotlibcpp::plot(t_status_, original_line_theta_,
291 {{"label", "relative theta"}, {"marker", "o"}});
292 matplotlibcpp::plot(t_status_, original_line_goal_theta_,
293 {{"label", "goal theta"}, {"marker", "o"}});
294 matplotlibcpp::plot(t_status_, original_line_distance_,
295 {{"label", "dist to target"}, {"marker", "o"}});
296 matplotlibcpp::grid(true);
297 matplotlibcpp::legend();
298
299 matplotlibcpp::figure();
300 ::std::vector<double> filt_lat_accel_ = FilterValues(lat_accel_, 10);
301 ::std::vector<double> filt_long_accel_ = FilterValues(long_accel_, 10);
302 ::std::vector<double> filt_z_accel_ = FilterValues(z_accel_, 10);
303 matplotlibcpp::plot(t_imu_, filt_lat_accel_,
304 {{"label", "lateral accel"}, {"marker", "o"}});
305 matplotlibcpp::plot(t_imu_, filt_long_accel_,
306 {{"label", "longitudinal accel"}, {"marker", "o"}});
307 matplotlibcpp::plot(t_imu_, filt_z_accel_,
308 {{"label", "z accel"}, {"marker", "o"}});
309 matplotlibcpp::grid(true);
310 matplotlibcpp::legend();
311
312 matplotlibcpp::show();
313#endif
314 }
315
316 private:
317 // Do a simple moving average with a width of N over the given raw data and
318 // return the results. For calculating values with N/2 of either edge, we
319 // assume that the space past the edges is filled with zeroes.
320 ::std::vector<double> FilterValues(const ::std::vector<double> &raw, int N) {
321 ::std::vector<double> filt;
322 for (int ii = 0; ii < static_cast<int>(raw.size()); ++ii) {
323 double sum = 0;
324 for (int jj = ::std::max(0, ii - N / 2);
325 jj < ::std::min(ii + N / 2, static_cast<int>(raw.size()) - 1);
326 ++jj) {
327 sum += raw[jj];
328 }
329 filt.push_back(sum / static_cast<double>(N));
330 }
331 return filt;
332 }
333
334 // TODO(james): Move to some sort of virtual event loop.
335 ::aos::ShmEventLoop event_loop_;
336
337 EventLoopLocalizer localizer_;
338 // Whether the robot has been enabled yet.
339 bool has_been_enabled_ = false;
340 // Cache of last gyro value to forward to the localizer.
Philipp Schrader790cb542023-07-05 21:06:52 -0700341 double latest_gyro_ = 0.0; // rad/sec
342 double battery_voltage_ = 12.0; // volts
James Kuszmaulf5f50dd2019-03-23 12:43:15 -0700343 ::Eigen::Matrix<double, 2, 1> last_U_{0, 0};
344 ::Eigen::Matrix<double, 2, 1> last_last_U_{0, 0};
345
346 ::aos::logging::linux_code::LogReplayer replayer_;
347
348 // Time at which to start the plot.
349 ::aos::monotonic_clock::time_point start_time_;
350 // Length of time to plot.
351 ::std::chrono::seconds plot_duration_{FLAGS_plot_duration};
352
353 // All the values to plot, organized in blocks by vectors of data that come in
354 // at the same time (e.g., status messages come in offset from position
355 // messages and so while they are all generally the same frequency, we can end
356 // receiving slightly different numbers of messages on different queues).
357
358 // TODO(james): Improve plotting support.
359 ::std::vector<double> t_status_;
360 ::std::vector<double> original_x_;
361 ::std::vector<double> original_y_;
362 ::std::vector<double> original_theta_;
363 ::std::vector<double> original_line_theta_;
364 ::std::vector<double> original_line_goal_theta_;
365 ::std::vector<double> original_line_distance_;
366
367 ::std::vector<double> t_pos_;
368 ::std::vector<double> new_x_;
369 ::std::vector<double> new_y_;
370 ::std::vector<double> new_left_encoder_;
371 ::std::vector<double> new_right_encoder_;
372 ::std::vector<double> new_left_velocity_;
373 ::std::vector<double> new_right_velocity_;
374 ::std::vector<double> new_theta_;
375 ::std::vector<double> left_voltage_;
376 ::std::vector<double> right_voltage_;
377
378 ::std::vector<double> t_goal_;
379 ::std::vector<double> drivetrain_mode_;
380 ::std::vector<double> throttle_;
381 ::std::vector<double> wheel_;
382
383 ::std::vector<double> t_imu_;
384 ::std::vector<double> lat_accel_;
385 ::std::vector<double> z_accel_;
386 ::std::vector<double> long_accel_;
387};
388
389} // namespace drivetrain
390} // namespace control_loops
391} // namespace y2019
392
393int main(int argc, char *argv[]) {
394 gflags::ParseCommandLineFlags(&argc, &argv, false);
395
396 if (FLAGS_logfile.empty()) {
397 fprintf(stderr, "Need a file to replay!\n");
398 return EXIT_FAILURE;
399 }
400
401 ::aos::network::OverrideTeamNumber(FLAGS_team);
402
James Kuszmaulf5f50dd2019-03-23 12:43:15 -0700403 ::y2019::control_loops::drivetrain::LocalizerReplayer replay;
404 replay.ProcessFile(FLAGS_logfile.c_str());
405
Austin Schuhae87e312020-08-01 16:15:01 -0700406 return 0;
James Kuszmaulf5f50dd2019-03-23 12:43:15 -0700407}