1
1
#include " robot.h"
2
- #include " quadrature .pio.h"
2
+ #include " encoder .pio.h"
3
3
#include " wpilibws.h"
4
4
5
5
#include < map>
@@ -23,7 +23,7 @@ Servo servo2;
23
23
// Encoder PIO
24
24
PIO _encoderPio = nullptr ;
25
25
uint _encoderPgmOffset = 0 ;
26
- PIOProgram _encoderPgm (&quadrature_program );
26
+ PIOProgram _encoderPgm (&encoder_program );
27
27
28
28
// Encoders
29
29
std::vector<std::pair<int , int > > _encoderPins = {
@@ -41,7 +41,6 @@ std::map<int, int> _encoderWPILibChannelToNativeMap;
41
41
42
42
// Internal helper functions
43
43
bool _initEncoders () {
44
- // TODO Use the PIOProgram wrapper here
45
44
for (int i = 0 ; i < 4 ; i++) {
46
45
int _pgmOffset = -1 ;
47
46
int _smIdx = -1 ;
@@ -58,16 +57,23 @@ bool _initEncoders() {
58
57
59
58
// Init the program
60
59
auto pins = _encoderPins.at (i);
61
- quadrature_program_init (_pio, _smIdx, _pgmOffset, pins.first , pins. second );
60
+ encoder_program_init (_pio, _smIdx, _pgmOffset, pins.first );
62
61
}
63
62
64
63
return true ;
65
- // uint offset0 = pio_add_program(_encoderPio, &quadrature_program);
66
-
67
- // quadrature_program_init(_encoderPio, ENC_SM_IDX_MOTOR_L, offset0, 4, 5);
68
- // quadrature_program_init(_encoderPio, ENC_SM_IDX_MOTOR_R, offset0, 12, 13);
69
- // quadrature_program_init(_encoderPio, ENC_SM_IDX_MOTOR_3, offset0, 0, 1);
70
- // quadrature_program_init(_encoderPio, ENC_SM_IDX_MOTOR_4, offset0, 8, 9);
64
+ }
65
+
66
+ int _readEncoderInternal (PIO _pio, uint _smIdx) {
67
+ int count;
68
+
69
+ // Read 5 times to get past buffer
70
+ count = pio_sm_get_blocking (_pio, _smIdx);
71
+ count = pio_sm_get_blocking (_pio, _smIdx);
72
+ count = pio_sm_get_blocking (_pio, _smIdx);
73
+ count = pio_sm_get_blocking (_pio, _smIdx);
74
+ count = pio_sm_get_blocking (_pio, _smIdx);
75
+
76
+ return count;
71
77
}
72
78
73
79
bool _readEncodersInternal () {
@@ -78,9 +84,8 @@ bool _readEncodersInternal() {
78
84
uint _smIdx = _encoderStateMachineIdx[i];
79
85
80
86
if (_pio != nullptr ) {
81
- pio_sm_exec_wait_blocking (_pio, _smIdx, pio_encode_in (pio_x, 32 ));
82
- _encoderValues[i] = pio_sm_get_blocking (_pio, _smIdx);
83
-
87
+ _encoderValues[i] = _readEncoderInternal (_pio, _smIdx);
88
+
84
89
if (_encoderValues[i] != _encoderValuesLast[i]) {
85
90
hasChange = true ;
86
91
}
0 commit comments