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 | |
| 10 | #include "aos/common/logging/logging.h" |
| 11 | #include "aos/common/logging/queue_logging.h" |
| 12 | #include "aos/common/util/phased_loop.h" |
| 13 | #include "aos/common/messages/robot_state.q.h" |
| 14 | #include "aos/common/time.h" |
| 15 | #include "aos/linux_code/init.h" |
| 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 | 268e13c | 2017-02-03 20:33:23 -0800 | [diff] [blame^] | 26 | GyroSender::GyroSender() { |
| 27 | PCHECK(system( |
| 28 | "ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p " |
| 29 | "33") == 0); |
| 30 | } |
| 31 | |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 32 | void GyroSender::operator()() { |
| 33 | ::aos::SetCurrentThreadName("Gyro"); |
| 34 | |
| 35 | // Try to initialize repeatedly as long as we're supposed to be running. |
| 36 | while (run_ && !gyro_.InitializeGyro()) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 37 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(50)); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 38 | } |
| 39 | LOG(INFO, "gyro initialized successfully\n"); |
| 40 | |
| 41 | auto message = ::frc971::sensors::gyro_part_id.MakeMessage(); |
| 42 | message->uid = gyro_.ReadPartID(); |
| 43 | LOG_STRUCT(INFO, "gyro ID", *message); |
| 44 | message.Send(); |
| 45 | |
| 46 | // In radians, ready to send out. |
| 47 | double angle = 0; |
| 48 | |
| 49 | int startup_cycles_left = 2 * kReadingRate; |
| 50 | |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 51 | zeroing::Averager<double, 6 * kReadingRate> zeroing_data; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 52 | bool zeroed = false; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 53 | double zero_offset = 0; |
| 54 | |
Austin Schuh | 268e13c | 2017-02-03 20:33:23 -0800 | [diff] [blame^] | 55 | ::aos::SetCurrentThreadRealtimePriority(33); |
| 56 | |
| 57 | ::aos::time::PhasedLoop phased_loop(::aos::time::FromRate(kReadingRate), |
| 58 | chrono::milliseconds(4)); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 59 | // How many timesteps the next reading represents. |
| 60 | int number_readings = 0; |
| 61 | |
Austin Schuh | 70ae593 | 2016-11-27 19:09:25 -0800 | [diff] [blame] | 62 | ::aos::SetCurrentThreadRealtimePriority(33); |
| 63 | |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 64 | while (run_) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 65 | number_readings += phased_loop.SleepUntilNext(); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 66 | |
| 67 | const uint32_t result = gyro_.GetReading(); |
| 68 | if (result == 0) { |
| 69 | LOG(WARNING, "normal gyro read failed\n"); |
| 70 | continue; |
| 71 | } |
| 72 | switch (gyro_.ExtractStatus(result)) { |
| 73 | case 0: |
| 74 | LOG(WARNING, "gyro says data is bad\n"); |
| 75 | continue; |
| 76 | case 1: |
| 77 | break; |
| 78 | default: |
| 79 | LOG(WARNING, "gyro gave weird status 0x%" PRIx8 "\n", |
| 80 | gyro_.ExtractStatus(result)); |
| 81 | continue; |
| 82 | } |
| 83 | if (gyro_.ExtractErrors(result) != 0) { |
| 84 | const uint8_t errors = gyro_.ExtractErrors(result); |
| 85 | if (errors & (1 << 6)) { |
| 86 | LOG(WARNING, "gyro gave PLL error\n"); |
| 87 | } |
| 88 | if (errors & (1 << 5)) { |
| 89 | LOG(WARNING, "gyro gave quadrature error\n"); |
| 90 | } |
| 91 | if (errors & (1 << 4)) { |
| 92 | LOG(WARNING, "gyro gave non-volatile memory error\n"); |
| 93 | } |
| 94 | if (errors & (1 << 3)) { |
| 95 | LOG(WARNING, "gyro gave volatile memory error\n"); |
| 96 | } |
| 97 | if (errors & (1 << 2)) { |
| 98 | LOG(WARNING, "gyro gave power error\n"); |
| 99 | } |
| 100 | if (errors & (1 << 1)) { |
| 101 | LOG(WARNING, "gyro gave continuous self-test error\n"); |
| 102 | } |
| 103 | if (errors & 1) { |
| 104 | LOG(WARNING, "gyro gave unexpected self-test mode\n"); |
| 105 | } |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | if (startup_cycles_left > 0) { |
| 110 | --startup_cycles_left; |
| 111 | continue; |
| 112 | } |
| 113 | |
Austin Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 114 | const double angle_rate = gyro_.ExtractAngle(result); |
| 115 | const double new_angle = angle_rate / static_cast<double>(kReadingRate); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 116 | auto message = ::frc971::sensors::gyro_reading.MakeMessage(); |
| 117 | if (zeroed) { |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 118 | angle += (new_angle + zero_offset) * number_readings; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 119 | message->angle = angle; |
Austin Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 120 | message->velocity = angle_rate + zero_offset * kReadingRate; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 121 | LOG_STRUCT(DEBUG, "sending", *message); |
| 122 | message.Send(); |
| 123 | } else { |
| 124 | // TODO(brian): Don't break without 6 seconds of standing still before |
| 125 | // enabling. Ideas: |
| 126 | // Don't allow driving until we have at least some data? |
| 127 | // Some kind of indicator light? |
| 128 | { |
| 129 | message->angle = new_angle; |
Austin Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 130 | message->velocity = angle_rate; |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 131 | LOG_STRUCT(DEBUG, "collected while zeroing", *message); |
Austin Schuh | 5125e08 | 2015-04-18 22:56:04 -0700 | [diff] [blame] | 132 | message->angle = 0.0; |
Austin Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 133 | message->velocity = 0.0; |
Austin Schuh | 5125e08 | 2015-04-18 22:56:04 -0700 | [diff] [blame] | 134 | message.Send(); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 135 | } |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 136 | zeroing_data.AddData(new_angle); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 137 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 138 | ::aos::joystick_state.FetchLatest(); |
| 139 | if (::aos::joystick_state.get() && ::aos::joystick_state->enabled && |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 140 | zeroing_data.full()) { |
| 141 | zero_offset = -zeroing_data.GetAverage(); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 142 | LOG(INFO, "total zero offset %f\n", zero_offset); |
| 143 | zeroed = true; |
| 144 | } |
| 145 | } |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 146 | number_readings = 0; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | } // namespace wpilib |
| 151 | } // namespace frc971 |