Skip to content

Commit cc17566

Browse files
author
Zhiquan Yeo
committed
Use new encoder implementation
1 parent 6b74a81 commit cc17566

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/robot.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "robot.h"
2-
#include "quadrature.pio.h"
2+
#include "encoder.pio.h"
33
#include "wpilibws.h"
44

55
#include <map>
@@ -23,7 +23,7 @@ Servo servo2;
2323
// Encoder PIO
2424
PIO _encoderPio = nullptr;
2525
uint _encoderPgmOffset = 0;
26-
PIOProgram _encoderPgm(&quadrature_program);
26+
PIOProgram _encoderPgm(&encoder_program);
2727

2828
// Encoders
2929
std::vector<std::pair<int, int> > _encoderPins = {
@@ -41,7 +41,6 @@ std::map<int, int> _encoderWPILibChannelToNativeMap;
4141

4242
// Internal helper functions
4343
bool _initEncoders() {
44-
// TODO Use the PIOProgram wrapper here
4544
for (int i = 0; i < 4; i++) {
4645
int _pgmOffset = -1;
4746
int _smIdx = -1;
@@ -58,16 +57,23 @@ bool _initEncoders() {
5857

5958
// Init the program
6059
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);
6261
}
6362

6463
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;
7177
}
7278

7379
bool _readEncodersInternal() {
@@ -78,9 +84,8 @@ bool _readEncodersInternal() {
7884
uint _smIdx = _encoderStateMachineIdx[i];
7985

8086
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+
8489
if (_encoderValues[i] != _encoderValuesLast[i]) {
8590
hasChange = true;
8691
}

0 commit comments

Comments
 (0)