Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame^] | 1 | #include <libusb-1.0/libusb.h> |
| 2 | #include <memory> |
| 3 | |
| 4 | #include "aos/common/inttypes.h" |
| 5 | #include "aos/atom_code/init.h" |
| 6 | #include "aos/common/logging/logging.h" |
| 7 | #include "aos/common/time.h" |
| 8 | #include "aos/common/sensors/sensor_unpacker.h" |
| 9 | #include "aos/common/sensors/sensor_receiver.h" |
| 10 | |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "frc971/control_loops/wrist/wrist_motor.q.h" |
| 13 | #include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h" |
| 14 | #include "frc971/control_loops/index/index_motor.q.h" |
| 15 | #include "frc971/control_loops/shooter/shooter_motor.q.h" |
| 16 | #include "frc971/input/gyro_board_data.h" |
| 17 | #include "gyro_board/src/libusb-driver/libusb_wrap.h" |
| 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) { |
| 85 | data->NetworkToHost(); |
| 86 | LOG(DEBUG, "processing a packet\n"); |
| 87 | |
| 88 | static ::aos::time::Time last_time = ::aos::time::Time::Now(); |
| 89 | if ((last_time - ::aos::time::Time::Now()) > |
| 90 | ::aos::time::Time::InMS(0.00205)) { |
| 91 | LOG(INFO, "missed one\n"); |
| 92 | } |
| 93 | |
| 94 | gyro.MakeWithBuilder() |
| 95 | .angle(data->gyro_angle / 16.0 / 1000.0 / 180.0 * M_PI) |
| 96 | .Send(); |
| 97 | |
| 98 | UpdateWrappingCounter(data->top_rise_count, |
| 99 | &last_top_rise_count_, &top_rise_count_); |
| 100 | UpdateWrappingCounter(data->top_fall_count, |
| 101 | &last_top_fall_count_, &top_fall_count_); |
| 102 | UpdateWrappingCounter(data->bottom_rise_count, |
| 103 | &last_bottom_rise_count_, &bottom_rise_count_); |
| 104 | UpdateWrappingCounter(data->bottom_fall_delay_count, |
| 105 | &last_bottom_fall_delay_count_, &bottom_fall_delay_count_); |
| 106 | UpdateWrappingCounter(data->bottom_fall_count, |
| 107 | &last_bottom_fall_count_, &bottom_fall_count_); |
| 108 | UpdateWrappingCounter(data->wrist_rise_count, |
| 109 | &last_wrist_rise_count_, &wrist_rise_count_); |
| 110 | UpdateWrappingCounter(data->shooter_angle_rise_count, |
| 111 | &last_shooter_angle_rise_count_, &shooter_angle_rise_count_); |
| 112 | |
| 113 | drivetrain.position.MakeWithBuilder() |
| 114 | .right_encoder(-drivetrain_translate(data->right_drive)) |
| 115 | .left_encoder(drivetrain_translate(data->left_drive)) |
| 116 | .Send(); |
| 117 | |
| 118 | wrist.position.MakeWithBuilder() |
| 119 | .pos(wrist_translate(data->wrist)) |
| 120 | .hall_effect(!data->wrist_hall_effect) |
| 121 | .calibration(wrist_translate(data->capture_wrist_rise)) |
| 122 | .Send(); |
| 123 | |
| 124 | angle_adjust.position.MakeWithBuilder() |
| 125 | .angle(angle_adjust_translate(data->shooter_angle)) |
| 126 | .bottom_hall_effect(!data->angle_adjust_bottom_hall_effect) |
| 127 | .middle_hall_effect(false) |
| 128 | .bottom_calibration(angle_adjust_translate( |
| 129 | data->capture_shooter_angle_rise)) |
| 130 | .middle_calibration(angle_adjust_translate( |
| 131 | 0)) |
| 132 | .Send(); |
| 133 | |
| 134 | shooter.position.MakeWithBuilder() |
| 135 | .position(shooter_translate(data->shooter)) |
| 136 | .Send(); |
| 137 | |
| 138 | index_loop.position.MakeWithBuilder() |
| 139 | .index_position(index_translate(data->indexer)) |
| 140 | .top_disc_detect(!data->top_disc) |
| 141 | .top_disc_posedge_count(top_rise_count_) |
| 142 | .top_disc_posedge_position(index_translate(data->capture_top_rise)) |
| 143 | .top_disc_negedge_count(top_fall_count_) |
| 144 | .top_disc_negedge_position(index_translate(data->capture_top_fall)) |
| 145 | .bottom_disc_detect(!data->bottom_disc) |
| 146 | .bottom_disc_posedge_count(bottom_rise_count_) |
| 147 | .bottom_disc_negedge_count(bottom_fall_count_) |
| 148 | .bottom_disc_negedge_wait_position(index_translate( |
| 149 | data->capture_bottom_fall_delay)) |
| 150 | .bottom_disc_negedge_wait_count(bottom_fall_delay_count_) |
| 151 | .Send(); |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | void UpdateWrappingCounter( |
| 156 | uint8_t current, uint8_t *last, int32_t *counter) { |
| 157 | if (*last > current) { |
| 158 | *counter += 0x100; |
| 159 | } |
| 160 | *counter = (*counter & 0xffffff00) | current; |
| 161 | *last = current; |
| 162 | } |
| 163 | |
| 164 | int32_t top_rise_count_; |
| 165 | uint8_t last_top_rise_count_; |
| 166 | int32_t top_fall_count_; |
| 167 | uint8_t last_top_fall_count_; |
| 168 | int32_t bottom_rise_count_; |
| 169 | uint8_t last_bottom_rise_count_; |
| 170 | int32_t bottom_fall_delay_count_; |
| 171 | uint8_t last_bottom_fall_delay_count_; |
| 172 | int32_t bottom_fall_count_; |
| 173 | uint8_t last_bottom_fall_count_; |
| 174 | int32_t wrist_rise_count_; |
| 175 | uint8_t last_wrist_rise_count_; |
| 176 | int32_t shooter_angle_rise_count_; |
| 177 | uint8_t last_shooter_angle_rise_count_; |
| 178 | }; |
| 179 | |
| 180 | class GyroSensorReceiver : |
| 181 | public ::aos::sensors::SensorReceiver<GyroBoardData> { |
| 182 | public: |
| 183 | GyroSensorReceiver( |
| 184 | ::aos::sensors::SensorUnpackerInterface<GyroBoardData> *unpacker) |
| 185 | : ::aos::sensors::SensorReceiver<GyroBoardData>(unpacker), |
| 186 | start_time_(0, 0) { |
| 187 | static_assert(sizeof(GyroBoardData) <= sizeof(data_), |
| 188 | "the buffer is too small"); |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | static const unsigned char kEndpoint = 0x81; |
| 193 | // in ms |
| 194 | // 0 is unlimited |
| 195 | static const unsigned int kReadTimeout = 1000; |
| 196 | |
| 197 | // vendor ID |
| 198 | static const int32_t kVid = 0x1424; |
| 199 | // product ID |
| 200 | static const int32_t kPid = 0xd243; |
| 201 | |
| 202 | virtual void DoReceiveData() { |
| 203 | // Loop and then return once we get a good one. |
| 204 | while (true) { |
| 205 | int read_bytes; |
| 206 | int r = dev_handle_->interrupt_transfer( |
| 207 | kEndpoint, data_, sizeof(data_), &read_bytes, kReadTimeout); |
| 208 | |
| 209 | if (r != 0) { |
| 210 | if (r == LIBUSB_ERROR_TIMEOUT) { |
| 211 | LOG(ERROR, "read timed out\n"); |
| 212 | continue; |
| 213 | } |
| 214 | LOG(FATAL, "libusb gave error %d\n", r); |
| 215 | } |
| 216 | |
| 217 | if (read_bytes < static_cast<ssize_t>(sizeof(GyroBoardData))) { |
| 218 | LOG(ERROR, "read %d bytes instead of at least %zd\n", |
| 219 | read_bytes, sizeof(GyroBoardData)); |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | memcpy(&data()->values, data_, sizeof(GyroBoardData)); |
| 224 | if (data()->count == 0) { |
| 225 | start_time_ = ::aos::time::Time::Now(); |
| 226 | data()->count = 1; |
| 227 | } else { |
| 228 | ::aos::time::Time delta_time = ::aos::time::Time::Now() - start_time_; |
| 229 | data()->count = static_cast<int32_t>( |
| 230 | (delta_time / ::aos::sensors::kSensorSendFrequency) + 0.5); |
| 231 | } |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | virtual void Reset() { |
| 237 | dev_handle_ = ::std::unique_ptr<LibUSBDeviceHandle>( |
| 238 | libusb_.FindDeviceWithVIDPID(kVid, kPid)); |
| 239 | if (!dev_handle_) { |
| 240 | LOG(FATAL, "couldn't find device\n"); |
| 241 | } |
| 242 | data()->count = 0; |
| 243 | } |
| 244 | |
| 245 | virtual void Synchronized(::aos::time::Time start_time) { |
| 246 | LOG(INFO, "offset %f\n", (::aos::time::Time::Now() - start_time).ToSeconds()); |
| 247 | start_time_ = start_time - |
| 248 | ::aos::sensors::kSensorSendFrequency * data()->count; |
| 249 | } |
| 250 | |
| 251 | ::std::unique_ptr<LibUSBDeviceHandle> dev_handle_; |
| 252 | |
| 253 | ::aos::time::Time start_time_; |
| 254 | |
| 255 | LibUSB libusb_; |
| 256 | |
| 257 | uint8_t data_[64]; |
| 258 | }; |
| 259 | |
| 260 | } // namespace frc971 |
| 261 | |
| 262 | int main() { |
| 263 | ::aos::Init(); |
| 264 | ::frc971::GyroSensorUnpacker unpacker; |
| 265 | ::frc971::GyroSensorReceiver receiver(&unpacker); |
| 266 | while (true) { |
| 267 | receiver.RunIteration(); |
| 268 | } |
| 269 | ::aos::Cleanup(); |
| 270 | } |