Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 1 | #include "get.h" |
| 2 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 4 | #include <unistd.h> |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 5 | #include <inttypes.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include <queue> |
| 9 | #include <iostream> |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 10 | #include <map> |
| 11 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 12 | #include <google/gflags.h> |
| 13 | #include "aos/common/mutex.h" |
| 14 | #include "aos/common/logging/logging_impl.h" |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 15 | |
| 16 | DEFINE_int32(vid, 0x1424, "Vendor ID of the device."); |
| 17 | DEFINE_int32(pid, 0xd243, "Product ID of the device."); |
| 18 | DEFINE_bool(send, true, |
| 19 | "True if the code should send packets. (default: true)"); |
| 20 | |
| 21 | // USB descriptors in use: |
| 22 | // 0x81 Interrupt IN endpoint with the received CAN packets. |
| 23 | // These packets are 64 bytes maximum, and have the same format as |
| 24 | // SocketCAN. |
| 25 | // 0x04 Interrupt OUT endpoint with the CAN packets to send. |
| 26 | // These packets are 64 bytes maximum, and have the same format as |
| 27 | // SocketCAN. |
| 28 | // 0x82 Bulk IN endpoint with printf output. |
| 29 | // 0x05 Bulk OUT endpoint for stdin. |
| 30 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 31 | class GyroDriver::PacketReceiver : public ::aos::util::Thread { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 32 | public: |
| 33 | // refusing to send any more frames. |
| 34 | static const int kMaxRXFrames = 128; |
| 35 | |
| 36 | explicit PacketReceiver(LibUSBDeviceHandle *dev_handle) |
Austin Schuh | 09f82df | 2013-03-30 10:13:54 +0000 | [diff] [blame] | 37 | : Thread(), dev_handle_(dev_handle) {} |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 38 | |
| 39 | // Serve. |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 40 | virtual void Run(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 41 | |
| 42 | bool GetCanFrame(struct can_frame *msg); |
| 43 | |
| 44 | private: |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 45 | LibUSBDeviceHandle *dev_handle_; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 46 | ::aos::Mutex rx_mutex_; |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | GyroDriver::GyroDriver(LibUSBDeviceHandle *dev_handle) |
| 50 | : dev_handle_(dev_handle), dbg_(new DbgReader(this)), |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 51 | rx_(new GyroDriver::PacketReceiver(dev_handle)) {} |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | std::string GyroDriver::GetDebugData() { |
| 55 | char data[64]; |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 56 | int transferred; |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame^] | 57 | dev_handle_->bulk_transfer(0x82, |
| 58 | (unsigned char *)data, sizeof(data), |
| 59 | &transferred, 0); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 60 | return std::string(data, transferred); |
| 61 | } |
| 62 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 63 | GyroDriver::~GyroDriver() { |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 64 | rx_->Join(); |
| 65 | dbg_->Join(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | //bool GyroDriver::GetCanFrame(struct can_frame *msg) { |
| 69 | // return rx_->GetCanFrame(msg); |
| 70 | //} |
| 71 | |
| 72 | struct DataStruct{ |
| 73 | int64_t gyro_angle; |
| 74 | |
| 75 | int32_t right_drive; |
| 76 | int32_t left_drive; |
| 77 | int32_t shooter_angle; |
| 78 | int32_t shooter; |
| 79 | int32_t indexer; |
| 80 | int32_t wrist; |
| 81 | |
| 82 | int32_t capture_top_rise; |
| 83 | int32_t capture_top_fall; |
| 84 | int32_t capture_bottom_fall_delay; |
| 85 | int32_t capture_wrist_rise; |
| 86 | int32_t capture_shooter_angle_rise; |
| 87 | |
| 88 | int8_t top_rise_count; |
| 89 | |
| 90 | int8_t top_fall_count; |
| 91 | |
| 92 | int8_t bottom_rise_count; |
| 93 | |
| 94 | int8_t bottom_fall_delay_count; |
| 95 | int8_t bottom_fall_count; |
| 96 | |
| 97 | int8_t wrist_rise_count; |
| 98 | |
| 99 | int8_t shooter_angle_rise_count; |
| 100 | } __attribute__((__packed__)); |
| 101 | |
Brian Silverman | c3c0629 | 2013-03-30 18:54:31 -0700 | [diff] [blame] | 102 | void GyroDriver::Start() { |
| 103 | rx_->Start(); |
| 104 | } |
| 105 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 106 | void GyroDriver::PacketReceiver::Run() { |
| 107 | int r; |
| 108 | int actual; |
| 109 | uint8_t data[64]; |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 110 | DataStruct *real_data; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 111 | static_assert(sizeof(*real_data) <= sizeof(data), "it doesn't fit"); |
| 112 | |
| 113 | uint8_t *data_pointer = data; |
| 114 | memcpy(&real_data, &data_pointer, sizeof(data_pointer)); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 115 | |
root | 11a49d0 | 2013-03-30 06:27:47 +0000 | [diff] [blame] | 116 | int count = 0; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 117 | while (should_continue()) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 118 | r = dev_handle_->interrupt_transfer( |
| 119 | 0x81, data, sizeof(data), &actual, 1000); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 120 | if (actual <= 0) { |
| 121 | LOG(FATAL, "didn't get any data\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 124 | if (r != 0) { |
| 125 | LOG(ERROR, "Read Error. Read %d\n", actual); |
| 126 | } |
root | 11a49d0 | 2013-03-30 06:27:47 +0000 | [diff] [blame] | 127 | |
| 128 | ++count; |
| 129 | if (count < 100) continue; |
| 130 | count = 0; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 131 | |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame^] | 132 | printf("angle: %" PRId64 "\n", real_data->gyro_angle); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 133 | printf("drivel: %d\n", real_data->left_drive); |
| 134 | printf("driver: %d\n", real_data->right_drive); |
| 135 | printf("shooter: %d\n", real_data->shooter); |
| 136 | printf("shooter_angle: %d\n", real_data->shooter_angle); |
| 137 | printf("indexer: %d\n", real_data->indexer); |
| 138 | printf("wrist: %d\n", real_data->wrist); |
| 139 | printf("capture:\n"); |
| 140 | printf(" wrist_rise{%d}: %d\n", real_data->wrist_rise_count, |
| 141 | real_data->capture_wrist_rise); |
| 142 | printf(" shooter_angle_rise{%d}: %d\n", real_data->shooter_angle_rise_count, |
| 143 | real_data->capture_shooter_angle_rise); |
| 144 | printf(" bottom_rise_delay{%d}; \n", real_data->bottom_rise_count); |
| 145 | printf(" bottom_fall_delay{%d,%d}: %d\n", real_data->bottom_fall_count, |
| 146 | real_data->bottom_fall_delay_count, |
| 147 | real_data->capture_bottom_fall_delay); |
| 148 | printf(" top_rise{%d}: %d\n", real_data->top_rise_count, |
| 149 | real_data->capture_top_rise); |
| 150 | printf(" top_fall{%d}: %d\n", real_data->top_fall_count, |
| 151 | real_data->capture_top_fall); |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | //if (actual > 1) { |
| 157 | // rx_cond_.notify_all(); |
| 158 | //} |
| 159 | } |
| 160 | //rx_cond_.notify_all(); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 161 | LOG(INFO, "Receive thread down."); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | DbgReader::DbgReader(GyroDriver *gyro) |
| 165 | : Thread(), gyro_(gyro) {} |
| 166 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 167 | void DbgReader::Run() { |
| 168 | LOG(INFO, "Running debug dump thread."); |
| 169 | while (should_continue()) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 170 | printf("%s", gyro_->GetDebugData().c_str()); |
| 171 | } |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 172 | LOG(INFO, "Exiting debug dump thread."); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
| 176 | int main(int argc, char ** argv) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 177 | google::ParseCommandLineFlags(&argc, &argv, true); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 178 | ::aos::logging::Init(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 179 | |
| 180 | LibUSB libusb; |
| 181 | |
| 182 | { |
| 183 | std::unique_ptr<LibUSBDeviceHandle> dev_handle( |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 184 | libusb.FindDeviceWithVIDPID(FLAGS_vid, FLAGS_pid)); |
| 185 | if (!dev_handle) { |
| 186 | LOG(FATAL, "couldn't find device\n"); |
| 187 | } |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 188 | |
| 189 | GyroDriver gyro(dev_handle.release()); |
Brian Silverman | c3c0629 | 2013-03-30 18:54:31 -0700 | [diff] [blame] | 190 | gyro.Start(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 191 | |
| 192 | while(true){ |
| 193 | sleep(50); |
| 194 | } |
| 195 | } |
| 196 | return 0; |
| 197 | } |