blob: 925588e433034e8cb6421233c020d9a0d042f625 [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
Austin Schuhdf6cbb12019-02-02 13:46:52 -080010#include "aos/events/event-loop.h"
11#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070012#include "aos/logging/logging.h"
13#include "aos/logging/queue_logging.h"
John Park33858a32018-09-28 23:05:48 -070014#include "aos/robot_state/robot_state.q.h"
15#include "aos/time/time.h"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080016#include "aos/util/phased_loop.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080017
18#include "frc971/queues/gyro.q.h"
Philipp Schrader29d54f22016-04-02 22:14:48 +000019#include "frc971/zeroing/averager.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080020
21namespace frc971 {
22namespace wpilib {
23
Austin Schuhf2a50ba2016-12-24 16:16:26 -080024namespace chrono = ::std::chrono;
25using ::aos::monotonic_clock;
Brian Silverman07ec88e2014-12-28 00:13:08 -080026
Austin Schuhdf6cbb12019-02-02 13:46:52 -080027GyroSender::GyroSender(::aos::EventLoop *event_loop)
28 : event_loop_(event_loop),
29 joystick_state_fetcher_(event_loop_->MakeFetcher<::aos::JoystickState>(
Austin Schuhcc1010e2019-05-12 20:38:01 -070030 ".aos.joystick_state")),
31 uid_sender_(event_loop_->MakeSender<::frc971::sensors::Uid>(
32 ".frc971.sensors.gyro_part_id")) {
Austin Schuhdf6cbb12019-02-02 13:46:52 -080033 PCHECK(
34 system("ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p "
Austin Schuh268e13c2017-02-03 20:33:23 -080035 "33") == 0);
36}
37
Brian Silverman07ec88e2014-12-28 00:13:08 -080038void GyroSender::operator()() {
39 ::aos::SetCurrentThreadName("Gyro");
40
41 // Try to initialize repeatedly as long as we're supposed to be running.
42 while (run_ && !gyro_.InitializeGyro()) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -080043 ::std::this_thread::sleep_for(::std::chrono::milliseconds(50));
Brian Silverman07ec88e2014-12-28 00:13:08 -080044 }
45 LOG(INFO, "gyro initialized successfully\n");
46
Austin Schuhcc1010e2019-05-12 20:38:01 -070047 auto message = uid_sender_.MakeMessage();
Brian Silverman07ec88e2014-12-28 00:13:08 -080048 message->uid = gyro_.ReadPartID();
49 LOG_STRUCT(INFO, "gyro ID", *message);
50 message.Send();
51
52 // In radians, ready to send out.
53 double angle = 0;
54
55 int startup_cycles_left = 2 * kReadingRate;
56
Philipp Schrader29d54f22016-04-02 22:14:48 +000057 zeroing::Averager<double, 6 * kReadingRate> zeroing_data;
Brian Silverman07ec88e2014-12-28 00:13:08 -080058 bool zeroed = false;
Brian Silverman07ec88e2014-12-28 00:13:08 -080059 double zero_offset = 0;
60
Austin Schuh268e13c2017-02-03 20:33:23 -080061 ::aos::SetCurrentThreadRealtimePriority(33);
62
63 ::aos::time::PhasedLoop phased_loop(::aos::time::FromRate(kReadingRate),
64 chrono::milliseconds(4));
Brian Silvermandcaa3f72015-11-29 05:32:08 +000065 // How many timesteps the next reading represents.
66 int number_readings = 0;
67
Austin Schuh70ae5932016-11-27 19:09:25 -080068 ::aos::SetCurrentThreadRealtimePriority(33);
69
Brian Silverman07ec88e2014-12-28 00:13:08 -080070 while (run_) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +000071 number_readings += phased_loop.SleepUntilNext();
Brian Silverman07ec88e2014-12-28 00:13:08 -080072
73 const uint32_t result = gyro_.GetReading();
74 if (result == 0) {
75 LOG(WARNING, "normal gyro read failed\n");
76 continue;
77 }
78 switch (gyro_.ExtractStatus(result)) {
79 case 0:
80 LOG(WARNING, "gyro says data is bad\n");
81 continue;
82 case 1:
83 break;
84 default:
85 LOG(WARNING, "gyro gave weird status 0x%" PRIx8 "\n",
86 gyro_.ExtractStatus(result));
87 continue;
88 }
89 if (gyro_.ExtractErrors(result) != 0) {
90 const uint8_t errors = gyro_.ExtractErrors(result);
91 if (errors & (1 << 6)) {
92 LOG(WARNING, "gyro gave PLL error\n");
93 }
94 if (errors & (1 << 5)) {
95 LOG(WARNING, "gyro gave quadrature error\n");
96 }
97 if (errors & (1 << 4)) {
98 LOG(WARNING, "gyro gave non-volatile memory error\n");
99 }
100 if (errors & (1 << 3)) {
101 LOG(WARNING, "gyro gave volatile memory error\n");
102 }
103 if (errors & (1 << 2)) {
104 LOG(WARNING, "gyro gave power error\n");
105 }
106 if (errors & (1 << 1)) {
107 LOG(WARNING, "gyro gave continuous self-test error\n");
108 }
109 if (errors & 1) {
110 LOG(WARNING, "gyro gave unexpected self-test mode\n");
111 }
112 continue;
113 }
114
115 if (startup_cycles_left > 0) {
116 --startup_cycles_left;
117 continue;
118 }
119
Austin Schuhc5a23752015-10-28 19:45:24 -0700120 const double angle_rate = gyro_.ExtractAngle(result);
121 const double new_angle = angle_rate / static_cast<double>(kReadingRate);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800122 auto message = ::frc971::sensors::gyro_reading.MakeMessage();
123 if (zeroed) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000124 angle += (new_angle + zero_offset) * number_readings;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800125 message->angle = angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700126 message->velocity = angle_rate + zero_offset * kReadingRate;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800127 LOG_STRUCT(DEBUG, "sending", *message);
128 message.Send();
129 } else {
130 // TODO(brian): Don't break without 6 seconds of standing still before
131 // enabling. Ideas:
132 // Don't allow driving until we have at least some data?
133 // Some kind of indicator light?
134 {
135 message->angle = new_angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700136 message->velocity = angle_rate;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800137 LOG_STRUCT(DEBUG, "collected while zeroing", *message);
Austin Schuh5125e082015-04-18 22:56:04 -0700138 message->angle = 0.0;
Austin Schuhc5a23752015-10-28 19:45:24 -0700139 message->velocity = 0.0;
Austin Schuh5125e082015-04-18 22:56:04 -0700140 message.Send();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800141 }
Philipp Schrader29d54f22016-04-02 22:14:48 +0000142 zeroing_data.AddData(new_angle);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800143
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800144 joystick_state_fetcher_.Fetch();
145 if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled &&
Philipp Schrader29d54f22016-04-02 22:14:48 +0000146 zeroing_data.full()) {
147 zero_offset = -zeroing_data.GetAverage();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800148 LOG(INFO, "total zero offset %f\n", zero_offset);
149 zeroed = true;
150 }
151 }
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000152 number_readings = 0;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800153 }
154}
155
156} // namespace wpilib
157} // namespace frc971