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