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 | |
| 31 | bool can_frame_priority_comparator::operator() ( |
| 32 | const struct can_frame& x, |
| 33 | const struct can_frame& y) const { |
| 34 | // Error frames win first. |
| 35 | if (x.can_id & CAN_EFF_FLAG) { |
| 36 | return false; |
| 37 | } |
| 38 | if (y.can_id & CAN_EFF_FLAG) { |
| 39 | return true; |
| 40 | } |
| 41 | // Extended frames send the first 11 bits out just like a standard frame. |
| 42 | int x_standard_bits = (x.can_id & CAN_SFF_MASK); |
| 43 | int y_standard_bits = (y.can_id & CAN_SFF_MASK); |
| 44 | if (x_standard_bits == y_standard_bits) { |
| 45 | // RTR wins next. |
| 46 | bool x_rtr = (x.can_id & CAN_RTR_FLAG); |
| 47 | bool y_rtr = (y.can_id & CAN_RTR_FLAG); |
| 48 | if (x_rtr == y_rtr) { |
| 49 | // Now check if it is an EFF packet. |
| 50 | bool x_eff = (x.can_id & CAN_EFF_FLAG); |
| 51 | bool y_eff = (y.can_id & CAN_EFF_FLAG); |
| 52 | if (x_eff == y_eff) { |
| 53 | // Now compare the bits in the extended frame. |
| 54 | return (x.can_id & CAN_EFF_MASK) < (y.can_id & CAN_EFF_MASK); |
| 55 | } else if (x_eff < y_eff) { |
| 56 | return false; |
| 57 | } else { |
| 58 | return true; |
| 59 | } |
| 60 | } else if (x_rtr < y_rtr) { |
| 61 | return true; |
| 62 | } else { |
| 63 | return false; |
| 64 | } |
| 65 | } else { |
| 66 | return x_standard_bits < y_standard_bits; |
| 67 | } |
| 68 | } |
| 69 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 70 | class GyroDriver::PacketReceiver : public ::aos::util::Thread { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 71 | public: |
| 72 | // refusing to send any more frames. |
| 73 | static const int kMaxRXFrames = 128; |
| 74 | |
| 75 | explicit PacketReceiver(LibUSBDeviceHandle *dev_handle) |
| 76 | : Thread(), dev_handle_(dev_handle), rx_queue_(new can_priority_queue) {} |
| 77 | |
| 78 | // Serve. |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 79 | virtual void Run(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 80 | |
| 81 | bool GetCanFrame(struct can_frame *msg); |
| 82 | |
| 83 | private: |
| 84 | typedef std::priority_queue<struct can_frame, |
| 85 | std::vector<struct can_frame>, |
| 86 | can_frame_priority_comparator> can_priority_queue; |
| 87 | |
| 88 | LibUSBDeviceHandle *dev_handle_; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 89 | ::aos::Mutex rx_mutex_; |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 90 | std::unique_ptr<can_priority_queue> rx_queue_; |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | GyroDriver::GyroDriver(LibUSBDeviceHandle *dev_handle) |
| 94 | : dev_handle_(dev_handle), dbg_(new DbgReader(this)), |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 95 | rx_(new GyroDriver::PacketReceiver(dev_handle)) {} |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | std::string GyroDriver::GetDebugData() { |
| 99 | char data[64]; |
| 100 | int r; |
| 101 | int transferred; |
| 102 | r = dev_handle_->bulk_transfer(0x82, |
| 103 | (unsigned char *)data, sizeof(data), |
| 104 | &transferred, 0); |
| 105 | return std::string(data, transferred); |
| 106 | } |
| 107 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 108 | GyroDriver::~GyroDriver() { |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 109 | rx_->Join(); |
| 110 | dbg_->Join(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | //bool GyroDriver::GetCanFrame(struct can_frame *msg) { |
| 114 | // return rx_->GetCanFrame(msg); |
| 115 | //} |
| 116 | |
| 117 | struct DataStruct{ |
| 118 | int64_t gyro_angle; |
| 119 | |
| 120 | int32_t right_drive; |
| 121 | int32_t left_drive; |
| 122 | int32_t shooter_angle; |
| 123 | int32_t shooter; |
| 124 | int32_t indexer; |
| 125 | int32_t wrist; |
| 126 | |
| 127 | int32_t capture_top_rise; |
| 128 | int32_t capture_top_fall; |
| 129 | int32_t capture_bottom_fall_delay; |
| 130 | int32_t capture_wrist_rise; |
| 131 | int32_t capture_shooter_angle_rise; |
| 132 | |
| 133 | int8_t top_rise_count; |
| 134 | |
| 135 | int8_t top_fall_count; |
| 136 | |
| 137 | int8_t bottom_rise_count; |
| 138 | |
| 139 | int8_t bottom_fall_delay_count; |
| 140 | int8_t bottom_fall_count; |
| 141 | |
| 142 | int8_t wrist_rise_count; |
| 143 | |
| 144 | int8_t shooter_angle_rise_count; |
| 145 | } __attribute__((__packed__)); |
| 146 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 147 | void GyroDriver::PacketReceiver::Run() { |
| 148 | int r; |
| 149 | int actual; |
| 150 | uint8_t data[64]; |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 151 | DataStruct *real_data; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 152 | static_assert(sizeof(*real_data) <= sizeof(data), "it doesn't fit"); |
| 153 | |
| 154 | uint8_t *data_pointer = data; |
| 155 | memcpy(&real_data, &data_pointer, sizeof(data_pointer)); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 156 | |
root | 11a49d0 | 2013-03-30 06:27:47 +0000 | [diff] [blame^] | 157 | int count = 0; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 158 | while (should_continue()) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 159 | r = dev_handle_->interrupt_transfer( |
| 160 | 0x81, data, sizeof(data), &actual, 1000); |
| 161 | printf("size: %d\n",sizeof(DataStruct)); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 162 | if (actual <= 0) { |
| 163 | LOG(FATAL, "didn't get any data\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 166 | if (r != 0) { |
| 167 | LOG(ERROR, "Read Error. Read %d\n", actual); |
| 168 | } |
root | 11a49d0 | 2013-03-30 06:27:47 +0000 | [diff] [blame^] | 169 | |
| 170 | ++count; |
| 171 | if (count < 100) continue; |
| 172 | count = 0; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 173 | |
| 174 | printf("angle: %"PRId64"\n", real_data->gyro_angle); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 175 | printf("drivel: %d\n", real_data->left_drive); |
| 176 | printf("driver: %d\n", real_data->right_drive); |
| 177 | printf("shooter: %d\n", real_data->shooter); |
| 178 | printf("shooter_angle: %d\n", real_data->shooter_angle); |
| 179 | printf("indexer: %d\n", real_data->indexer); |
| 180 | printf("wrist: %d\n", real_data->wrist); |
| 181 | printf("capture:\n"); |
| 182 | printf(" wrist_rise{%d}: %d\n", real_data->wrist_rise_count, |
| 183 | real_data->capture_wrist_rise); |
| 184 | printf(" shooter_angle_rise{%d}: %d\n", real_data->shooter_angle_rise_count, |
| 185 | real_data->capture_shooter_angle_rise); |
| 186 | printf(" bottom_rise_delay{%d}; \n", real_data->bottom_rise_count); |
| 187 | printf(" bottom_fall_delay{%d,%d}: %d\n", real_data->bottom_fall_count, |
| 188 | real_data->bottom_fall_delay_count, |
| 189 | real_data->capture_bottom_fall_delay); |
| 190 | printf(" top_rise{%d}: %d\n", real_data->top_rise_count, |
| 191 | real_data->capture_top_rise); |
| 192 | printf(" top_fall{%d}: %d\n", real_data->top_fall_count, |
| 193 | real_data->capture_top_fall); |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | //if (actual > 1) { |
| 199 | // rx_cond_.notify_all(); |
| 200 | //} |
| 201 | } |
| 202 | //rx_cond_.notify_all(); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 203 | LOG(INFO, "Receive thread down."); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | DbgReader::DbgReader(GyroDriver *gyro) |
| 207 | : Thread(), gyro_(gyro) {} |
| 208 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 209 | void DbgReader::Run() { |
| 210 | LOG(INFO, "Running debug dump thread."); |
| 211 | while (should_continue()) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 212 | printf("%s", gyro_->GetDebugData().c_str()); |
| 213 | } |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 214 | LOG(INFO, "Exiting debug dump thread."); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | int main(int argc, char ** argv) { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 219 | google::ParseCommandLineFlags(&argc, &argv, true); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 220 | ::aos::logging::Init(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 221 | |
| 222 | LibUSB libusb; |
| 223 | |
| 224 | { |
| 225 | std::unique_ptr<LibUSBDeviceHandle> dev_handle( |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 226 | libusb.FindDeviceWithVIDPID(FLAGS_vid, FLAGS_pid)); |
| 227 | if (!dev_handle) { |
| 228 | LOG(FATAL, "couldn't find device\n"); |
| 229 | } |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 230 | |
| 231 | GyroDriver gyro(dev_handle.release()); |
| 232 | |
| 233 | while(true){ |
| 234 | sleep(50); |
| 235 | } |
| 236 | } |
| 237 | return 0; |
| 238 | } |