blob: 8cfdc390698822e9649ae9f9cc364643e81a279e [file] [log] [blame]
Brian Silverman07ec88e2014-12-28 00:13:08 -08001#include "frc971/wpilib/gyro_sender.h"
2
3#include <inttypes.h>
4
5#include "aos/common/logging/logging.h"
6#include "aos/common/logging/queue_logging.h"
7#include "aos/common/util/phased_loop.h"
8#include "aos/common/messages/robot_state.q.h"
9#include "aos/common/time.h"
10#include "aos/linux_code/init.h"
11
12#include "frc971/queues/gyro.q.h"
Philipp Schrader29d54f22016-04-02 22:14:48 +000013#include "frc971/zeroing/averager.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080014
15namespace frc971 {
16namespace wpilib {
17
18GyroSender::GyroSender() {}
19
20void GyroSender::operator()() {
21 ::aos::SetCurrentThreadName("Gyro");
22
23 // Try to initialize repeatedly as long as we're supposed to be running.
24 while (run_ && !gyro_.InitializeGyro()) {
25 ::aos::time::SleepFor(::aos::time::Time::InMS(50));
26 }
27 LOG(INFO, "gyro initialized successfully\n");
28
29 auto message = ::frc971::sensors::gyro_part_id.MakeMessage();
30 message->uid = gyro_.ReadPartID();
31 LOG_STRUCT(INFO, "gyro ID", *message);
32 message.Send();
33
34 // In radians, ready to send out.
35 double angle = 0;
36
37 int startup_cycles_left = 2 * kReadingRate;
38
Philipp Schrader29d54f22016-04-02 22:14:48 +000039 zeroing::Averager<double, 6 * kReadingRate> zeroing_data;
Brian Silverman07ec88e2014-12-28 00:13:08 -080040 bool zeroed = false;
Brian Silverman07ec88e2014-12-28 00:13:08 -080041 double zero_offset = 0;
42
Brian Silvermandcaa3f72015-11-29 05:32:08 +000043 ::aos::time::PhasedLoop phased_loop(
44 ::aos::time::Time::FromRate(kReadingRate));
45 // How many timesteps the next reading represents.
46 int number_readings = 0;
47
Austin Schuh70ae5932016-11-27 19:09:25 -080048 ::aos::SetCurrentThreadRealtimePriority(33);
49
Brian Silverman07ec88e2014-12-28 00:13:08 -080050 while (run_) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +000051 number_readings += phased_loop.SleepUntilNext();
Brian Silverman07ec88e2014-12-28 00:13:08 -080052
53 const uint32_t result = gyro_.GetReading();
54 if (result == 0) {
55 LOG(WARNING, "normal gyro read failed\n");
56 continue;
57 }
58 switch (gyro_.ExtractStatus(result)) {
59 case 0:
60 LOG(WARNING, "gyro says data is bad\n");
61 continue;
62 case 1:
63 break;
64 default:
65 LOG(WARNING, "gyro gave weird status 0x%" PRIx8 "\n",
66 gyro_.ExtractStatus(result));
67 continue;
68 }
69 if (gyro_.ExtractErrors(result) != 0) {
70 const uint8_t errors = gyro_.ExtractErrors(result);
71 if (errors & (1 << 6)) {
72 LOG(WARNING, "gyro gave PLL error\n");
73 }
74 if (errors & (1 << 5)) {
75 LOG(WARNING, "gyro gave quadrature error\n");
76 }
77 if (errors & (1 << 4)) {
78 LOG(WARNING, "gyro gave non-volatile memory error\n");
79 }
80 if (errors & (1 << 3)) {
81 LOG(WARNING, "gyro gave volatile memory error\n");
82 }
83 if (errors & (1 << 2)) {
84 LOG(WARNING, "gyro gave power error\n");
85 }
86 if (errors & (1 << 1)) {
87 LOG(WARNING, "gyro gave continuous self-test error\n");
88 }
89 if (errors & 1) {
90 LOG(WARNING, "gyro gave unexpected self-test mode\n");
91 }
92 continue;
93 }
94
95 if (startup_cycles_left > 0) {
96 --startup_cycles_left;
97 continue;
98 }
99
Austin Schuhc5a23752015-10-28 19:45:24 -0700100 const double angle_rate = gyro_.ExtractAngle(result);
101 const double new_angle = angle_rate / static_cast<double>(kReadingRate);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800102 auto message = ::frc971::sensors::gyro_reading.MakeMessage();
103 if (zeroed) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000104 angle += (new_angle + zero_offset) * number_readings;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800105 message->angle = angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700106 message->velocity = angle_rate + zero_offset * kReadingRate;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800107 LOG_STRUCT(DEBUG, "sending", *message);
108 message.Send();
109 } else {
110 // TODO(brian): Don't break without 6 seconds of standing still before
111 // enabling. Ideas:
112 // Don't allow driving until we have at least some data?
113 // Some kind of indicator light?
114 {
115 message->angle = new_angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700116 message->velocity = angle_rate;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800117 LOG_STRUCT(DEBUG, "collected while zeroing", *message);
Austin Schuh5125e082015-04-18 22:56:04 -0700118 message->angle = 0.0;
Austin Schuhc5a23752015-10-28 19:45:24 -0700119 message->velocity = 0.0;
Austin Schuh5125e082015-04-18 22:56:04 -0700120 message.Send();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800121 }
Philipp Schrader29d54f22016-04-02 22:14:48 +0000122 zeroing_data.AddData(new_angle);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800123
Brian Silverman699f0cb2015-02-05 19:45:01 -0500124 ::aos::joystick_state.FetchLatest();
125 if (::aos::joystick_state.get() && ::aos::joystick_state->enabled &&
Philipp Schrader29d54f22016-04-02 22:14:48 +0000126 zeroing_data.full()) {
127 zero_offset = -zeroing_data.GetAverage();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800128 LOG(INFO, "total zero offset %f\n", zero_offset);
129 zeroed = true;
130 }
131 }
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000132 number_readings = 0;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800133 }
134}
135
136} // namespace wpilib
137} // namespace frc971