Skip to content

Commit 407b907

Browse files
Encoder: remove operator [] to access encoder channel
1 parent dc3631d commit 407b907

File tree

3 files changed

+96
-27
lines changed

3 files changed

+96
-27
lines changed

Diff for: examples/Encoders/Encoders.ino

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ void setup() {
88
void loop() {
99
// put your main code here, to run repeatedly:
1010
Serial.print("Encoder 0 State: ");
11-
Serial.println(MachineControl_Encoders[0].getCurrentState(),BIN);
11+
Serial.println(MachineControl_Encoders.getCurrentState(0),BIN);
1212
Serial.print("Encoder 0 Pulses: ");
13-
Serial.println(MachineControl_Encoders[0].getPulses());
13+
Serial.println(MachineControl_Encoders.getPulses(0));
1414
Serial.print("Encoder 0 Revolutions: ");
15-
Serial.println(MachineControl_Encoders[0].getRevolutions());
15+
Serial.println(MachineControl_Encoders.getRevolutions(0));
1616
Serial.println();
1717

1818
Serial.print("Encoder 1 State: ");
19-
Serial.println(MachineControl_Encoders[1].getCurrentState(),BIN);
19+
Serial.println(MachineControl_Encoders.getCurrentState(1),BIN);
2020
Serial.print("Encoder 1 Pulses: ");
21-
Serial.println(MachineControl_Encoders[1].getPulses());
21+
Serial.println(MachineControl_Encoders.getPulses(1));
2222
Serial.print("Encoder 1 Revolutions: ");
23-
Serial.println(MachineControl_Encoders[1].getRevolutions());
23+
Serial.println(MachineControl_Encoders.getRevolutions(1));
2424
Serial.println();
2525
delay(25);
2626
}

Diff for: src/EncoderClass.cpp

+41-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file EncoderClass.cpp
33
* @author Leonardo Cavagnis
4-
* @brief Source file for the EncoderClass of the Portenta Machine Control.
4+
* @brief Source file for the encoder module of the Portenta Machine Control.
55
*/
66

77
/* Includes -----------------------------------------------------------------*/
@@ -17,15 +17,49 @@ EncoderClass::EncoderClass(PinName enc0_A_pin, PinName enc0_B_pin, PinName enc0_
1717
EncoderClass::~EncoderClass()
1818
{ }
1919

20-
QEI& EncoderClass::operator[](int index) {
21-
switch (index) {
20+
void EncoderClass::reset(int channel) {
21+
switch (channel) {
2222
case 0:
23-
return _enc0;
23+
_enc0.reset();
24+
break;
2425
case 1:
25-
return _enc1;
26+
_enc1.reset();
27+
break;
2628
default:
27-
// Return encoder 0 by default if an invalid index is provided
28-
return _enc0;
29+
return;
30+
}
31+
}
32+
33+
int EncoderClass::getCurrentState(int channel) {
34+
switch (channel) {
35+
case 0:
36+
return _enc0.getCurrentState();
37+
case 1:
38+
return _enc1.getCurrentState();
39+
default:
40+
return -1;
41+
}
42+
}
43+
44+
int EncoderClass::getPulses(int channel) {
45+
switch (channel) {
46+
case 0:
47+
return _enc0.getPulses();
48+
case 1:
49+
return _enc1.getPulses();
50+
default:
51+
return -1;
52+
}
53+
}
54+
55+
int EncoderClass::getRevolutions(int channel) {
56+
switch (channel) {
57+
case 0:
58+
return _enc0.getRevolutions();
59+
case 1:
60+
return _enc1.getRevolutions();
61+
default:
62+
return -1;
2963
}
3064
}
3165

Diff for: src/EncoderClass.h

+49-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file EncoderClass.h
33
* @author Leonardo Cavagnis
4-
* @brief Header file for the EncoderClass of the Portenta Machine Control.
4+
* @brief Header file for the encoder module of the Portenta Machine Control.
55
*
66
* This library provides a class to manage the Quadrature Encoder Interface devices
77
* of the Portenta Machine Control. It allows interfacing with two encoders through
@@ -22,6 +22,10 @@
2222
/**
2323
* @class EncoderClass
2424
* @brief Class for managing Quadrature Encoder Interface devices of the Portenta Machine Control.
25+
*
26+
* This class provides methods to interact with two quadrature encoders. Each encoder
27+
* has two channels (A and B) for quadrature signals and an index channel. The class
28+
* allows reading the current state, pulses, and revolutions of each encoder.
2529
*/
2630
class EncoderClass {
2731
public:
@@ -31,12 +35,12 @@ class EncoderClass {
3135
* This constructor initializes the two QEI objects for encoder 0 and encoder 1
3236
* with the specified pin assignments.
3337
*
34-
* @param enc0_A_pin Pin assignment for encoder 0 channel A (default: PA_0).
35-
* @param enc0_B_pin Pin assignment for encoder 0 channel B (default: PB_0).
36-
* @param enc0_I_pin Pin assignment for encoder 0 Index channel (default: PC_0).
37-
* @param enc1_A_pin Pin assignment for encoder 1 channel A (default: PD_0).
38-
* @param enc1_B_pin Pin assignment for encoder 1 channel B (default: PE_0).
39-
* @param enc1_I_pin Pin assignment for encoder 1 Index channel (default: PF_0).
38+
* @param enc0_A_pin Pin assignment for encoder 0 channel A (default: PJ_8).
39+
* @param enc0_B_pin Pin assignment for encoder 0 channel B (default: PH_12).
40+
* @param enc0_I_pin Pin assignment for encoder 0 Index channel (default: PH_11).
41+
* @param enc1_A_pin Pin assignment for encoder 1 channel A (default: PC_13).
42+
* @param enc1_B_pin Pin assignment for encoder 1 channel B (default: PI_7).
43+
* @param enc1_I_pin Pin assignment for encoder 1 Index channel (default: PJ_10).
4044
*/
4145
EncoderClass(PinName enc0_A_pin = PJ_8, PinName enc0_B_pin = PH_12, PinName enc0_I_pin = PH_11,
4246
PinName enc1_A_pin = PC_13, PinName enc1_B_pin = PI_7, PinName enc1_I_pin = PJ_10);
@@ -49,14 +53,45 @@ class EncoderClass {
4953
~EncoderClass();
5054

5155
/**
52-
* @brief Get the QEI object for the specified encoder index.
53-
*
54-
* This method returns a reference to the QEI object for the specified encoder index.
55-
*
56-
* @param index The index for selecting the encoder (0 or 1).
57-
* @return A reference to the corresponding QEI object.
56+
* @brief Reset the encoder counter for the specified channel.
57+
*
58+
* @param channel The encoder channel (0 or 1) to reset.
59+
*/
60+
void reset(int channel);
61+
62+
/**
63+
* @brief Get the current state of the specified encoder channel.
64+
*
65+
* The current state is the value of the encoder counter.
66+
*
67+
* @param channel The encoder channel (0 or 1) to read the state from.
68+
* @return The current state of the encoder channel as a 2-bit number, where:
69+
* bit 0 = The reading from channel B
70+
* bit 1 = The reading from channel A
71+
*/
72+
int getCurrentState(int channel);
73+
74+
/**
75+
* @brief Get the number of pulses counted by the specified encoder channel.
76+
*
77+
* This method returns the number of pulses counted by the encoder. Each pulse
78+
* corresponds to a change in the encoder's quadrature signal.
79+
*
80+
* @param channel The encoder channel (0 or 1) to read the pulses from.
81+
* @return The number of pulses counted by the encoder channel.
82+
*/
83+
int getPulses(int channel);
84+
85+
/**
86+
* @brief Get the number of revolutions counted by the specified encoder channel.
87+
*
88+
* This method returns the number of full revolutions counted by the encoder.
89+
* It utilizes the index channel to track revolutions.
90+
*
91+
* @param channel The encoder channel (0 or 1) to read the revolutions from.
92+
* @return The number of revolutions counted by the encoder channel.
5893
*/
59-
QEI& operator[](int index);
94+
int getRevolutions(int channel);
6095

6196
private:
6297
QEI _enc0; // QEI object for encoder 0

0 commit comments

Comments
 (0)