Skip to content

Commit 02506aa

Browse files
Encoder class refactoring
1 parent e10ffbc commit 02506aa

File tree

5 files changed

+108
-43
lines changed

5 files changed

+108
-43
lines changed

examples/Encoders/Encoders.ino

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <Arduino_MachineControl.h>
22

3-
using namespace machinecontrol;
4-
53
void setup() {
64
Serial.begin(9600);
75
while (!Serial);
@@ -10,19 +8,19 @@ void setup() {
108
void loop() {
119
// put your main code here, to run repeatedly:
1210
Serial.print("Encoder 0 State: ");
13-
Serial.println(encoders[0].getCurrentState(),BIN);
11+
Serial.println(MachineControl_Encoders[0].getCurrentState(),BIN);
1412
Serial.print("Encoder 0 Pulses: ");
15-
Serial.println(encoders[0].getPulses());
13+
Serial.println(MachineControl_Encoders[0].getPulses());
1614
Serial.print("Encoder 0 Revolutions: ");
17-
Serial.println(encoders[0].getRevolutions());
15+
Serial.println(MachineControl_Encoders[0].getRevolutions());
1816
Serial.println();
1917

2018
Serial.print("Encoder 1 State: ");
21-
Serial.println(encoders[1].getCurrentState(),BIN);
19+
Serial.println(MachineControl_Encoders[1].getCurrentState(),BIN);
2220
Serial.print("Encoder 1 Pulses: ");
23-
Serial.println(encoders[1].getPulses());
21+
Serial.println(MachineControl_Encoders[1].getPulses());
2422
Serial.print("Encoder 1 Revolutions: ");
25-
Serial.println(encoders[1].getRevolutions());
23+
Serial.println(MachineControl_Encoders[1].getRevolutions());
2624
Serial.println();
2725
delay(25);
2826
}

src/Arduino_MachineControl.h

+1-34
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "TempProbesClass.h"
2121
#include "RtcControllerClass.h"
2222
#include "USBClass.h"
23+
#include "EncoderClass.h"
2324

2425
namespace machinecontrol {
2526

@@ -95,39 +96,5 @@ class COMMClass {
9596

9697
extern COMMClass comm_protocols;
9798

98-
/*
99-
TODO: writeme
100-
Use QEI library for mbed since it implements index pin
101-
*/
102-
/**
103-
* The EncoderClass is a wrapper for manipulating Quadrature Encoder Interface devices.
104-
*/
105-
class EncoderClass {
106-
public:
107-
/**
108-
* returns the encoder variable depending on the index
109-
* @param index integer for selecting the encoder (0 or 1)
110-
* @return enc_0 for index = 0, enc_1 for index = 1
111-
*/
112-
EncoderClass()
113-
: enc_0{PJ_8, PH_12, PH_11, 0}
114-
, enc_1{PC_13, PI_7, PJ_10, 0} {};
115-
116-
117-
QEI& operator[](int index) {
118-
switch (index) {
119-
case 0:
120-
return enc_0;
121-
case 1:
122-
return enc_1;
123-
}
124-
}
125-
private:
126-
QEI enc_0;
127-
QEI enc_1;
128-
};
129-
130-
extern EncoderClass encoders;
131-
13299
}
133100
#endif

src/EncoderClass.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @file EncoderClass.cpp
3+
* @author Leonardo Cavagnis
4+
* @brief Source file for the EncoderClass of the Portenta Machine Control.
5+
*/
6+
7+
/* Includes -----------------------------------------------------------------*/
8+
#include "EncoderClass.h"
9+
10+
/* Functions -----------------------------------------------------------------*/
11+
EncoderClass::EncoderClass(PinName enc0_A_pin, PinName enc0_B_pin, PinName enc0_I_pin,
12+
PinName enc1_A_pin, PinName enc1_B_pin, PinName enc1_I_pin)
13+
: enc_0(enc0_A_pin, enc0_B_pin, enc0_I_pin, 0),
14+
enc_1(enc1_A_pin, enc1_B_pin, enc1_I_pin, 0)
15+
{ }
16+
17+
EncoderClass::~EncoderClass()
18+
{ }
19+
20+
QEI& EncoderClass::operator[](int index) {
21+
switch (index) {
22+
case 0:
23+
return enc_0;
24+
case 1:
25+
return enc_1;
26+
default:
27+
// Return encoder 0 by default if an invalid index is provided
28+
return enc_0;
29+
}
30+
}
31+
32+
EncoderClass MachineControl_Encoders;
33+
/**** END OF FILE ****/

src/EncoderClass.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @file EncoderClass.h
3+
* @author Leonardo Cavagnis
4+
* @brief Header file for the EncoderClass of the Portenta Machine Control.
5+
*
6+
* This library provides a class to manage the Quadrature Encoder Interface devices
7+
* of the Portenta Machine Control. It allows interfacing with two encoders through
8+
* the QEI (Quadrature Encoder Interface) library and provides methods to access
9+
* and control each encoder individually.
10+
*/
11+
12+
#ifndef __ENCODER_CLASS_H
13+
#define __ENCODER_CLASS_H
14+
15+
/* Includes -------------------------------------------------------------------*/
16+
#include "utility/QEI/QEI.h"
17+
#include <Arduino.h>
18+
#include <mbed.h>
19+
20+
/* Class ----------------------------------------------------------------------*/
21+
22+
/**
23+
* @class EncoderClass
24+
* @brief Class for managing Quadrature Encoder Interface devices of the Portenta Machine Control.
25+
*/
26+
class EncoderClass {
27+
public:
28+
/**
29+
* @brief Construct an EncoderClass object.
30+
*
31+
* This constructor initializes the two QEI objects for encoder 0 and encoder 1
32+
* with the specified pin assignments.
33+
*
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).
40+
*/
41+
EncoderClass(PinName enc0_A_pin = PJ_8, PinName enc0_B_pin = PH_12, PinName enc0_I_pin = PH_11,
42+
PinName enc1_A_pin = PC_13, PinName enc1_B_pin = PI_7, PinName enc1_I_pin = PJ_10);
43+
44+
/**
45+
* @brief Destruct the EncoderClass object.
46+
*
47+
* This destructor releases any resources used by the EncoderClass object.
48+
*/
49+
~EncoderClass();
50+
51+
/**
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.
58+
*/
59+
QEI& operator[](int index);
60+
61+
private:
62+
QEI enc_0; // QEI object for encoder 0
63+
QEI enc_1; // QEI object for encoder 1
64+
};
65+
66+
extern EncoderClass MachineControl_Encoders;
67+
68+
#endif /* __ENCODER_CLASS_H */

src/Objects.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22

33
namespace machinecontrol {
44
COMMClass comm_protocols;
5-
EncoderClass encoders;
65
USBClass usb_controller;
76
}

0 commit comments

Comments
 (0)