-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfg-haptic.c
More file actions
1019 lines (856 loc) · 30.8 KB
/
fg-haptic.c
File metadata and controls
1019 lines (856 loc) · 30.8 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Haptic
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_haptic.h>
#include <SDL2/SDL_net.h>
#include <stdio.h> /* printf */
#include <string.h> /* strstr */
#include <ctype.h> /* isdigit */
#include <stdbool.h> /* bool */
#include <math.h>
#include <signal.h>
#include <time.h>
/* From fgfsclient */
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdarg.h>
#define DFLTHOST "localhost"
#define DFLTPORT 5401
#define MAXMSG 512
#define fgfsclose SDLNet_TCP_Close
#define NAMELEN 30
// Currently supported effects:
// 0) Constant force = pilot G and control surface loading
// 1) Rumble = stick shaker
#define TIMEOUT 1 // 1 sec
#define READ_TIMEOUT 5 // 5 secs
#define CONN_TIMEOUT 30 // 30 seconds
#define AXES 3 // Maximum axes supported
#define CONST_X 0
#define CONST_Y 1
#define CONST_Z 2
#define STICK_SHAKER 3
#define FRICTION 4
#define DAMPER 5
#define EFFECTS 9
const char axes[AXES] = { 'x', 'y', 'z' };
//void init_sockaddr(struct sockaddr_in *name, const char *hostname, unsigned port);
TCPsocket fgfsconnect(const char *hostname, const int port, bool server);
int fgfswrite(TCPsocket sock, char *msg, ...);
const char *fgfsread(TCPsocket sock, int wait);
void fgfsflush(TCPsocket sock);
// Socket used to communicate with flightgear
TCPsocket telnet_sock, server_sock, client_sock;
SDLNet_SocketSet socketset;
// Effect struct definitions, used to store parameters
typedef struct __effectParams {
float pilot[AXES];
float stick[AXES];
int shaker_trigger;
float rumble_period; // Ground rumble period, 0=disable
float x; // Forces
float y;
float z;
} effectParams;
int num_devices;
typedef struct __hapticdevice {
SDL_Haptic *device;
char name[NAMELEN + 1]; // Name
unsigned int num; // Num of this device
unsigned int supported; // Capabilities
unsigned int axes; // Count of axes
unsigned int numEffects, numEffectsPlaying;
bool open;
SDL_HapticEffect effect[EFFECTS];
int effectId[EFFECTS];
effectParams params;
// Configuration
float autocenter;
float gain;
unsigned short shaker_dir;
unsigned short shaker_period;
float pilot_gain;
float stick_gain;
float shaker_gain;
float rumble_gain;
// TODO: Possibility to invert axes
signed char pilot_axes[AXES]; // Axes mapping, -1 = not used
signed char stick_axes[AXES];
unsigned int last_rumble;
float lowpass; // Low pass filter tau, in ms
} hapticDevice;
static hapticDevice *devices = NULL;
bool reconf_request = false;
bool quit = false;
effectParams new_params;
/*
* prototypes
*/
void abort_execution(int signal);
void HapticPrintSupported(SDL_Haptic * haptic);
float clamp(float x, float l, float h)
{
return ((x) > (h) ? (h) : ((x) < (l) ? (l) : (x)));
}
void init_haptic(void)
{
/* Initialize the force feedbackness */
SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
// Initialize network
SDLNet_Init();
num_devices = SDL_NumHaptics();
printf("%d Haptic devices detected.\n", num_devices);
devices = (hapticDevice *) malloc(num_devices * sizeof(hapticDevice));
if (!devices) {
printf("Fatal error: Could not allocate memory for devices!\n");
abort_execution(-1);
}
// Zero
memset(devices, 0, num_devices * sizeof(hapticDevice));
// Send all devices' data to flightgear
for (int i = 0; i < num_devices; i++) {
devices[i].num = i + 1; // Add one, so we get around flightgear reading empty properties as 0
devices[i].device = SDL_HapticOpen(i);
if (devices[i].device) {
devices[i].open = true;
HapticPrintSupported(devices[i].device);
// Copy devices name with ascii
const char *p = SDL_HapticName(i);
strncpy(devices[i].name, p, NAMELEN);
// Add device number after name, if there is multiples with same name
for (int a = 0; a < i; a++) {
if (strcmp(devices[i].name, devices[a].name) == 0) {
size_t len = strlen(devices[i].name);
if (len < NAMELEN - 2) { // Enough space to add number after name
devices[i].name[len] = ' ';
devices[i].name[len + 1] = '1' + i;
} else {
devices[i].name[NAMELEN - 2] = ' ';
devices[i].name[NAMELEN - 1] = '1' + i;
}
}
}
printf("Device %d name is %s\n", devices[i].num, devices[i].name);
// Capabilities
devices[i].supported = SDL_HapticQuery(devices[i].device);
devices[i].axes = SDL_HapticNumAxes(devices[i].device);
if (devices[i].axes > AXES)
devices[i].axes = AXES;
devices[i].numEffects = SDL_HapticNumEffects(devices[i].device);
devices[i].numEffectsPlaying = SDL_HapticNumEffectsPlaying(devices[i].device);
// Default effect parameters
for (int a = 0; a < devices[i].axes && a < AXES; a++) {
devices[i].pilot_axes[a] = a;
devices[i].stick_axes[a] = a;
}
devices[i].autocenter = 0.0;
devices[i].gain = 1.0;
devices[i].pilot_gain = 0.1;
devices[i].stick_gain = 1.0;
devices[i].shaker_gain = 1.0;
devices[i].shaker_period = 100.0;
devices[i].rumble_gain = 0.4;
devices[i].lowpass = 300.0;
} else {
printf("Unable to open haptic devices %d: %s\n", i, SDL_GetError());
devices[i].open = false;
}
}
/* We only want force feedback errors. */
SDL_ClearError();
}
void send_devices(void)
{
// Init general properties
fgfswrite(telnet_sock, "set /haptic/reconfigure 0");
// Init devices
for (int i = 0; i < num_devices; i++) {
// Write devices to flightgear
fgfswrite(telnet_sock, "set /haptic/device[%d]/number %d", i, devices[i].num);
fgfswrite(telnet_sock, "set /haptic/device[%d]/name %s", i, devices[i].name);
fgfswrite(telnet_sock, "set /haptic/device[%d]/supported %d", i, devices[i].supported);
fgfswrite(telnet_sock, "set /haptic/device[%d]/axes %d", i, devices[i].axes);
fgfswrite(telnet_sock, "set /haptic/device[%d]/num-effects %d", i, devices[i].numEffects);
fgfswrite(telnet_sock, "set /haptic/device[%d]/num-effects-playing %d", i, devices[i].numEffectsPlaying);
fgfswrite(telnet_sock, "set /haptic/device[%d]/low-pass-filter %.6f", i, devices[i].lowpass);
// Write supported effects
if (devices[i].supported & SDL_HAPTIC_CONSTANT) {
// Constant force -> pilot G forces and aileron loading
// Currently support 3 axis only
for (int x = 0; x < devices[i].axes && x < AXES; x++) {
fgfswrite(telnet_sock, "set /haptic/device[%d]/pilot/%c %d", i, axes[x], devices[i].pilot_axes[x]);
fgfswrite(telnet_sock, "set /haptic/device[%d]/stick-force/%c %d", i, axes[x], devices[i].stick_axes[x]);
}
fgfswrite(telnet_sock, "set /haptic/device[%d]/pilot/gain %f", i, devices[i].pilot_gain);
fgfswrite(telnet_sock, "set /haptic/device[%d]/stick-force/gain %f", i, devices[i].stick_gain);
fgfswrite(telnet_sock, "set /haptic/device[%d]/stick-force/supported 1", i);
fgfswrite(telnet_sock, "set /haptic/device[%d]/pilot/supported 1", i);
fgfswrite(telnet_sock, "set /haptic/device[%d]/ground-rumble/period 0.0", i);
fgfswrite(telnet_sock, "set /haptic/device[%d]/ground-rumble/gain %f", i, devices[i].rumble_gain);
fgfswrite(telnet_sock, "set /haptic/device[%d]/ground-rumble/supported 1", i);
}
if (devices[i].supported & SDL_HAPTIC_SINE) {
// Sine effect -> rumble is stick shaker
fgfswrite(telnet_sock, "set /haptic/device[%d]/stick-shaker/direction %f", i, devices[i].shaker_dir);
fgfswrite(telnet_sock, "set /haptic/device[%d]/stick-shaker/period %f", i, devices[i].shaker_period);
fgfswrite(telnet_sock, "set /haptic/device[%d]/stick-shaker/gain %f", i, devices[i].shaker_gain);
fgfswrite(telnet_sock, "set /haptic/device[%d]/stick-shaker/trigger 0", i);
fgfswrite(telnet_sock, "set /haptic/device[%d]/stick-shaker/supported 1", i);
}
if (devices[i].supported & SDL_HAPTIC_GAIN) {
fgfswrite(telnet_sock, "set /haptic/device[%d]/gain %f", i, devices[i].gain);
fgfswrite(telnet_sock, "set /haptic/device[%d]/gain-supported 1", i);
}
if (devices[i].supported & SDL_HAPTIC_AUTOCENTER) {
fgfswrite(telnet_sock, "set /haptic/device[%d]/autocenter %f", i, devices[i].autocenter);
fgfswrite(telnet_sock, "set /haptic/device[%d]/autocenter-supported 1", i);
}
}
}
void read_devices(void)
{
int idata;
float fdata;
int read = 0;
const char *p;
fgfsflush(telnet_sock);
printf("Reading device setup from FG\n");
for (int i = 0; i < num_devices; i++) {
// Constant device settings
fgfswrite(telnet_sock, "get /haptic/device[%d]/low-pass-filter", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].lowpass = fdata;
}
if (devices[i].supported & SDL_HAPTIC_GAIN) {
fgfswrite(telnet_sock, "get /haptic/device[%d]/gain", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].gain = fdata;
}
}
if (devices[i].supported & SDL_HAPTIC_AUTOCENTER) {
fgfswrite(telnet_sock, "get /haptic/device[%d]/autocenter", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].autocenter = fdata;
}
}
// Constant force -> pilot G forces and aileron loading
// Currently support 3 axis only
if (devices[i].supported & SDL_HAPTIC_CONSTANT) {
for (int x = 0; x < devices[i].axes && x < AXES; x++) {
fgfswrite(telnet_sock, "get /haptic/device[%d]/pilot/%c", i, axes[x]);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%d", &idata);
if (read == 1)
devices[i].pilot_axes[x] = idata;
}
fgfswrite(telnet_sock, "get /haptic/device[%d]/stick-force/%c", i, axes[x]);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%d", &idata);
if (read == 1)
devices[i].stick_axes[x] = idata;
}
}
fgfswrite(telnet_sock, "get /haptic/device[%d]/pilot/gain", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].pilot_gain = fdata;
}
fgfswrite(telnet_sock, "get /haptic/device[%d]/stick-force/gain", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].stick_gain = fdata;
}
fgfswrite(telnet_sock, "get /haptic/device[%d]/ground-rumble/gain", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].rumble_gain = fdata;
}
}
if (devices[i].supported & SDL_HAPTIC_SINE) {
fgfswrite(telnet_sock, "get /haptic/device[%d]/stick-shaker/direction", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].shaker_dir = fdata;
}
fgfswrite(telnet_sock, "get /haptic/device[%d]/stick-shaker/period", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].shaker_period = fdata;
}
fgfswrite(telnet_sock, "get /haptic/device[%d]/stick-shaker/gain", i);
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p) {
read = sscanf(p, "%f", &fdata);
if (read == 1)
devices[i].shaker_gain = fdata;
}
}
}
fgfswrite(telnet_sock, "set /haptic/reconfigure 0");
printf("Waiting for the command to go through...\n");
do {
fgfswrite(telnet_sock, "get /haptic/reconfigure");
p = fgfsread(telnet_sock, READ_TIMEOUT);
if (p)
read = sscanf(p, "%d", &idata);
else
read = 0;
} while (read == 1 && idata == 1);
printf("Done\n");
fgfsflush(client_sock); // Get rid of FF data that was received during reinitialization
return;
}
void create_effects(void)
{
for (int i = 0; i < num_devices; i++) {
// Delete existing effects
for (int x = 0; x < SDL_HapticNumEffects(devices[i].device); x++) {
SDL_HapticDestroyEffect(devices[i].device, x);
if (x < sizeof(devices[i].effectId) / sizeof(devices[i].effectId[0])) {
devices[i].effectId[x] = -1;
}
}
memset(&devices[i].effect[0], 0, sizeof(SDL_HapticEffect) * EFFECTS);
printf("Creating effects for device %d\n", i + 1);
// Set autocenter and gain
if (devices[i].supported & SDL_HAPTIC_AUTOCENTER)
SDL_HapticSetAutocenter(devices[i].device, devices[i].autocenter * 100);
if (devices[i].supported & SDL_HAPTIC_GAIN)
SDL_HapticSetGain(devices[i].device, devices[i].gain * 100);
// Stick shaker
if (devices[i].supported & SDL_HAPTIC_SINE && devices[i].shaker_gain > 0.001) {
devices[i].effect[STICK_SHAKER].type = SDL_HAPTIC_SINE;
devices[i].effect[STICK_SHAKER].periodic.direction.type = SDL_HAPTIC_POLAR;
devices[i].effect[STICK_SHAKER].periodic.direction.dir[0] = devices[i].shaker_dir;
devices[i].effect[STICK_SHAKER].periodic.direction.dir[1] = 0;
devices[i].effect[STICK_SHAKER].periodic.direction.dir[2] = 0;
devices[i].effect[STICK_SHAKER].periodic.length = 5000; // Default 5 seconds?
devices[i].effect[STICK_SHAKER].periodic.period = devices[i].shaker_period;
devices[i].effect[STICK_SHAKER].periodic.magnitude = 0x4000;
devices[i].effect[STICK_SHAKER].periodic.attack_length = 300; // 0.3 sec fade in
devices[i].effect[STICK_SHAKER].periodic.fade_length = 300; // 0.3 sec fade out
devices[i].effectId[STICK_SHAKER] = SDL_HapticNewEffect(devices[i].device, &devices[i].effect[STICK_SHAKER]);
if (devices[i].effectId[STICK_SHAKER] < 0) {
printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
devices[i].effectId[STICK_SHAKER] = -1;
devices[i].supported &= ~SDL_HAPTIC_SINE;
}
}
// X axis
if (devices[i].supported & SDL_HAPTIC_CONSTANT && devices[i].axes > 0) {
devices[i].effect[CONST_X].type = SDL_HAPTIC_CONSTANT;
devices[i].effect[CONST_X].constant.direction.type = SDL_HAPTIC_CARTESIAN;
devices[i].effect[CONST_X].constant.direction.dir[0] = 0x1000;
devices[i].effect[CONST_X].constant.direction.dir[1] = 0;
devices[i].effect[CONST_X].constant.direction.dir[2] = 0;
devices[i].effect[CONST_X].constant.length = 60000; // By default constant fore is always applied
devices[i].effect[CONST_X].constant.level = 0x1000;
devices[i].effectId[CONST_X] = SDL_HapticNewEffect(devices[i].device, &devices[i].effect[CONST_X]);
if (devices[i].effectId[CONST_X] < 0) {
printf("UPLOADING CONST_X EFFECT ERROR: %s\n", SDL_GetError());
devices[i].effectId[CONST_X] = -1;
devices[i].supported &= ~SDL_HAPTIC_CONSTANT;
}
}
// Y axis
if (devices[i].supported & SDL_HAPTIC_CONSTANT && devices[i].axes > 1) {
devices[i].effect[CONST_Y].type = SDL_HAPTIC_CONSTANT;
devices[i].effect[CONST_Y].constant.direction.type = SDL_HAPTIC_CARTESIAN;
devices[i].effect[CONST_Y].constant.direction.dir[0] = 0;
devices[i].effect[CONST_Y].constant.direction.dir[1] = -0x1000;
devices[i].effect[CONST_Y].constant.direction.dir[2] = 0;
devices[i].effect[CONST_Y].constant.length = 60000; // By default constant fore is always applied
devices[i].effect[CONST_Y].constant.level = 0x1000;
devices[i].effectId[CONST_Y] = SDL_HapticNewEffect(devices[i].device, &devices[i].effect[CONST_Y]);
if (devices[i].effectId[CONST_Y] < 0) {
printf("UPLOADING CONST_Y EFFECT ERROR: %s\n", SDL_GetError());
devices[i].effectId[CONST_Y] = -1;
devices[i].supported &= ~SDL_HAPTIC_CONSTANT;
}
}
// Z axis
if (devices[i].supported & SDL_HAPTIC_CONSTANT && devices[i].axes > 2) {
devices[i].effect[CONST_Z].type = SDL_HAPTIC_CONSTANT;
devices[i].effect[CONST_Z].constant.direction.type = SDL_HAPTIC_CARTESIAN;
devices[i].effect[CONST_Z].constant.direction.dir[0] = 0;
devices[i].effect[CONST_Z].constant.direction.dir[1] = 0;
devices[i].effect[CONST_Z].constant.direction.dir[2] = 0x1000;
devices[i].effect[CONST_Z].constant.length = 60000; // By default constant fore is always applied
devices[i].effect[CONST_Z].constant.level = 0x1000;
devices[i].effectId[CONST_Z] = SDL_HapticNewEffect(devices[i].device, &devices[i].effect[CONST_Z]);
if (devices[i].effectId[CONST_Z] < 0) {
printf("UPLOADING CONST_Y EFFECT ERROR: %s\n", SDL_GetError());
devices[i].effectId[CONST_Z] = -1;
devices[i].supported &= ~SDL_HAPTIC_CONSTANT;
}
}
}
}
void reload_effect(hapticDevice * device, SDL_HapticEffect * effect, int *effectId, bool run)
{
if (!device->device || !device->open)
return;
if (SDL_HapticUpdateEffect(device->device, *effectId, effect) < 0)
printf("Update error: %s\n", SDL_GetError());
if (run)
if (SDL_HapticRunEffect(device->device, *effectId, 1) < 0)
printf("Run error: %s\n", SDL_GetError());
}
void read_fg(void)
{
int reconf, read;
const char *p;
p = fgfsread(client_sock, TIMEOUT);
if (!p)
return; // Null pointer, read failed
memset(&new_params, 0, sizeof(effectParams));
// Divide the buffer into chunks
read = sscanf(p, "%d|%f|%f|%f|%f|%f|%f|%d|%f", &reconf,
&new_params.pilot[0], &new_params.pilot[1], &new_params.pilot[2],
&new_params.stick[0], &new_params.stick[1], &new_params.stick[2],
&new_params.shaker_trigger, &new_params.rumble_period);
if (read != 9) {
printf("Error reading generic I/O!\n");
return;
}
// printf("%s, %d\n", p, reconf);
// Do it the easy way...
// memcpy(&devices[0].params, &new_params, sizeof(effectParams));
if (reconf & 1)
reconf_request = true;
}
void test_effects(void)
{
unsigned int start;
unsigned int runtime = 0.0;
unsigned int dt;
for (int i = 0; i < num_devices; i++) {
printf("\nTesting device number %d, %s.\n", i + 1, devices[i].name);
printf("HOLD FIRMLY TO YOUR JOYSTICK DURING THE TEST!\n\n");
if (devices[i].supported & SDL_HAPTIC_CONSTANT) {
printf("Press [enter] to start constant force test.\n");
getchar();
start = SDL_GetTicks();
do {
runtime = SDL_GetTicks();
dt = runtime - start;
float x = cos(3.14159 * dt / 3000.0) * 32760.0;
float y = sin(3.14159 * dt / 3000.0) * 32760.0;
float z = sin(3.14159 * dt / 3000.0) * 32760.0;
if (runtime > start + 6000) {
x = y = z = 0.0;
}
if (devices[i].axes > 0 && devices[i].effectId[CONST_X] != -1) {
devices[i].effect[CONST_X].constant.level = (signed short)clamp(x, -32760.0, 32760.0);
reload_effect(&devices[i], &devices[i].effect[CONST_X], &devices[i].effectId[CONST_X], true);
}
if (devices[i].axes > 1 && devices[i].effectId[CONST_Y] != -1) {
devices[i].effect[CONST_Y].constant.level = (signed short)clamp(y, -32760.0, 32760.0);
reload_effect(&devices[i], &devices[i].effect[CONST_Y], &devices[i].effectId[CONST_Y], true);
}
if (devices[i].axes > 2 && devices[i].effectId[CONST_Z] != -1) {
devices[i].effect[CONST_Z].constant.level = (signed short)clamp(z, -32760.0, 32760.0);
reload_effect(&devices[i], &devices[i].effect[CONST_Z], &devices[i].effectId[CONST_Z], true);
}
SDL_Delay(100);
} while (runtime < start + 6500);
} else
printf("Skipping constant force test: Not supported or effect creation was failed\n");
if ((devices[i].supported & SDL_HAPTIC_SINE) && devices[i].effectId[STICK_SHAKER] != -1) {
printf("Press [enter] to start rumble test.\n");
getchar();
reload_effect(&devices[i], &devices[i].effect[STICK_SHAKER], &devices[i].effectId[STICK_SHAKER], true);
start = SDL_GetTicks();
do {
runtime = SDL_GetTicks();
SDL_Delay(100);
} while (runtime < start + 5000);
} else
printf("Skipping rumble test: Not supported or effect creation was failed\n");
}
printf("\nTest done!\n");
}
/**
* @brief The entry point of this force feedback demo.
* @param[in] argc Number of arguments.
* @param[in] argv Array of argc arguments.
*/
int main(int argc, char **argv)
{
int i = 0;
char *name = NULL;
struct sigaction signal_handler;
effectParams *oldParams = NULL;
unsigned int runtime = 0;
unsigned int dt = 0;
bool test_mode = false;
// Handlers for ctrl+c etc quitting methods
signal_handler.sa_handler = abort_execution;
sigemptyset(&signal_handler.sa_mask);
signal_handler.sa_flags = 0;
sigaction(SIGINT, &signal_handler, NULL);
sigaction(SIGQUIT, &signal_handler, NULL);
printf("fg-haptic version 0.5\n");
printf("Force feedback support for Flight Gear\n");
printf("Copyright 2011, 2014 Lauri Peltonen, released under GPLv2 or later\n\n");
if (argc > 1) {
name = argv[1];
if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
printf("USAGE: %s [optional parameters]\n"
" -h or --help : Show this help\n"
" -t or --test : Test force feedback effects\n\n"
"Telnet port for FlightGear is %d and generic\n"
"port is %d. See Readme for details.\n", argv[0], DFLTPORT, DFLTPORT + 1);
return 0;
}
if ((strcmp(name, "--test") == 0) || (strcmp(name, "-t") == 0)) {
printf("Test mode enabled.\n");
test_mode = true;
}
}
// Initialize SDL haptics
init_haptic();
// Create & upload force feedback effects
create_effects();
if (test_mode) {
test_effects();
abort_execution(0);
}
socketset = SDLNet_AllocSocketSet(2);
if (!socketset) {
printf("Unable to create socket set: %s\n", SDLNet_GetError());
abort_execution(-1);
}
// Wait for a connection from flightgear generic io
printf("\n\nWaiting for flightgear generic IO at port %d, please run Flight Gear now!\n", DFLTPORT + 1);
server_sock = fgfsconnect(DFLTHOST, DFLTPORT + 1, true);
if (!server_sock) {
printf("Failed to connect!\n");
abort_execution(-1);
}
printf("Got connection, sending haptic details through telnet at port %d\n", DFLTPORT);
// Connect to flightgear using telnet
telnet_sock = fgfsconnect(DFLTHOST, DFLTPORT, false);
if (!telnet_sock) {
printf("Could not connect to flightgear with telnet!\n");
abort_execution(-1);
}
// Add sockets to a socket set for polling/selecting
SDLNet_TCP_AddSocket(socketset, client_sock);
SDLNet_TCP_AddSocket(socketset, telnet_sock);
// Switch to data mode
fgfswrite(telnet_sock, "data");
// send the devices to flightgear
send_devices();
// allocate memory for old param values
oldParams = (effectParams *) malloc(num_devices * sizeof(effectParams));
if (!oldParams) {
printf("Fatal error: Could not allocate memory!\n");
abort_execution(-1);
}
printf("Running...\n");
// Main loop
while (!quit) // Loop as long as the connection is alive
{
dt = runtime;
runtime = SDL_GetTicks(); // Run time in ms
dt = runtime - dt;
// Back up old parameters
for (int i = 0; i < num_devices; i++)
memcpy((void *)&oldParams[i], (void *)&devices[i].params, sizeof(effectParams));
memset((void *)&devices[i].params, 0, sizeof(effectParams));
// Read new parameters
read_fg();
// If parameters have changed, apply them
for (int i = 0; i < num_devices; i++) {
if (!devices[i].device || !devices[i].open)
continue; // Break if device is not opened correctly
// Constant forces (stick forces, pilot G forces
if ((devices[i].supported & SDL_HAPTIC_CONSTANT)) {
// Stick forces with axis mapping
if (devices[i].stick_axes[0] >= 0)
devices[i].params.x = new_params.stick[devices[i].stick_axes[0]] * devices[i].stick_gain;
if (devices[i].stick_axes[1] >= 0)
devices[i].params.y = new_params.stick[devices[i].stick_axes[1]] * devices[i].stick_gain;
if (devices[i].stick_axes[2] >= 0)
devices[i].params.z = new_params.stick[devices[i].stick_axes[2]] * devices[i].stick_gain;
// Pilot forces
if (devices[i].stick_axes[0] >= 0)
devices[i].params.x += new_params.stick[devices[i].stick_axes[0]] * devices[i].stick_gain;
if (devices[i].pilot_axes[1] >= 0)
devices[i].params.y += new_params.pilot[devices[i].pilot_axes[1]] * devices[i].pilot_gain;
if (devices[i].pilot_axes[2] >= 0)
devices[i].params.z += new_params.pilot[devices[i].pilot_axes[2]] * devices[i].pilot_gain;
devices[i].params.x *= 32760.0;
devices[i].params.y *= 32760.0;
devices[i].params.z *= 32760.0;
// Low pass filter
float g1 = ((float)dt / (devices[i].lowpass + dt));
float g2 = (devices[i].lowpass / (devices[i].lowpass + dt));
devices[i].params.x = devices[i].params.x * g1 + oldParams[i].x * g2;
devices[i].params.y = devices[i].params.y * g1 + oldParams[i].y * g2;
devices[i].params.z = devices[i].params.z * g1 + oldParams[i].z * g2;
// Add ground rumble
float rumble = 0.0;
if (new_params.rumble_period > 0.00001) {
if ((runtime - devices[i].last_rumble) > new_params.rumble_period) {
rumble = devices[i].rumble_gain * 32760.0;
devices[i].last_rumble = runtime;
}
}
if (devices[i].axes > 0 && devices[i].effectId[CONST_X] != -1) {
devices[i].effect[CONST_X].constant.level =
(signed short)clamp(devices[i].params.x, -32760.0, 32760.0);
reload_effect(&devices[i], &devices[i].effect[CONST_X], &devices[i].effectId[CONST_X], true);
}
if (devices[i].axes > 1 && devices[i].effectId[CONST_Y] != -1) {
devices[i].effect[CONST_Y].constant.level =
(signed short)clamp(devices[i].params.y + rumble, -32760.0, 32760.0);
reload_effect(&devices[i], &devices[i].effect[CONST_Y], &devices[i].effectId[CONST_Y], true);
}
if (devices[i].axes > 2 && devices[i].effectId[CONST_Z] != -1) {
devices[i].effect[CONST_Z].constant.level =
(signed short)clamp(devices[i].params.z, -32760.0, 32760.0);;
reload_effect(&devices[i], &devices[i].effect[CONST_Z], &devices[i].effectId[CONST_Z], true);
}
// printf("dt: %d X: %.6f Y: %.6f\n", (unsigned int)dt, devices[i].params.x, devices[i].params.y);
}
// Stick shaker trigger
if ((devices[i].supported & SDL_HAPTIC_SINE) && devices[i].effectId[STICK_SHAKER] != -1) {
if (new_params.shaker_trigger && !oldParams[i].shaker_trigger)
reload_effect(&devices[i], &devices[i].effect[STICK_SHAKER], &devices[i].effectId[STICK_SHAKER],
true);
else if (!new_params.shaker_trigger && oldParams[i].shaker_trigger)
SDL_HapticStopEffect(devices[i].device, devices[i].effectId[STICK_SHAKER]);
}
}
if (reconf_request) {
reconf_request = false;
read_devices();
create_effects();
}
SDL_Delay(10);
}
// Close flightgear telnet connection
fgfswrite(telnet_sock, "quit");
fgfsclose(telnet_sock);
// Close generic connection
fgfsclose(client_sock);
fgfsclose(server_sock);
SDLNet_FreeSocketSet(socketset);
if (oldParams)
free(oldParams);
oldParams = NULL;
// Close haptic devices
for (int i = 0; i < num_devices; i++)
if ( /*devices[i].open && */ devices[i].device)
SDL_HapticClose(devices[i].device);
if (devices)
free(devices);
devices = NULL;
SDL_Quit();
return 0;
}
/*
* Cleans up a bit.
*/
void abort_execution(int signal)
{
printf("\nAborting program execution.\n");
// Close flightgear telnet connection
fgfswrite(telnet_sock, "quit");
fgfsclose(telnet_sock);
// Adn generic
fgfsclose(client_sock);
fgfsclose(server_sock);
SDLNet_FreeSocketSet(socketset);
// Close haptic devices
for (int i = 0; i < num_devices; i++)
if (devices[i].open && devices[i].device)
SDL_HapticClose(devices[i].device);
if (devices)
free(devices);
devices = NULL;
SDLNet_Quit();
SDL_Quit();
exit(1);
}
/*
* Displays information about the haptic device.
*/
void HapticPrintSupported(SDL_Haptic * haptic)
{
unsigned int supported;
supported = SDL_HapticQuery(haptic);
printf(" Device has %d axis\n", SDL_HapticNumAxes(haptic));
printf(" Supported effects [%d effects, %d playing]:\n", SDL_HapticNumEffects(haptic), SDL_HapticNumEffectsPlaying(haptic));
if (supported & SDL_HAPTIC_CONSTANT)
printf(" constant\n");
if (supported & SDL_HAPTIC_SINE)
printf(" sine\n");
/* if (supported & SDL_HAPTIC_SQUARE)
printf(" square\n");*/
if (supported & SDL_HAPTIC_TRIANGLE)
printf(" triangle\n");
if (supported & SDL_HAPTIC_SAWTOOTHUP)
printf(" sawtoothup\n");
if (supported & SDL_HAPTIC_SAWTOOTHDOWN)
printf(" sawtoothdown\n");
if (supported & SDL_HAPTIC_RAMP)
printf(" ramp\n");
if (supported & SDL_HAPTIC_FRICTION)
printf(" friction\n");
if (supported & SDL_HAPTIC_SPRING)
printf(" spring\n");
if (supported & SDL_HAPTIC_DAMPER)
printf(" damper\n");
if (supported & SDL_HAPTIC_INERTIA)
printf(" intertia\n");
if (supported & SDL_HAPTIC_CUSTOM)
printf(" custom\n");
printf(" Supported capabilities:\n");
if (supported & SDL_HAPTIC_GAIN)
printf(" gain\n");
if (supported & SDL_HAPTIC_AUTOCENTER)
printf(" autocenter\n");
if (supported & SDL_HAPTIC_STATUS)
printf(" status\n");
}
int fgfswrite(TCPsocket sock, char *msg, ...)
{
va_list va;
ssize_t len;
char buf[MAXMSG];
if (!sock)
return 0;
va_start(va, msg);
vsnprintf(buf, MAXMSG - 2, msg, va);
va_end(va);
//printf("SEND: \t<%s>\n", buf);
strcat(buf, "\r\n");
len = SDLNet_TCP_Send(sock, buf, strlen(buf));
if (len < 0) {
printf("Error in fgfswrite: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
return len;
}
const char *fgfsread(TCPsocket sock, int timeout)
{
static char buf[MAXMSG];
char *p = &buf[0];
size_t len = 0;
int ready = 0;
time_t start;
memset(buf, 0, MAXMSG);
start = clock();
do {
ready = SDLNet_CheckSockets(socketset, timeout * 1000);
if (!ready) {
//printf("Timeout!\n");
return NULL;
}
SDL_Delay(5);
// TODO: Loop until socket is ready or timeout?
if (SDLNet_SocketReady(sock)) {
ready = 1;
}
} while (!ready && clock() < (start + timeout * CLOCKS_PER_SEC));
if (!ready) {
//printf("Error in fgfsread: Socket was not ready (timeout)!\n");
return NULL;
}
ready = 0;
do {
if (SDLNet_TCP_Recv(sock, p, 1) <= 0) {
// printf("Error in fgfsread: Recv returned zero!\n");
quit = true;
return NULL;
}
len++;
if (len == MAXMSG - 1) {
printf("Warning in fgfsread: Buffer size exceeded!\n");
ready = 1;
} else if (*p == '\n') {
ready = 1;
}
p++;
} while (!ready);
for (p = &buf[len - 1]; p >= buf; p--)
if (*p != '\r' && *p != '\n')
break;
*++p = '\0';
// if(strlen(buf)) printf("RECV: %s\n", buf);
return strlen(buf) ? buf : NULL;
}
void fgfsflush(TCPsocket sock)
{
const char *p;
while ((p = fgfsread(sock, 0)) != NULL) {
//printf("IGNORE: \t<%s>\n", p);
}
}
TCPsocket fgfsconnect(const char *hostname, const int port, bool server)
{
IPaddress serv_addr, cli_addr;
TCPsocket _sock, _clientsock;
time_t start;
if (!server) // Act as a client -> connect to address
{
if (SDLNet_ResolveHost(&cli_addr, hostname, port) == -1) {
printf("Error in fgfsconnect, resolve host: %s\n", SDLNet_GetError());
return NULL;
}
_sock = SDLNet_TCP_Open(&cli_addr);
if (!_sock) {
printf("Error in fgfsconnect, connect: %s\n", SDLNet_GetError());
return NULL;
}
return _sock;
} else { // Act as a server, wait for connections
if (SDLNet_ResolveHost(&serv_addr, NULL, port) == -1) {
printf("Error in fgfsconnect, server resolve host: %s\n", SDLNet_GetError());
return NULL;
}
_sock = SDLNet_TCP_Open(&serv_addr);
if (!_sock) {
printf("Error in fgfsconnect, server connect: %s\n", SDLNet_GetError());