-
Notifications
You must be signed in to change notification settings - Fork 2
/
SSC32.h
898 lines (761 loc) · 31.4 KB
/
SSC32.h
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
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#ifndef SSC32_H
#define SSC32_H
#include "OSMisc.h"
#include "RS232Port.h"
#ifndef DISABLE_SSC32THREAD
#include "OSThread.h"
#endif // !DISABLE_SSC32THREAD
// Need to be undefined at the end of the file...
// min and max might cause incompatibilities...
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif // !max
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif // !min
//#define TIMEOUT_MESSAGE_SSC32 4.0 // In s.
// Should be at least 2 * number of bytes to be sure to contain entirely the biggest desired message (or group of messages) + 1.
#define MAX_NB_BYTES_SSC32 512
#define NB_CHANNELS_PWM_SSC32 32
// In us.
#define DEFAULT_ABSOLUTE_MIN_PW_SSC32 500
// In us.
#define DEFAULT_MIN_PW_SSC32 1000
// In us.
#define DEFAULT_MID_PW_SSC32 1500
// In us.
#define DEFAULT_MAX_PW_SSC32 2000
// In us.
#define DEFAULT_ABSOLUTE_MAX_PW_SSC32 2500
struct SSC32
{
RS232PORT RS232Port;
FILE* pfSaveFile; // Used to save raw data, should be handled specifically...
int LastPWs[NB_CHANNELS_PWM_SSC32];
char szCfgFilePath[256];
// Parameters.
char szDevPath[256];
int BaudRate;
int timeout;
int threadperiod;
BOOL bSaveRawData;
int bytedelayus;
int MinPWs[NB_CHANNELS_PWM_SSC32];
int MidPWs[NB_CHANNELS_PWM_SSC32];
int MaxPWs[NB_CHANNELS_PWM_SSC32];
int InitPWs[NB_CHANNELS_PWM_SSC32];
int ThresholdPWs[NB_CHANNELS_PWM_SSC32];
double CoefPWs[NB_CHANNELS_PWM_SSC32];
int bProportionalPWs[NB_CHANNELS_PWM_SSC32];
int rudderchan;
int rightthrusterchan;
int leftthrusterchan;
int rightfluxchan;
int leftfluxchan;
double MinAngle;
double MidAngle;
double MaxAngle;
};
typedef struct SSC32 SSC32;
inline int WriteDataSSC32(SSC32* pSSC32, uint8* writebuf, int writebuflen, int bytedelayus)
{
if (bytedelayus < 0) return WriteAllRS232Port(&pSSC32->RS232Port, writebuf, writebuflen);
else return WriteAllWithByteDelayRS232Port(&pSSC32->RS232Port, writebuf, writebuflen, bytedelayus);
}
/*
Set a digital output channel.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
int channel : (IN) Channel number.
int value : (IN) Digital value (0 or 1).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetDigitalOutputSSC32(SSC32* pSSC32, int channel, int value)
{
char sendbuf[MAX_NB_BYTES_SSC32];
int sendbuflen = 0;
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
if (value)
sprintf(sendbuf, "#%dH\r", channel);
else
sprintf(sendbuf, "#%dL\r", channel);
sendbuflen = (int)strlen(sendbuf);
if (WriteDataSSC32(pSSC32, (unsigned char*)sendbuf, sendbuflen, pSSC32->bytedelayus) != EXIT_SUCCESS)
{
printf("Error writing data to a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
return EXIT_SUCCESS;
}
/*
Get a digital input channel value.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
int channel : (IN) Channel number.
int* pValue : (INOUT) Valid pointer that will receive the digital value (0 or 1).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int GetDigitalInputSSC32(SSC32* pSSC32, int channel, int* pValue)
{
char sendbuf[MAX_NB_BYTES_SSC32];
int sendbuflen = 0;
char recvbuf[1];
int recvbuflen = 0;
char c = 0;
char v = 0;
switch (channel)
{
case 0: c = 'A'; break;
case 1: c = 'B'; break;
case 2: c = 'C'; break;
case 3: c = 'D'; break;
default:
printf("Invalid parameter : channel %d.\n", channel);
return EXIT_FAILURE;
}
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
sprintf(sendbuf, "%c\r", c);
sendbuflen = (int)strlen(sendbuf);
if (WriteDataSSC32(pSSC32, (unsigned char*)sendbuf, sendbuflen, pSSC32->bytedelayus) != EXIT_SUCCESS)
{
printf("Error writing data to a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = 1;
if (ReadAllRS232Port(&pSSC32->RS232Port, (unsigned char*)recvbuf, recvbuflen) != EXIT_SUCCESS)
{
printf("Error reading data from a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(recvbuf, recvbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
v = recvbuf[0];
*pValue = (v == '0')?0:1;
return EXIT_SUCCESS;
}
/*
Get an analog input channel voltage.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
int channel : (IN) Channel number.
int* pVoltage : (INOUT) Valid pointer that will receive the voltage (in V).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int GetVoltageSSC32(SSC32* pSSC32, int channel, double* pVoltage)
{
char sendbuf[MAX_NB_BYTES_SSC32];
int sendbuflen = 0;
char recvbuf[1];
int recvbuflen = 0;
char c = 0;
unsigned char v = 0;
switch (channel)
{
case 0: c = 'A'; break;
case 1: c = 'B'; break;
case 2: c = 'C'; break;
case 3: c = 'D'; break;
default:
printf("Invalid parameter : channel %d.\n", channel);
return EXIT_FAILURE;
}
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
sprintf(sendbuf, "V%c\r", c);
sendbuflen = (int)strlen(sendbuf);
if (WriteDataSSC32(pSSC32, (unsigned char*)sendbuf, sendbuflen, pSSC32->bytedelayus) != EXIT_SUCCESS)
{
printf("Error writing data to a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = 1;
if (ReadAllRS232Port(&pSSC32->RS232Port, (unsigned char*)recvbuf, recvbuflen) != EXIT_SUCCESS)
{
printf("Error reading data from a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(recvbuf, recvbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
v = (unsigned char)recvbuf[0];
*pVoltage = v*5.0/256.0;
return EXIT_SUCCESS;
}
/*
Get the pulse width from a pulse input channel (only available when connected to IOIOSrv).
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
int channel : (IN) Channel number.
int* pPw : (INOUT) Valid pointer that will receive the pulse width (in us).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int GetPWMSSC32(SSC32* pSSC32, int channel, int* pPw)
{
char sendbuf[MAX_NB_BYTES_SSC32];
int sendbuflen = 0;
char recvbuf[2];
int recvbuflen = 0;
unsigned short pw = 0;
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
sprintf(sendbuf, "#%dPI\r", channel);
sendbuflen = (int)strlen(sendbuf);
if (WriteDataSSC32(pSSC32, (unsigned char*)sendbuf, sendbuflen, pSSC32->bytedelayus) != EXIT_SUCCESS)
{
printf("Error writing data to a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = 2;
if (ReadAllRS232Port(&pSSC32->RS232Port, (unsigned char*)recvbuf, recvbuflen) != EXIT_SUCCESS)
{
printf("Error reading data from a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(recvbuf, recvbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
// pw in us as a 16 bit integer.
memcpy((char*)&pw, recvbuf, sizeof(unsigned short));
*pPw = pw;
return EXIT_SUCCESS;
}
/*
Set a PWM channel.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
int channel : (IN) Channel number (from 0 to NB_CHANNELS_PWM_SSC32-1).
int pw : (IN) Desired pulse width (in us). For example, if a servomotor is connected,
pass 1500 to put it at a neutral state, 1000 in one side or 2000 in the other side.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetPWMSSC32(SSC32* pSSC32, int channel, int pw)
{
char sendbuf[MAX_NB_BYTES_SSC32];
int sendbuflen = 0;
if (pSSC32->bProportionalPWs[channel])
{
pw = (int)(pSSC32->CoefPWs[channel]*(pw-DEFAULT_MID_PW_SSC32));
if (pw >= 0)
pw = pSSC32->MidPWs[channel]+pw*(pSSC32->MaxPWs[channel]-pSSC32->MidPWs[channel])
/(DEFAULT_MAX_PW_SSC32-DEFAULT_MID_PW_SSC32);
else
pw = pSSC32->MidPWs[channel]+pw*(pSSC32->MinPWs[channel]-pSSC32->MidPWs[channel])
/(DEFAULT_MIN_PW_SSC32-DEFAULT_MID_PW_SSC32);
}
else
{
pw = DEFAULT_MID_PW_SSC32+(int)(pSSC32->CoefPWs[channel]*(pw-DEFAULT_MID_PW_SSC32));
}
pw = max(min(pw, pSSC32->MaxPWs[channel]), pSSC32->MinPWs[channel]);
//pw = max(min(pw, DEFAULT_ABSOLUTE_MAX_PW_SSC32), DEFAULT_ABSOLUTE_MIN_PW_SSC32);
// The requested PWM is only applied if it is slightly different from the current value.
if (abs(pw-pSSC32->LastPWs[channel]) < pSSC32->ThresholdPWs[channel]) return EXIT_SUCCESS;
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
sprintf(sendbuf, "#%dP%d\r", channel, pw);
sendbuflen = (int)strlen(sendbuf);
if (WriteDataSSC32(pSSC32, (unsigned char*)sendbuf, sendbuflen, pSSC32->bytedelayus) != EXIT_SUCCESS)
{
printf("Error writing data to a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
// Update last known value.
pSSC32->LastPWs[channel] = pw;
return EXIT_SUCCESS;
}
/*
Set selected PWM channels.
For example, if a servomotor is connected to channel 2, set pws[2] to 1500 to put it at a neutral state, 1000 in
one side or 2000 in the other side, and set selectedchannels[2] to 1.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
int* selectedchannels : (IN) Valid pointer to a table of NB_CHANNELS_PWM_SSC32 elements to indicate which channels
should be considered in pws (0 to ignore the channel or 1 to select it).
int* pws : (IN) Valid pointer to a table of NB_CHANNELS_PWM_SSC32 elements with the desired pulse width for each
channel (in us).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetAllPWMsSSC32(SSC32* pSSC32, int* selectedchannels, int* pws)
{
char szTmp[MAX_NB_BYTES_SSC32];
char sendbuf[MAX_NB_BYTES_SSC32];
int sendbuflen = 0;
int channel = 0;
int pws_tmp[NB_CHANNELS_PWM_SSC32];
memset(szTmp, 0, sizeof(szTmp));
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
memcpy(pws_tmp, pws, sizeof(pws_tmp));
for (channel = 0; channel < NB_CHANNELS_PWM_SSC32; channel++)
{
if (!selectedchannels[channel]) continue;
if (pSSC32->bProportionalPWs[channel])
{
pws_tmp[channel] = (int)(pSSC32->CoefPWs[channel]*(pws_tmp[channel]-DEFAULT_MID_PW_SSC32));
if (pws_tmp[channel] >= 0)
pws_tmp[channel] = pSSC32->MidPWs[channel]+pws_tmp[channel]*(pSSC32->MaxPWs[channel]-pSSC32->MidPWs[channel])
/(DEFAULT_MAX_PW_SSC32-DEFAULT_MID_PW_SSC32);
else
pws_tmp[channel] = pSSC32->MidPWs[channel]+pws_tmp[channel]*(pSSC32->MinPWs[channel]-pSSC32->MidPWs[channel])
/(DEFAULT_MIN_PW_SSC32-DEFAULT_MID_PW_SSC32);
}
else
{
pws_tmp[channel] = DEFAULT_MID_PW_SSC32+(int)(pSSC32->CoefPWs[channel]*(pws_tmp[channel]-DEFAULT_MID_PW_SSC32));
}
pws_tmp[channel] = max(min(pws_tmp[channel], pSSC32->MaxPWs[channel]), pSSC32->MinPWs[channel]);
//pws_tmp[channel] = max(min(pws_tmp[channel], DEFAULT_ABSOLUTE_MAX_PW_SSC32), DEFAULT_ABSOLUTE_MIN_PW_SSC32);
// The requested PWM is only applied if it is slightly different from the current value.
if (abs(pws_tmp[channel]-pSSC32->LastPWs[channel]) < pSSC32->ThresholdPWs[channel]) continue;
//printf("%d %d %d %d %d\n", channel, pws_tmp[channel], pSSC32->LastPWs[channel], abs(pws_tmp[channel]-pSSC32->LastPWs[channel]), pSSC32->ThresholdPWs[channel]);
sprintf(szTmp, "#%dP%d", channel, pws_tmp[channel]);
strcat(sendbuf, szTmp);
}
if (sendbuf[0] == 0) return EXIT_SUCCESS;
strcat(sendbuf, "\r");
sendbuflen = (int)strlen(sendbuf);
//printf("%s\n", sendbuf);
if (WriteDataSSC32(pSSC32, (unsigned char*)sendbuf, sendbuflen, pSSC32->bytedelayus) != EXIT_SUCCESS)
{
printf("Error writing data to a SSC32. \n");
return EXIT_FAILURE;
}
if ((pSSC32->bSaveRawData)&&(pSSC32->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pSSC32->pfSaveFile);
fflush(pSSC32->pfSaveFile);
}
for (channel = 0; channel < NB_CHANNELS_PWM_SSC32; channel++)
{
if (!selectedchannels[channel]) continue;
// The requested PWM should have been only applied if it was slightly different from the current value.
if (abs(pws_tmp[channel]-pSSC32->LastPWs[channel]) < pSSC32->ThresholdPWs[channel]) continue;
// Update last known value.
pSSC32->LastPWs[channel] = pws_tmp[channel];
}
return EXIT_SUCCESS;
}
/*
Set rudderchan PWM channel as a rudder angle.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
double angle : (IN) Desired rudder angle (in rad, should be in [-max(fabs(ssc32.MinAngle),fabs(ssc32.MaxAngle));max(fabs(ssc32.MinAngle),fabs(ssc32.MaxAngle))]).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetRudderSSC32(SSC32* pSSC32, double angle)
{
int pw = 0;
#ifndef DISABLE_RUDDER_MIDANGLE
double angletmp = 0;
#endif // DISABLE_RUDDER_MIDANGLE
// Convert angle (in rad) into SSC32 pulse width (in us).
#ifndef DISABLE_RUDDER_MIDANGLE
angletmp = angle >= 0? pSSC32->MidAngle+angle*(pSSC32->MaxAngle-pSSC32->MidAngle)/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)): pSSC32->MidAngle+angle*(pSSC32->MidAngle-pSSC32->MinAngle)/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle));
//angletmp = angle >= 0? pSSC32->MidAngle+urudder*(pSSC32->MaxAngle-pSSC32->MidAngle): pSSC32->MidAngle+urudder*(pSSC32->MidAngle-pSSC32->MinAngle);
if (angletmp >= 0)
pw = DEFAULT_MID_PW_SSC32+(int)(angletmp*(DEFAULT_MAX_PW_SSC32-DEFAULT_MID_PW_SSC32)
/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)));
else
pw = DEFAULT_MID_PW_SSC32+(int)(angletmp*(DEFAULT_MID_PW_SSC32-DEFAULT_MIN_PW_SSC32)
/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)));
#else
pw = DEFAULT_MID_PW_SSC32+(int)(angle*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)
/(pSSC32->MaxAngle-pSSC32->MinAngle));
#endif // DISABLE_RUDDER_MIDANGLE
pw = max(min(pw, DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
return SetPWMSSC32(pSSC32, pSSC32->rudderchan, pw);
}
/*
Set rightthrusterchan and leftthrusterchan PWM channels as thrusters inputs.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
double urt : (IN) Desired right thruster input (in [-1;1]).
double ult : (IN) Desired left thruster input (in [-1;1]).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetThrustersSSC32(SSC32* pSSC32, double urt, double ult)
{
int selectedchannels[NB_CHANNELS_PWM_SSC32];
int pws[NB_CHANNELS_PWM_SSC32];
memset(selectedchannels, 0, sizeof(selectedchannels));
memset(pws, 0, sizeof(pws));
// Convert u (in [-1;1]) into SSC32 pulse width (in us).
pws[pSSC32->rightthrusterchan] = DEFAULT_MID_PW_SSC32+(int)(urt*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)/2.0);
pws[pSSC32->leftthrusterchan] = DEFAULT_MID_PW_SSC32+(int)(ult*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)/2.0);
pws[pSSC32->rightthrusterchan] = max(min(pws[pSSC32->rightthrusterchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
pws[pSSC32->leftthrusterchan] = max(min(pws[pSSC32->leftthrusterchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
selectedchannels[pSSC32->rightthrusterchan] = 1;
selectedchannels[pSSC32->leftthrusterchan] = 1;
return SetAllPWMsSSC32(pSSC32, selectedchannels, pws);
}
/*
Set rudderchan PWM channel as a rudder angle and rightthrusterchan PWM channel as a thruster input.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
double angle : (IN) Desired rudder angle (in rad, should be in [-max(fabs(ssc32.MinAngle),fabs(ssc32.MaxAngle));max(fabs(ssc32.MinAngle),fabs(ssc32.MaxAngle))]).
double urt : (IN) Desired thruster input (in [-1;1]).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetRudderThrusterSSC32(SSC32* pSSC32, double angle, double urt)
{
int selectedchannels[NB_CHANNELS_PWM_SSC32];
int pws[NB_CHANNELS_PWM_SSC32];
#ifndef DISABLE_RUDDER_MIDANGLE
double angletmp = 0;
#endif // DISABLE_RUDDER_MIDANGLE
memset(selectedchannels, 0, sizeof(selectedchannels));
memset(pws, 0, sizeof(pws));
// Convert angle (in rad) into SSC32 pulse width (in us).
#ifndef DISABLE_RUDDER_MIDANGLE
angletmp = angle >= 0? pSSC32->MidAngle+angle*(pSSC32->MaxAngle-pSSC32->MidAngle)/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)): pSSC32->MidAngle+angle*(pSSC32->MidAngle-pSSC32->MinAngle)/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle));
//angletmp = angle >= 0? pSSC32->MidAngle+urudder*(pSSC32->MaxAngle-pSSC32->MidAngle): pSSC32->MidAngle+urudder*(pSSC32->MidAngle-pSSC32->MinAngle);
if (angletmp >= 0)
pws[pSSC32->rudderchan] = DEFAULT_MID_PW_SSC32+(int)(angletmp*(DEFAULT_MAX_PW_SSC32-DEFAULT_MID_PW_SSC32)
/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)));
else
pws[pSSC32->rudderchan] = DEFAULT_MID_PW_SSC32+(int)(angletmp*(DEFAULT_MID_PW_SSC32-DEFAULT_MIN_PW_SSC32)
/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)));
#else
pws[pSSC32->rudderchan] = DEFAULT_MID_PW_SSC32+(int)(angle*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)
/(pSSC32->MaxAngle-pSSC32->MinAngle));
#endif // DISABLE_RUDDER_MIDANGLE
// Convert u (in [-1;1]) into SSC32 pulse width (in us).
pws[pSSC32->rightthrusterchan] = DEFAULT_MID_PW_SSC32+(int)(urt*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)/2.0);
pws[pSSC32->rudderchan] = max(min(pws[pSSC32->rudderchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
pws[pSSC32->rightthrusterchan] = max(min(pws[pSSC32->rightthrusterchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
selectedchannels[pSSC32->rudderchan] = 1;
selectedchannels[pSSC32->rightthrusterchan] = 1;
return SetAllPWMsSSC32(pSSC32, selectedchannels, pws);
}
/*
Set rudderchan PWM channel as a rudder angle, rightthrusterchan and leftthrusterchan PWM channels as thrusters inputs,
rightfluxchan and leftfluxchan as flux direction inputs.
SSC32* pSSC32 : (INOUT) Valid pointer to a structure corresponding to a SSC-32.
double angle : (IN) Desired rudder angle (in rad, should be in [-max(fabs(ssc32.MinAngle),fabs(ssc32.MaxAngle));max(fabs(ssc32.MinAngle),fabs(ssc32.MaxAngle))]).
double urt : (IN) Desired right thruster input (in [-1;1]).
double ult : (IN) Desired left thruster input (in [-1;1]).
double urf : (IN) Desired right flux direction input (in [-1;1]).
double ulf : (IN) Desired left flux direction input (in [-1;1]).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetRudderThrustersFluxSSC32(SSC32* pSSC32, double angle, double urt, double ult, double urf, double ulf)
{
int selectedchannels[NB_CHANNELS_PWM_SSC32];
int pws[NB_CHANNELS_PWM_SSC32];
#ifndef DISABLE_RUDDER_MIDANGLE
double angletmp = 0;
#endif // DISABLE_RUDDER_MIDANGLE
memset(selectedchannels, 0, sizeof(selectedchannels));
memset(pws, 0, sizeof(pws));
// Convert angle (in rad) into SSC32 pulse width (in us).
#ifndef DISABLE_RUDDER_MIDANGLE
angletmp = angle >= 0? pSSC32->MidAngle+angle*(pSSC32->MaxAngle-pSSC32->MidAngle)/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)): pSSC32->MidAngle+angle*(pSSC32->MidAngle-pSSC32->MinAngle)/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle));
//angletmp = angle >= 0? pSSC32->MidAngle+urudder*(pSSC32->MaxAngle-pSSC32->MidAngle): pSSC32->MidAngle+urudder*(pSSC32->MidAngle-pSSC32->MinAngle);
if (angletmp >= 0)
pws[pSSC32->rudderchan] = DEFAULT_MID_PW_SSC32+(int)(angletmp*(DEFAULT_MAX_PW_SSC32-DEFAULT_MID_PW_SSC32)
/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)));
else
pws[pSSC32->rudderchan] = DEFAULT_MID_PW_SSC32+(int)(angletmp*(DEFAULT_MID_PW_SSC32-DEFAULT_MIN_PW_SSC32)
/max(fabs(pSSC32->MinAngle),fabs(pSSC32->MaxAngle)));
#else
pws[pSSC32->rudderchan] = DEFAULT_MID_PW_SSC32+(int)(angle*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)
/(pSSC32->MaxAngle-pSSC32->MinAngle));
#endif // DISABLE_RUDDER_MIDANGLE
// Convert u (in [-1;1]) into SSC32 pulse width (in us).
pws[pSSC32->rightthrusterchan] = DEFAULT_MID_PW_SSC32+(int)(urt*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)/2.0);
pws[pSSC32->leftthrusterchan] = DEFAULT_MID_PW_SSC32+(int)(ult*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)/2.0);
pws[pSSC32->rightfluxchan] = DEFAULT_MID_PW_SSC32+(int)(urf*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)/2.0);
pws[pSSC32->leftfluxchan] = DEFAULT_MID_PW_SSC32+(int)(ulf*(DEFAULT_MAX_PW_SSC32-DEFAULT_MIN_PW_SSC32)/2.0);
pws[pSSC32->rudderchan] = max(min(pws[pSSC32->rudderchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
pws[pSSC32->rightthrusterchan] = max(min(pws[pSSC32->rightthrusterchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
pws[pSSC32->leftthrusterchan] = max(min(pws[pSSC32->leftthrusterchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
pws[pSSC32->rightfluxchan] = max(min(pws[pSSC32->rightfluxchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
pws[pSSC32->leftfluxchan] = max(min(pws[pSSC32->leftfluxchan], DEFAULT_MAX_PW_SSC32), DEFAULT_MIN_PW_SSC32);
selectedchannels[pSSC32->rudderchan] = 1;
selectedchannels[pSSC32->rightthrusterchan] = 1;
selectedchannels[pSSC32->leftthrusterchan] = 1;
selectedchannels[pSSC32->rightfluxchan] = 1;
selectedchannels[pSSC32->leftfluxchan] = 1;
return SetAllPWMsSSC32(pSSC32, selectedchannels, pws);
}
inline int CheckRudderSSC32(SSC32* pSSC32)
{
if (SetRudderSSC32(pSSC32, -0.25) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetRudderSSC32(pSSC32, 0.25) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetRudderSSC32(pSSC32, 0.0) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
return EXIT_SUCCESS;
}
inline int CheckThrustersSSC32(SSC32* pSSC32)
{
if (SetThrustersSSC32(pSSC32, -0.25, -0.25) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetThrustersSSC32(pSSC32, 0.0, 0.0) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetThrustersSSC32(pSSC32, 0.25, 0.25) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetThrustersSSC32(pSSC32, 0.0, 0.0) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
return EXIT_SUCCESS;
}
// SSC32 must be initialized to 0 before (e.g. SSC32 ssc32; memset(&ssc32, 0, sizeof(SSC32));)!
inline int ConnectSSC32(SSC32* pSSC32, char* szCfgFilePath)
{
FILE* file = NULL;
char line[256];
int channel = 0;
memset(pSSC32->szCfgFilePath, 0, sizeof(pSSC32->szCfgFilePath));
sprintf(pSSC32->szCfgFilePath, "%.255s", szCfgFilePath);
// If szCfgFilePath starts with "hardcoded://", parameters are assumed to be already set in the structure,
// otherwise it should be loaded from a configuration file.
if (strncmp(szCfgFilePath, "hardcoded://", strlen("hardcoded://")) != 0)
{
memset(line, 0, sizeof(line));
// Default values.
memset(pSSC32->szDevPath, 0, sizeof(pSSC32->szDevPath));
sprintf(pSSC32->szDevPath, "COM1");
pSSC32->BaudRate = 115200;
pSSC32->timeout = 1000;
pSSC32->threadperiod = 50;
pSSC32->bSaveRawData = 1;
pSSC32->bytedelayus = -1;
for (channel = 0; channel < NB_CHANNELS_PWM_SSC32; channel++)
{
pSSC32->MinPWs[channel] = 1000;
pSSC32->MidPWs[channel] = 1500;
pSSC32->MaxPWs[channel] = 2000;
pSSC32->InitPWs[channel] = 1500;
pSSC32->ThresholdPWs[channel] = 0;
pSSC32->CoefPWs[channel] = 1;
pSSC32->bProportionalPWs[channel] = 1;
}
pSSC32->rudderchan = 2;
pSSC32->rightthrusterchan = 1;
pSSC32->leftthrusterchan = 0;
pSSC32->rightfluxchan = 4;
pSSC32->leftfluxchan = 3;
pSSC32->MinAngle = -0.5;
pSSC32->MidAngle = 0;
pSSC32->MaxAngle = 0.5;
// Load data from a file.
file = fopen(szCfgFilePath, "r");
if (file != NULL)
{
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%255s", pSSC32->szDevPath) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->BaudRate) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->timeout) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->threadperiod) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->bSaveRawData) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->bytedelayus) != 1) printf("Invalid configuration file.\n");
for (channel = 0; channel < NB_CHANNELS_PWM_SSC32; channel++)
{
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->MinPWs[channel]) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->MidPWs[channel]) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->MaxPWs[channel]) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->InitPWs[channel]) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->ThresholdPWs[channel]) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%lf", &pSSC32->CoefPWs[channel]) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->bProportionalPWs[channel]) != 1) printf("Invalid configuration file.\n");
}
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->rudderchan) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->rightthrusterchan) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->leftthrusterchan) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->rightfluxchan) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pSSC32->leftfluxchan) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%lf", &pSSC32->MinAngle) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%lf", &pSSC32->MidAngle) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%lf", &pSSC32->MaxAngle) != 1) printf("Invalid configuration file.\n");
if (fclose(file) != EXIT_SUCCESS) printf("fclose() failed.\n");
}
else
{
printf("Configuration file not found.\n");
}
}
if (pSSC32->threadperiod < 0)
{
printf("Invalid parameter : threadperiod.\n");
pSSC32->threadperiod = 50;
}
for (channel = 0; channel < NB_CHANNELS_PWM_SSC32; channel++)
{
if (
(pSSC32->MinPWs[channel] < DEFAULT_ABSOLUTE_MIN_PW_SSC32)||(pSSC32->MinPWs[channel] > DEFAULT_ABSOLUTE_MAX_PW_SSC32)||
(pSSC32->MidPWs[channel] < DEFAULT_ABSOLUTE_MIN_PW_SSC32)||(pSSC32->MidPWs[channel] > DEFAULT_ABSOLUTE_MAX_PW_SSC32)||
(pSSC32->MaxPWs[channel] < DEFAULT_ABSOLUTE_MIN_PW_SSC32)||(pSSC32->MaxPWs[channel] > DEFAULT_ABSOLUTE_MAX_PW_SSC32)
||(
(pSSC32->InitPWs[channel] != 0)&&
((pSSC32->InitPWs[channel] < DEFAULT_ABSOLUTE_MIN_PW_SSC32)||(pSSC32->InitPWs[channel] > DEFAULT_ABSOLUTE_MAX_PW_SSC32)||
(pSSC32->MinPWs[channel] > pSSC32->InitPWs[channel])||(pSSC32->InitPWs[channel] > pSSC32->MaxPWs[channel]))
)||
(pSSC32->MinPWs[channel] > pSSC32->MidPWs[channel])||(pSSC32->MidPWs[channel] > pSSC32->MaxPWs[channel])||
(pSSC32->ThresholdPWs[channel] < 0)
)
{
printf("Invalid parameters : channel %d.\n", channel);
pSSC32->MinPWs[channel] = 1000;
pSSC32->MidPWs[channel] = 1500;
pSSC32->MaxPWs[channel] = 2000;
pSSC32->InitPWs[channel] = 1500;
pSSC32->ThresholdPWs[channel] = 0;
pSSC32->CoefPWs[channel] = 1;
pSSC32->bProportionalPWs[channel] = 1;
}
}
if ((pSSC32->rudderchan < 0)||(pSSC32->rudderchan >= NB_CHANNELS_PWM_SSC32))
{
printf("Invalid parameter : rudderchan.\n");
pSSC32->rudderchan = 2;
}
if ((pSSC32->rightthrusterchan < 0)||(pSSC32->rightthrusterchan >= NB_CHANNELS_PWM_SSC32))
{
printf("Invalid parameter : rightthrusterchan.\n");
pSSC32->rightthrusterchan = 1;
}
if ((pSSC32->leftthrusterchan < 0)||(pSSC32->leftthrusterchan >= NB_CHANNELS_PWM_SSC32))
{
printf("Invalid parameter : leftthrusterchan.\n");
pSSC32->leftthrusterchan = 0;
}
if ((pSSC32->rightfluxchan < 0)||(pSSC32->rightfluxchan >= NB_CHANNELS_PWM_SSC32))
{
printf("Invalid parameter : rightfluxchan.\n");
pSSC32->rightfluxchan = 4;
}
if ((pSSC32->leftfluxchan < 0)||(pSSC32->leftfluxchan >= NB_CHANNELS_PWM_SSC32))
{
printf("Invalid parameter : leftfluxchan.\n");
pSSC32->leftfluxchan = 3;
}
if ((pSSC32->MaxAngle-pSSC32->MidAngle <= 0.001)||(pSSC32->MidAngle-pSSC32->MinAngle <= 0.001))
{
printf("Invalid parameters : MinAngle, MidAngle or MaxAngle.\n");
pSSC32->MinAngle = -0.5;
pSSC32->MidAngle = 0;
pSSC32->MaxAngle = 0.5;
}
// Used to save raw data, should be handled specifically...
//pSSC32->pfSaveFile = NULL;
for (channel = 0; channel < NB_CHANNELS_PWM_SSC32; channel++)
{
pSSC32->LastPWs[channel] = 0;
}
if (OpenRS232Port(&pSSC32->RS232Port, pSSC32->szDevPath) != EXIT_SUCCESS)
{
printf("Unable to connect to a SSC32.\n");
return EXIT_FAILURE;
}
if (SetOptionsRS232Port(&pSSC32->RS232Port, pSSC32->BaudRate, NOPARITY, FALSE, 8,
ONESTOPBIT, (UINT)pSSC32->timeout) != EXIT_SUCCESS)
{
printf("Unable to connect to a SSC32.\n");
CloseRS232Port(&pSSC32->RS232Port);
return EXIT_FAILURE;
}
printf("SSC32 connected.\n");
return EXIT_SUCCESS;
}
inline int DisconnectSSC32(SSC32* pSSC32)
{
if (CloseRS232Port(&pSSC32->RS232Port) != EXIT_SUCCESS)
{
printf("SSC32 disconnection failed.\n");
return EXIT_FAILURE;
}
printf("SSC32 disconnected.\n");
return EXIT_SUCCESS;
}
#ifndef DISABLE_SSC32THREAD
THREAD_PROC_RETURN_VALUE SSC32Thread(void* pParam);
#endif // !DISABLE_SSC32THREAD
// min and max might cause incompatibilities...
#ifdef max
#undef max
#endif // max
#ifdef min
#undef min
#endif // min
#endif // !SSC32_H