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