/
forcefeedback.cpp
417 lines (361 loc) · 11.5 KB
/
forcefeedback.cpp
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/************************************************************************/
/* */
/* This file is part of VDrift. */
/* */
/* VDrift is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* VDrift is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with VDrift. If not, see <http://www.gnu.org/licenses/>. */
/* */
/************************************************************************/
#include "forcefeedback.h"
#include <cstring>
#if SDL_VERSION_ATLEAST(2,0,0)
FORCEFEEDBACK::FORCEFEEDBACK(
std::string device,
std::ostream & error_output,
std::ostream & info_output) :
device_name(device),
enabled(true),
stop_and_play(false),
lastforce(0),
haptic(0),
effect_id(-1)
{
// Close haptic if already open.
if (haptic)
SDL_HapticClose(haptic);
// Does the device support haptic?
//if (!SDL_JoystickIsHaptic(joystick))
// return;
// Try to create haptic device.
//haptic = SDL_HapticOpenFromJoystick(joystick);
// Does the device support haptic?
int haptic_id = 0;
int haptic_num = SDL_NumHaptics();/*
while (haptic_id < haptic_num)
{
std::string haptic_name(SDL_HapticName(haptic_id++));
if (haptic_name == device_name)
break;
}*/
if (haptic_id == haptic_num)
return;
// Try to create haptic device.
haptic = SDL_HapticOpen(haptic_id);
if (!haptic)
{
error_output << "Failed to initialize force feedback device: " << SDL_GetError();
return;
}
// Check for constant force support.
unsigned int haptic_query = SDL_HapticQuery(haptic);
if (!(haptic_query & SDL_HAPTIC_CONSTANT))
{
SDL_HapticClose(haptic);
haptic = NULL;
return;
}
// Create the effect.
memset(&effect, 0, sizeof(SDL_HapticEffect) ); // 0 is safe default
effect.type = SDL_HAPTIC_CONSTANT;
effect.constant.direction.type = SDL_HAPTIC_CARTESIAN; // Using cartesian direction encoding.
effect.constant.direction.dir[0] = 1; // X position
effect.constant.direction.dir[1] = 0; // Y position
effect.constant.length = 0xffff;
effect.constant.delay = 0;
effect.constant.button = 0;
effect.constant.interval = 0;
effect.constant.level = 0;
effect.constant.attack_length = 0;
effect.constant.attack_level = 0;
effect.constant.fade_length = 0;
effect.constant.fade_level = 0;
// Upload the effect.
effect_id = SDL_HapticNewEffect(haptic, &effect);
if (effect_id == -1)
{
error_output << "Failed to initialize force feedback effect: " << SDL_GetError();
return;
}
info_output << "Force feedback enabled." << std::endl;
}
FORCEFEEDBACK::~FORCEFEEDBACK()
{
if (haptic)
SDL_HapticClose(haptic);
}
void FORCEFEEDBACK::update(
double force,
double * position,
double dt,
std::ostream & error_output)
{
if (!enabled || !haptic || (effect_id != -1))
return;
// Clamp force.
if (force > 1.0) force = 1.0;
if (force < -1.0) force = -1.0;
// Low pass filter.
lastforce = (lastforce + force) * 0.5;
// Update effect.
effect.constant.level = Sint16(lastforce * 32767.0);
int new_effect_id = SDL_HapticUpdateEffect(haptic, effect_id, &effect);
if (new_effect_id == -1)
{
//error_output << "Failed to update force feedback effect: " << SDL_GetError();
//return;
}
else
{
effect_id = new_effect_id;
}
// Run effect.
if (SDL_HapticRunEffect(haptic, effect_id, 1) == -1)
{
error_output << "Failed to run force feedback effect: " << SDL_GetError();
return;
}
}
#elif defined(linux) || defined(__linux)
#include <linux/input.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
using std::string;
using std::endl;
//#define TEST_BIT(bit,bits) (((bits[bit>>5]>>(bit&0x1f))&1)!=0)
/* Number of bits for 1 unsigned char */
#define nBitsPerUchar (sizeof(unsigned char) * 8)
/* Number of unsigned chars to contain a given number of bits */
#define nUcharsForNBits(nBits) ((((nBits)-1)/nBitsPerUchar)+1)
/* Index=Offset of given bit in 1 unsigned char */
#define bitOffsetInUchar(bit) ((bit)%nBitsPerUchar)
/* Index=Offset of the unsigned char associated to the bit
at the given index=offset */
#define ucharIndexForBit(bit) ((bit)/nBitsPerUchar)
/* Value of an unsigned char with bit set at given index=offset */
#define ucharValueForBit(bit) (((unsigned char)(1))<<bitOffsetInUchar(bit))
/* Test the bit with given index=offset in an unsigned char array */
#define testBit(bit, array) ((array[ucharIndexForBit(bit)] >> bitOffsetInUchar(bit)) & 1)
FORCEFEEDBACK::FORCEFEEDBACK(
string device,
std::ostream & error_output,
std::ostream & info_output) :
device_name(device),
enabled(true),
stop_and_play(false),
lastforce(0)
{
unsigned char key_bits[1 + KEY_MAX/8/sizeof(unsigned char)];
unsigned char abs_bits[1 + ABS_MAX/8/sizeof(unsigned char)];
unsigned char ff_bits[1 + FF_MAX/8/sizeof(unsigned char)];
struct input_event event;
int valbuf[16];
// Open event device with write permission
device_handle = open(device_name.c_str(),O_RDWR|O_NONBLOCK);
if (device_handle<0)
{
error_output << "force feedback: can not open " << device_name << " (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
// Which buttons has the device?
memset(key_bits,0,sizeof(key_bits));
if (ioctl(device_handle,EVIOCGBIT(EV_KEY,sizeof(key_bits)),key_bits)<0)
{
error_output << "force feedback: can not get key bits (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
// Which axes has the device?
memset(abs_bits,0,sizeof(abs_bits));
if (ioctl(device_handle,EVIOCGBIT(EV_ABS,sizeof(abs_bits)),abs_bits)<0)
{
error_output << "force feedback: can not get abs bits (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
// Now get some information about force feedback
memset(ff_bits,0,sizeof(ff_bits));
if (ioctl(device_handle,EVIOCGBIT(EV_FF ,sizeof(ff_bits)),ff_bits)<0)
{
error_output << "force feedback: can not get ff bits (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
// Which axis is the x-axis?
if (testBit(ABS_X ,abs_bits)) axis_code=ABS_X;
else if (testBit(ABS_RX ,abs_bits)) axis_code=ABS_RX;
else if (testBit(ABS_WHEEL,abs_bits)) axis_code=ABS_WHEEL;
else
{
error_output << "force feedback: no suitable x-axis found [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
// get axis value range
if (ioctl(device_handle,EVIOCGABS(axis_code),valbuf)<0)
{
error_output << "force feedback: can not get axis value range (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
axis_min=valbuf[1];
axis_max=valbuf[2];
if (axis_min>=axis_max)
{
error_output << "force feedback: bad axis value range (" << axis_min << "," << axis_max << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
// force feedback supported?
if (!testBit(FF_CONSTANT,ff_bits))
{
error_output << "force feedback: device (or driver) has no force feedback support [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
// Switch off auto centering
memset(&event,0,sizeof(event));
event.type=EV_FF;
event.code=FF_AUTOCENTER;
event.value=0;
if (write(device_handle,&event,sizeof(event))!=sizeof(event))
{
error_output << "force feedback: failed to disable auto centering (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
// Initialize constant force effect
memset(&effect,0,sizeof(effect));
effect.type=FF_CONSTANT;
effect.id=-1;
effect.trigger.button=0;
effect.trigger.interval=0;
effect.replay.length=0xffff;
effect.replay.delay=0;
effect.u.constant.level=0;
effect.direction=0xC000;
effect.u.constant.envelope.attack_length=0;
effect.u.constant.envelope.attack_level=0;
effect.u.constant.envelope.fade_length=0;
effect.u.constant.envelope.fade_level=0;
// Upload effect
if (ioctl(device_handle,EVIOCSFF,&effect)==-1)
{
error_output << "force feedback: uploading effect failed (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
/* Start effect */
memset(&event,0,sizeof(event));
event.type=EV_FF;
event.code=effect.id;
event.value=1;
if (write(device_handle,&event,sizeof(event))!=sizeof(event))
{
error_output << "force feedback: starting effect failed (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
info_output << "Force feedback initialized successfully" << std::endl;
}
FORCEFEEDBACK::~FORCEFEEDBACK()
{
// dtor
}
void FORCEFEEDBACK::update(
double force,
double * position,
double dt,
std::ostream & error_output)
{
if ( !enabled )
return;
struct input_event event;
// Delete effect
if (stop_and_play && effect.id!=-1)
{
if (ioctl(device_handle,EVIOCRMFF,effect.id)==-1) {
error_output << "force feedback: removing effect failed (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
effect.id=-1;
}
// Clamp force
if (force>1.0) force=1.0;
if (force<-1.0) force=-1.0;
// Low pass filter
lastforce = (lastforce + force) * 0.5;
effect.direction=0xC000;
effect.u.constant.level=(short)(lastforce * 32767.0); /* only to be safe */
effect.u.constant.envelope.attack_level=effect.u.constant.level;
effect.u.constant.envelope.fade_level=effect.u.constant.level;
// Upload effect
if (ioctl(device_handle,EVIOCSFF,&effect)==-1)
{
//error_output << "error uploading effect" << endl;
/* We do not exit here. Indeed, too frequent updates may be
* refused, but that is not a fatal error */
}
else if (effect.id!=-1)
// Start effect
//if (stop_and_play && effect.id!=-1)
{
memset(&event,0,sizeof(event));
event.type=EV_FF;
event.code=effect.id;
event.value=1;
if (write(device_handle,&event,sizeof(event))!=sizeof(event))
{
error_output << "force feedback: re-starting effect failed (" << strerror(errno) << ") [" << __FILE__ << ":" << __LINE__ << "]" << endl;
disable();
return;
}
}
// Get events
while (read(device_handle,&event,sizeof(event))==sizeof(event))
{
if (event.type==EV_ABS && event.code==axis_code)
{
*position=((double)(((short)event.value)-axis_min))*2.0/(axis_max-axis_min)-1.0;
if (*position>1.0) *position=1.0;
else if (*position<-1.0) *position=-1.0;
}
}
}
#else
FORCEFEEDBACK::FORCEFEEDBACK(
std::string device,
std::ostream & error_output,
std::ostream & info_output)
{
// Constructor
}
FORCEFEEDBACK::~FORCEFEEDBACK()
{
// Destructor
}
void FORCEFEEDBACK::update(
double force,
double * position,
double dt,
std::ostream & error_output)
{
// No force feedback support
}
#endif
void FORCEFEEDBACK::disable()
{
enabled = false;
}