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" |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 16 | #include "aos/util/phased_loop.h" |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 17 | |
| 18 | #include "frc971/queues/gyro.q.h" |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 19 | #include "frc971/zeroing/averager.h" |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 20 | |
| 21 | namespace frc971 { |
| 22 | namespace wpilib { |
| 23 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 24 | namespace chrono = ::std::chrono; |
| 25 | using ::aos::monotonic_clock; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 26 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 27 | GyroSender::GyroSender(::aos::EventLoop *event_loop) |
| 28 | : event_loop_(event_loop), |
| 29 | joystick_state_fetcher_(event_loop_->MakeFetcher<::aos::JoystickState>( |
Austin Schuh | cc1010e | 2019-05-12 20:38:01 -0700 | [diff] [blame^] | 30 | ".aos.joystick_state")), |
| 31 | uid_sender_(event_loop_->MakeSender<::frc971::sensors::Uid>( |
| 32 | ".frc971.sensors.gyro_part_id")) { |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 33 | PCHECK( |
| 34 | system("ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p " |
Austin Schuh | 268e13c | 2017-02-03 20:33:23 -0800 | [diff] [blame] | 35 | "33") == 0); |
| 36 | } |
| 37 | |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 38 | void 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 Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 43 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(50)); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 44 | } |
| 45 | LOG(INFO, "gyro initialized successfully\n"); |
| 46 | |
Austin Schuh | cc1010e | 2019-05-12 20:38:01 -0700 | [diff] [blame^] | 47 | auto message = uid_sender_.MakeMessage(); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 48 | 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 Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 57 | zeroing::Averager<double, 6 * kReadingRate> zeroing_data; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 58 | bool zeroed = false; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 59 | double zero_offset = 0; |
| 60 | |
Austin Schuh | 268e13c | 2017-02-03 20:33:23 -0800 | [diff] [blame] | 61 | ::aos::SetCurrentThreadRealtimePriority(33); |
| 62 | |
| 63 | ::aos::time::PhasedLoop phased_loop(::aos::time::FromRate(kReadingRate), |
| 64 | chrono::milliseconds(4)); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 65 | // How many timesteps the next reading represents. |
| 66 | int number_readings = 0; |
| 67 | |
Austin Schuh | 70ae593 | 2016-11-27 19:09:25 -0800 | [diff] [blame] | 68 | ::aos::SetCurrentThreadRealtimePriority(33); |
| 69 | |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 70 | while (run_) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 71 | number_readings += phased_loop.SleepUntilNext(); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 72 | |
| 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 Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 120 | const double angle_rate = gyro_.ExtractAngle(result); |
| 121 | const double new_angle = angle_rate / static_cast<double>(kReadingRate); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 122 | auto message = ::frc971::sensors::gyro_reading.MakeMessage(); |
| 123 | if (zeroed) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 124 | angle += (new_angle + zero_offset) * number_readings; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 125 | message->angle = angle; |
Austin Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 126 | message->velocity = angle_rate + zero_offset * kReadingRate; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 127 | 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 Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 136 | message->velocity = angle_rate; |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 137 | LOG_STRUCT(DEBUG, "collected while zeroing", *message); |
Austin Schuh | 5125e08 | 2015-04-18 22:56:04 -0700 | [diff] [blame] | 138 | message->angle = 0.0; |
Austin Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 139 | message->velocity = 0.0; |
Austin Schuh | 5125e08 | 2015-04-18 22:56:04 -0700 | [diff] [blame] | 140 | message.Send(); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 141 | } |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 142 | zeroing_data.AddData(new_angle); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 143 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 144 | joystick_state_fetcher_.Fetch(); |
| 145 | if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled && |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 146 | zeroing_data.full()) { |
| 147 | zero_offset = -zeroing_data.GetAverage(); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 148 | LOG(INFO, "total zero offset %f\n", zero_offset); |
| 149 | zeroed = true; |
| 150 | } |
| 151 | } |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 152 | number_readings = 0; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | } // namespace wpilib |
| 157 | } // namespace frc971 |