EEEN21000
GROUP 30 Line Following Buggy
Loading...
Searching...
No Matches
encoder.h
Go to the documentation of this file.
1#ifndef ENCODER_H
2#define ENCODER_H
3
4#include "QEI.h"
5#include "mbed.h"
6#include <chrono>
7
9#define GEAR_RATIO 18.75
11#define DIAMETER 0.08
13#define WHEEL_DISTANCE 0.212
15#define COUNTS_PER_REV 256
17#define POLLING_PERIOD 10ms
18
19#ifndef M_PI
21#define M_PI 3.14159265358979323846264338327950288
22#endif
23
28class Encoder : public QEI {
29 private:
31 volatile int m_start_pulse;
32
34 volatile int m_end_pulse;
35
37 volatile int m_pulse_width;
38
40 volatile int m_pulse_counter;
41
43 volatile double m_pulse_per_sec;
44
46 volatile double m_velocity;
47
49 Timer m_t;
50
52 Timeout m_to;
53
54 public:
62 Encoder(PinName A, PinName B);
63
67 static constexpr double CIRCUMFERENCE = DIAMETER * M_PI;
68
72 static constexpr double FULL_ROTATION_SIDE_PIVOT = 2.0 * M_PI * WHEEL_DISTANCE;
73
77 static constexpr double FULL_ROTATION_CENTER_PIVOT = M_PI * WHEEL_DISTANCE;
78
83 void start();
84
90 int getCounter() const;
91
96
102 double getVelocity() const;
103
112 double getDistance() const;
113
119 double getPPS() const;
120
130 static double getAverageAngular(const Encoder &right_encoder, const Encoder &left_encoder);
131
141 static double getAverageVelocity(const Encoder &right_encoder, const Encoder &left_encoder);
142
152 static double getAverageDistance(const Encoder &right_encoder, const Encoder &left_encoder);
153
154 private:
163 void m_calculateSpeed(const double &time_elapsed);
164
168 void m_stop();
169};
170
171#endif
Encoder class with helper functions, derived from QEI.
Definition: encoder.h:28
void m_calculateSpeed(const double &time_elapsed)
Calculate the velocity of the wheel.
static double getAverageVelocity(const Encoder &right_encoder, const Encoder &left_encoder)
Get average of linear velocity between two wheels.
static double getAverageAngular(const Encoder &right_encoder, const Encoder &left_encoder)
Get average of angular velocity between two wheels.
double getDistance() const
Get total distance travelled.
Timer m_t
Timer for high-precision clocking.
Definition: encoder.h:49
double getPPS() const
Get current pulses per unit second (PPS).
volatile int m_pulse_counter
Counter for total pulses from start.
Definition: encoder.h:40
static constexpr double FULL_ROTATION_CENTER_PIVOT
Distance covered in full rotation when turning using both wheels.
Definition: encoder.h:77
volatile int m_pulse_width
Counter for pulse difference.
Definition: encoder.h:37
void resetCounter()
Reset the running sum of pulses used for getDistance().
static constexpr double FULL_ROTATION_SIDE_PIVOT
Distance covered in full rotation when turning only using one wheel.
Definition: encoder.h:72
volatile int m_end_pulse
Counter for latest pulse.
Definition: encoder.h:34
void m_stop()
Stop the timer and get current pulse.
static double getAverageDistance(const Encoder &right_encoder, const Encoder &left_encoder)
Get average of total distance between two wheels.
Timeout m_to
Timeout to interrupt every POLLING_PERIOD.
Definition: encoder.h:52
volatile int m_start_pulse
Counter for previous pulse.
Definition: encoder.h:31
int getCounter() const
Get current running sum of pulses.
volatile double m_pulse_per_sec
Pulse per unit second.
Definition: encoder.h:43
double getVelocity() const
Get current velocity.
void start()
Start the routine to measure pulses periodically.
volatile double m_velocity
Velocity.
Definition: encoder.h:46
Encoder(PinName A, PinName B)
Constructor.
static constexpr double CIRCUMFERENCE
Circumference of the wheel.
Definition: encoder.h:67
Handle reading pulses from encoder.
Definition: QEI.h:84
#define DIAMETER
Diameter of the wheel.
Definition: encoder.h:11
#define M_PI
Define M_PI on ArmC6.
Definition: encoder.h:21
#define WHEEL_DISTANCE
Distance between two wheels.
Definition: encoder.h:13