-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencoder.cpp
87 lines (71 loc) · 1.79 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
79
80
81
82
83
84
85
86
87
#include "encoder.h"
Encoder::Encoder(PinName mosi, PinName miso, PinName sck, PinName cs) :
m_spi_(mosi, miso, sck), csl_(cs) {
csl_=1;
m_spi_.format(8, 1);
m_spi_.frequency(1000000);
wait(0.01);
set_offset(1974); //est 4/11/2017
//set_offset(148); // est 4/6/2017
//set_offset(2093); // est. 4/4/2017 for Salto1p2
//set_offset(2134); // est. 9/13/2016
//set_offset(9300); // Calibrated offset for jumper = 8086+1820. 7145,8541-same, 8086:backward, 9451:works, 9300: smooth
csl_=1;
write_spi(0x0019, 0); //SETTINGS2
write_spi(0x001D, 0);
write_spi(0x0018, 0); //SETTINGS1
write_spi(0x0018, 0);
pos_ = 0;
oticks_ = 0;
offset_ = 0;
calibPos_ = 0;
}
Encoder::~Encoder() {}
unsigned int Encoder::get_cal_state(){
return pos_;
}
void Encoder::set_offset(uint16_t zpos){
uint16_t zhold;
zhold = zpos >> 6; //8MSB of zero position
write_spi(0x0016, 0);
write_spi(zhold, 0);
zhold = zpos & 0x003F; //6 LSB of zero position
write_spi(0x0017, 0);
write_spi(zhold, 0);
}
uint16_t Encoder::write_spi(uint16_t reg, uint8_t rw){
uint16_t data;
reg |= rw << 14;
reg |= bit_parity(reg) << 15;
csl_=0;
data = m_spi_.write(reg>>8) << 8;
data |= m_spi_.write(reg);
csl_=1;
return data;
}
uint16_t Encoder::read_spi(uint16_t reg) {
uint16_t data;
write_spi(reg, 1);
data = write_spi(0x0000, 1);
return data;
}
uint16_t Encoder::bit_parity(uint16_t value) {
value ^= value>>8;
value ^= value>>4;
value ^= value>>2;
value ^= value>>1;
value &= 1;
return value;
}
void Encoder::update_state(){
pos_ = ams_read();
volatile unsigned int test;
test = pos_;
// TODO: calibrate state
}
unsigned int Encoder::ams_read() {
unsigned int enc_data;
write_spi(0x3FFE, 1);
enc_data = write_spi(0x0000, 1);
return enc_data & 0x3FFF;
}