blob: 311a8346c9c336bc2e74fd73a05aece9fefc089d [file] [log] [blame]
Austin Schuh9b360e92013-03-27 04:47:04 +00001#include "get.h"
2
Austin Schuh9b360e92013-03-27 04:47:04 +00003#include <stdio.h>
Austin Schuh9b360e92013-03-27 04:47:04 +00004#include <unistd.h>
Brian Silverman798c7782013-03-28 16:48:02 -07005#include <inttypes.h>
6#include <string.h>
7
8#include <queue>
9#include <iostream>
Austin Schuh9b360e92013-03-27 04:47:04 +000010#include <map>
11
Brian Silverman798c7782013-03-28 16:48:02 -070012#include <google/gflags.h>
13#include "aos/common/mutex.h"
14#include "aos/common/logging/logging_impl.h"
Austin Schuh9b360e92013-03-27 04:47:04 +000015
16DEFINE_int32(vid, 0x1424, "Vendor ID of the device.");
17DEFINE_int32(pid, 0xd243, "Product ID of the device.");
18DEFINE_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 Silverman798c7782013-03-28 16:48:02 -070031class GyroDriver::PacketReceiver : public ::aos::util::Thread {
Austin Schuh9b360e92013-03-27 04:47:04 +000032 public:
33 // refusing to send any more frames.
34 static const int kMaxRXFrames = 128;
35
36 explicit PacketReceiver(LibUSBDeviceHandle *dev_handle)
Austin Schuh09f82df2013-03-30 10:13:54 +000037 : Thread(), dev_handle_(dev_handle) {}
Austin Schuh9b360e92013-03-27 04:47:04 +000038
39 // Serve.
Brian Silverman798c7782013-03-28 16:48:02 -070040 virtual void Run();
Austin Schuh9b360e92013-03-27 04:47:04 +000041
42 bool GetCanFrame(struct can_frame *msg);
43
44 private:
Austin Schuh9b360e92013-03-27 04:47:04 +000045 LibUSBDeviceHandle *dev_handle_;
Brian Silverman798c7782013-03-28 16:48:02 -070046 ::aos::Mutex rx_mutex_;
Austin Schuh9b360e92013-03-27 04:47:04 +000047};
48
49GyroDriver::GyroDriver(LibUSBDeviceHandle *dev_handle)
50 : dev_handle_(dev_handle), dbg_(new DbgReader(this)),
Brian Silverman798c7782013-03-28 16:48:02 -070051 rx_(new GyroDriver::PacketReceiver(dev_handle)) {}
Austin Schuh9b360e92013-03-27 04:47:04 +000052
53
54std::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 Schuh9b360e92013-03-27 04:47:04 +000064GyroDriver::~GyroDriver() {
Brian Silverman798c7782013-03-28 16:48:02 -070065 rx_->Join();
66 dbg_->Join();
Austin Schuh9b360e92013-03-27 04:47:04 +000067}
68
69//bool GyroDriver::GetCanFrame(struct can_frame *msg) {
70// return rx_->GetCanFrame(msg);
71//}
72
73struct 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 Silverman798c7782013-03-28 16:48:02 -0700103void GyroDriver::PacketReceiver::Run() {
104 int r;
105 int actual;
106 uint8_t data[64];
Austin Schuh9b360e92013-03-27 04:47:04 +0000107 DataStruct *real_data;
Brian Silverman798c7782013-03-28 16:48:02 -0700108 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 Schuh9b360e92013-03-27 04:47:04 +0000112
root11a49d02013-03-30 06:27:47 +0000113 int count = 0;
Brian Silverman798c7782013-03-28 16:48:02 -0700114 while (should_continue()) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000115 r = dev_handle_->interrupt_transfer(
116 0x81, data, sizeof(data), &actual, 1000);
117 printf("size: %d\n",sizeof(DataStruct));
Brian Silverman798c7782013-03-28 16:48:02 -0700118 if (actual <= 0) {
119 LOG(FATAL, "didn't get any data\n");
Austin Schuh9b360e92013-03-27 04:47:04 +0000120 }
121
Brian Silverman798c7782013-03-28 16:48:02 -0700122 if (r != 0) {
123 LOG(ERROR, "Read Error. Read %d\n", actual);
124 }
root11a49d02013-03-30 06:27:47 +0000125
126 ++count;
127 if (count < 100) continue;
128 count = 0;
Brian Silverman798c7782013-03-28 16:48:02 -0700129
130 printf("angle: %"PRId64"\n", real_data->gyro_angle);
Austin Schuh9b360e92013-03-27 04:47:04 +0000131 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 Silverman798c7782013-03-28 16:48:02 -0700159 LOG(INFO, "Receive thread down.");
Austin Schuh9b360e92013-03-27 04:47:04 +0000160}
161
162DbgReader::DbgReader(GyroDriver *gyro)
163 : Thread(), gyro_(gyro) {}
164
Brian Silverman798c7782013-03-28 16:48:02 -0700165void DbgReader::Run() {
166 LOG(INFO, "Running debug dump thread.");
167 while (should_continue()) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000168 printf("%s", gyro_->GetDebugData().c_str());
169 }
Brian Silverman798c7782013-03-28 16:48:02 -0700170 LOG(INFO, "Exiting debug dump thread.");
Austin Schuh9b360e92013-03-27 04:47:04 +0000171}
172
173
174int main(int argc, char ** argv) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000175 google::ParseCommandLineFlags(&argc, &argv, true);
Brian Silverman798c7782013-03-28 16:48:02 -0700176 ::aos::logging::Init();
Austin Schuh9b360e92013-03-27 04:47:04 +0000177
178 LibUSB libusb;
179
180 {
181 std::unique_ptr<LibUSBDeviceHandle> dev_handle(
Brian Silverman798c7782013-03-28 16:48:02 -0700182 libusb.FindDeviceWithVIDPID(FLAGS_vid, FLAGS_pid));
183 if (!dev_handle) {
184 LOG(FATAL, "couldn't find device\n");
185 }
Austin Schuh9b360e92013-03-27 04:47:04 +0000186
187 GyroDriver gyro(dev_handle.release());
188
189 while(true){
190 sleep(50);
191 }
192 }
193 return 0;
194}