-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pololu.h
1501 lines (1311 loc) · 62.3 KB
/
Pololu.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
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
// 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 POLOLU_H
#define POLOLU_H
#include "OSMisc.h"
#include "RS232Port.h"
#ifndef DISABLE_POLOLUTHREAD
#include "OSThread.h"
#endif // !DISABLE_POLOLUTHREAD
// 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_POLOLU 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_POLOLU 512
#define NB_CHANNELS_POLOLU 24
#define NB_CHANNELS_PWM_POLOLU NB_CHANNELS_POLOLU
#define NB_CHANNELS_AI_POLOLU NB_CHANNELS_POLOLU
// 11 in decimal...
#define DEFAULT_DEVICE_NUMBER_JRK 0x0B
// 12 in decimal...
#define DEFAULT_DEVICE_NUMBER_MAESTRO 0x0C
// 170 in decimal...
#define BAUD_RATE_INDICATION_BYTE_POLOLU 0xAA
#define SET_TARGET_COMMAND_POLOLU 0x84
#define SET_MULTIPLE_TARGETS_COMMAND_POLOLU 0x9F
#define SET_SPEED_COMMAND_POLOLU 0x87
#define SET_ACCELERATION_COMMAND_POLOLU 0x89
#define SET_PWM_COMMAND_POLOLU 0x8A
#define GET_POSITION_COMMAND_POLOLU 0x90
#define GET_MOVING_STATE_COMMAND_POLOLU 0x93
#define GET_ERRORS_COMMAND_POLOLU 0xA1
#define GO_HOME_COMMAND_POLOLU 0xA2
// In us.
#define DEFAULT_ABSOLUTE_MIN_PW_POLOLU 500
// In us.
#define DEFAULT_MIN_PW_POLOLU 1000
// In us.
#define DEFAULT_MID_PW_POLOLU 1500
// In us.
#define DEFAULT_MAX_PW_POLOLU 2000
// In us.
#define DEFAULT_ABSOLUTE_MAX_PW_POLOLU 2500
#define MAX_NB_TELEMETERS_POLOLU 12
struct POLOLU
{
RS232PORT RS232Port;
FILE* pfSaveFile; // Used to save raw data, should be handled specifically...
int LastPWs[NB_CHANNELS_PWM_POLOLU];
int LastAIs[NB_CHANNELS_AI_POLOLU];
char szCfgFilePath[256];
// Parameters.
char szDevPath[256];
int BaudRate;
int timeout;
int threadperiod;
BOOL bSaveRawData;
int RangingDelay;
BOOL bMedianFilter;
int PololuType;
int DeviceNumber;
int MinPWs[NB_CHANNELS_PWM_POLOLU];
int MidPWs[NB_CHANNELS_PWM_POLOLU];
int MaxPWs[NB_CHANNELS_PWM_POLOLU];
int InitPWs[NB_CHANNELS_PWM_POLOLU];
int ThresholdPWs[NB_CHANNELS_PWM_POLOLU];
double CoefPWs[NB_CHANNELS_PWM_POLOLU];
int bProportionalPWs[NB_CHANNELS_PWM_POLOLU];
double analoginputoffset[NB_CHANNELS_AI_POLOLU];
double analoginputthreshold[NB_CHANNELS_AI_POLOLU];
double analoginputcoef[NB_CHANNELS_AI_POLOLU];
double analoginputx[NB_CHANNELS_AI_POLOLU];
double analoginputy[NB_CHANNELS_AI_POLOLU];
double analoginputz[NB_CHANNELS_AI_POLOLU];
double analoginputphi[NB_CHANNELS_AI_POLOLU];
double analoginputtheta[NB_CHANNELS_AI_POLOLU];
double analoginputpsi[NB_CHANNELS_AI_POLOLU];
double analoginputmin[NB_CHANNELS_AI_POLOLU];
double analoginputmax[NB_CHANNELS_AI_POLOLU];
int rudderchan;
int rightthrusterchan;
int leftthrusterchan;
int rightfluxchan;
int leftfluxchan;
int campanchan;
int camtiltchan;
int gripperclosechan;
int gripperrotationchan;
int armbaserotationchan;
int armrotation1chan;
int armrotation2chan;
int armrotation3chan;
int armrotation4chan;
int extra1chan;
int extra2chan;
int extra3chan;
int extra4chan;
int winddiranaloginputchan;
int windspeedanaloginputchan;
int vbat1analoginputchan;
int ibat1analoginputchan;
int vbat2analoginputchan;
int ibat2analoginputchan;
int switchanaloginputchan;
int telem1analoginputchan;
int telem2analoginputchan;
int telem3analoginputchan;
int telem4analoginputchan;
int telem5analoginputchan;
int telem6analoginputchan;
int telem7analoginputchan;
int telem8analoginputchan;
int telem9analoginputchan;
int telem10analoginputchan;
int telem11analoginputchan;
int telem12analoginputchan;
double MinAngle;
double MidAngle;
double MaxAngle;
double alpha_max_err;
double d_max_err;
BOOL bEnableSetMultipleTargets;
};
typedef struct POLOLU POLOLU;
/*
Get a channel value.
If the channel is configured as analog input in Maestro Control Center, voltage = value*5.0/1023.0 V. If it is a digital
input, bit = (value == 1023)? 1: 0. If it is a digital output, bit = (value < 6000)? 0: 1. If it is a servo output,
pw = value/4 us.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
int channel : (IN) Channel number.
int* pValue : (INOUT) Valid pointer that will receive the value.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int GetValuePololu(POLOLU* pPololu, int channel, int* pValue)
{
unsigned char sendbuf[MAX_NB_BYTES_POLOLU];
unsigned char recvbuf[MAX_NB_BYTES_POLOLU];
int sendbuflen = 0;
int recvbuflen = 0;
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
sendbuf[0] = (unsigned char)BAUD_RATE_INDICATION_BYTE_POLOLU;
sendbuf[1] = (unsigned char)pPololu->DeviceNumber;
sendbuf[2] = (unsigned char)(GET_POSITION_COMMAND_POLOLU & 0x7F);
sendbuf[3] = (unsigned char)channel;
sendbuflen = 4;
if (WriteAllRS232Port(&pPololu->RS232Port, (unsigned char*)sendbuf, sendbuflen) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
if ((pPololu->bSaveRawData)&&(pPololu->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pPololu->pfSaveFile);
fflush(pPololu->pfSaveFile);
}
mSleep(10); // Added because sometimes there was a timeout on the read()
// (even though the data were available if read just after the timeout...)...
// Prepare the buffer that should receive data from device.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = 2;
if (ReadAllRS232Port(&pPololu->RS232Port, (unsigned char*)recvbuf, recvbuflen) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
if ((pPololu->bSaveRawData)&&(pPololu->pfSaveFile))
{
fwrite(recvbuf, recvbuflen, 1, pPololu->pfSaveFile);
fflush(pPololu->pfSaveFile);
}
// Display and analyze received data.
//printf("Received : \"%s\"\n", recvbuf);
*pValue = recvbuf[0] + 256*recvbuf[1];
return EXIT_SUCCESS;
}
/*
Get selected channels value.
For example, if the value of channel 2 needs to be retrieved, set selectedchannels[2] to 1 and check ais[2]. If
the channel is configured as analog input in Maestro Control Center, voltage = ais[2]*5.0/1023.0 V. If it is a digital
input, bit = (ais[2] == 1023)? 1: 0. If it is a digital output, bit = (ais[2] < 6000)? 0: 1. If it is a servo output,
pw = ais[2]/4 us.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
int* selectedchannels : (IN) Valid pointer to a table of NB_CHANNELS_AI_POLOLU elements to indicate which channels
should be considered in ais (0 to ignore the channel or 1 to select it).
int* ais : (IN) Valid pointer to a table of NB_CHANNELS_AI_POLOLU elements that will receive the value for each
channel.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int GetAllValuesPololu(POLOLU* pPololu, int* selectedchannels, int* ais)
{
unsigned char sendbuf[MAX_NB_BYTES_POLOLU];
unsigned char recvbuf[MAX_NB_BYTES_POLOLU];
int sendbuflen = 0;
int recvbuflen = 0;
int channel = 0;
int index = 0, nbselectedchannels = 0;
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
nbselectedchannels = 0;
index = 0;
for (channel = 0; channel < NB_CHANNELS_AI_POLOLU; channel++)
{
if (!selectedchannels[channel]) continue;
sendbuf[index] = (unsigned char)BAUD_RATE_INDICATION_BYTE_POLOLU;
sendbuf[index+1] = (unsigned char)pPololu->DeviceNumber;
sendbuf[index+2] = (unsigned char)(GET_POSITION_COMMAND_POLOLU & 0x7F);
sendbuf[index+3] = (unsigned char)channel;
nbselectedchannels++;
index += 4;
}
if (nbselectedchannels == 0) return EXIT_SUCCESS;
sendbuflen = 4*nbselectedchannels;
//printf("%s\n", sendbuf);
if (WriteAllRS232Port(&pPololu->RS232Port, (unsigned char*)sendbuf, sendbuflen) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
if ((pPololu->bSaveRawData)&&(pPololu->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pPololu->pfSaveFile);
fflush(pPololu->pfSaveFile);
}
mSleep(10); // Added because sometimes there was a timeout on the read()
// (even though the data were available if read just after the timeout...)...
// Prepare the buffer that should receive data from device.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = 2*nbselectedchannels;
if (ReadAllRS232Port(&pPololu->RS232Port, (unsigned char*)recvbuf, recvbuflen) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
if ((pPololu->bSaveRawData)&&(pPololu->pfSaveFile))
{
fwrite(recvbuf, recvbuflen, 1, pPololu->pfSaveFile);
fflush(pPololu->pfSaveFile);
}
// Display and analyze received data.
//printf("Received : \"%s\"\n", recvbuf);
memset(ais, 0, NB_CHANNELS_AI_POLOLU*sizeof(int));
index = 0;
for (channel = 0; channel < NB_CHANNELS_AI_POLOLU; channel++)
{
if (!selectedchannels[channel]) continue;
ais[channel] = recvbuf[index] + 256*recvbuf[index+1];
// Update last known value.
pPololu->LastAIs[channel] = ais[channel];
index += 2;
}
return EXIT_SUCCESS;
}
/*
Set a PWM channel.
The channel needs to be configured as servo output in Maestro Control Center. If it is configured as digital output,
bit = (pw >= 1500)? 1 : 0;.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
int channel : (IN) Channel number (from 0 to NB_CHANNELS_PWM_POLOLU-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 SetPWMPololu(POLOLU* pPololu, int channel, int pw)
{
unsigned char sendbuf[MAX_NB_BYTES_POLOLU];
int target = 0;
int sendbuflen = 0;
if (pPololu->bProportionalPWs[channel])
{
pw = (int)(pPololu->CoefPWs[channel]*(pw-DEFAULT_MID_PW_POLOLU));
if (pw >= 0)
pw = pPololu->MidPWs[channel]+pw*(pPololu->MaxPWs[channel]-pPololu->MidPWs[channel])
/(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU);
else
pw = pPololu->MidPWs[channel]+pw*(pPololu->MinPWs[channel]-pPololu->MidPWs[channel])
/(DEFAULT_MIN_PW_POLOLU-DEFAULT_MID_PW_POLOLU);
}
else
{
pw = DEFAULT_MID_PW_POLOLU+(int)(pPololu->CoefPWs[channel]*(pw-DEFAULT_MID_PW_POLOLU));
}
pw = max(min(pw, pPololu->MaxPWs[channel]), pPololu->MinPWs[channel]);
//pw = max(min(pw, DEFAULT_ABSOLUTE_MAX_PW_POLOLU), DEFAULT_ABSOLUTE_MIN_PW_POLOLU);
// The requested PWM is only applied if it is slightly different from the current value.
if (abs(pw-pPololu->LastPWs[channel]) < pPololu->ThresholdPWs[channel]) return EXIT_SUCCESS;
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
sendbuf[0] = (unsigned char)BAUD_RATE_INDICATION_BYTE_POLOLU;
sendbuf[1] = (unsigned char)pPololu->DeviceNumber;
sendbuf[2] = (unsigned char)(SET_TARGET_COMMAND_POLOLU & 0x7F);
sendbuf[3] = (unsigned char)channel;
target = pw*4;
sendbuf[4] = (unsigned char)(target & 0x7F);
sendbuf[5] = (unsigned char)((target >> 7) & 0x7F);
sendbuflen = 6;
if (WriteAllRS232Port(&pPololu->RS232Port, (unsigned char*)sendbuf, sendbuflen) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
if ((pPololu->bSaveRawData)&&(pPololu->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pPololu->pfSaveFile);
fflush(pPololu->pfSaveFile);
}
// Update last known value.
pPololu->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. The channel needs to be configured as servo
output in Maestro Control Center. If it is configured as digital output, bit = (pws[2] >= 1500)? 1 : 0;.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
int* selectedchannels : (IN) Valid pointer to a table of NB_CHANNELS_PWM_POLOLU 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_POLOLU elements with the desired pulse width for each
channel (in us).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetAllPWMsPololu(POLOLU* pPololu, int* selectedchannels, int* pws)
{
unsigned char sendbuf[MAX_NB_BYTES_POLOLU];
int sendbuflen = 0;
int channel = 0;
int target = 0;
int pws_tmp[NB_CHANNELS_PWM_POLOLU];
int index = 0, firstselectedchannel = 0, nbselectedchannels = 0;
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
if (pPololu->bEnableSetMultipleTargets)
{
sendbuf[0] = (unsigned char)BAUD_RATE_INDICATION_BYTE_POLOLU;
sendbuf[1] = (unsigned char)pPololu->DeviceNumber;
sendbuf[2] = (unsigned char)(SET_MULTIPLE_TARGETS_COMMAND_POLOLU & 0x7F);
firstselectedchannel = NB_CHANNELS_PWM_POLOLU;
nbselectedchannels = 0;
index = 5;
memcpy(pws_tmp, pws, sizeof(pws_tmp));
for (channel = 0; channel < NB_CHANNELS_PWM_POLOLU; channel++)
{
if (!selectedchannels[channel]) continue;
// To check...
if (channel > firstselectedchannel+nbselectedchannels)
{
printf("Pololu multiple channels must be continuous.\n");
return EXIT_FAILURE;
}
if (pPololu->bProportionalPWs[channel])
{
pws_tmp[channel] = (int)(pPololu->CoefPWs[channel]*(pws_tmp[channel]-DEFAULT_MID_PW_POLOLU));
if (pws_tmp[channel] >= 0)
pws_tmp[channel] = pPololu->MidPWs[channel]+pws_tmp[channel]*(pPololu->MaxPWs[channel]-pPololu->MidPWs[channel])
/(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU);
else
pws_tmp[channel] = pPololu->MidPWs[channel]+pws_tmp[channel]*(pPololu->MinPWs[channel]-pPololu->MidPWs[channel])
/(DEFAULT_MIN_PW_POLOLU-DEFAULT_MID_PW_POLOLU);
}
else
{
pws_tmp[channel] = DEFAULT_MID_PW_POLOLU+(int)(pPololu->CoefPWs[channel]*(pws_tmp[channel]-DEFAULT_MID_PW_POLOLU));
}
pws_tmp[channel] = max(min(pws_tmp[channel], pPololu->MaxPWs[channel]), pPololu->MinPWs[channel]);
//pws_tmp[channel] = max(min(pws_tmp[channel], DEFAULT_ABSOLUTE_MAX_PW_POLOLU), DEFAULT_ABSOLUTE_MIN_PW_POLOLU);
// The requested PWM is only applied if it is slightly different from the current value.
if (abs(pws_tmp[channel]-pPololu->LastPWs[channel]) < pPololu->ThresholdPWs[channel]) pws_tmp[channel] = pPololu->LastPWs[channel];
//printf("%d %d %d %d %d\n", channel, pws_tmp[channel], pPololu->LastPWs[channel], abs(pws_tmp[channel]-pPololu->LastPWs[channel]), pPololu->ThresholdPWs[channel]);
target = pws_tmp[channel]*4;
sendbuf[index] = (unsigned char)(target & 0x7F);
sendbuf[index+1] = (unsigned char)((target >> 7) & 0x7F);
firstselectedchannel = min(channel, firstselectedchannel);
nbselectedchannels++;
index += 2;
}
if (nbselectedchannels == 0) return EXIT_SUCCESS;
sendbuf[3] = (unsigned char)nbselectedchannels;
sendbuf[4] = (unsigned char)firstselectedchannel;
sendbuflen = 5+2*nbselectedchannels;
}
else
{
nbselectedchannels = 0;
index = 0;
memcpy(pws_tmp, pws, sizeof(pws_tmp));
for (channel = 0; channel < NB_CHANNELS_PWM_POLOLU; channel++)
{
if (!selectedchannels[channel]) continue;
if (pPololu->bProportionalPWs[channel])
{
pws_tmp[channel] = (int)(pPololu->CoefPWs[channel]*(pws_tmp[channel]-DEFAULT_MID_PW_POLOLU));
if (pws_tmp[channel] >= 0)
pws_tmp[channel] = pPololu->MidPWs[channel]+pws_tmp[channel]*(pPololu->MaxPWs[channel]-pPololu->MidPWs[channel])
/(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU);
else
pws_tmp[channel] = pPololu->MidPWs[channel]+pws_tmp[channel]*(pPololu->MinPWs[channel]-pPololu->MidPWs[channel])
/(DEFAULT_MIN_PW_POLOLU-DEFAULT_MID_PW_POLOLU);
}
else
{
pws_tmp[channel] = DEFAULT_MID_PW_POLOLU+(int)(pPololu->CoefPWs[channel]*(pws_tmp[channel]-DEFAULT_MID_PW_POLOLU));
}
pws_tmp[channel] = max(min(pws_tmp[channel], pPololu->MaxPWs[channel]), pPololu->MinPWs[channel]);
//pws_tmp[channel] = max(min(pws_tmp[channel], DEFAULT_ABSOLUTE_MAX_PW_POLOLU), DEFAULT_ABSOLUTE_MIN_PW_POLOLU);
// The requested PWM is only applied if it is slightly different from the current value.
if (abs(pws_tmp[channel]-pPololu->LastPWs[channel]) < pPololu->ThresholdPWs[channel]) continue;
//printf("%d %d %d %d %d\n", channel, pws_tmp[channel], pPololu->LastPWs[channel], abs(pws_tmp[channel]-pPololu->LastPWs[channel]), pPololu->ThresholdPWs[channel]);
sendbuf[index] = (unsigned char)BAUD_RATE_INDICATION_BYTE_POLOLU;
sendbuf[index+1] = (unsigned char)pPololu->DeviceNumber;
sendbuf[index+2] = (unsigned char)(SET_TARGET_COMMAND_POLOLU & 0x7F);
sendbuf[index+3] = (unsigned char)channel;
target = pws_tmp[channel]*4;
sendbuf[index+4] = (unsigned char)(target & 0x7F);
sendbuf[index+5] = (unsigned char)((target >> 7) & 0x7F);
nbselectedchannels++;
index += 6;
}
if (nbselectedchannels == 0) return EXIT_SUCCESS;
sendbuflen = 6*nbselectedchannels;
}
//printf("%s\n", sendbuf);
if (WriteAllRS232Port(&pPololu->RS232Port, (unsigned char*)sendbuf, sendbuflen) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
if ((pPololu->bSaveRawData)&&(pPololu->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pPololu->pfSaveFile);
fflush(pPololu->pfSaveFile);
}
for (channel = 0; channel < NB_CHANNELS_PWM_POLOLU; 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]-pPololu->LastPWs[channel]) < pPololu->ThresholdPWs[channel]) continue;
// Update last known value.
pPololu->LastPWs[channel] = pws_tmp[channel];
}
return EXIT_SUCCESS;
}
// Pololu Jrk motor controller quick adaptation (default device number is 11)...
inline int SetPWMJrkPololu(POLOLU* pPololu, int pw)
{
unsigned char sendbuf[MAX_NB_BYTES_POLOLU];
int target = 0;
int sendbuflen = 0;
int channel = 0; // Only 1 motor on Jrk...
if (pPololu->bProportionalPWs[channel])
{
pw = (int)(pPololu->CoefPWs[channel]*(pw-DEFAULT_MID_PW_POLOLU));
if (pw >= 0)
pw = pPololu->MidPWs[channel]+pw*(pPololu->MaxPWs[channel]-pPololu->MidPWs[channel])
/(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU);
else
pw = pPololu->MidPWs[channel]+pw*(pPololu->MinPWs[channel]-pPololu->MidPWs[channel])
/(DEFAULT_MIN_PW_POLOLU-DEFAULT_MID_PW_POLOLU);
}
else
{
pw = DEFAULT_MID_PW_POLOLU+(int)(pPololu->CoefPWs[channel]*(pw-DEFAULT_MID_PW_POLOLU));
}
pw = max(min(pw, pPololu->MaxPWs[channel]), pPololu->MinPWs[channel]);
//pw = max(min(pw, DEFAULT_ABSOLUTE_MAX_PW_POLOLU), DEFAULT_ABSOLUTE_MIN_PW_POLOLU);
// The requested PWM is only applied if it is slightly different from the current value.
if (abs(pw-pPololu->LastPWs[channel]) < pPololu->ThresholdPWs[channel]) return EXIT_SUCCESS;
// bEnableSetMultipleTargets is used here to choose between Pololu protocol and compact protocol modes...
if (pPololu->bEnableSetMultipleTargets)
{
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
sendbuf[0] = (unsigned char)BAUD_RATE_INDICATION_BYTE_POLOLU;
sendbuf[1] = (unsigned char)pPololu->DeviceNumber;
target = (pw-DEFAULT_MIN_PW_POLOLU)*4095/(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU);
sendbuf[2] = (unsigned char)(0x40 + (target & 0x1F)); // Command byte holds the lower 5 bits of target.
sendbuf[3] = (unsigned char)((target >> 5) & 0x7F); // Data byte holds the upper 7 bits of target.
sendbuflen = 4;
}
else
{
// Prepare data to send to device.
memset(sendbuf, 0, sizeof(sendbuf));
target = (pw-DEFAULT_MIN_PW_POLOLU)*4095/(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU);
sendbuf[0] = (unsigned char)(0xC0 + (target & 0x1F)); // Command byte holds the lower 5 bits of target.
sendbuf[1] = (unsigned char)((target >> 5) & 0x7F); // Data byte holds the upper 7 bits of target.
sendbuflen = 2;
}
if (WriteAllRS232Port(&pPololu->RS232Port, (unsigned char*)sendbuf, sendbuflen) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
if ((pPololu->bSaveRawData)&&(pPololu->pfSaveFile))
{
fwrite(sendbuf, sendbuflen, 1, pPololu->pfSaveFile);
fflush(pPololu->pfSaveFile);
}
// Update last known value.
pPololu->LastPWs[channel] = pw;
return EXIT_SUCCESS;
}
/*
Set rudderchan PWM channel as a rudder angle.
The channel needs to be configured as servo output in Maestro Control Center.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
double angle : (IN) Desired rudder angle (in rad, should be in [-max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle));max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle))]).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetRudderPololu(POLOLU* pPololu, double angle)
{
int pw = 0;
#ifndef DISABLE_RUDDER_MIDANGLE
double angletmp = 0;
#endif // DISABLE_RUDDER_MIDANGLE
// Convert angle (in rad) into Pololu pulse width (in us).
#ifndef DISABLE_RUDDER_MIDANGLE
angletmp = angle >= 0? pPololu->MidAngle+angle*(pPololu->MaxAngle-pPololu->MidAngle)/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)): pPololu->MidAngle+angle*(pPololu->MidAngle-pPololu->MinAngle)/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle));
//angletmp = angle >= 0? pPololu->MidAngle+urudder*(pPololu->MaxAngle-pPololu->MidAngle): pPololu->MidAngle+urudder*(pPololu->MidAngle-pPololu->MinAngle);
if (angletmp >= 0)
pw = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU)
/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)));
else
pw = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MID_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)));
#else
pw = DEFAULT_MID_PW_POLOLU+(int)(angle*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/(pPololu->MaxAngle-pPololu->MinAngle));
#endif // !DISABLE_POLOLU_MIDANGLE
pw = max(min(pw, DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
return SetPWMPololu(pPololu, pPololu->rudderchan, pw);
}
/*
Set rightthrusterchan and leftthrusterchan PWM channels as thrusters inputs.
The channels need to be configured as servo output in Maestro Control Center.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
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 SetThrustersPololu(POLOLU* pPololu, double urt, double ult)
{
int selectedchannels[NB_CHANNELS_PWM_POLOLU];
int pws[NB_CHANNELS_PWM_POLOLU];
memset(selectedchannels, 0, sizeof(selectedchannels));
memset(pws, 0, sizeof(pws));
// Convert u (in [-1;1]) into Pololu pulse width (in us).
pws[pPololu->rightthrusterchan] = DEFAULT_MID_PW_POLOLU+(int)(urt*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->leftthrusterchan] = DEFAULT_MID_PW_POLOLU+(int)(ult*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->rightthrusterchan] = max(min(pws[pPololu->rightthrusterchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
pws[pPololu->leftthrusterchan] = max(min(pws[pPololu->leftthrusterchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
selectedchannels[pPololu->rightthrusterchan] = 1;
selectedchannels[pPololu->leftthrusterchan] = 1;
return SetAllPWMsPololu(pPololu, selectedchannels, pws);
}
/*
Set rudderchan PWM channel as a rudder angle and rightthrusterchan PWM channel as a thruster input.
The channels need to be configured as servo output in Maestro Control Center.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
double angle : (IN) Desired rudder angle (in rad, should be in [-max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle));max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle))]).
double urt : (IN) Desired thruster input (in [-1;1]).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetRudderThrusterPololu(POLOLU* pPololu, double angle, double urt)
{
int selectedchannels[NB_CHANNELS_PWM_POLOLU];
int pws[NB_CHANNELS_PWM_POLOLU];
#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 Pololu pulse width (in us).
#ifndef DISABLE_RUDDER_MIDANGLE
angletmp = angle >= 0? pPololu->MidAngle+angle*(pPololu->MaxAngle-pPololu->MidAngle)/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)): pPololu->MidAngle+angle*(pPololu->MidAngle-pPololu->MinAngle)/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle));
//angletmp = angle >= 0? pPololu->MidAngle+urudder*(pPololu->MaxAngle-pPololu->MidAngle): pPololu->MidAngle+urudder*(pPololu->MidAngle-pPololu->MinAngle);
if (angletmp >= 0)
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU)
/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)));
else
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MID_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)));
#else
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angle*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/(pPololu->MaxAngle-pPololu->MinAngle));
#endif // DISABLE_RUDDER_MIDANGLE
// Convert u (in [-1;1]) into Pololu pulse width (in us).
pws[pPololu->rightthrusterchan] = DEFAULT_MID_PW_POLOLU+(int)(urt*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->rudderchan] = max(min(pws[pPololu->rudderchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
pws[pPololu->rightthrusterchan] = max(min(pws[pPololu->rightthrusterchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
selectedchannels[pPololu->rudderchan] = 1;
selectedchannels[pPololu->rightthrusterchan] = 1;
return SetAllPWMsPololu(pPololu, selectedchannels, pws);
}
/*
Set rudderchan PWM channel as a rudder angle, rightthrusterchan and leftthrusterchan PWM channels as thrusters inputs.
The channels need to be configured as servo output in Maestro Control Center.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
double angle : (IN) Desired rudder angle (in rad, should be in [-max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle));max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle))]).
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 SetRudderThrustersPololu(POLOLU* pPololu, double angle, double urt, double ult)
{
int selectedchannels[NB_CHANNELS_PWM_POLOLU];
int pws[NB_CHANNELS_PWM_POLOLU];
#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 Pololu pulse width (in us).
#ifndef DISABLE_RUDDER_MIDANGLE
angletmp = angle >= 0? pPololu->MidAngle+angle*(pPololu->MaxAngle-pPololu->MidAngle)/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)): pPololu->MidAngle+angle*(pPololu->MidAngle-pPololu->MinAngle)/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle));
//angletmp = angle >= 0? pPololu->MidAngle+urudder*(pPololu->MaxAngle-pPololu->MidAngle): pPololu->MidAngle+urudder*(pPololu->MidAngle-pPololu->MinAngle);
if (angletmp >= 0)
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU)
/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)));
else
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MID_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)));
#else
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angle*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/(pPololu->MaxAngle-pPololu->MinAngle));
#endif // DISABLE_RUDDER_MIDANGLE
// Convert u (in [-1;1]) into Pololu pulse width (in us).
pws[pPololu->rightthrusterchan] = DEFAULT_MID_PW_POLOLU+(int)(urt*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->leftthrusterchan] = DEFAULT_MID_PW_POLOLU+(int)(ult*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->rudderchan] = max(min(pws[pPololu->rudderchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
pws[pPololu->rightthrusterchan] = max(min(pws[pPololu->rightthrusterchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
pws[pPololu->leftthrusterchan] = max(min(pws[pPololu->leftthrusterchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
selectedchannels[pPololu->rudderchan] = 1;
selectedchannels[pPololu->rightthrusterchan] = 1;
selectedchannels[pPololu->leftthrusterchan] = 1;
return SetAllPWMsPololu(pPololu, 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.
The channels need to be configured as servo output in Maestro Control Center.
POLOLU* pPololu : (INOUT) Valid pointer to a structure corresponding to a Pololu Maestro.
double angle : (IN) Desired rudder angle (in rad, should be in [-max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle));max(fabs(pololu.MinAngle),fabs(pololu.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 SetRudderThrustersFluxPololu(POLOLU* pPololu, double angle, double urt, double ult, double urf, double ulf)
{
int selectedchannels[NB_CHANNELS_PWM_POLOLU];
int pws[NB_CHANNELS_PWM_POLOLU];
#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 Pololu pulse width (in us).
#ifndef DISABLE_RUDDER_MIDANGLE
angletmp = angle >= 0? pPololu->MidAngle+angle*(pPololu->MaxAngle-pPololu->MidAngle)/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)): pPololu->MidAngle+angle*(pPololu->MidAngle-pPololu->MinAngle)/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle));
//angletmp = angle >= 0? pPololu->MidAngle+urudder*(pPololu->MaxAngle-pPololu->MidAngle): pPololu->MidAngle+urudder*(pPololu->MidAngle-pPololu->MinAngle);
if (angletmp >= 0)
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU)
/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)));
else
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MID_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/max(fabs(pPololu->MinAngle), fabs(pPololu->MaxAngle)));
#else
pws[pPololu->rudderchan] = DEFAULT_MID_PW_POLOLU+(int)(angle*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/(pPololu->MaxAngle-pPololu->MinAngle));
#endif // DISABLE_RUDDER_MIDANGLE
// Convert u (in [-1;1]) into Pololu pulse width (in us).
pws[pPololu->rightthrusterchan] = DEFAULT_MID_PW_POLOLU+(int)(urt*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->leftthrusterchan] = DEFAULT_MID_PW_POLOLU+(int)(ult*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->rightfluxchan] = DEFAULT_MID_PW_POLOLU+(int)(urf*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->leftfluxchan] = DEFAULT_MID_PW_POLOLU+(int)(ulf*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pws[pPololu->rudderchan] = max(min(pws[pPololu->rudderchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
pws[pPololu->rightthrusterchan] = max(min(pws[pPololu->rightthrusterchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
pws[pPololu->leftthrusterchan] = max(min(pws[pPololu->leftthrusterchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
pws[pPololu->rightfluxchan] = max(min(pws[pPololu->rightfluxchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
pws[pPololu->leftfluxchan] = max(min(pws[pPololu->leftfluxchan], DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
selectedchannels[pPololu->rudderchan] = 1;
selectedchannels[pPololu->rightthrusterchan] = 1;
selectedchannels[pPololu->leftthrusterchan] = 1;
selectedchannels[pPololu->rightfluxchan] = 1;
selectedchannels[pPololu->leftfluxchan] = 1;
return SetAllPWMsPololu(pPololu, selectedchannels, pws);
}
// Pololu Jrk motor controller quick adaptation (default device number is 11)...
// u should be in [-1;1].
inline int SetMotorJrkPololu(POLOLU* pPololu, double umotor)
{
int pw = 0;
// Convert u (in [-1;1]) into Pololu pulse width (in us).
pw = DEFAULT_MID_PW_POLOLU+(int)(umotor*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)/2.0);
pw = max(min(pw, DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
return SetPWMJrkPololu(pPololu, pw);
}
// Pololu Jrk motor controller quick adaptation (default device number is 11)...
// angle should be in [-max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle));max(fabs(pololu.MinAngle),fabs(pololu.MaxAngle))].
inline int SetRudderJrkPololu(POLOLU* pPololu, double angle)
{
int pw = 0;
#ifndef DISABLE_RUDDER_MIDANGLE
double angletmp = 0;
#endif // DISABLE_RUDDER_MIDANGLE
// Convert angle (in rad) into Pololu pulse width (in us).
#ifndef DISABLE_RUDDER_MIDANGLE
angletmp = angle >= 0? pPololu->MidAngle+angle*(pPololu->MaxAngle-pPololu->MidAngle)/max(fabs(pPololu->MinAngle),fabs(pPololu->MaxAngle)): pPololu->MidAngle+angle*(pPololu->MidAngle-pPololu->MinAngle)/max(fabs(pPololu->MinAngle),fabs(pPololu->MaxAngle));
//angletmp = angle >= 0? pPololu->MidAngle+urudder*(pPololu->MaxAngle-pPololu->MidAngle): pPololu->MidAngle+urudder*(pPololu->MidAngle-pPololu->MinAngle);
if (angletmp >= 0)
pw = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MID_PW_POLOLU)
/max(fabs(pPololu->MinAngle),fabs(pPololu->MaxAngle)));
else
pw = DEFAULT_MID_PW_POLOLU+(int)(angletmp*(DEFAULT_MID_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/max(fabs(pPololu->MinAngle),fabs(pPololu->MaxAngle)));
#else
pw = DEFAULT_MID_PW_POLOLU+(int)(angle*(DEFAULT_MAX_PW_POLOLU-DEFAULT_MIN_PW_POLOLU)
/(pPololu->MaxAngle-pPololu->MinAngle));
#endif // DISABLE_RUDDER_MIDANGLE
pw = max(min(pw, DEFAULT_MAX_PW_POLOLU), DEFAULT_MIN_PW_POLOLU);
return SetPWMJrkPololu(pPololu, pw);
}
// In m.
inline int GetTelemetersPololu(POLOLU* pPololu, double* pDist1, double* pDist2, double* pDist3, double* pDist4, double* pDist5, double* pDist6, double* pDist7, double* pDist8, double* pDist9, double* pDist10, double* pDist11, double* pDist12)
{
int selectedchannels[NB_CHANNELS_AI_POLOLU];
int ais[NB_CHANNELS_AI_POLOLU];
int i = 0;
memset(selectedchannels, 0, sizeof(selectedchannels));
memset(ais, 0, sizeof(ais));
selectedchannels[pPololu->telem1analoginputchan] = 1;
selectedchannels[pPololu->telem2analoginputchan] = 1;
selectedchannels[pPololu->telem3analoginputchan] = 1;
selectedchannels[pPololu->telem4analoginputchan] = 1;
selectedchannels[pPololu->telem5analoginputchan] = 1;
selectedchannels[pPololu->telem6analoginputchan] = 1;
selectedchannels[pPololu->telem7analoginputchan] = 1;
selectedchannels[pPololu->telem8analoginputchan] = 1;
selectedchannels[pPololu->telem9analoginputchan] = 1;
selectedchannels[pPololu->telem10analoginputchan] = 1;
selectedchannels[pPololu->telem11analoginputchan] = 1;
selectedchannels[pPololu->telem12analoginputchan] = 1;
if (GetAllValuesPololu(pPololu, selectedchannels, ais) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
i = pPololu->telem1analoginputchan;
*pDist1 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem2analoginputchan;
*pDist2 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem3analoginputchan;
*pDist3 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem4analoginputchan;
*pDist4 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem5analoginputchan;
*pDist5 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem6analoginputchan;
*pDist6 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem7analoginputchan;
*pDist7 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem8analoginputchan;
*pDist8 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem9analoginputchan;
*pDist9 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem10analoginputchan;
*pDist10 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem11analoginputchan;
*pDist11 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
i = pPololu->telem12analoginputchan;
*pDist12 = pPololu->analoginputcoef[i]*ais[i]*5.0/1023.0+pPololu->analoginputoffset[i];
return EXIT_SUCCESS;
}
// duration in us.
inline int SetPulsePololu(POLOLU* pPololu, int channel, int duration)
{
if (SetPWMPololu(pPololu, channel, 2000) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(duration/1000);
if (SetPWMPololu(pPololu, channel, 1000) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
inline int CheckPololu(POLOLU* pPololu)
{
if (SetRudderPololu(pPololu, -0.25) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetRudderPololu(pPololu, 0.25) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetRudderPololu(pPololu, 0.0) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetThrustersPololu(pPololu, -0.25, -0.25) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetThrustersPololu(pPololu, 0.0, 0.0) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetThrustersPololu(pPololu, 0.25, 0.25) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
if (SetThrustersPololu(pPololu, 0.0, 0.0) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
mSleep(2000);
return EXIT_SUCCESS;
}
// Pololu must be initialized to 0 before (e.g. POLOLU pololu; memset(&pololu, 0, sizeof(POLOLU));)!
inline int ConnectPololu(POLOLU* pPololu, char* szCfgFilePath)
{
FILE* file = NULL;
char line[256];
int channel = 0;
memset(pPololu->szCfgFilePath, 0, sizeof(pPololu->szCfgFilePath));