blob: b3cdc283c9e7ab056459f394bf0383e218a94a1d [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 Silvermanc3c06292013-03-30 18:54:31 -0700103void GyroDriver::Start() {
104 rx_->Start();
105}
106
Brian Silverman798c7782013-03-28 16:48:02 -0700107void GyroDriver::PacketReceiver::Run() {
108 int r;
109 int actual;
110 uint8_t data[64];
Austin Schuh9b360e92013-03-27 04:47:04 +0000111 DataStruct *real_data;
Brian Silverman798c7782013-03-28 16:48:02 -0700112 static_assert(sizeof(*real_data) <= sizeof(data), "it doesn't fit");
113
114 uint8_t *data_pointer = data;
115 memcpy(&real_data, &data_pointer, sizeof(data_pointer));
Austin Schuh9b360e92013-03-27 04:47:04 +0000116
root11a49d02013-03-30 06:27:47 +0000117 int count = 0;
Brian Silverman798c7782013-03-28 16:48:02 -0700118 while (should_continue()) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000119 r = dev_handle_->interrupt_transfer(
120 0x81, data, sizeof(data), &actual, 1000);
Brian Silverman798c7782013-03-28 16:48:02 -0700121 if (actual <= 0) {
122 LOG(FATAL, "didn't get any data\n");
Austin Schuh9b360e92013-03-27 04:47:04 +0000123 }
124
Brian Silverman798c7782013-03-28 16:48:02 -0700125 if (r != 0) {
126 LOG(ERROR, "Read Error. Read %d\n", actual);
127 }
root11a49d02013-03-30 06:27:47 +0000128
129 ++count;
130 if (count < 100) continue;
131 count = 0;
Brian Silverman798c7782013-03-28 16:48:02 -0700132
133 printf("angle: %"PRId64"\n", real_data->gyro_angle);
Austin Schuh9b360e92013-03-27 04:47:04 +0000134 printf("drivel: %d\n", real_data->left_drive);
135 printf("driver: %d\n", real_data->right_drive);
136 printf("shooter: %d\n", real_data->shooter);
137 printf("shooter_angle: %d\n", real_data->shooter_angle);
138 printf("indexer: %d\n", real_data->indexer);
139 printf("wrist: %d\n", real_data->wrist);
140 printf("capture:\n");
141 printf(" wrist_rise{%d}: %d\n", real_data->wrist_rise_count,
142 real_data->capture_wrist_rise);
143 printf(" shooter_angle_rise{%d}: %d\n", real_data->shooter_angle_rise_count,
144 real_data->capture_shooter_angle_rise);
145 printf(" bottom_rise_delay{%d}; \n", real_data->bottom_rise_count);
146 printf(" bottom_fall_delay{%d,%d}: %d\n", real_data->bottom_fall_count,
147 real_data->bottom_fall_delay_count,
148 real_data->capture_bottom_fall_delay);
149 printf(" top_rise{%d}: %d\n", real_data->top_rise_count,
150 real_data->capture_top_rise);
151 printf(" top_fall{%d}: %d\n", real_data->top_fall_count,
152 real_data->capture_top_fall);
153
154
155
156
157 //if (actual > 1) {
158 // rx_cond_.notify_all();
159 //}
160 }
161 //rx_cond_.notify_all();
Brian Silverman798c7782013-03-28 16:48:02 -0700162 LOG(INFO, "Receive thread down.");
Austin Schuh9b360e92013-03-27 04:47:04 +0000163}
164
165DbgReader::DbgReader(GyroDriver *gyro)
166 : Thread(), gyro_(gyro) {}
167
Brian Silverman798c7782013-03-28 16:48:02 -0700168void DbgReader::Run() {
169 LOG(INFO, "Running debug dump thread.");
170 while (should_continue()) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000171 printf("%s", gyro_->GetDebugData().c_str());
172 }
Brian Silverman798c7782013-03-28 16:48:02 -0700173 LOG(INFO, "Exiting debug dump thread.");
Austin Schuh9b360e92013-03-27 04:47:04 +0000174}
175
176
177int main(int argc, char ** argv) {
Austin Schuh9b360e92013-03-27 04:47:04 +0000178 google::ParseCommandLineFlags(&argc, &argv, true);
Brian Silverman798c7782013-03-28 16:48:02 -0700179 ::aos::logging::Init();
Austin Schuh9b360e92013-03-27 04:47:04 +0000180
181 LibUSB libusb;
182
183 {
184 std::unique_ptr<LibUSBDeviceHandle> dev_handle(
Brian Silverman798c7782013-03-28 16:48:02 -0700185 libusb.FindDeviceWithVIDPID(FLAGS_vid, FLAGS_pid));
186 if (!dev_handle) {
187 LOG(FATAL, "couldn't find device\n");
188 }
Austin Schuh9b360e92013-03-27 04:47:04 +0000189
190 GyroDriver gyro(dev_handle.release());
Brian Silvermanc3c06292013-03-30 18:54:31 -0700191 gyro.Start();
Austin Schuh9b360e92013-03-27 04:47:04 +0000192
193 while(true){
194 sleep(50);
195 }
196 }
197 return 0;
198}