-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_setup.cpp
More file actions
62 lines (52 loc) · 1.05 KB
/
encoder_setup.cpp
File metadata and controls
62 lines (52 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "encoder_setup.h"
QuadEncoder::QuadEncoder(int clk_pin, int dir_pin, float ppr)
{
clkPin = clk_pin;
dirPin = dir_pin;
pulsePerRev = ppr;
pinMode(clkPin, INPUT_PULLUP);
pinMode(dirPin, INPUT_PULLUP);
oldFreqTime = micros();
checkFreqTime = micros();
}
void QuadEncoder::setPulsePerRev(float ppr)
{
pulsePerRev = ppr;
}
float QuadEncoder::getAngPos()
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
return (2.00 * PI * (float)tickCount) / pulsePerRev;
}
}
float QuadEncoder::getAbsAngPosDeg()
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
return absAngDeg((2.00 * PI * (float)tickCount) / pulsePerRev);
}
}
float QuadEncoder::getAngVel()
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
return 2.00 * PI * frequency;
}
}
void QuadEncoder::setStopFreqInUs(unsigned long freq)
{
freqSampleTime = freq;
}
void QuadEncoder::resetFrequency()
{
if (micros() - checkFreqTime >= freqSampleTime)
{
frequency = 0;
}
}
float QuadEncoder::absAngDeg(float incAngRad)
{
float incAngDeg = incAngRad * 180.0 / PI;
return (int)incAngDeg % 360;
}