blob: f5297c0ef2efeb8713ecaad9c972c60153ef438a [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
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "aos/events/event_loop.h"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080011#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070012#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "aos/robot_state/robot_state_generated.h"
John Park33858a32018-09-28 23:05:48 -070014#include "aos/time/time.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080015
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "frc971/queues/gyro_generated.h"
Philipp Schrader29d54f22016-04-02 22:14:48 +000017#include "frc971/zeroing/averager.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080018
19namespace frc971 {
20namespace wpilib {
21
Austin Schuhf2a50ba2016-12-24 16:16:26 -080022namespace chrono = ::std::chrono;
23using ::aos::monotonic_clock;
Brian Silverman07ec88e2014-12-28 00:13:08 -080024
Austin Schuhdf6cbb12019-02-02 13:46:52 -080025GyroSender::GyroSender(::aos::EventLoop *event_loop)
26 : event_loop_(event_loop),
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 joystick_state_fetcher_(
28 event_loop_->MakeFetcher<aos::RobotState>("/aos")),
29 uid_sender_(event_loop_->MakeSender<frc971::sensors::Uid>("/drivetrain")),
Austin Schuh1ea89bb2019-05-27 16:59:59 -070030 gyro_reading_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 event_loop_->MakeSender<frc971::sensors::GyroReading>(
32 "/drivetrain")) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070033 AOS_PCHECK(
Austin Schuhdf6cbb12019-02-02 13:46:52 -080034 system("ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p "
Austin Schuh268e13c2017-02-03 20:33:23 -080035 "33") == 0);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070036 event_loop_->set_name("Gyro");
37 event_loop_->SetRuntimeRealtimePriority(33);
38
39 // TODO(austin): This should be synchronized with SensorReader... Pull out
40 // the sync logic and re-use it here.
41 event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); },
42 ::aos::time::FromRate(kReadingRate),
43 chrono::milliseconds(4));
Austin Schuh268e13c2017-02-03 20:33:23 -080044}
45
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070046void GyroSender::Loop(const int iterations) {
47 switch (state_) {
48 case State::INITIALIZING: {
49 const monotonic_clock::time_point monotonic_now =
50 event_loop_->monotonic_now();
51 if (last_initialize_time_ + chrono::milliseconds(50) < monotonic_now) {
52 if (gyro_.InitializeGyro()) {
53 state_ = State::RUNNING;
Austin Schuhf257f3c2019-10-27 21:00:43 -070054 AOS_LOG(INFO, "gyro initialized successfully\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080055
Alex Perrycb7da4b2019-08-28 19:35:56 -070056 auto builder = uid_sender_.MakeBuilder();
57 builder.Send(
58 frc971::sensors::CreateUid(*builder.fbb(), gyro_.ReadPartID()));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070059 }
60 last_initialize_time_ = monotonic_now;
Brian Silverman07ec88e2014-12-28 00:13:08 -080061 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070062 } break;
63 case State::RUNNING: {
64 const uint32_t result = gyro_.GetReading();
65 if (result == 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070066 AOS_LOG(WARNING, "normal gyro read failed\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070067 return;
Brian Silverman07ec88e2014-12-28 00:13:08 -080068 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070069 switch (gyro_.ExtractStatus(result)) {
70 case 0:
Austin Schuhf257f3c2019-10-27 21:00:43 -070071 AOS_LOG(WARNING, "gyro says data is bad\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070072 return;
73 case 1:
74 break;
75 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -070076 AOS_LOG(WARNING, "gyro gave weird status 0x%" PRIx8 "\n",
77 gyro_.ExtractStatus(result));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070078 return;
Brian Silverman07ec88e2014-12-28 00:13:08 -080079 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070080 if (gyro_.ExtractErrors(result) != 0) {
81 const uint8_t errors = gyro_.ExtractErrors(result);
82 if (errors & (1 << 6)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070083 AOS_LOG(WARNING, "gyro gave PLL error\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070084 }
85 if (errors & (1 << 5)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070086 AOS_LOG(WARNING, "gyro gave quadrature error\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070087 }
88 if (errors & (1 << 4)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070089 AOS_LOG(WARNING, "gyro gave non-volatile memory error\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070090 }
91 if (errors & (1 << 3)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070092 AOS_LOG(WARNING, "gyro gave volatile memory error\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070093 }
94 if (errors & (1 << 2)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070095 AOS_LOG(WARNING, "gyro gave power error\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070096 }
97 if (errors & (1 << 1)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070098 AOS_LOG(WARNING, "gyro gave continuous self-test error\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070099 }
100 if (errors & 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700101 AOS_LOG(WARNING, "gyro gave unexpected self-test mode\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700102 }
103 return;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800104 }
Brian Silverman07ec88e2014-12-28 00:13:08 -0800105
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700106 if (startup_cycles_left_ > 0) {
107 --startup_cycles_left_;
108 return;
109 }
Brian Silverman07ec88e2014-12-28 00:13:08 -0800110
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700111 const double angle_rate = gyro_.ExtractAngle(result);
112 const double new_angle = angle_rate / static_cast<double>(kReadingRate);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700113 auto builder = gyro_reading_sender_.MakeBuilder();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700114 if (zeroed_) {
115 angle_ += (new_angle + zero_offset_) * iterations;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700116 sensors::GyroReading::Builder gyro_builder =
117 builder.MakeBuilder<sensors::GyroReading>();
118 gyro_builder.add_angle(angle_);
119 gyro_builder.add_velocity(angle_rate + zero_offset_ * kReadingRate);
120 builder.Send(gyro_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700121 } else {
122 // TODO(brian): Don't break without 6 seconds of standing still before
123 // enabling. Ideas:
124 // Don't allow driving until we have at least some data?
125 // Some kind of indicator light?
126 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700127 sensors::GyroReading::Builder gyro_builder =
128 builder.MakeBuilder<sensors::GyroReading>();
129 gyro_builder.add_angle(0.0);
130 gyro_builder.add_velocity(0.0);
131 builder.Send(gyro_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700132 }
133 zeroing_data_.AddData(new_angle);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800134
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700135 joystick_state_fetcher_.Fetch();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700136 if (joystick_state_fetcher_.get() &&
137 joystick_state_fetcher_->outputs_enabled() &&
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700138 zeroing_data_.full()) {
139 zero_offset_ = -zeroing_data_.GetAverage();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700140 AOS_LOG(INFO, "total zero offset %f\n", zero_offset_);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700141 zeroed_ = true;
142 }
Brian Silverman07ec88e2014-12-28 00:13:08 -0800143 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700144 } break;
Brian Silverman07ec88e2014-12-28 00:13:08 -0800145 }
146}
147
148} // namespace wpilib
149} // namespace frc971