blob: 4d7f7cb348e834b45c22f5791c723226ae55accb [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];
Austin Schuh9b360e92013-03-27 04:47:04 +000056 int transferred;
Brian Silverman8efe23e2013-07-07 23:31:37 -070057 dev_handle_->bulk_transfer(0x82,
58 (unsigned char *)data, sizeof(data),
59 &transferred, 0);
Austin Schuh9b360e92013-03-27 04:47:04 +000060 return std::string(data, transferred);
61}
62
Austin Schuh9b360e92013-03-27 04:47:04 +000063GyroDriver::~GyroDriver() {
Brian Silverman798c7782013-03-28 16:48:02 -070064 rx_->Join();
65 dbg_->Join();
Austin Schuh9b360e92013-03-27 04:47:04 +000066}
67
68//bool GyroDriver::GetCanFrame(struct can_frame *msg) {
69// return rx_->GetCanFrame(msg);
70//}
71
72struct 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 Silvermanc3c06292013-03-30 18:54:31 -0700102void GyroDriver::Start() {
103 rx_->Start();
104}
105
Brian Silverman798c7782013-03-28 16:48:02 -0700106void GyroDriver::PacketReceiver::Run() {
107 int r;
108 int actual;
109 uint8_t data[64];
Austin Schuh9b360e92013-03-27 04:47:04 +0000110 DataStruct *real_data;
Brian Silverman798c7782013-03-28 16:48:02 -0700111 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 Schuh9b360e92013-03-27 04:47:04 +0000115
root11a49d02013-03-30 06:27:47 +0000116 int count = 0;
Brian Silverman798c7782013-03-28 16:48:02 -0700117 while (should_continue()) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000118 r = dev_handle_->interrupt_transfer(
119 0x81, data, sizeof(data), &actual, 1000);
Brian Silverman798c7782013-03-28 16:48:02 -0700120 if (actual <= 0) {
121 LOG(FATAL, "didn't get any data\n");
Austin Schuh9b360e92013-03-27 04:47:04 +0000122 }
123
Brian Silverman798c7782013-03-28 16:48:02 -0700124 if (r != 0) {
125 LOG(ERROR, "Read Error. Read %d\n", actual);
126 }
root11a49d02013-03-30 06:27:47 +0000127
128 ++count;
129 if (count < 100) continue;
130 count = 0;
Brian Silverman798c7782013-03-28 16:48:02 -0700131
Brian Silverman8efe23e2013-07-07 23:31:37 -0700132 printf("angle: %" PRId64 "\n", real_data->gyro_angle);
Austin Schuh9b360e92013-03-27 04:47:04 +0000133 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 Silverman798c7782013-03-28 16:48:02 -0700161 LOG(INFO, "Receive thread down.");
Austin Schuh9b360e92013-03-27 04:47:04 +0000162}
163
164DbgReader::DbgReader(GyroDriver *gyro)
165 : Thread(), gyro_(gyro) {}
166
Brian Silverman798c7782013-03-28 16:48:02 -0700167void DbgReader::Run() {
168 LOG(INFO, "Running debug dump thread.");
169 while (should_continue()) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000170 printf("%s", gyro_->GetDebugData().c_str());
171 }
Brian Silverman798c7782013-03-28 16:48:02 -0700172 LOG(INFO, "Exiting debug dump thread.");
Austin Schuh9b360e92013-03-27 04:47:04 +0000173}
174
175
176int main(int argc, char ** argv) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000177 google::ParseCommandLineFlags(&argc, &argv, true);
Brian Silverman798c7782013-03-28 16:48:02 -0700178 ::aos::logging::Init();
Austin Schuh9b360e92013-03-27 04:47:04 +0000179
180 LibUSB libusb;
181
182 {
183 std::unique_ptr<LibUSBDeviceHandle> dev_handle(
Brian Silverman798c7782013-03-28 16:48:02 -0700184 libusb.FindDeviceWithVIDPID(FLAGS_vid, FLAGS_pid));
185 if (!dev_handle) {
186 LOG(FATAL, "couldn't find device\n");
187 }
Austin Schuh9b360e92013-03-27 04:47:04 +0000188
189 GyroDriver gyro(dev_handle.release());
Brian Silvermanc3c06292013-03-30 18:54:31 -0700190 gyro.Start();
Austin Schuh9b360e92013-03-27 04:47:04 +0000191
192 while(true){
193 sleep(50);
194 }
195 }
196 return 0;
197}