blob: f410c19e6d55b3369ce960870c28ea250d7eb77b [file] [log] [blame]
Brian Silverman07ec88e2014-12-28 00:13:08 -08001#include "frc971/wpilib/gyro_sender.h"
2
Austin Schuhf2a50ba2016-12-24 16:16:26 -08003#include <fcntl.h>
Brian Silverman07ec88e2014-12-28 00:13:08 -08004#include <inttypes.h>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08005#include <sys/stat.h>
6#include <sys/types.h>
7
8#include <chrono>
Brian Silverman07ec88e2014-12-28 00:13:08 -08009
10#include "aos/common/logging/logging.h"
11#include "aos/common/logging/queue_logging.h"
12#include "aos/common/util/phased_loop.h"
13#include "aos/common/messages/robot_state.q.h"
14#include "aos/common/time.h"
15#include "aos/linux_code/init.h"
16
17#include "frc971/queues/gyro.q.h"
Philipp Schrader29d54f22016-04-02 22:14:48 +000018#include "frc971/zeroing/averager.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080019
20namespace frc971 {
21namespace wpilib {
22
23GyroSender::GyroSender() {}
Austin Schuhf2a50ba2016-12-24 16:16:26 -080024namespace chrono = ::std::chrono;
25using ::aos::monotonic_clock;
Brian Silverman07ec88e2014-12-28 00:13:08 -080026
27void GyroSender::operator()() {
28 ::aos::SetCurrentThreadName("Gyro");
29
30 // Try to initialize repeatedly as long as we're supposed to be running.
31 while (run_ && !gyro_.InitializeGyro()) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -080032 ::std::this_thread::sleep_for(::std::chrono::milliseconds(50));
Brian Silverman07ec88e2014-12-28 00:13:08 -080033 }
34 LOG(INFO, "gyro initialized successfully\n");
35
36 auto message = ::frc971::sensors::gyro_part_id.MakeMessage();
37 message->uid = gyro_.ReadPartID();
38 LOG_STRUCT(INFO, "gyro ID", *message);
39 message.Send();
40
41 // In radians, ready to send out.
42 double angle = 0;
43
44 int startup_cycles_left = 2 * kReadingRate;
45
Philipp Schrader29d54f22016-04-02 22:14:48 +000046 zeroing::Averager<double, 6 * kReadingRate> zeroing_data;
Brian Silverman07ec88e2014-12-28 00:13:08 -080047 bool zeroed = false;
Brian Silverman07ec88e2014-12-28 00:13:08 -080048 double zero_offset = 0;
49
Austin Schuhea9d6022017-01-02 13:38:07 -080050 ::aos::time::PhasedLoop phased_loop(::aos::time::FromRate(kReadingRate));
Brian Silvermandcaa3f72015-11-29 05:32:08 +000051 // How many timesteps the next reading represents.
52 int number_readings = 0;
53
Austin Schuh70ae5932016-11-27 19:09:25 -080054 ::aos::SetCurrentThreadRealtimePriority(33);
55
Brian Silverman07ec88e2014-12-28 00:13:08 -080056 while (run_) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +000057 number_readings += phased_loop.SleepUntilNext();
Brian Silverman07ec88e2014-12-28 00:13:08 -080058
59 const uint32_t result = gyro_.GetReading();
60 if (result == 0) {
61 LOG(WARNING, "normal gyro read failed\n");
62 continue;
63 }
64 switch (gyro_.ExtractStatus(result)) {
65 case 0:
66 LOG(WARNING, "gyro says data is bad\n");
67 continue;
68 case 1:
69 break;
70 default:
71 LOG(WARNING, "gyro gave weird status 0x%" PRIx8 "\n",
72 gyro_.ExtractStatus(result));
73 continue;
74 }
75 if (gyro_.ExtractErrors(result) != 0) {
76 const uint8_t errors = gyro_.ExtractErrors(result);
77 if (errors & (1 << 6)) {
78 LOG(WARNING, "gyro gave PLL error\n");
79 }
80 if (errors & (1 << 5)) {
81 LOG(WARNING, "gyro gave quadrature error\n");
82 }
83 if (errors & (1 << 4)) {
84 LOG(WARNING, "gyro gave non-volatile memory error\n");
85 }
86 if (errors & (1 << 3)) {
87 LOG(WARNING, "gyro gave volatile memory error\n");
88 }
89 if (errors & (1 << 2)) {
90 LOG(WARNING, "gyro gave power error\n");
91 }
92 if (errors & (1 << 1)) {
93 LOG(WARNING, "gyro gave continuous self-test error\n");
94 }
95 if (errors & 1) {
96 LOG(WARNING, "gyro gave unexpected self-test mode\n");
97 }
98 continue;
99 }
100
101 if (startup_cycles_left > 0) {
102 --startup_cycles_left;
103 continue;
104 }
105
Austin Schuhc5a23752015-10-28 19:45:24 -0700106 const double angle_rate = gyro_.ExtractAngle(result);
107 const double new_angle = angle_rate / static_cast<double>(kReadingRate);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800108 auto message = ::frc971::sensors::gyro_reading.MakeMessage();
109 if (zeroed) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000110 angle += (new_angle + zero_offset) * number_readings;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800111 message->angle = angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700112 message->velocity = angle_rate + zero_offset * kReadingRate;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800113 LOG_STRUCT(DEBUG, "sending", *message);
114 message.Send();
115 } else {
116 // TODO(brian): Don't break without 6 seconds of standing still before
117 // enabling. Ideas:
118 // Don't allow driving until we have at least some data?
119 // Some kind of indicator light?
120 {
121 message->angle = new_angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700122 message->velocity = angle_rate;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800123 LOG_STRUCT(DEBUG, "collected while zeroing", *message);
Austin Schuh5125e082015-04-18 22:56:04 -0700124 message->angle = 0.0;
Austin Schuhc5a23752015-10-28 19:45:24 -0700125 message->velocity = 0.0;
Austin Schuh5125e082015-04-18 22:56:04 -0700126 message.Send();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800127 }
Philipp Schrader29d54f22016-04-02 22:14:48 +0000128 zeroing_data.AddData(new_angle);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800129
Brian Silverman699f0cb2015-02-05 19:45:01 -0500130 ::aos::joystick_state.FetchLatest();
131 if (::aos::joystick_state.get() && ::aos::joystick_state->enabled &&
Philipp Schrader29d54f22016-04-02 22:14:48 +0000132 zeroing_data.full()) {
133 zero_offset = -zeroing_data.GetAverage();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800134 LOG(INFO, "total zero offset %f\n", zero_offset);
135 zeroed = true;
136 }
137 }
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000138 number_readings = 0;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800139 }
140}
141
142} // namespace wpilib
143} // namespace frc971