-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoyd.c
More file actions
388 lines (347 loc) · 10.9 KB
/
Copy pathjoyd.c
File metadata and controls
388 lines (347 loc) · 10.9 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* -*- mode: C; c-basic-offset: 4 -*- */
/* ex: set shiftwidth=4 expandtab: */
/*
* Copyright (c) 2013, Georgia Tech Research Corporation
* All rights reserved.
*
* Author(s): Neil T. Dantam <ntd@gatech.edu>
* Georgia Tech Humanoid Robotics Lab
* Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
*
*
* This file is provided under the following "BSD-style" License:
*
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
/** Author: jscholz, Neil Dantam
*/
#include <argp.h>
#include <syslog.h>
#include <sns.h>
#include <signal.h>
#include <unistd.h>
#include <inttypes.h>
#include "js.h"
// context struct
typedef struct {
// CLI options
const char *opt_chan_name;
uint8_t opt_jsdev;
uint32_t opt_axis_cnt;
size_t opt_button_cnt;
double opt_deadzone; // TODO: add CLI arg to set this
struct timespec opt_period; // provide message at least every period
unsigned opt_axis_num;
struct {
double initial;
double offset;
double scale;
} opt_axis[JS_AXIS_CNT];
// Running vars
ach_channel_t chan;
timer_t timer;
js_t *js;
struct sns_msg_joystick *msg;
clockid_t clock;
} cx_t;
/* ---------- */
/* ARGP Junk */
/* ---------- */
static struct argp_option options[] = {
{
.name = "jsdev",
.key = 'j',
.arg = "device-num",
.flags = 0,
.doc = "specifies which device to use (default 0)"
},
{
.name = "verbose",
.key = 'v',
.arg = NULL,
.flags = 0,
.doc = "Causes verbose output"
},
{
.name = "quiet",
.key = 'q',
.arg = NULL,
.flags = 0,
.doc = "Less verbose output"
},
{
.name = "channel",
.key = 'c',
.arg = "channel",
.flags = 0,
.doc = "ach channel to use"
},
{
.name = "axes",
.key = 'a',
.arg = "axis-count",
.flags = 0,
.doc = "number of joystick axes (default to 6)"
},
{
.name = "axis",
.key = 'i',
.arg = "axis-number",
.flags = 0,
.doc = "an axis to set options for"
},
{
.name = "axis-initial",
.key = '0',
.arg = "initial-value",
.flags = 0,
.doc = "set initial value for an axis"
},
{
.name = "scale",
.key = 's',
.arg = "factor",
.flags = 0,
.doc = "multiply axis by this value"
},
{
.name = "offset",
.key = 'o',
.arg = "increment",
.flags = 0,
.doc = "add this value to axis"
},
{
.name = NULL,
.key = 0,
.arg = NULL,
.flags = 0,
.doc = NULL
}
};
/// argp parsing function
static int parse_opt( int key, char *arg, struct argp_state *state);
/// argp program version
const char *argp_program_version = "jachd-0.0.1";
/// argp program arguments documentation
static char args_doc[] = "";
/// argp program doc line
static char doc[] = "reads from linux joystick and pushes out ach messages";
/// argp object
static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL };
static int parse_opt( int key, char *optarg, struct argp_state *state) {
cx_t *cx = (cx_t*)state->input;
switch(key) {
SNS_OPTCASES
case 'j':
cx->opt_jsdev = (uint8_t)atoi(optarg);
break;
case 'c':
cx->opt_chan_name = strdup( optarg );
break;
case 'a':
cx->opt_axis_cnt = (uint32_t)atoi(optarg);
SNS_REQUIRE( cx->opt_axis_cnt <= JS_AXIS_CNT,
"Max %d axes\n", JS_AXIS_CNT );
break;
case 'i':
cx->opt_axis_num = (uint32_t)atoi(optarg);
SNS_REQUIRE( cx->opt_axis_num <= JS_AXIS_CNT,
"Max %d axes\n", JS_AXIS_CNT );
break;
case '0':
cx->opt_axis[cx->opt_axis_num].initial = atof(optarg);
break;
case 's':
cx->opt_axis[cx->opt_axis_num].scale = atof(optarg);
break;
case 'o':
cx->opt_axis[cx->opt_axis_num].offset = atof(optarg);
break;
case 0:
break;
}
//somatic_d_argp_parse( key, arg, &cx->d_opts );
return 0;
}
/* ------------- */
/* Function Defs */
/* ------------- */
/**
* Block, waiting for a mouse event
*/
static int jach_read_to_msg( cx_t *cx )
{
int status = js_poll_state( cx->js );
if( !status ) {
for( size_t i = 0; i < cx->msg->header.n && i < JS_AXIS_CNT; i++ ) {
double x = aa_fdeadzone( cx->js->state.axes[i], -cx->opt_deadzone, cx->opt_deadzone, 0.0 );
//double x = cx->js->state.axes[i];
cx->msg->axis[i] = (x*cx->opt_axis[i].scale) + cx->opt_axis[i].offset;
}
cx->msg->buttons = 0;
for( size_t i = 0; i < sizeof(cx->msg->buttons)*8 && i < JS_BUTTON_CNT; i++ ) {
cx->msg->buttons |= ( (uint64_t)(cx->js->state.buttons[i] ? 1 : 0) << i );
}
if( SNS_LOG_PRIORITY( LOG_DEBUG ) && isatty(STDERR_FILENO) ) {
sns_msg_joystick_dump( stderr, cx->msg );
}
}
return status;
}
static void timer_handler(int sig) {
// do nothing, the read will get EINTR
(void)sig;
}
static int create_timer(cx_t *cx) {
struct sigevent sev;
struct itimerspec its;
int r;
struct sigaction sa;
// setup sighandler
memset(&sa,0,sizeof(sa));
sa.sa_handler = timer_handler;
sigemptyset(&sa.sa_mask);
if( 0 != (r = sigaction(SIGALRM, &sa, NULL)) ) {
syslog(LOG_WARNING, "failed sigaction: %s", strerror(errno));
return r;
}
// create timer
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGALRM;
sev.sigev_value.sival_ptr = &cx->timer;
if( 0 != (r = timer_create(CLOCK_MONOTONIC, &sev, &cx->timer)) ) {
syslog(LOG_WARNING, "failed timer_create: %s", strerror(errno));
return r;
}
// start
its.it_value = cx->opt_period;
its.it_interval = cx->opt_period;
if( 0 != (r = timer_settime(cx->timer, 0, &its, NULL)) ) {
syslog(LOG_WARNING, "failed timer_settime: %s", strerror(errno));
return r;
}
return 0;
}
static void jach_run( cx_t *cx ) {
(void)cx;
// main loop
int64_t period_ns = cx->opt_period.tv_sec * (int64_t)1e9 + cx->opt_period.tv_nsec;
while (!sns_cx.shutdown) {
int status = jach_read_to_msg( cx );
if( !status || (EINTR == errno) ) {
// ok, send the message
struct timespec now;
clock_gettime( cx->clock, &now );
sns_msg_set_time(&cx->msg->header, &now, 2*period_ns);
cx->msg->header.seq++;
ach_status_t r = ach_put( &cx->chan, cx->msg,
sns_msg_joystick_size( cx->msg ) );
SNS_CHECK( ACH_OK == r, LOG_EMERG, 0,
"Failed to put joystick message: %s", ach_result_to_string(r) );
} else if( EAGAIN == errno ) {
// some system limit, try again
sns_event( LOG_ERR, 0, "joystick EAGAIN" );
} else {
SNS_DIE( "joystick failure: %s", strerror(errno) );
}
}
}
/* ---- */
/* MAIN */
/* ---- */
int main( int argc, char **argv ) {
static cx_t cx;
memset(&cx, 0, sizeof(cx));
for( size_t i = 0; i < JS_AXIS_CNT; i++ ) {
cx.opt_axis[i].offset = 0;
cx.opt_axis[i].scale = 1;
cx.opt_axis[i].initial = 0;
}
// default options
cx.opt_chan_name = "joystick";
//cx.d_opts.ident = "jachd";
//cx.d_opts.sched_rt = SOMATIC_D_SCHED_UI;
cx.opt_jsdev = 0;
cx.opt_axis_cnt = 6;
cx.opt_button_cnt = 10;
cx.opt_deadzone = 1e-4;
cx.opt_period = aa_tm_sec2timespec(1.0 / 30.0);
argp_parse (&argp, argc, argv, 0, NULL, &cx);
//-- initialize --
sns_init();
sns_sigcancel( NULL, sns_sig_term_default );
cx.msg = sns_msg_joystick_heap_alloc( cx.opt_axis_cnt );
// Open joystick device
cx.js = js_open( cx.opt_jsdev );
SNS_REQUIRE( NULL != cx.js,
"joystick: %s", strerror(errno) );
for( size_t i = 0; i < cx.opt_axis_cnt; i++ ) {
cx.msg->axis[i] = cx.opt_axis[i].initial;
cx.js->state.axes[i] = cx.opt_axis[i].initial;
}
// open channel
{
ach_attr_t attr;
ach_attr_init(&attr);
ach_attr_set_lock_source( &attr, 1 );
sns_chan_open( &cx.chan, cx.opt_chan_name, &attr );
}
cx.clock = ACH_DEFAULT_CLOCK;
//Somatic__Joystick *msg = somatic_joystick_alloc(cx.opt_axis_cnt, cx.opt_button_cnt);
SNS_LOG( LOG_DEBUG, "\n* JSD *\n");
SNS_LOG( LOG_DEBUG, "jsdev: %d\n", cx.opt_jsdev);
SNS_LOG( LOG_DEBUG, "js channel: %s\n", cx.opt_chan_name);
SNS_LOG( LOG_DEBUG, "js period: %fs\n", aa_tm_timespec2sec(cx.opt_period));
if( SNS_LOG_PRIORITY( LOG_DEBUG ) ) {
for( size_t i = 0; i < cx.opt_axis_cnt; i++ ) {
SNS_LOG( LOG_DEBUG, "axis %"PRIuPTR": y0=%f, y=x*%f+%f\n",
i, cx.opt_axis[i].initial,
cx.opt_axis[i].scale,
cx.opt_axis[i].offset );
}
}
//fprintf(stderr, "message size: %"PRIuPTR"\n", somatic__joystick__get_packed_size(msg) );
// This gives read() an EINTR when the timer expires,
// so we can republish the message instead of blocking on the joystick read.
// Note that blocks on ach channels take an expiration time directly, so
// no timer would be needed for waits there.
if( create_timer(&cx) ) {
SNS_LOG(LOG_WARNING, "Couldn't create timer, joyd will block: %s", strerror(errno));
}
// run
sns_start();
jach_run( &cx );
// Cleanup:
//somatic_joystick_free(msg);
sns_chan_close( &cx.chan );
sns_end();
js_close(cx.js);
return 0;
}