-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyowi.c
199 lines (169 loc) · 5.33 KB
/
pyowi.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
/*
* Adapter code to allow access to the owi.c functions from Python.
*/
#include <Python.h>
#include <time.h>
#include <stdbool.h>
#include "libowi.h"
static bool parse_usleep_arg(PyObject *args, long *out_usleep_time) {
double sleep_time_seconds = 0.0;
if (!PyArg_ParseTuple(args, "d", &sleep_time_seconds)) {
return false;
}
long usleep_time = (long) (sleep_time_seconds * 1000000);
*out_usleep_time = usleep_time;
return true;
}
static PyObject * py_owi_init(PyObject *self, PyObject *args) {
owi_init();
return Py_None;
}
static PyObject * py_owi_shutdown(PyObject *self, PyObject *args) {
owi_shutdown();
return Py_None;
}
static PyObject * py_owi_toggle_light(PyObject *self, PyObject *args) {
owi_toggle_light();
return Py_None;
}
static PyObject * py_owi_light_on(PyObject *self, PyObject *args) {
owi_light_on();
return Py_None;
}
static PyObject * py_owi_light_off(PyObject *self, PyObject *args) {
owi_light_off();
return Py_None;
}
static PyObject * py_owi_base_left(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_base_clockwise();
usleep(usleep_time);
owi_base_off();
return Py_None;
}
static PyObject * py_owi_base_right(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_base_counterclockwise();
usleep(usleep_time);
owi_base_off();
return Py_None;
}
static PyObject * py_owi_shoulder_up(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_m4_forward();
usleep(usleep_time);
owi_m4_off();
return Py_None;
}
static PyObject * py_owi_shoulder_down(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_m4_reverse();
usleep(usleep_time);
owi_m4_off();
return Py_None;
}
static PyObject * py_owi_elbow_up(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_m3_forward();
usleep(usleep_time);
owi_m3_off();
return Py_None;
}
static PyObject * py_owi_elbow_down(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_m3_reverse();
usleep(usleep_time);
owi_m3_off();
return Py_None;
}
static PyObject * py_owi_wrist_up(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_m2_forward();
usleep(usleep_time);
owi_m2_off();
return Py_None;
}
static PyObject * py_owi_wrist_down(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_m2_reverse();
usleep(usleep_time);
owi_m2_off();
return Py_None;
}
static PyObject * py_owi_grip_open(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_m1_open();
usleep(usleep_time);
owi_m1_off();
return Py_None;
}
static PyObject * py_owi_grip_close(PyObject *self, PyObject *args) {
long usleep_time;
if (!parse_usleep_arg(args, &usleep_time)) {
return NULL;
}
owi_m1_close();
usleep(usleep_time);
owi_m1_off();
return Py_None;
}
static PyObject * py_owi_stop(PyObject *self, PyObject *args) {
owi_stop();
return Py_None;
}
static PyMethodDef OwiMethods[] = {
{ "init", py_owi_init, METH_VARARGS, "Initialize the OWI robot arm via USB." },
{ "shutdown", py_owi_shutdown, METH_VARARGS, "Shut down the OWI robot arm via USB." },
{ "light_toggle", py_owi_toggle_light, METH_VARARGS, "Toggle the light on or off." },
{ "light_on", py_owi_light_on, METH_VARARGS, "Turn the light on." },
{ "light_off", py_owi_light_off, METH_VARARGS, "Turn the light off." },
{ "base_left", py_owi_base_left, METH_VARARGS, "Turn the base left for the given number of seconds." },
{ "base_right", py_owi_base_right, METH_VARARGS, "Turn the base right for the given number of seconds." },
{ "shoulder_up", py_owi_shoulder_up, METH_VARARGS, "Move the shoulder up for the given number of seconds." },
{ "shoulder_down", py_owi_shoulder_down, METH_VARARGS, "Move the shoulder down for the given number of seconds." },
{ "elbow_up", py_owi_elbow_up, METH_VARARGS, "Move the elbow up for the given number of seconds." },
{ "elbow_down", py_owi_elbow_down, METH_VARARGS, "Move the elbow down for the given number of seconds." },
{ "wrist_up", py_owi_wrist_up, METH_VARARGS, "Move the wrist up for the given number of seconds." },
{ "wrist_down", py_owi_wrist_down, METH_VARARGS, "Move the wrist down for the given number of seconds." },
{ "grip_open", py_owi_grip_open, METH_VARARGS, "Open the grip for the given number of seconds." },
{ "grip_close", py_owi_grip_close, METH_VARARGS, "Close the grip for the given number of seconds." },
{ "stop", py_owi_stop, METH_VARARGS, "Stop all motion of the robot arm and turn off the light." },
{ NULL, NULL, 0, NULL } /* sentinal */
};
static struct PyModuleDef owimodule = {
PyModuleDef_HEAD_INIT,
"owi",
NULL,
-1,
OwiMethods
};
PyMODINIT_FUNC PyInit_owi(void) {
return PyModule_Create(&owimodule);
}