-
Notifications
You must be signed in to change notification settings - Fork 54
/
DriverStationEnhancedIO.cpp
995 lines (922 loc) · 30.6 KB
/
DriverStationEnhancedIO.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "DriverStationEnhancedIO.h"
#include "NetworkCommunication/UsageReporting.h"
#include "Synchronized.h"
#include "WPIErrors.h"
#include <strLib.h>
/**
* DriverStationEnhancedIO contructor.
*
* This is only called once when the DriverStation constructor is called.
*/
DriverStationEnhancedIO::DriverStationEnhancedIO()
: m_inputValid (false)
, m_outputValid (false)
, m_configChanged (false)
, m_requestEnhancedEnable (false)
{
bzero((char*)&m_inputData, sizeof(m_inputData));
bzero((char*)&m_outputData, sizeof(m_outputData));
m_outputData.size = sizeof(m_outputData) - 1;
m_outputData.id = kOutputBlockID;
// Expected to be active low, so initialize inactive.
m_outputData.data.fixed_digital_out = 0x3;
m_inputDataSemaphore = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
m_outputDataSemaphore = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
m_encoderOffsets[0] = 0;
m_encoderOffsets[1] = 0;
}
/**
* DriverStationEnhancedIO destructor.
*
* Called only when the DriverStation class is destroyed.
*/
DriverStationEnhancedIO::~DriverStationEnhancedIO()
{
semDelete(m_outputDataSemaphore);
semDelete(m_inputDataSemaphore);
}
/**
* Called by the DriverStation class when data is available.
* This function will set any modified configuration / output,
* then read the input and configuration from the IO.
*/
void DriverStationEnhancedIO::UpdateData()
{
int32_t retVal;
{
status_block_t tempOutputData;
Synchronized sync(m_outputDataSemaphore);
if (m_outputValid || m_configChanged || m_requestEnhancedEnable)
{
m_outputData.flags = kStatusValid;
if (m_requestEnhancedEnable)
{
// Someone called one of the get config APIs, but we are not in enhanced mode.
m_outputData.flags |= kForceEnhancedMode;
}
if (m_configChanged)
{
if (!m_outputValid)
{
// Someone called one of the set config APIs, but we are not in enhanced mode.
m_outputData.flags |= kForceEnhancedMode;
}
m_outputData.flags |= kStatusConfigChanged;
}
overrideIOConfig((char*)&m_outputData, 5);
}
retVal = getDynamicControlData(kOutputBlockID, (char*)&tempOutputData, sizeof(status_block_t), 5);
if (retVal == 0)
{
if (m_outputValid)
{
if (m_configChanged)
{
// If our config change made the round trip then clear the flag.
if (IsConfigEqual(tempOutputData, m_outputData))
{
m_configChanged = false;
}
}
else
{
// TODO: This won't work until artf1128 is fixed
//if (tempOutputData.flags & kStatusConfigChanged)
{
// Configuration was updated on the DS, so update our local cache.
MergeConfigIntoOutput(tempOutputData, m_outputData);
}
}
}
else
{
// Initialize the local cache.
MergeConfigIntoOutput(tempOutputData, m_outputData);
}
m_requestEnhancedEnable = false;
m_outputValid = true;
}
else
{
m_outputValid = false;
m_inputValid = false;
}
}
{
Synchronized sync(m_inputDataSemaphore);
control_block_t tempInputData;
retVal = getDynamicControlData(kInputBlockID, (char*)&tempInputData, sizeof(control_block_t), 5);
if (retVal == 0 && tempInputData.data.api_version == kSupportedAPIVersion)
{
m_inputData = tempInputData;
m_inputValid = true;
}
else
{
m_outputValid = false;
m_inputValid = false;
}
}
}
/**
* Merge the config portion of the DS output block into the local cache.
*/
void DriverStationEnhancedIO::MergeConfigIntoOutput(const status_block_t &dsOutputBlock, status_block_t &localCache)
{
localCache.data.digital = (localCache.data.digital & dsOutputBlock.data.digital_oe) |
(dsOutputBlock.data.digital & ~dsOutputBlock.data.digital_oe);
localCache.data.digital_oe = dsOutputBlock.data.digital_oe;
localCache.data.digital_pe = dsOutputBlock.data.digital_pe;
localCache.data.pwm_period[0] = dsOutputBlock.data.pwm_period[0];
localCache.data.pwm_period[1] = dsOutputBlock.data.pwm_period[1];
localCache.data.enables = dsOutputBlock.data.enables;
}
/**
* Compare the config portion of the output blocks.
*/
bool DriverStationEnhancedIO::IsConfigEqual(const status_block_t &dsOutputBlock, const status_block_t &localCache)
{
if (localCache.data.digital_oe != dsOutputBlock.data.digital_oe) return false;
if ((localCache.data.digital & ~dsOutputBlock.data.digital) !=
(dsOutputBlock.data.digital & ~dsOutputBlock.data.digital)) return false;
if (localCache.data.digital_pe != dsOutputBlock.data.digital_pe) return false;
if (localCache.data.pwm_period[0] != dsOutputBlock.data.pwm_period[0]) return false;
if (localCache.data.pwm_period[1] != dsOutputBlock.data.pwm_period[1]) return false;
if (localCache.data.enables != dsOutputBlock.data.enables) return false;
return true;
}
/**
* Query an accelerometer channel on the DS IO.
*
* @param channel The channel number to read.
* @return The current acceleration on the channel in Gs.
*/
double DriverStationEnhancedIO::GetAcceleration(tAccelChannel channel)
{
if (channel < 1 || channel > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 2");
return 0.0;
}
if (!m_inputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0.0;
}
static uint8_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_Acceleration);
reported_mask |= (1 >> channel);
}
Synchronized sync(m_inputDataSemaphore);
return (m_inputData.data.accel[channel] - kAccelOffset) / kAccelScale;
}
/**
* Query an analog input channel on the DS IO.
*
* @param channel The channel number to read. [1,8]
* @return The analog input voltage for the channel.
*/
double DriverStationEnhancedIO::GetAnalogIn(uint32_t channel)
{
// 3.3V is the analog reference voltage
return GetAnalogInRatio(channel) * kAnalogInputReference;
}
/**
* Query an analog input channel on the DS IO in ratiometric form.
*
* @param channel The channel number to read. [1,8]
* @return The analog input percentage for the channel.
*/
double DriverStationEnhancedIO::GetAnalogInRatio(uint32_t channel)
{
if (channel < 1 || channel > 8)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 8");
return 0.0;
}
if (!m_inputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0.0;
}
static uint16_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_AnalogIn);
reported_mask |= (1 >> channel);
}
Synchronized sync(m_inputDataSemaphore);
return m_inputData.data.analog[channel-1] / kAnalogInputResolution;
}
/**
* Query the voltage currently being output.
*
* AO1 is pin 11 on the top connector (P2).
* AO2 is pin 12 on the top connector (P2).
*
* @param channel The analog output channel on the DS IO. [1,2]
* @return The voltage being output on the channel.
*/
double DriverStationEnhancedIO::GetAnalogOut(uint32_t channel)
{
if (channel < 1 || channel > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 2");
return 0.0;
}
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0.0;
}
Synchronized sync(m_outputDataSemaphore);
return m_outputData.data.dac[channel-1] * kAnalogOutputReference / kAnalogOutputResolution;
}
/**
* Set the analog output voltage.
*
* AO1 is pin 11 on the top connector (P2).
* AO2 is pin 12 on the top connector (P2).
* AO1 is the reference voltage for the 2 analog comparators on DIO15 and DIO16.
*
* The output range is 0V to 4V, however due to the supply voltage don't expect more than about 3V.
* Current supply capability is only 100uA.
*
* @param channel The analog output channel on the DS IO. [1,2]
* @param value The voltage to output on the channel.
*/
void DriverStationEnhancedIO::SetAnalogOut(uint32_t channel, double value)
{
if (channel < 1 || channel > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 2");
return;
}
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return;
}
if (value < 0.0) value = 0.0;
if (value > kAnalogOutputReference) value = kAnalogOutputReference;
static uint8_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_AnalogOut);
reported_mask |= (1 >> channel);
}
Synchronized sync(m_outputDataSemaphore);
m_outputData.data.dac[channel-1] = (uint8_t)(value / kAnalogOutputReference * kAnalogOutputResolution);
}
/**
* Get the state of a button on the IO board.
*
* Button1 is the physical button "S1".
* Button2 is pin 4 on the top connector (P2).
* Button3 is pin 6 on the top connector (P2).
* Button4 is pin 8 on the top connector (P2).
* Button5 is pin 10 on the top connector (P2).
* Button6 is pin 7 on the top connector (P2).
*
* Button2 through Button6 are Capacitive Sense buttons.
*
* @param channel The button channel to read. [1,6]
* @return The state of the selected button.
*/
bool DriverStationEnhancedIO::GetButton(uint32_t channel)
{
if (channel < 1 || channel > 6)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 6");
return false;
}
static uint8_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_Button);
reported_mask |= (1 >> channel);
}
return ((GetButtons() >> (channel-1)) & 1) != 0;
}
/**
* Get the state of all the button channels.
*
* @return The state of the 6 button channels in the 6 lsb of the returned byte.
*/
uint8_t DriverStationEnhancedIO::GetButtons()
{
if (!m_inputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0;
}
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, 0, nUsageReporting::kDriverStationEIO_Button);
Synchronized sync(m_inputDataSemaphore);
return m_inputData.data.buttons;
}
/**
* Set the state of an LED on the IO board.
*
* @param channel The LED channel to set. [1,8]
* @param value True to turn the LED on.
*/
void DriverStationEnhancedIO::SetLED(uint32_t channel, bool value)
{
if (channel < 1 || channel > 8)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 8");
return;
}
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return;
}
static uint16_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_LED);
reported_mask |= (1 >> channel);
}
uint8_t leds;
Synchronized sync(m_outputDataSemaphore);
leds = m_outputData.data.leds;
leds &= ~(1 << (channel-1));
if (value) leds |= 1 << (channel-1);
m_outputData.data.leds = leds;
}
/**
* Set the state of all 8 LEDs on the IO board.
*
* @param value The state of each LED. LED1 is lsb and LED8 is msb.
*/
void DriverStationEnhancedIO::SetLEDs(uint8_t value)
{
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return;
}
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, 0, nUsageReporting::kDriverStationEIO_LED);
Synchronized sync(m_outputDataSemaphore);
m_outputData.data.leds = value;
}
/**
* Get the current state of a DIO channel regardless of mode.
*
* @param channel The DIO channel to read. [1,16]
* @return The state of the selected digital line.
*/
bool DriverStationEnhancedIO::GetDigital(uint32_t channel)
{
if (channel < 1 || channel > 16)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 16");
return false;
}
static uint32_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_DigitalIn);
reported_mask |= (1 >> channel);
}
return ((GetDigitals() >> (channel-1)) & 1) != 0;
}
/**
* Get the state of all 16 DIO lines regardless of mode.
*
* @return The state of all DIO lines. DIO1 is lsb and DIO16 is msb.
*/
uint16_t DriverStationEnhancedIO::GetDigitals()
{
if (!m_inputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0;
}
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, 0, nUsageReporting::kDriverStationEIO_DigitalIn);
Synchronized sync(m_inputDataSemaphore);
return m_inputData.data.digital;
}
/**
* Set the state of a DIO line that is configured for digital output.
*
* @param channel The DIO channel to set. [1,16]
* @param value The state to set the selected channel to.
*/
void DriverStationEnhancedIO::SetDigitalOutput(uint32_t channel, bool value)
{
if (channel < 1 || channel > 16)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 16");
return;
}
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return;
}
static uint32_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_DigitalOut);
reported_mask |= (1 >> channel);
}
uint16_t digital;
Synchronized sync(m_outputDataSemaphore);
if (m_outputData.data.digital_oe & (1 << (channel-1)))
{
digital = m_outputData.data.digital;
digital &= ~(1 << (channel-1));
if (value) digital |= 1 << (channel-1);
m_outputData.data.digital = digital;
}
else
{
wpi_setWPIError(LineNotOutput);
}
}
/**
* Get the current configuration for a DIO line.
*
* This has the side effect of forcing the Driver Station to switch to Enhanced mode if it's not when called.
* If Enhanced mode is not enabled when this is called, it will return kUnknown.
*
* @param channel The DIO channel config to get. [1,16]
* @return The configured mode for the DIO line.
*/
DriverStationEnhancedIO::tDigitalConfig DriverStationEnhancedIO::GetDigitalConfig(uint32_t channel)
{
if (channel < 1 || channel > 16)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 16");
return kUnknown;
}
if (!m_outputValid)
{
m_requestEnhancedEnable = true;
wpi_setWPIError(EnhancedIOMissing);
return kUnknown;
}
Synchronized sync(m_outputDataSemaphore);
if ((channel >= 1) && (channel <= 4))
{
if (m_outputData.data.pwm_enable & (1 << (channel - 1)))
{
return kPWM;
}
}
if ((channel >= 15) && (channel <= 16))
{
if (m_outputData.data.comparator_enable & (1 << (channel - 15)))
{
return kAnalogComparator;
}
}
if (m_outputData.data.digital_oe & (1 << (channel - 1)))
{
return kOutput;
}
if (!(m_outputData.data.digital_pe & (1 << (channel - 1))))
{
return kInputFloating;
}
if (m_outputData.data.digital & (1 << (channel - 1)))
{
return kInputPullUp;
}
else
{
return kInputPullDown;
}
}
/**
* Override the DS's configuration of a DIO line.
*
* If configured to kInputFloating, the selected DIO line will be tri-stated with no internal pull resistor.
*
* If configured to kInputPullUp, the selected DIO line will be tri-stated with a 5k-Ohm internal pull-up resistor enabled.
*
* If configured to kInputPullDown, the selected DIO line will be tri-stated with a 5k-Ohm internal pull-down resistor enabled.
*
* If configured to kOutput, the selected DIO line will actively drive to 0V or Vddio (specified by J1 and J4).
* DIO1 through DIO12, DIO15, and DIO16 can source 4mA and can sink 8mA.
* DIO12 and DIO13 can source 4mA and can sink 25mA.
*
* In addition to the common configurations, DIO1 through DIO4 can be configured to kPWM to enable PWM output.
*
* In addition to the common configurations, DIO15 and DIO16 can be configured to kAnalogComparator to enable
* analog comparators on those 2 DIO lines. When enabled, the lines are tri-stated and will accept analog voltages
* between 0V and 3.3V. If the input voltage is greater than the voltage output by AO1, the DIO will read as true,
* if less then false.
*
* @param channel The DIO line to configure. [1,16]
* @param config The mode to put the DIO line in.
*/
void DriverStationEnhancedIO::SetDigitalConfig(uint32_t channel, tDigitalConfig config)
{
if (channel < 1 || channel > 16)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 16");
return;
}
if (config == kPWM && (channel < 1 || channel > 4))
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel in PWM mode must be between 1 and 4");
return;
}
if (config == kAnalogComparator && (channel < 15 || channel > 16))
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel in Analog Comparator mode must be between 15 and 16");
return;
}
Synchronized sync(m_outputDataSemaphore);
m_configChanged = true;
if ((channel >= 1) && (channel <= 4))
{
if (config == kPWM)
{
m_outputData.data.pwm_enable |= 1 << (channel - 1);
m_outputData.data.digital &= ~(1 << (channel - 1));
m_outputData.data.digital_oe |= 1 << (channel - 1);
m_outputData.data.digital_pe &= ~(1 << (channel - 1));
return;
}
else
{
m_outputData.data.pwm_enable &= ~(1 << (channel - 1));
}
}
else if ((channel >= 15) && (channel <= 16))
{
if (config == kAnalogComparator)
{
m_outputData.data.comparator_enable |= 1 << (channel - 15);
m_outputData.data.digital &= ~(1 << (channel - 1));
m_outputData.data.digital_oe &= ~(1 << (channel - 1));
m_outputData.data.digital_pe &= ~(1 << (channel - 1));
return;
}
else
{
m_outputData.data.comparator_enable &= ~(1 << (channel - 15));
}
}
if (config == kInputFloating)
{
m_outputData.data.digital &= ~(1 << (channel - 1));
m_outputData.data.digital_oe &= ~(1 << (channel - 1));
m_outputData.data.digital_pe &= ~(1 << (channel - 1));
}
else if (config == kInputPullUp)
{
m_outputData.data.digital |= 1 << (channel - 1);
m_outputData.data.digital_oe &= ~(1 << (channel - 1));
m_outputData.data.digital_pe |= 1 << (channel - 1);
}
else if (config == kInputPullDown)
{
m_outputData.data.digital &= ~(1 << (channel - 1));
m_outputData.data.digital_oe &= ~(1 << (channel - 1));
m_outputData.data.digital_pe |= 1 << (channel - 1);
}
else if (config == kOutput)
{
m_outputData.data.digital_oe |= 1 << (channel - 1);
m_outputData.data.digital_pe &= ~(1 << (channel - 1));
}
else
{
// Something went wrong.
}
}
/**
* Get the period of a PWM generator.
*
* This has the side effect of forcing the Driver Station to switch to Enhanced mode if it's not when called.
* If Enhanced mode is not enabled when this is called, it will return 0.
*
* @param channels Select the generator by specifying the two channels to which it is connected.
* @return The period of the PWM generator in seconds.
*/
double DriverStationEnhancedIO::GetPWMPeriod(tPWMPeriodChannels channels)
{
if (channels < kPWMChannels1and2 || channels > kPWMChannels3and4)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channels must be kPWMChannels1and2 or kPWMChannels3and4");
return 0.0;
}
if (!m_outputValid)
{
m_requestEnhancedEnable = true;
wpi_setWPIError(EnhancedIOMissing);
return 0.0;
}
Synchronized sync(m_outputDataSemaphore);
return m_outputData.data.pwm_period[channels] / 24000000.0;
}
/**
* Set the period of a PWM generator.
*
* There are 2 PWM generators on the IO board. One can generate PWM signals on DIO1 and DIO2,
* the other on DIO3 and DIO4. Each generator has one counter and two compare registers. As such,
* each pair of PWM outputs share the output period but have independent duty cycles.
*
* @param channels Select the generator by specifying the two channels to which it is connected.
* @param period The period of the PWM generator in seconds. [0.0,0.002731]
*/
void DriverStationEnhancedIO::SetPWMPeriod(tPWMPeriodChannels channels, double period)
{
if (channels < kPWMChannels1and2 || channels > kPWMChannels3and4)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channels must be kPWMChannels1and2 or kPWMChannels3and4");
return;
}
// Convert to ticks based on the IO board's 24MHz clock
double ticks = period * 24000000.0;
// Limit the range of the ticks... warn if too big.
if (ticks > 65534.0)
{
wpi_setWPIError(EnhancedIOPWMPeriodOutOfRange);
ticks = 65534.0;
}
else if (ticks < 0.0) ticks = 0.0;
// Preserve the duty cycles.
double dutyCycles[2];
dutyCycles[0] = GetPWMOutput((channels << 1) + 1);
dutyCycles[1] = GetPWMOutput((channels << 1) + 2);
{
Synchronized sync(m_outputDataSemaphore);
// Update the period
m_outputData.data.pwm_period[channels] = (uint16_t)ticks;
m_configChanged = true;
}
// Restore the duty cycles
SetPWMOutput((channels << 1) + 1, dutyCycles[0]);
SetPWMOutput((channels << 1) + 2, dutyCycles[1]);
}
/**
* Get the state being output on a fixed digital output.
*
* @param channel The FixedDO line to get. [1,2]
* @return The state of the FixedDO line.
*/
bool DriverStationEnhancedIO::GetFixedDigitalOutput(uint32_t channel)
{
if (channel < 1 || channel > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 2");
return 0;
}
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0;
}
Synchronized sync(m_outputDataSemaphore);
return ((m_outputData.data.fixed_digital_out >> (channel-1)) & 1) != 0;
}
/**
* Set the state to output on a Fixed High Current Digital Output line.
*
* FixedDO1 is pin 5 on the top connector (P2).
* FixedDO2 is pin 3 on the top connector (P2).
*
* The FixedDO lines always output 0V and 3.3V regardless of J1 and J4.
* They can source 4mA and can sink 25mA. Because of this, they are expected to be used
* in an active low configuration, such as connecting to the cathode of a bright LED.
* Because they are expected to be active low, they default to true.
*
* @param channel The FixedDO channel to set.
* @param value The state to set the FixedDO.
*/
void DriverStationEnhancedIO::SetFixedDigitalOutput(uint32_t channel, bool value)
{
if (channel < 1 || channel > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 2");
return;
}
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return;
}
static uint8_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_FixedDigitalOut);
reported_mask |= (1 >> channel);
}
uint8_t digital;
Synchronized sync(m_outputDataSemaphore);
digital = m_outputData.data.fixed_digital_out;
digital &= ~(1 << (channel-1));
if (value) digital |= 1 << (channel-1);
m_outputData.data.fixed_digital_out = digital;
}
/**
* Get the position of a quadrature encoder.
*
* There are two signed 16-bit 4X quadrature decoders on the IO board. These decoders are always monitoring
* the state of the lines assigned to them, but these lines do not have to be used for encoders.
*
* Encoder1 uses DIO4 for "A", DIO6 for "B", and DIO8 for "Index".
* Encoder2 uses DIO5 for "A", DIO7 for "B", and DIO9 for "Index".
*
* The index functionality can be enabled or disabled using SetEncoderIndexEnable().
*
* @param encoderNumber The quadrature encoder to access. [1,2]
* @return The current position of the quadrature encoder.
*/
int16_t DriverStationEnhancedIO::GetEncoder(uint32_t encoderNumber)
{
if (encoderNumber < 1 || encoderNumber > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "encoderNumber must be between 1 and 2");
return 0;
}
if (!m_inputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0;
}
static uint8_t reported_mask = 0;
if (!(reported_mask & (1 >> encoderNumber)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, encoderNumber, nUsageReporting::kDriverStationEIO_Encoder);
reported_mask |= (1 >> encoderNumber);
}
Synchronized sync(m_inputDataSemaphore);
return m_inputData.data.quad[encoderNumber - 1] - m_encoderOffsets[encoderNumber - 1];
}
/**
* Reset the position of an encoder to 0.
*
* This simply stores an offset locally. It does not reset the hardware counter on the IO board.
* If you use this method with Index enabled, you may get unexpected results.
*
* @param encoderNumber The quadrature encoder to reset. [1,2]
*/
void DriverStationEnhancedIO::ResetEncoder(uint32_t encoderNumber)
{
if (encoderNumber < 1 || encoderNumber > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "encoderNumber must be between 1 and 2");
return;
}
if (!m_inputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return;
}
Synchronized sync(m_inputDataSemaphore);
m_encoderOffsets[encoderNumber - 1] = m_inputData.data.quad[encoderNumber - 1];
}
/**
* Get the current configuration of a quadrature encoder index channel.
*
* This has the side effect of forcing the Driver Station to switch to Enhanced mode if it's not when called.
* If Enhanced mode is not enabled when this is called, it will return false.
*
* @param encoderNumber The quadrature encoder. [1,2]
* @return Is the index channel of the encoder enabled.
*/
bool DriverStationEnhancedIO::GetEncoderIndexEnable(uint32_t encoderNumber)
{
if (encoderNumber < 1 || encoderNumber > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "encoderNumber must be between 1 and 2");
return false;
}
if (!m_outputValid)
{
m_requestEnhancedEnable = true;
wpi_setWPIError(EnhancedIOMissing);
return false;
}
Synchronized sync(m_outputDataSemaphore);
return ((m_outputData.data.quad_index_enable >> (encoderNumber - 1)) & 1) != 0;
}
/**
* Enable or disable the index channel of a quadrature encoder.
*
* The quadrature decoders on the IO board support an active-low index input.
*
* Encoder1 uses DIO8 for "Index".
* Encoder2 uses DIO9 for "Index".
*
* When enabled, the decoder's counter will be reset to 0 when A, B, and Index are all low.
*
* @param encoderNumber The quadrature encoder. [1,2]
* @param enable If true, reset the encoder in an index condition.
*/
void DriverStationEnhancedIO::SetEncoderIndexEnable(uint32_t encoderNumber, bool enable)
{
if (encoderNumber < 1 || encoderNumber > 2)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "encoderNumber must be between 1 and 2");
return;
}
Synchronized sync(m_outputDataSemaphore);
m_outputData.data.quad_index_enable &= ~(1 << (encoderNumber - 1));
if (enable) m_outputData.data.quad_index_enable |= 1 << (encoderNumber - 1);
m_configChanged = true;
}
/**
* Get the value of the Capacitive Sense touch slider.
*
* @return Value between 0.0 (toward center of board) and 1.0 (toward edge of board). -1.0 means no touch detected.
*/
double DriverStationEnhancedIO::GetTouchSlider()
{
if (!m_inputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0.0;
}
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, 1, nUsageReporting::kDriverStationEIO_TouchSlider);
Synchronized sync(m_inputDataSemaphore);
uint8_t value = m_inputData.data.capsense_slider;
return value == 255 ? -1.0 : value / 254.0;
}
/**
* Get the percent duty-cycle that the PWM generator channel is configured to output.
*
* @param channel The DIO line's PWM generator to get the duty-cycle from. [1,4]
* @return The percent duty-cycle being output (if the DIO line is configured for PWM). [0.0,1.0]
*/
double DriverStationEnhancedIO::GetPWMOutput(uint32_t channel)
{
if (channel < 1 || channel > 4)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 4");
return 0.0;
}
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return 0.0;
}
Synchronized sync(m_outputDataSemaphore);
return (double)m_outputData.data.pwm_compare[channel - 1] / (double)m_outputData.data.pwm_period[(channel - 1) >> 1];
}
/**
* Set the percent duty-cycle to output on a PWM enabled DIO line.
*
* DIO1 through DIO4 have the ability to output a PWM signal. The period of the
* signal can be configured in pairs using SetPWMPeriod().
*
* @param channel The DIO line's PWM generator to set. [1,4]
* @param value The percent duty-cycle to output from the PWM generator. [0.0,1.0]
*/
void DriverStationEnhancedIO::SetPWMOutput(uint32_t channel, double value)
{
if (channel < 1 || channel > 4)
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "channel must be between 1 and 4");
return;
}
if (!m_outputValid)
{
wpi_setWPIError(EnhancedIOMissing);
return;
}
static uint8_t reported_mask = 0;
if (!(reported_mask & (1 >> channel)))
{
nUsageReporting::report(nUsageReporting::kResourceType_DriverStationEIO, channel, nUsageReporting::kDriverStationEIO_PWM);
reported_mask |= (1 >> channel);
}
if (value > 1.0) value = 1.0;
else if (value < 0.0) value = 0.0;
Synchronized sync(m_outputDataSemaphore);
m_outputData.data.pwm_compare[channel - 1] = (uint16_t)(value * (double)m_outputData.data.pwm_period[(channel - 1) >> 1]);
}
/**
* Get the firmware version running on the IO board.
*
* This also has the side effect of forcing the driver station to switch to Enhanced mode if it is not.
* If you plan to switch between Driver Stations with unknown IO configurations, you can call this
* until it returns a non-0 version to ensure that this API is accessible before proceeding.
*
* @return The version of the firmware running on the IO board. 0 if the board is not attached or not in Enhanced mode.
*/
uint8_t DriverStationEnhancedIO::GetFirmwareVersion()
{
if (!m_inputValid)
{
m_requestEnhancedEnable = true;
wpi_setWPIError(EnhancedIOMissing);
return 0;
}
Synchronized sync(m_inputDataSemaphore);
return m_inputData.data.fw_version;
}