Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/gyro_sender.h" |
| 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <fcntl.h> |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 4 | #include <inttypes.h> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | |
| 8 | #include <chrono> |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 9 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 10 | #include "aos/events/event-loop.h" |
| 11 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 12 | #include "aos/logging/logging.h" |
| 13 | #include "aos/logging/queue_logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 14 | #include "aos/robot_state/robot_state.q.h" |
| 15 | #include "aos/time/time.h" |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 16 | |
| 17 | #include "frc971/queues/gyro.q.h" |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 18 | #include "frc971/zeroing/averager.h" |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 19 | |
| 20 | namespace frc971 { |
| 21 | namespace wpilib { |
| 22 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 23 | namespace chrono = ::std::chrono; |
| 24 | using ::aos::monotonic_clock; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 25 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 26 | GyroSender::GyroSender(::aos::EventLoop *event_loop) |
| 27 | : event_loop_(event_loop), |
| 28 | joystick_state_fetcher_(event_loop_->MakeFetcher<::aos::JoystickState>( |
Austin Schuh | cc1010e | 2019-05-12 20:38:01 -0700 | [diff] [blame] | 29 | ".aos.joystick_state")), |
| 30 | uid_sender_(event_loop_->MakeSender<::frc971::sensors::Uid>( |
Austin Schuh | 1ea89bb | 2019-05-27 16:59:59 -0700 | [diff] [blame] | 31 | ".frc971.sensors.gyro_part_id")), |
| 32 | gyro_reading_sender_( |
| 33 | event_loop_->MakeSender<::frc971::sensors::GyroReading>( |
| 34 | ".frc971.sensors.gyro_reading")) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 35 | AOS_PCHECK( |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 36 | system("ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p " |
Austin Schuh | 268e13c | 2017-02-03 20:33:23 -0800 | [diff] [blame] | 37 | "33") == 0); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 38 | event_loop_->set_name("Gyro"); |
| 39 | event_loop_->SetRuntimeRealtimePriority(33); |
| 40 | |
| 41 | // TODO(austin): This should be synchronized with SensorReader... Pull out |
| 42 | // the sync logic and re-use it here. |
| 43 | event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 44 | ::aos::time::FromRate(kReadingRate), |
| 45 | chrono::milliseconds(4)); |
Austin Schuh | 268e13c | 2017-02-03 20:33:23 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 48 | void GyroSender::Loop(const int iterations) { |
| 49 | switch (state_) { |
| 50 | case State::INITIALIZING: { |
| 51 | const monotonic_clock::time_point monotonic_now = |
| 52 | event_loop_->monotonic_now(); |
| 53 | if (last_initialize_time_ + chrono::milliseconds(50) < monotonic_now) { |
| 54 | if (gyro_.InitializeGyro()) { |
| 55 | state_ = State::RUNNING; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 56 | AOS_LOG(INFO, "gyro initialized successfully\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 57 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 58 | { |
| 59 | auto message = uid_sender_.MakeMessage(); |
| 60 | message->uid = gyro_.ReadPartID(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 61 | AOS_LOG_STRUCT(INFO, "gyro ID", *message); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 62 | message.Send(); |
| 63 | } |
| 64 | } |
| 65 | last_initialize_time_ = monotonic_now; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 66 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 67 | } break; |
| 68 | case State::RUNNING: { |
| 69 | const uint32_t result = gyro_.GetReading(); |
| 70 | if (result == 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 71 | AOS_LOG(WARNING, "normal gyro read failed\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 72 | return; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 73 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 74 | switch (gyro_.ExtractStatus(result)) { |
| 75 | case 0: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 76 | AOS_LOG(WARNING, "gyro says data is bad\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 77 | return; |
| 78 | case 1: |
| 79 | break; |
| 80 | default: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 81 | AOS_LOG(WARNING, "gyro gave weird status 0x%" PRIx8 "\n", |
| 82 | gyro_.ExtractStatus(result)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 83 | return; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 84 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 85 | if (gyro_.ExtractErrors(result) != 0) { |
| 86 | const uint8_t errors = gyro_.ExtractErrors(result); |
| 87 | if (errors & (1 << 6)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 88 | AOS_LOG(WARNING, "gyro gave PLL error\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 89 | } |
| 90 | if (errors & (1 << 5)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 91 | AOS_LOG(WARNING, "gyro gave quadrature error\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 92 | } |
| 93 | if (errors & (1 << 4)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 94 | AOS_LOG(WARNING, "gyro gave non-volatile memory error\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 95 | } |
| 96 | if (errors & (1 << 3)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 97 | AOS_LOG(WARNING, "gyro gave volatile memory error\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 98 | } |
| 99 | if (errors & (1 << 2)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 100 | AOS_LOG(WARNING, "gyro gave power error\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 101 | } |
| 102 | if (errors & (1 << 1)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 103 | AOS_LOG(WARNING, "gyro gave continuous self-test error\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 104 | } |
| 105 | if (errors & 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 106 | AOS_LOG(WARNING, "gyro gave unexpected self-test mode\n"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 107 | } |
| 108 | return; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 109 | } |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 110 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 111 | if (startup_cycles_left_ > 0) { |
| 112 | --startup_cycles_left_; |
| 113 | return; |
| 114 | } |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 115 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 116 | const double angle_rate = gyro_.ExtractAngle(result); |
| 117 | const double new_angle = angle_rate / static_cast<double>(kReadingRate); |
| 118 | auto message = gyro_reading_sender_.MakeMessage(); |
| 119 | if (zeroed_) { |
| 120 | angle_ += (new_angle + zero_offset_) * iterations; |
| 121 | message->angle = angle_; |
| 122 | message->velocity = angle_rate + zero_offset_ * kReadingRate; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 123 | AOS_LOG_STRUCT(DEBUG, "sending", *message); |
Austin Schuh | 5125e08 | 2015-04-18 22:56:04 -0700 | [diff] [blame] | 124 | message.Send(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 125 | } else { |
| 126 | // TODO(brian): Don't break without 6 seconds of standing still before |
| 127 | // enabling. Ideas: |
| 128 | // Don't allow driving until we have at least some data? |
| 129 | // Some kind of indicator light? |
| 130 | { |
| 131 | message->angle = new_angle; |
| 132 | message->velocity = angle_rate; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 133 | AOS_LOG_STRUCT(DEBUG, "collected while zeroing", *message); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 134 | message->angle = 0.0; |
| 135 | message->velocity = 0.0; |
| 136 | message.Send(); |
| 137 | } |
| 138 | zeroing_data_.AddData(new_angle); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 139 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 140 | joystick_state_fetcher_.Fetch(); |
| 141 | if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled && |
| 142 | zeroing_data_.full()) { |
| 143 | zero_offset_ = -zeroing_data_.GetAverage(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 144 | AOS_LOG(INFO, "total zero offset %f\n", zero_offset_); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 145 | zeroed_ = true; |
| 146 | } |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 147 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 148 | } break; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | } // namespace wpilib |
| 153 | } // namespace frc971 |