NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
vive.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  *
18  * Based off libsurvive: https://github.com/cnlohr/libsurvive
19  */
20 
21 #ifndef VIVE_VIVE_H_
22 #define VIVE_VIVE_H_
23 
24 #include <libusb-1.0/libusb.h>
25 
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <string.h>
31 
32 #define MAX_PACKET_LEN 64
33 #define PREAMBLE_LENGTH 17
34 
35 #define MAX_NUM_LIGHTHOUSES 2
36 #define MAX_NUM_TRACKERS 128
37 #define MAX_NUM_SENSORS 32
38 #define MAX_SERIAL_LENGTH 32
39 #define USB_INT_BUFF_LENGTH 64
40 
41 #define USB_VEND_HTC 0x28de
42 #define USB_PROD_TRACKER_V1 0x2022
43 #define USB_PROD_TRACKER_V2 0x2300
44 #define USB_PROD_CONTROLLER_V1 0x2012
45 #define USB_PROD_WATCHMAN 0x2101
46 
47 #define USB_ENDPOINT_WATCHMAN 0x81
48 #define USB_ENDPOINT_V1_GENERAL 0x81
49 #define USB_ENDPOINT_V1_LIGHT 0x82
50 #define USB_ENDPOINT_V1_BUTTONS 0x83
51 #define USB_ENDPOINT_V2_GENERAL 0x81
52 #define USB_ENDPOINT_V2_LIGHT 0x83
53 #define USB_ENDPOINT_V2_BUTTONS 0x84
54 #define MAX_ENDPOINTS 3
55 
56 #define DEFAULT_ACC_SCALE (float)(9.80665/4096.0)
57 #define DEFAULT_GYR_SCALE (float)((1./32.768)*(3.14159/180.));
58 
59 // Richer debug info
60 // #define DEBUG_PRINTF(f_, ...) printf((f_), ##__VA_ARGS__)
61 #define DEBUG_PRINTF(f_, ...)
62 
63 // Forward declaration of driver context
64 typedef struct Driver driver_t;
65 typedef struct Tracker tracker_t;
66 typedef struct Lighthouse lighthouse_t;
67 
68 // Extrinsics axes
69 typedef enum Data {
71 } data_t;
72 
73 // Button types
74 typedef enum Event {
75  EVENT_TRIGGER = (1<<8),
76  EVENT_GRIP = (1<<10),
77  EVENT_MENU = (1<<20),
78  EVENT_PAD_CLICK = (1<<26),
79  EVENT_PAD_TOUCH = (1<<28)
80 } event_t;
81 
82 // Interrupt buffer for an endpoint
83 typedef struct Endpoint {
86  struct libusb_transfer *tx;
88 } endpoint_t;
89 
90 // Calibration for a given tracker
91 typedef struct Calibration {
92  uint32_t timestamp; // Time of last update
93  uint8_t num_channels; // Number of photodiodes (PDs)
94  uint8_t channels[MAX_NUM_SENSORS]; // Channel assignment for PDs
95  float positions[MAX_NUM_SENSORS][3]; // PD positions
96  float normals[MAX_NUM_SENSORS][3]; // PD normals
97  float acc_bias[3]; // Acceleromater bias
98  float acc_scale[3]; // Accelerometer scale
99  float gyr_bias[3]; // Gyro bias
100  float gyr_scale[3]; // Gyro scale
101  // Transforms below are (wx, qy, qz, qw, X, Y, Z)
102  float imu_transform[7]; // Tracker -> IMU trasform
103  float head_transform[7]; // Tracker -> Head transform
104 } calibration_t;
105 
106 // OOTX state machine for each lighthouse
108 typedef struct OOTX {
109  state_t state; // Current RX state
110  uint8_t data[MAX_PACKET_LEN]; // Data buffer
111  uint16_t length; // Length in bytes
112  uint8_t pad; // Padding length in bytes : 0 or 1
113  uint16_t pos; // Bit position
114  uint16_t syn; // Sync bit counter
115  uint32_t crc; // CRC32 Checksum
116  uint8_t preamble; // Preamble
117  uint32_t lasttime; // Last sync time
118  lighthouse_t *lighthouse; // Lighthouse reference...
119 } ootx_t;
120 
121 typedef struct LightcapSweepData {
125 
126 typedef struct PerSweepData {
136 
137 typedef struct GlobalData {
138  double acode_offset;
139 } global_data_t;
140 
141 typedef struct LightcapData {
146 
147 // Information about a tracked device
148 typedef struct Tracker {
149  uint16_t type; // Tracker type
150  driver_t * driver; // Parent driver
151  struct libusb_device_handle * udev; // Udev handle
152  char serial[MAX_SERIAL_LENGTH]; // Serial number
153  endpoint_t endpoints[MAX_ENDPOINTS]; // USB endpoints
154  calibration_t cal; // Calibration data
155  lightcap_data_t lcd; // Lightcap data
157  uint8_t charge; // Current charge
158  uint8_t ischarging:1; // Charging?
159  uint8_t ison:1; // Turned on?
160  uint8_t axis[3]; // Gravitational axis
161  uint8_t buttonmask; // Buttom mask
162  uint32_t timecode; // Timecode of last update
163 } tracker_t;
164 
165 // Motor information
167 
168 // Motor data structure
169 typedef struct Motor {
170  float phase;
171  float tilt;
172  float gibphase;
173  float gibmag;
174  float curve;
175 } motor_t;
176 
177 // Lighthouse information
178 typedef struct Lighthouse {
179  uint32_t timestamp; // Time of last update (0 = invalud)
180  uint8_t id; // ID of this lighthouse
181  uint16_t fw_version; // Firmware version
182  char serial[MAX_SERIAL_LENGTH]; // Unique serial number
183  motor_t motors[MAX_NUM_MOTORS]; // Motor calibration data
184  float accel[3]; // acceleration vector
185  uint8_t sys_unlock_count; // Lowest 8 bits of desynchronization
186  uint8_t hw_version; // Hardware version
187  uint8_t mode_current; // Current mode (default: 0=A, 1=B, 2=C)
188  uint8_t sys_faults; // "fault detect flags" (should be 0)
189 } lighthouse_t;
190 
191 // General configuration
192 typedef struct General {
193  int32_t timebase_hz; // 48,000,000 (checked)
194  int32_t timecenter_ticks; // 200,000 (checked) (2x = sweep length)
195  int32_t pulsedist_max_ticks; // 500,000 (guessed)
196  int32_t pulselength_min_sync; // 2,200 (guessed)
197  int32_t pulse_in_clear_time; // 35,000 (guessed)
198  int32_t pulse_max_for_sweep; // 1,800 (guessed)
199  int32_t pulse_synctime_offset; // 20,000 (guessed)
200  int32_t pulse_synctime_slack; // 5,000 (guessed)
201 } general_t;
202 
203 typedef struct Light {
204  uint32_t timestamp;
205  uint8_t sensor_id;
206  uint16_t length;
207 } light_t;
208 
209 typedef struct IMU {
210  uint32_t timestamp;
211  int16_t acc[3];
212  int16_t gyr[3];
213 } imu_t;
214 
215 typedef struct Button {
216  uint32_t mask;
217  int16_t trigger;
218  int16_t horizontal;
219  int16_t vertical;
220 } button_t;
221 
222 // Callbacks
223 typedef void (*light_cb_t)(tracker_t const * tracker, uint8_t lh, uint8_t axis,
224  uint32_t synctime, uint16_t num_sensors, light_t const * measurement);
225 typedef void (*imu_cb_t)(tracker_t const * tracker, imu_t const * measurement);
226 typedef void (*button_cb_t)(tracker_t const * tracker, button_t const * measurement);
227 typedef void (*tracker_cb_t)(tracker_t const * tracker);
228 typedef void (*lighthouse_cb_t)(lighthouse_t const * lighthouse);
229 
230 // Driver context
231 typedef struct Driver {
232  struct libusb_context* usb;
233  uint16_t num_trackers;
235  light_cb_t light_cb; // Called when new light data arrives
236  imu_cb_t imu_cb; // Called when new IMU data arrives
237  button_cb_t button_cb; // Called when new button data arrives
238  tracker_cb_t tracker_cb; // Called when tracker cal info is ready
239  lighthouse_cb_t lighthouse_cb; // Called when lighthouse cal info is ready
241  general_t general; // General configuration
242  uint8_t pushed; // Have we pushed thie tracker/general config
243 } driver_t;
244 
245 // Initialize the driver
246 driver_t * vive_init();
247 
248 // Register a light callback function
249 void vive_install_light_fn(driver_t * drv, light_cb_t fbp);
250 
251 // Register an IMU callback function
252 void vive_install_imu_fn(driver_t * drv, imu_cb_t fbp);
253 
254 // Register a button callback function
256 
257 // Register a tracker callback function
259 
260 // Register a lighthouse callback function
262 
263 // Get the general configuration data
265 
266 // Get the calibration data for the lighthouse with the given serial number
267 lighthouse_t * vive_lighthouse(driver_t * drv, const char* id);
268 
269 // Get the calibration data for a tracker with the given serial number
270 tracker_t * vive_tracker(driver_t * tracker, const char* id);
271 
272 // Poll the driver for events
273 uint8_t vive_poll(driver_t * drv);
274 
275 // Close the driver and clean up memory
276 void vive_kill(driver_t * drv);
277 
278 #endif // VIVE_VIVE_H_
General::pulse_synctime_offset
int32_t pulse_synctime_offset
Definition: vive.h:199
General::timecenter_ticks
int32_t timecenter_ticks
Definition: vive.h:194
PerSweepData::activeAcode
int activeAcode
Definition: vive.h:130
Calibration::head_transform
float head_transform[7]
Definition: vive.h:103
Calibration::timestamp
uint32_t timestamp
Definition: vive.h:92
lightcap_sweep_data_t
struct LightcapSweepData lightcap_sweep_data_t
General::timebase_hz
int32_t timebase_hz
Definition: vive.h:193
vive_kill
void vive_kill(driver_t *drv)
endpoint_t
struct Endpoint endpoint_t
imu_cb_t
void(* imu_cb_t)(tracker_t const *tracker, imu_t const *measurement)
Definition: vive.h:225
Driver
Definition: vive.h:231
Lighthouse::hw_version
uint8_t hw_version
Definition: vive.h:186
Calibration::positions
float positions[MAX_NUM_SENSORS][3]
Definition: vive.h:95
motor_t
struct Motor motor_t
MAX_PACKET_LEN
#define MAX_PACKET_LEN
Definition: vive.h:32
Calibration::acc_bias
float acc_bias[3]
Definition: vive.h:97
OOTX::pos
uint16_t pos
Definition: vive.h:113
lighthouse_cb_t
void(* lighthouse_cb_t)(lighthouse_t const *lighthouse)
Definition: vive.h:228
MAX_NUM_TRACKERS
#define MAX_NUM_TRACKERS
Definition: vive.h:36
Motor::curve
float curve
Definition: vive.h:174
EVENT_TRIGGER
@ EVENT_TRIGGER
Definition: vive.h:75
Tracker::udev
struct libusb_device_handle * udev
Definition: vive.h:151
DATA_LIGHT_9
@ DATA_LIGHT_9
Definition: vive.h:70
per_sweep_data_t
struct PerSweepData per_sweep_data_t
ootx_t
struct OOTX ootx_t
LightcapSweepData::sweep_time
uint32_t sweep_time[MAX_NUM_SENSORS]
Definition: vive.h:122
MAX_ENDPOINTS
#define MAX_ENDPOINTS
Definition: vive.h:54
LightcapSweepData
Definition: vive.h:121
vive_tracker
tracker_t * vive_tracker(driver_t *tracker, const char *id)
IMU::gyr
int16_t gyr[3]
Definition: vive.h:212
OOTX::syn
uint16_t syn
Definition: vive.h:114
Calibration::imu_transform
float imu_transform[7]
Definition: vive.h:102
Button::trigger
int16_t trigger
Definition: vive.h:217
MOTOR_AXIS0
@ MOTOR_AXIS0
Definition: vive.h:166
Tracker::lcd
lightcap_data_t lcd
Definition: vive.h:155
OOTX
Definition: vive.h:108
state_t
state_t
Definition: vive.h:107
data_t
enum Data data_t
Lighthouse::serial
char serial[MAX_SERIAL_LENGTH]
Definition: vive.h:182
Light::sensor_id
uint8_t sensor_id
Definition: vive.h:205
Tracker::cal
calibration_t cal
Definition: vive.h:154
MAX_SERIAL_LENGTH
#define MAX_SERIAL_LENGTH
Definition: vive.h:38
General
Definition: vive.h:192
tracker_t
struct Tracker tracker_t
Definition: vive.h:65
light_t
struct Light light_t
Lighthouse
Definition: vive.h:178
OOTX::state
state_t state
Definition: vive.h:109
PerSweepData
Definition: vive.h:126
IMU
Definition: vive.h:209
Tracker::charge
uint8_t charge
Definition: vive.h:157
Driver::tracker_cb
tracker_cb_t tracker_cb
Definition: vive.h:238
Lighthouse::fw_version
uint16_t fw_version
Definition: vive.h:181
Lighthouse::sys_faults
uint8_t sys_faults
Definition: vive.h:188
vive_install_lighthouse_fn
void vive_install_lighthouse_fn(driver_t *drv, lighthouse_cb_t fbp)
GlobalData
Definition: vive.h:137
button_cb_t
void(* button_cb_t)(tracker_t const *tracker, button_t const *measurement)
Definition: vive.h:226
MAX_NUM_LIGHTHOUSES
#define MAX_NUM_LIGHTHOUSES
Definition: vive.h:35
general_t
struct General general_t
LENGTH
@ LENGTH
Definition: vive.h:107
Tracker::ootx
ootx_t ootx[MAX_NUM_LIGHTHOUSES]
Definition: vive.h:156
Light
Definition: vive.h:203
OOTX::length
uint16_t length
Definition: vive.h:111
LightcapData
Definition: vive.h:141
Driver::num_trackers
uint16_t num_trackers
Definition: vive.h:233
PerSweepData::activeSweepStartTime
int activeSweepStartTime
Definition: vive.h:128
OOTX::lasttime
uint32_t lasttime
Definition: vive.h:117
Tracker::ison
uint8_t ison
Definition: vive.h:159
MOTOR_AXIS1
@ MOTOR_AXIS1
Definition: vive.h:166
Driver::lighthouses
lighthouse_t lighthouses[MAX_NUM_LIGHTHOUSES]
Definition: vive.h:240
Driver::lighthouse_cb
lighthouse_cb_t lighthouse_cb
Definition: vive.h:239
LightcapSweepData::sweep_len
uint16_t sweep_len[MAX_NUM_SENSORS]
Definition: vive.h:123
Button
Definition: vive.h:215
General::pulsedist_max_ticks
int32_t pulsedist_max_ticks
Definition: vive.h:195
GlobalData::acode_offset
double acode_offset
Definition: vive.h:138
PerSweepData::current_lh
int current_lh
Definition: vive.h:134
vive_install_light_fn
void vive_install_light_fn(driver_t *drv, light_cb_t fbp)
vive_general
general_t * vive_general(driver_t *drv)
vive_lighthouse
lighthouse_t * vive_lighthouse(driver_t *drv, const char *id)
EVENT_GRIP
@ EVENT_GRIP
Definition: vive.h:76
vive_init
driver_t * vive_init()
PAYLOAD
@ PAYLOAD
Definition: vive.h:107
PerSweepData::lh_max_pulse_length
int lh_max_pulse_length[MAX_NUM_LIGHTHOUSES]
Definition: vive.h:132
Calibration::acc_scale
float acc_scale[3]
Definition: vive.h:98
imu_t
struct IMU imu_t
Event
Event
Definition: vive.h:74
Motor::phase
float phase
Definition: vive.h:170
EVENT_PAD_CLICK
@ EVENT_PAD_CLICK
Definition: vive.h:78
PerSweepData::lh_start_time
int lh_start_time[MAX_NUM_LIGHTHOUSES]
Definition: vive.h:131
OOTX::pad
uint8_t pad
Definition: vive.h:112
DATA_WATCHMAN
@ DATA_WATCHMAN
Definition: vive.h:70
Driver::general
general_t general
Definition: vive.h:241
global_data_t
struct GlobalData global_data_t
PerSweepData::recent_sync_time
int recent_sync_time
Definition: vive.h:127
OOTX::preamble
uint8_t preamble
Definition: vive.h:116
button_t
struct Button button_t
Calibration::normals
float normals[MAX_NUM_SENSORS][3]
Definition: vive.h:96
LightcapData::sweep
lightcap_sweep_data_t sweep
Definition: vive.h:142
PREAMBLE
@ PREAMBLE
Definition: vive.h:107
Lighthouse::motors
motor_t motors[MAX_NUM_MOTORS]
Definition: vive.h:183
PerSweepData::activeLighthouse
int activeLighthouse
Definition: vive.h:129
MAX_NUM_SENSORS
#define MAX_NUM_SENSORS
Definition: vive.h:37
Tracker
Definition: vive.h:148
OOTX::data
uint8_t data[MAX_PACKET_LEN]
Definition: vive.h:110
Endpoint
Definition: vive.h:83
Endpoint::tx
struct libusb_transfer * tx
Definition: vive.h:86
Light::timestamp
uint32_t timestamp
Definition: vive.h:204
LightcapData::global
global_data_t global
Definition: vive.h:144
vive_poll
uint8_t vive_poll(driver_t *drv)
Driver::pushed
uint8_t pushed
Definition: vive.h:242
Calibration::channels
uint8_t channels[MAX_NUM_SENSORS]
Definition: vive.h:94
General::pulse_max_for_sweep
int32_t pulse_max_for_sweep
Definition: vive.h:198
OOTX::crc
uint32_t crc
Definition: vive.h:115
Calibration::gyr_scale
float gyr_scale[3]
Definition: vive.h:100
USB_INT_BUFF_LENGTH
#define USB_INT_BUFF_LENGTH
Definition: vive.h:39
EVENT_PAD_TOUCH
@ EVENT_PAD_TOUCH
Definition: vive.h:79
calibration_t
struct Calibration calibration_t
Lighthouse::accel
float accel[3]
Definition: vive.h:184
tracker_cb_t
void(* tracker_cb_t)(tracker_t const *tracker)
Definition: vive.h:227
CHECKSUM
@ CHECKSUM
Definition: vive.h:107
Motor::tilt
float tilt
Definition: vive.h:171
Axis
Axis
Definition: vive.h:166
driver_t
struct Driver driver_t
Definition: vive.h:64
Motor::gibmag
float gibmag
Definition: vive.h:173
lightcap_data_t
struct LightcapData lightcap_data_t
Tracker::type
uint16_t type
Definition: vive.h:149
Button::vertical
int16_t vertical
Definition: vive.h:219
lighthouse_t
struct Lighthouse lighthouse_t
Definition: vive.h:66
EVENT_MENU
@ EVENT_MENU
Definition: vive.h:77
Driver::button_cb
button_cb_t button_cb
Definition: vive.h:237
Calibration
Definition: vive.h:91
General::pulselength_min_sync
int32_t pulselength_min_sync
Definition: vive.h:196
Tracker::axis
uint8_t axis[3]
Definition: vive.h:160
Lighthouse::sys_unlock_count
uint8_t sys_unlock_count
Definition: vive.h:185
General::pulse_in_clear_time
int32_t pulse_in_clear_time
Definition: vive.h:197
Driver::light_cb
light_cb_t light_cb
Definition: vive.h:235
Tracker::endpoints
endpoint_t endpoints[MAX_ENDPOINTS]
Definition: vive.h:153
Calibration::gyr_bias
float gyr_bias[3]
Definition: vive.h:99
Tracker::driver
driver_t * driver
Definition: vive.h:150
Button::mask
uint32_t mask
Definition: vive.h:216
event_t
enum Event event_t
General::pulse_synctime_slack
int32_t pulse_synctime_slack
Definition: vive.h:200
IMU::acc
int16_t acc[3]
Definition: vive.h:211
LightcapData::per_sweep
per_sweep_data_t per_sweep
Definition: vive.h:143
Motor::gibphase
float gibphase
Definition: vive.h:172
Tracker::timecode
uint32_t timecode
Definition: vive.h:162
Lighthouse::timestamp
uint32_t timestamp
Definition: vive.h:179
Lighthouse::id
uint8_t id
Definition: vive.h:180
DATA_BUTTONS
@ DATA_BUTTONS
Definition: vive.h:70
Endpoint::buffer
uint8_t buffer[USB_INT_BUFF_LENGTH]
Definition: vive.h:87
Driver::trackers
tracker_t * trackers[MAX_NUM_TRACKERS]
Definition: vive.h:234
vive_install_imu_fn
void vive_install_imu_fn(driver_t *drv, imu_cb_t fbp)
Endpoint::type
data_t type
Definition: vive.h:85
light_cb_t
void(* light_cb_t)(tracker_t const *tracker, uint8_t lh, uint8_t axis, uint32_t synctime, uint16_t num_sensors, light_t const *measurement)
Definition: vive.h:223
Tracker::buttonmask
uint8_t buttonmask
Definition: vive.h:161
Light::length
uint16_t length
Definition: vive.h:206
axis_t
enum Axis axis_t
Driver::imu_cb
imu_cb_t imu_cb
Definition: vive.h:236
MAX_NUM_MOTORS
@ MAX_NUM_MOTORS
Definition: vive.h:166
Calibration::num_channels
uint8_t num_channels
Definition: vive.h:93
DATA_IMU
@ DATA_IMU
Definition: vive.h:70
Endpoint::tracker
tracker_t * tracker
Definition: vive.h:84
OOTX::lighthouse
lighthouse_t * lighthouse
Definition: vive.h:118
vive_install_tracker_fn
void vive_install_tracker_fn(driver_t *drv, tracker_cb_t fbp)
Tracker::ischarging
uint8_t ischarging
Definition: vive.h:158
Data
Data
Definition: vive.h:69
DATA_LIGHT_7
@ DATA_LIGHT_7
Definition: vive.h:70
IMU::timestamp
uint32_t timestamp
Definition: vive.h:210
vive_install_button_fn
void vive_install_button_fn(driver_t *drv, button_cb_t fbp)
Lighthouse::mode_current
uint8_t mode_current
Definition: vive.h:187
Tracker::serial
char serial[MAX_SERIAL_LENGTH]
Definition: vive.h:152
PerSweepData::lh_acode
int8_t lh_acode[MAX_NUM_LIGHTHOUSES]
Definition: vive.h:133
Button::horizontal
int16_t horizontal
Definition: vive.h:218
Motor
Definition: vive.h:169
Driver::usb
struct libusb_context * usb
Definition: vive.h:232