-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.cpp
More file actions
95 lines (92 loc) · 3.19 KB
/
encoder.cpp
File metadata and controls
95 lines (92 loc) · 3.19 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
//When importing the QEI library, in QEI.h change Encoding encoding = X2_ENCODING to X4_ENCODING in the constructor
//Use these object in the main code
//TickingEncoder* wheel_left = new TickingEncoder(ENC_2_A_PIN, ENC_2_B_PIN);
//TickingEncoder* wheel_right = new TickingEncoder(ENC_1_A_PIN, ENC_1_B_PIN);
//To get speed for e.g. use wheel_right->get_speed();
#include <QEI.h>
#define WHEEL_CIRCUMFERENCE 251.33f//In mm
#define ENCODER_MEASURE_PERIOD 0.001//Encoder pulses are checked at this periodicity
#define NO_PULS_PER_REV 1024//Pulses per revolution of enc disk (256*4)
#define GAP_LENGTH 10.0f//mm
#define STOPPED_SPEED 0.0//mm/s
#define PI 3.1415926f
#define QUARTER_CIRCLE 270.0f//mm
//Pins
#define ENC_1_A_PIN PB_2
#define ENC_1_B_PIN PB_15
#define ENC_2_A_PIN PB_13
#define ENC_2_B_PIN PB_14
class TickingEncoder : public QEI
{
private:
Ticker tick_enc;
volatile int pulses;
double dx;
volatile double speed;//mm/s
volatile double ang_speed;//rad/s
volatile bool no_line_var;//Flag to indicate that there is no line
//This flag tells main to stop the motors/that buggy has turned 180 deg
volatile bool flag;
volatile bool count_q;//Quarter turn flag
void enc_isr()
{
//Called to fetch encoder data by tick_enc
pulses = getPulses();//Stores number of pulses
speed = (((double)pulses/NO_PULS_PER_REV)*(WHEEL_CIRCUMFERENCE))/ENCODER_MEASURE_PERIOD;
ang_speed = (((double)pulses/NO_PULS_PER_REV)*2*PI)/ENCODER_MEASURE_PERIOD;
//Quarter circle counter
if(count_q == true){
dx += ((double)pulses/NO_PULS_PER_REV)*(WHEEL_CIRCUMFERENCE);//In mm
if(dx >= QUARTER_CIRCLE){
flag = true;
zero_dx();
count_q = false;
}
}
//Detection distance in case of line break
else if (no_line_var == true) {
dx += ((double)pulses/NO_PULS_PER_REV)*(WHEEL_CIRCUMFERENCE);//In mm
if (dx >= GAP_LENGTH) {
flag = true;
zero_dx();
}
}
else zero_dx();//experimental
reset();//Resets the count
}
void zero_dx(){dx = 0.0;}
public:
TickingEncoder(PinName channelA, PinName channelB):QEI(channelA, channelB, NC, NO_PULS_PER_REV)
{
dx = 0.0;
count_q = false;
no_line_var = false;
flag = false;
reset();
tick_enc.attach(callback(this, &TickingEncoder::enc_isr), ENCODER_MEASURE_PERIOD);
}
//Selectors
// double get_speed(){return speed;}
double get_ang_speed(){return ang_speed;}
bool get_flag(){
bool flag_old = flag;
flag = false;
return flag_old;
}
int pos_dirr()//1 for positive dirr, 0 for stationary, and -1 for negative
{
if(ang_speed > 0.0)return 1;
else if (ang_speed == 0.0) return 0;
else return -1;
}
// double get_dx(){return dx;}
//Modifiers
void no_line(){no_line_var = true;}
void line()
{
no_line_var = false;
zero_dx();
}
void quarter_circle(){count_q = true;}
};