blob: 51281102aab906f7a0a1b43a1d2a2a581728ca20 [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
Austin Schuhf2a50ba2016-12-24 16:16:26 -080023namespace chrono = ::std::chrono;
24using ::aos::monotonic_clock;
Brian Silverman07ec88e2014-12-28 00:13:08 -080025
Austin Schuh268e13c2017-02-03 20:33:23 -080026GyroSender::GyroSender() {
27 PCHECK(system(
28 "ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p "
29 "33") == 0);
30}
31
Brian Silverman07ec88e2014-12-28 00:13:08 -080032void GyroSender::operator()() {
33 ::aos::SetCurrentThreadName("Gyro");
34
35 // Try to initialize repeatedly as long as we're supposed to be running.
36 while (run_ && !gyro_.InitializeGyro()) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -080037 ::std::this_thread::sleep_for(::std::chrono::milliseconds(50));
Brian Silverman07ec88e2014-12-28 00:13:08 -080038 }
39 LOG(INFO, "gyro initialized successfully\n");
40
41 auto message = ::frc971::sensors::gyro_part_id.MakeMessage();
42 message->uid = gyro_.ReadPartID();
43 LOG_STRUCT(INFO, "gyro ID", *message);
44 message.Send();
45
46 // In radians, ready to send out.
47 double angle = 0;
48
49 int startup_cycles_left = 2 * kReadingRate;
50
Philipp Schrader29d54f22016-04-02 22:14:48 +000051 zeroing::Averager<double, 6 * kReadingRate> zeroing_data;
Brian Silverman07ec88e2014-12-28 00:13:08 -080052 bool zeroed = false;
Brian Silverman07ec88e2014-12-28 00:13:08 -080053 double zero_offset = 0;
54
Austin Schuh268e13c2017-02-03 20:33:23 -080055 ::aos::SetCurrentThreadRealtimePriority(33);
56
57 ::aos::time::PhasedLoop phased_loop(::aos::time::FromRate(kReadingRate),
58 chrono::milliseconds(4));
Brian Silvermandcaa3f72015-11-29 05:32:08 +000059 // How many timesteps the next reading represents.
60 int number_readings = 0;
61
Austin Schuh70ae5932016-11-27 19:09:25 -080062 ::aos::SetCurrentThreadRealtimePriority(33);
63
Brian Silverman07ec88e2014-12-28 00:13:08 -080064 while (run_) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +000065 number_readings += phased_loop.SleepUntilNext();
Brian Silverman07ec88e2014-12-28 00:13:08 -080066
67 const uint32_t result = gyro_.GetReading();
68 if (result == 0) {
69 LOG(WARNING, "normal gyro read failed\n");
70 continue;
71 }
72 switch (gyro_.ExtractStatus(result)) {
73 case 0:
74 LOG(WARNING, "gyro says data is bad\n");
75 continue;
76 case 1:
77 break;
78 default:
79 LOG(WARNING, "gyro gave weird status 0x%" PRIx8 "\n",
80 gyro_.ExtractStatus(result));
81 continue;
82 }
83 if (gyro_.ExtractErrors(result) != 0) {
84 const uint8_t errors = gyro_.ExtractErrors(result);
85 if (errors & (1 << 6)) {
86 LOG(WARNING, "gyro gave PLL error\n");
87 }
88 if (errors & (1 << 5)) {
89 LOG(WARNING, "gyro gave quadrature error\n");
90 }
91 if (errors & (1 << 4)) {
92 LOG(WARNING, "gyro gave non-volatile memory error\n");
93 }
94 if (errors & (1 << 3)) {
95 LOG(WARNING, "gyro gave volatile memory error\n");
96 }
97 if (errors & (1 << 2)) {
98 LOG(WARNING, "gyro gave power error\n");
99 }
100 if (errors & (1 << 1)) {
101 LOG(WARNING, "gyro gave continuous self-test error\n");
102 }
103 if (errors & 1) {
104 LOG(WARNING, "gyro gave unexpected self-test mode\n");
105 }
106 continue;
107 }
108
109 if (startup_cycles_left > 0) {
110 --startup_cycles_left;
111 continue;
112 }
113
Austin Schuhc5a23752015-10-28 19:45:24 -0700114 const double angle_rate = gyro_.ExtractAngle(result);
115 const double new_angle = angle_rate / static_cast<double>(kReadingRate);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800116 auto message = ::frc971::sensors::gyro_reading.MakeMessage();
117 if (zeroed) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000118 angle += (new_angle + zero_offset) * number_readings;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800119 message->angle = angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700120 message->velocity = angle_rate + zero_offset * kReadingRate;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800121 LOG_STRUCT(DEBUG, "sending", *message);
122 message.Send();
123 } else {
124 // TODO(brian): Don't break without 6 seconds of standing still before
125 // enabling. Ideas:
126 // Don't allow driving until we have at least some data?
127 // Some kind of indicator light?
128 {
129 message->angle = new_angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700130 message->velocity = angle_rate;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800131 LOG_STRUCT(DEBUG, "collected while zeroing", *message);
Austin Schuh5125e082015-04-18 22:56:04 -0700132 message->angle = 0.0;
Austin Schuhc5a23752015-10-28 19:45:24 -0700133 message->velocity = 0.0;
Austin Schuh5125e082015-04-18 22:56:04 -0700134 message.Send();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800135 }
Philipp Schrader29d54f22016-04-02 22:14:48 +0000136 zeroing_data.AddData(new_angle);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800137
Brian Silverman699f0cb2015-02-05 19:45:01 -0500138 ::aos::joystick_state.FetchLatest();
139 if (::aos::joystick_state.get() && ::aos::joystick_state->enabled &&
Philipp Schrader29d54f22016-04-02 22:14:48 +0000140 zeroing_data.full()) {
141 zero_offset = -zeroing_data.GetAverage();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800142 LOG(INFO, "total zero offset %f\n", zero_offset);
143 zeroed = true;
144 }
145 }
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000146 number_readings = 0;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800147 }
148}
149
150} // namespace wpilib
151} // namespace frc971