-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoder.cpp
78 lines (61 loc) · 1.89 KB
/
Encoder.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Encoder to Speed
*
* Reading encoder on the wheel and converting to robot speed.
*
* Vinicius Goecks
* Dec 15, 2016
*/
#include "Arduino.h"
#include "Encoder.h"
#include <Filters.h>
Encoder::Encoder(int pin, float WheelDiameter, int MaxMotorRotation)
{
_pin = pin;
_WheelDiameter = WheelDiameter;
_MaxMotorRotation = MaxMotorRotation;
}
double Encoder::CalculateVel()
{
// Create one pole RC filter
FilterOnePole Filters::lowpassFilter( LOWPASS, filterFrequency );
while (true) {
// Start counting time
startTime = millis();
// Reset number of rotations
numberRotations = 0;
for(int i = 0; i < 3; i++){
// Read sensor
oldReading = newReading - noise; // Correct for noise
newReading = analogRead(encoderPin);
firstTime[i] = micros();
// Check if completed one more rotation
if (newReading < oldReading) {
numberRotations += 1;
// Serial.println(numberRotations);
}
// Our first measurement is always numberRotations = 0
if (i == 0) {
numberRotations = 0;
}
firstPos[i] = (newReading + 1024*numberRotations); // in counts
delay(1); // delay 1 milliseconds between each reading
}
// Calculate velocity and filter it
v = Encoder::processVelocity(firstPos,firstTime);
v = lowpassFilter.input(v);
Serial.println(v);
// Stop counting time
finalTime = millis();
runTime = finalTime - startTime;
// Make sure samplingInterval will happen
delay(samplingInterval - runTime); // ms
return v;
}
double Encoder::processVelocity(long firstPos[3], long firstTime[3])
{
h = (firstTime[2] - firstTime[0])/2.0;
omega = ( (1.5)*firstPos[2] + (-2.0)*firstPos[1] + (0.5)*firstPos[0] )/h; // in counts/us
omega = omega*2*pi/1024*1000000; // in radians/s
v = (r*omega); // velocity in m/s
return v;
}