-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibowi.c
269 lines (223 loc) · 4.56 KB
/
libowi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* OWI Edge Robotic Arm Controller
*
* Harold Lee
* harold@hotelling.net
*
* Based on the code found at:
* http://notbrainsurgery.livejournal.com/38622.html
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <libusb-1.0/libusb.h>
#include "libowi.h"
#define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
#define ARM_VENDOR 0x1267
#define ARM_PRODUCT 0x0000
#define CMD_DATALEN 3
/* Current action being executed by the arm. */
static unsigned char cmd[3];
static struct libusb_device_handle *devh = NULL;
static libusb_device **devs = NULL;
static libusb_device * _owi_find_arm(libusb_device **devs)
{
libusb_device *dev;
int i = 0;
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
fprintf(stderr, "Failed to get device descriptor\n");
return NULL;
}
if(desc.idVendor == ARM_VENDOR &&
desc.idProduct == ARM_PRODUCT)
{
return dev;
}
}
return NULL;
}
static libusb_device ** _owi_detect_robot_arm()
{
libusb_device **devs;
libusb_device *dev;
int r;
ssize_t cnt;
r = libusb_init(NULL);
if (r < 0) {
fprintf(stderr, "Failed to initialize libusb.\n");
return NULL;
}
libusb_set_debug(NULL,2);
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return NULL;
dev = _owi_find_arm(devs);
if(!dev) {
fprintf(stderr, "Robot Arm not found.\n");
return NULL;
}
r = libusb_open(dev,&devh);
if(r!=0) {
fprintf(stderr, "Error opening USB device for Robot Arm.\n");
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return NULL;
}
return devs;
}
void owi_init()
{
devs = _owi_detect_robot_arm();
if (devs == NULL) {
devh = NULL;
return;
}
cmd[0] = 0;
cmd[1] = 0;
cmd[2] = 0;
owi_send_command(0, 0, 0);
}
int owi_send_command(unsigned char b1, unsigned char b2, unsigned char b3)
{
cmd[0] = b1;
cmd[1] = b2;
cmd[2] = b3;
fprintf(stderr, "Sending %02X %02X %02X\n",
(int)cmd[0], (int)cmd[1], (int)cmd[2]);
if (devh == NULL) {
return 0;
}
int actual_length = -1;
int r;
r = libusb_control_transfer(devh,
0x40, //uint8_t bmRequestType,
6, //uint8_t bRequest,
0x100, //uint16_t wValue,
0,//uint16_t wIndex,
cmd,
CMD_DATALEN,
0);
if(!(r == 0 && actual_length >= CMD_DATALEN)) {
fprintf(stderr, "Write err %d. len=%d\n",r,actual_length);
}
return r;
}
int owi_stop()
{
return owi_send_command(0, 0, 0);
}
int owi_toggle_light()
{
cmd[2] = 1 - cmd[2];
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_light_off()
{
cmd[2] = 0;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_light_on()
{
cmd[2] = 1;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m4_forward()
{
cmd[0] &= 0x3f;
cmd[0] |= 0x80;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m4_off()
{
cmd[0] &= 0x3f;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m4_reverse()
{
cmd[0] &= 0x3f;
cmd[0] |= 0x40;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m3_forward()
{
cmd[0] &= 0xcf;
cmd[0] |= 0x20;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m3_off()
{
cmd[0] &= 0xcf;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m3_reverse()
{
cmd[0] &= 0xcf;
cmd[0] |= 0x10;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m2_forward()
{
cmd[0] &= 0xf3;
cmd[0] |= 0x08;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m2_off()
{
cmd[0] &= 0xf3;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m2_reverse()
{
cmd[0] &= 0xf3;
cmd[0] |= 0x04;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m1_close()
{
cmd[0] &= 0xfc;
cmd[0] |= 0x01;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m1_off()
{
cmd[0] &= 0xfc;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_m1_open()
{
cmd[0] &= 0xfc;
cmd[0] |= 0x02;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_base_clockwise()
{
cmd[1] = 2;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_base_off()
{
cmd[1] = 0;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
int owi_base_counterclockwise()
{
cmd[1] = 1;
return owi_send_command(cmd[0], cmd[1], cmd[2]);
}
void owi_shutdown()
{
/* all stop! */
owi_send_command(0, 0, 0);
if (devh != NULL) {
libusb_close(devh);
}
if (devs != NULL) {
libusb_free_device_list(devs, 1);
}
libusb_exit(NULL);
}