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]; |
| 56 | int r; |
| 57 | int transferred; |
| 58 | r = dev_handle_->bulk_transfer(0x82, |
| 59 | (unsigned char *)data, sizeof(data), |
| 60 | &transferred, 0); |
| 61 | return std::string(data, transferred); |
| 62 | } |
| 63 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 64 | GyroDriver::~GyroDriver() { |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 65 | rx_->Join(); |
| 66 | dbg_->Join(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | //bool GyroDriver::GetCanFrame(struct can_frame *msg) { |
| 70 | // return rx_->GetCanFrame(msg); |
| 71 | //} |
| 72 | |
| 73 | struct DataStruct{ |
| 74 | int64_t gyro_angle; |
| 75 | |
| 76 | int32_t right_drive; |
| 77 | int32_t left_drive; |
| 78 | int32_t shooter_angle; |
| 79 | int32_t shooter; |
| 80 | int32_t indexer; |
| 81 | int32_t wrist; |
| 82 | |
| 83 | int32_t capture_top_rise; |
| 84 | int32_t capture_top_fall; |
| 85 | int32_t capture_bottom_fall_delay; |
| 86 | int32_t capture_wrist_rise; |
| 87 | int32_t capture_shooter_angle_rise; |
| 88 | |
| 89 | int8_t top_rise_count; |
| 90 | |
| 91 | int8_t top_fall_count; |
| 92 | |
| 93 | int8_t bottom_rise_count; |
| 94 | |
| 95 | int8_t bottom_fall_delay_count; |
| 96 | int8_t bottom_fall_count; |
| 97 | |
| 98 | int8_t wrist_rise_count; |
| 99 | |
| 100 | int8_t shooter_angle_rise_count; |
| 101 | } __attribute__((__packed__)); |
| 102 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 103 | void GyroDriver::PacketReceiver::Run() { |
| 104 | int r; |
| 105 | int actual; |
| 106 | uint8_t data[64]; |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 107 | DataStruct *real_data; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 108 | static_assert(sizeof(*real_data) <= sizeof(data), "it doesn't fit"); |
| 109 | |
| 110 | uint8_t *data_pointer = data; |
| 111 | memcpy(&real_data, &data_pointer, sizeof(data_pointer)); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 112 | |
root | 11a49d0 | 2013-03-30 06:27:47 +0000 | [diff] [blame] | 113 | int count = 0; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 114 | while (should_continue()) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 115 | r = dev_handle_->interrupt_transfer( |
| 116 | 0x81, data, sizeof(data), &actual, 1000); |
| 117 | printf("size: %d\n",sizeof(DataStruct)); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 118 | if (actual <= 0) { |
| 119 | LOG(FATAL, "didn't get any data\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 122 | if (r != 0) { |
| 123 | LOG(ERROR, "Read Error. Read %d\n", actual); |
| 124 | } |
root | 11a49d0 | 2013-03-30 06:27:47 +0000 | [diff] [blame] | 125 | |
| 126 | ++count; |
| 127 | if (count < 100) continue; |
| 128 | count = 0; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 129 | |
| 130 | printf("angle: %"PRId64"\n", real_data->gyro_angle); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 131 | printf("drivel: %d\n", real_data->left_drive); |
| 132 | printf("driver: %d\n", real_data->right_drive); |
| 133 | printf("shooter: %d\n", real_data->shooter); |
| 134 | printf("shooter_angle: %d\n", real_data->shooter_angle); |
| 135 | printf("indexer: %d\n", real_data->indexer); |
| 136 | printf("wrist: %d\n", real_data->wrist); |
| 137 | printf("capture:\n"); |
| 138 | printf(" wrist_rise{%d}: %d\n", real_data->wrist_rise_count, |
| 139 | real_data->capture_wrist_rise); |
| 140 | printf(" shooter_angle_rise{%d}: %d\n", real_data->shooter_angle_rise_count, |
| 141 | real_data->capture_shooter_angle_rise); |
| 142 | printf(" bottom_rise_delay{%d}; \n", real_data->bottom_rise_count); |
| 143 | printf(" bottom_fall_delay{%d,%d}: %d\n", real_data->bottom_fall_count, |
| 144 | real_data->bottom_fall_delay_count, |
| 145 | real_data->capture_bottom_fall_delay); |
| 146 | printf(" top_rise{%d}: %d\n", real_data->top_rise_count, |
| 147 | real_data->capture_top_rise); |
| 148 | printf(" top_fall{%d}: %d\n", real_data->top_fall_count, |
| 149 | real_data->capture_top_fall); |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | //if (actual > 1) { |
| 155 | // rx_cond_.notify_all(); |
| 156 | //} |
| 157 | } |
| 158 | //rx_cond_.notify_all(); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 159 | LOG(INFO, "Receive thread down."); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | DbgReader::DbgReader(GyroDriver *gyro) |
| 163 | : Thread(), gyro_(gyro) {} |
| 164 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 165 | void DbgReader::Run() { |
| 166 | LOG(INFO, "Running debug dump thread."); |
| 167 | while (should_continue()) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 168 | printf("%s", gyro_->GetDebugData().c_str()); |
| 169 | } |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 170 | LOG(INFO, "Exiting debug dump thread."); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
| 174 | int main(int argc, char ** argv) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 175 | google::ParseCommandLineFlags(&argc, &argv, true); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 176 | ::aos::logging::Init(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 177 | |
| 178 | LibUSB libusb; |
| 179 | |
| 180 | { |
| 181 | std::unique_ptr<LibUSBDeviceHandle> dev_handle( |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 182 | libusb.FindDeviceWithVIDPID(FLAGS_vid, FLAGS_pid)); |
| 183 | if (!dev_handle) { |
| 184 | LOG(FATAL, "couldn't find device\n"); |
| 185 | } |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 186 | |
| 187 | GyroDriver gyro(dev_handle.release()); |
| 188 | |
| 189 | while(true){ |
| 190 | sleep(50); |
| 191 | } |
| 192 | } |
| 193 | return 0; |
| 194 | } |