Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 1 | #include <memory> |
| 2 | |
| 3 | #include "aos/common/inttypes.h" |
| 4 | #include "aos/atom_code/init.h" |
| 5 | #include "aos/common/logging/logging.h" |
| 6 | #include "aos/common/time.h" |
| 7 | #include "aos/common/sensors/sensor_unpacker.h" |
| 8 | #include "aos/common/sensors/sensor_receiver.h" |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 9 | #include "aos/common/glibusb/glibusb.h" |
| 10 | #include "aos/common/glibusb/gbuffer.h" |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 11 | |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 13 | #include "frc971/control_loops/wrist/wrist_motor.q.h" |
| 14 | #include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h" |
| 15 | #include "frc971/control_loops/index/index_motor.q.h" |
| 16 | #include "frc971/control_loops/shooter/shooter_motor.q.h" |
| 17 | #include "frc971/input/gyro_board_data.h" |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 18 | #include "frc971/queues/GyroAngle.q.h" |
| 19 | |
| 20 | #ifndef M_PI |
| 21 | #define M_PI 3.14159265358979323846 |
| 22 | #endif |
| 23 | |
| 24 | using ::frc971::control_loops::drivetrain; |
| 25 | using ::frc971::control_loops::wrist; |
| 26 | using ::frc971::control_loops::angle_adjust; |
| 27 | using ::frc971::control_loops::shooter; |
| 28 | using ::frc971::control_loops::index_loop; |
| 29 | using ::frc971::sensors::gyro; |
| 30 | |
| 31 | namespace frc971 { |
| 32 | namespace { |
| 33 | |
| 34 | inline double drivetrain_translate(int32_t in) { |
| 35 | return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) * |
| 36 | (19.0 / 50.0) /*output reduction*/ * (64.0 / 24.0) /*encoder gears*/ * |
| 37 | (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI); |
| 38 | } |
| 39 | |
| 40 | inline double wrist_translate(int32_t in) { |
| 41 | return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) * |
| 42 | (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI); |
| 43 | } |
| 44 | |
| 45 | inline double angle_adjust_translate(int32_t in) { |
| 46 | static const double kCableDiameter = 0.060; |
| 47 | return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) * |
| 48 | ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ * |
| 49 | (2 * M_PI); |
| 50 | } |
| 51 | |
| 52 | inline double shooter_translate(int32_t in) { |
| 53 | return static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) * |
| 54 | (15.0 / 34.0) /*gears*/ * (2 * M_PI); |
| 55 | } |
| 56 | |
| 57 | inline double index_translate(int32_t in) { |
| 58 | return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*quad*/) * |
| 59 | (1.0) /*gears*/ * (2 * M_PI); |
| 60 | } |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | class GyroSensorUnpacker : |
| 65 | public ::aos::sensors::SensorUnpackerInterface<GyroBoardData> { |
| 66 | public: |
| 67 | GyroSensorUnpacker() |
| 68 | : top_rise_count_(0), |
| 69 | last_top_rise_count_(0), |
| 70 | top_fall_count_(0), |
| 71 | last_top_fall_count_(0), |
| 72 | bottom_rise_count_(0), |
| 73 | last_bottom_rise_count_(0), |
| 74 | bottom_fall_delay_count_(0), |
| 75 | last_bottom_fall_delay_count_(0), |
| 76 | bottom_fall_count_(0), |
| 77 | last_bottom_fall_count_(0), |
| 78 | wrist_rise_count_(0), |
| 79 | last_wrist_rise_count_(0), |
| 80 | shooter_angle_rise_count_(0), |
| 81 | last_shooter_angle_rise_count_(0) { |
| 82 | } |
| 83 | |
| 84 | void UnpackFrom(GyroBoardData *data) { |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 85 | if (data->robot_id != 0) { |
| 86 | LOG(ERROR, "gyro board sent data for robot id %hhd!" |
Brian Silverman | b245127 | 2013-10-11 21:42:22 -0700 | [diff] [blame^] | 87 | " dip switches are %x\n", data->robot_id, data->base_status & 0xF); |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 88 | return; |
| 89 | } else { |
Brian Silverman | b245127 | 2013-10-11 21:42:22 -0700 | [diff] [blame^] | 90 | LOG(DEBUG, "processing a packet dip switches %x\n", |
| 91 | data->base_status & 0xF); |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 92 | } |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 93 | |
| 94 | static ::aos::time::Time last_time = ::aos::time::Time::Now(); |
| 95 | if ((last_time - ::aos::time::Time::Now()) > |
| 96 | ::aos::time::Time::InMS(0.00205)) { |
| 97 | LOG(INFO, "missed one\n"); |
| 98 | } |
| 99 | |
| 100 | gyro.MakeWithBuilder() |
| 101 | .angle(data->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI) |
| 102 | .Send(); |
| 103 | |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 104 | UpdateWrappingCounter(data->main.top_rise_count, |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 105 | &last_top_rise_count_, &top_rise_count_); |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 106 | UpdateWrappingCounter(data->main.top_fall_count, |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 107 | &last_top_fall_count_, &top_fall_count_); |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 108 | UpdateWrappingCounter(data->main.bottom_rise_count, |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 109 | &last_bottom_rise_count_, &bottom_rise_count_); |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 110 | UpdateWrappingCounter(data->main.bottom_fall_delay_count, |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 111 | &last_bottom_fall_delay_count_, &bottom_fall_delay_count_); |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 112 | UpdateWrappingCounter(data->main.bottom_fall_count, |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 113 | &last_bottom_fall_count_, &bottom_fall_count_); |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 114 | UpdateWrappingCounter(data->main.wrist_rise_count, |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 115 | &last_wrist_rise_count_, &wrist_rise_count_); |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 116 | UpdateWrappingCounter(data->main.shooter_angle_rise_count, |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 117 | &last_shooter_angle_rise_count_, &shooter_angle_rise_count_); |
| 118 | |
| 119 | drivetrain.position.MakeWithBuilder() |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 120 | .right_encoder(drivetrain_translate(data->main.right_drive)) |
| 121 | .left_encoder(-drivetrain_translate(data->main.left_drive)) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 122 | .Send(); |
| 123 | |
| 124 | wrist.position.MakeWithBuilder() |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 125 | .pos(wrist_translate(data->main.wrist)) |
| 126 | .hall_effect(!data->main.wrist_hall_effect) |
| 127 | .calibration(wrist_translate(data->main.capture_wrist_rise)) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 128 | .Send(); |
| 129 | |
| 130 | angle_adjust.position.MakeWithBuilder() |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 131 | .angle(angle_adjust_translate(data->main.shooter_angle)) |
| 132 | .bottom_hall_effect(!data->main.angle_adjust_bottom_hall_effect) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 133 | .middle_hall_effect(false) |
| 134 | .bottom_calibration(angle_adjust_translate( |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 135 | data->main.capture_shooter_angle_rise)) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 136 | .middle_calibration(angle_adjust_translate( |
| 137 | 0)) |
| 138 | .Send(); |
| 139 | |
| 140 | shooter.position.MakeWithBuilder() |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 141 | .position(shooter_translate(data->main.shooter)) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 142 | .Send(); |
| 143 | |
| 144 | index_loop.position.MakeWithBuilder() |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 145 | .index_position(index_translate(data->main.indexer)) |
| 146 | .top_disc_detect(!data->main.top_disc) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 147 | .top_disc_posedge_count(top_rise_count_) |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 148 | .top_disc_posedge_position(index_translate(data->main.capture_top_rise)) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 149 | .top_disc_negedge_count(top_fall_count_) |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 150 | .top_disc_negedge_position(index_translate(data->main.capture_top_fall)) |
| 151 | .bottom_disc_detect(!data->main.bottom_disc) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 152 | .bottom_disc_posedge_count(bottom_rise_count_) |
| 153 | .bottom_disc_negedge_count(bottom_fall_count_) |
| 154 | .bottom_disc_negedge_wait_position(index_translate( |
Brian Silverman | f92396c | 2013-09-12 20:13:13 -0700 | [diff] [blame] | 155 | data->main.capture_bottom_fall_delay)) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 156 | .bottom_disc_negedge_wait_count(bottom_fall_delay_count_) |
Brian Silverman | b245127 | 2013-10-11 21:42:22 -0700 | [diff] [blame^] | 157 | .loader_top(data->main.loader_top) |
| 158 | .loader_bottom(data->main.loader_bottom) |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 159 | .Send(); |
| 160 | } |
| 161 | |
| 162 | private: |
| 163 | void UpdateWrappingCounter( |
| 164 | uint8_t current, uint8_t *last, int32_t *counter) { |
| 165 | if (*last > current) { |
| 166 | *counter += 0x100; |
| 167 | } |
| 168 | *counter = (*counter & 0xffffff00) | current; |
| 169 | *last = current; |
| 170 | } |
| 171 | |
| 172 | int32_t top_rise_count_; |
| 173 | uint8_t last_top_rise_count_; |
| 174 | int32_t top_fall_count_; |
| 175 | uint8_t last_top_fall_count_; |
| 176 | int32_t bottom_rise_count_; |
| 177 | uint8_t last_bottom_rise_count_; |
| 178 | int32_t bottom_fall_delay_count_; |
| 179 | uint8_t last_bottom_fall_delay_count_; |
| 180 | int32_t bottom_fall_count_; |
| 181 | uint8_t last_bottom_fall_count_; |
| 182 | int32_t wrist_rise_count_; |
| 183 | uint8_t last_wrist_rise_count_; |
| 184 | int32_t shooter_angle_rise_count_; |
| 185 | uint8_t last_shooter_angle_rise_count_; |
| 186 | }; |
| 187 | |
| 188 | class GyroSensorReceiver : |
| 189 | public ::aos::sensors::SensorReceiver<GyroBoardData> { |
| 190 | public: |
| 191 | GyroSensorReceiver( |
| 192 | ::aos::sensors::SensorUnpackerInterface<GyroBoardData> *unpacker) |
| 193 | : ::aos::sensors::SensorReceiver<GyroBoardData>(unpacker), |
| 194 | start_time_(0, 0) { |
Brian Silverman | 9a574d5 | 2013-03-31 00:53:53 -0700 | [diff] [blame] | 195 | static_assert(sizeof(GyroBoardData) <= kDataLength, |
| 196 | "the buffer will be too small"); |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | private: |
| 200 | static const unsigned char kEndpoint = 0x81; |
| 201 | // in ms |
| 202 | // 0 is unlimited |
| 203 | static const unsigned int kReadTimeout = 1000; |
| 204 | |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 205 | static constexpr glibusb::VendorProductId kDeviceId = |
| 206 | glibusb::VendorProductId(0x1424 /* vendor ID */, |
| 207 | 0xd243 /* product ID */); |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 208 | |
Brian Silverman | 9a574d5 | 2013-03-31 00:53:53 -0700 | [diff] [blame] | 209 | // How big of a buffer to give the function. |
| 210 | static const size_t kDataLength = 64; |
| 211 | |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 212 | virtual bool DoReceiveData() { |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 213 | // Loop and then return once we get a good one. |
| 214 | while (true) { |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 215 | using glibusb::UsbEndpoint; |
| 216 | UsbEndpoint::IoStatus result = |
| 217 | endpoint_->ReadAtMostWithTimeout(sizeof(GyroBoardData), |
| 218 | kReadTimeout, |
| 219 | &buffer_); |
| 220 | switch (result) { |
| 221 | case UsbEndpoint::kSuccess: |
| 222 | break; |
| 223 | case UsbEndpoint::kTimeout: |
| 224 | LOG(WARNING, "read timed out\n"); |
| 225 | continue; |
| 226 | case UsbEndpoint::kNoDevice: |
| 227 | LOG(ERROR, "no device\n"); |
| 228 | return true; |
| 229 | case UsbEndpoint::kUnknown: |
| 230 | case UsbEndpoint::kFail: |
| 231 | case UsbEndpoint::kAbort: |
| 232 | LOG(ERROR, "read failed\n"); |
| 233 | continue; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 234 | } |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 235 | if (buffer_.Length() < sizeof(GyroBoardData)) { |
| 236 | LOG(ERROR, "read %zd bytes instead of at least %zd\n", |
| 237 | buffer_.Length(), sizeof(GyroBoardData)); |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 238 | continue; |
| 239 | } |
| 240 | |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 241 | memcpy(&data()->values, buffer_.GetBufferPointer(sizeof(GyroBoardData)), |
Brian Silverman | 9a574d5 | 2013-03-31 00:53:53 -0700 | [diff] [blame] | 242 | sizeof(GyroBoardData)); |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 243 | if (data()->count == 0) { |
| 244 | start_time_ = ::aos::time::Time::Now(); |
| 245 | data()->count = 1; |
| 246 | } else { |
| 247 | ::aos::time::Time delta_time = ::aos::time::Time::Now() - start_time_; |
| 248 | data()->count = static_cast<int32_t>( |
| 249 | (delta_time / ::aos::sensors::kSensorSendFrequency) + 0.5); |
| 250 | } |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 251 | return false; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | virtual void Reset() { |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 256 | device_ = ::std::unique_ptr<glibusb::UsbDevice>( |
| 257 | libusb_.FindSingleMatchingDeviceOrLose(kDeviceId)); |
| 258 | CHECK(device_); |
| 259 | endpoint_ = ::std::unique_ptr<glibusb::UsbInEndpoint>( |
| 260 | device_->InEndpoint(kEndpoint)); |
Brian Silverman | 9a574d5 | 2013-03-31 00:53:53 -0700 | [diff] [blame] | 261 | |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 262 | data()->count = 0; |
| 263 | } |
| 264 | |
| 265 | virtual void Synchronized(::aos::time::Time start_time) { |
Brian Silverman | 9a574d5 | 2013-03-31 00:53:53 -0700 | [diff] [blame] | 266 | // Subtract off how many packets it read while synchronizing from the time. |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 267 | start_time_ = start_time - |
| 268 | ::aos::sensors::kSensorSendFrequency * data()->count; |
| 269 | } |
| 270 | |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 271 | ::std::unique_ptr<glibusb::UsbDevice> device_; |
| 272 | ::std::unique_ptr<glibusb::UsbInEndpoint> endpoint_; |
| 273 | glibusb::Buffer buffer_; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 274 | |
| 275 | ::aos::time::Time start_time_; |
| 276 | |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 277 | glibusb::Libusb libusb_; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 278 | }; |
Brian Silverman | 93871ee | 2013-09-14 18:15:28 -0700 | [diff] [blame] | 279 | constexpr glibusb::VendorProductId GyroSensorReceiver::kDeviceId; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 280 | |
| 281 | } // namespace frc971 |
| 282 | |
| 283 | int main() { |
| 284 | ::aos::Init(); |
| 285 | ::frc971::GyroSensorUnpacker unpacker; |
| 286 | ::frc971::GyroSensorReceiver receiver(&unpacker); |
| 287 | while (true) { |
| 288 | receiver.RunIteration(); |
| 289 | } |
| 290 | ::aos::Cleanup(); |
| 291 | } |