blob: 121665f946815ec0e8f3f90c7dd17f3826193949 [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>(
30 ".aos.joystick_state")) {
31 PCHECK(
32 system("ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p "
Austin Schuh268e13c2017-02-03 20:33:23 -080033 "33") == 0);
34}
35
Brian Silverman07ec88e2014-12-28 00:13:08 -080036void GyroSender::operator()() {
37 ::aos::SetCurrentThreadName("Gyro");
38
39 // Try to initialize repeatedly as long as we're supposed to be running.
40 while (run_ && !gyro_.InitializeGyro()) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -080041 ::std::this_thread::sleep_for(::std::chrono::milliseconds(50));
Brian Silverman07ec88e2014-12-28 00:13:08 -080042 }
43 LOG(INFO, "gyro initialized successfully\n");
44
45 auto message = ::frc971::sensors::gyro_part_id.MakeMessage();
46 message->uid = gyro_.ReadPartID();
47 LOG_STRUCT(INFO, "gyro ID", *message);
48 message.Send();
49
50 // In radians, ready to send out.
51 double angle = 0;
52
53 int startup_cycles_left = 2 * kReadingRate;
54
Philipp Schrader29d54f22016-04-02 22:14:48 +000055 zeroing::Averager<double, 6 * kReadingRate> zeroing_data;
Brian Silverman07ec88e2014-12-28 00:13:08 -080056 bool zeroed = false;
Brian Silverman07ec88e2014-12-28 00:13:08 -080057 double zero_offset = 0;
58
Austin Schuh268e13c2017-02-03 20:33:23 -080059 ::aos::SetCurrentThreadRealtimePriority(33);
60
61 ::aos::time::PhasedLoop phased_loop(::aos::time::FromRate(kReadingRate),
62 chrono::milliseconds(4));
Brian Silvermandcaa3f72015-11-29 05:32:08 +000063 // How many timesteps the next reading represents.
64 int number_readings = 0;
65
Austin Schuh70ae5932016-11-27 19:09:25 -080066 ::aos::SetCurrentThreadRealtimePriority(33);
67
Brian Silverman07ec88e2014-12-28 00:13:08 -080068 while (run_) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +000069 number_readings += phased_loop.SleepUntilNext();
Brian Silverman07ec88e2014-12-28 00:13:08 -080070
71 const uint32_t result = gyro_.GetReading();
72 if (result == 0) {
73 LOG(WARNING, "normal gyro read failed\n");
74 continue;
75 }
76 switch (gyro_.ExtractStatus(result)) {
77 case 0:
78 LOG(WARNING, "gyro says data is bad\n");
79 continue;
80 case 1:
81 break;
82 default:
83 LOG(WARNING, "gyro gave weird status 0x%" PRIx8 "\n",
84 gyro_.ExtractStatus(result));
85 continue;
86 }
87 if (gyro_.ExtractErrors(result) != 0) {
88 const uint8_t errors = gyro_.ExtractErrors(result);
89 if (errors & (1 << 6)) {
90 LOG(WARNING, "gyro gave PLL error\n");
91 }
92 if (errors & (1 << 5)) {
93 LOG(WARNING, "gyro gave quadrature error\n");
94 }
95 if (errors & (1 << 4)) {
96 LOG(WARNING, "gyro gave non-volatile memory error\n");
97 }
98 if (errors & (1 << 3)) {
99 LOG(WARNING, "gyro gave volatile memory error\n");
100 }
101 if (errors & (1 << 2)) {
102 LOG(WARNING, "gyro gave power error\n");
103 }
104 if (errors & (1 << 1)) {
105 LOG(WARNING, "gyro gave continuous self-test error\n");
106 }
107 if (errors & 1) {
108 LOG(WARNING, "gyro gave unexpected self-test mode\n");
109 }
110 continue;
111 }
112
113 if (startup_cycles_left > 0) {
114 --startup_cycles_left;
115 continue;
116 }
117
Austin Schuhc5a23752015-10-28 19:45:24 -0700118 const double angle_rate = gyro_.ExtractAngle(result);
119 const double new_angle = angle_rate / static_cast<double>(kReadingRate);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800120 auto message = ::frc971::sensors::gyro_reading.MakeMessage();
121 if (zeroed) {
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000122 angle += (new_angle + zero_offset) * number_readings;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800123 message->angle = angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700124 message->velocity = angle_rate + zero_offset * kReadingRate;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800125 LOG_STRUCT(DEBUG, "sending", *message);
126 message.Send();
127 } else {
128 // TODO(brian): Don't break without 6 seconds of standing still before
129 // enabling. Ideas:
130 // Don't allow driving until we have at least some data?
131 // Some kind of indicator light?
132 {
133 message->angle = new_angle;
Austin Schuhc5a23752015-10-28 19:45:24 -0700134 message->velocity = angle_rate;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800135 LOG_STRUCT(DEBUG, "collected while zeroing", *message);
Austin Schuh5125e082015-04-18 22:56:04 -0700136 message->angle = 0.0;
Austin Schuhc5a23752015-10-28 19:45:24 -0700137 message->velocity = 0.0;
Austin Schuh5125e082015-04-18 22:56:04 -0700138 message.Send();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800139 }
Philipp Schrader29d54f22016-04-02 22:14:48 +0000140 zeroing_data.AddData(new_angle);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800141
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800142 joystick_state_fetcher_.Fetch();
143 if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled &&
Philipp Schrader29d54f22016-04-02 22:14:48 +0000144 zeroing_data.full()) {
145 zero_offset = -zeroing_data.GetAverage();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800146 LOG(INFO, "total zero offset %f\n", zero_offset);
147 zeroed = true;
148 }
149 }
Brian Silvermandcaa3f72015-11-29 05:32:08 +0000150 number_readings = 0;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800151 }
152}
153
154} // namespace wpilib
155} // namespace frc971