-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCode.c
209 lines (188 loc) · 4.19 KB
/
Code.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//ENCODER VARIABLES
int indexx; //Interpolation index variable
long int interp; //Interpolation calc
int A; //Input for the interpolation function
int Sin,Cos; //Angle sector calculation
int X,Y; //Angle sector calculation
long int XY; //Angle sector calculation
int Sa,Ca,S,C; //Quadrature calculation, like a normal pulsed encoder
long int thetaI,Turns,Theta_Turns; //Multi turn calculation
uint16_t B,B2,B3; //Multi turn calculation
long int tets; //Multi turn calculation
float theta,theta2,theta_e,AbsTheta,AbsTheta2; //Multi turn calculation
int octant,octant2; //Angle octant
long int T,T2,temps;
//Analog acquisition inside the ADC OnEnd interruption function (not showed here) saves in Sin and Cos variables and run Enc_Process()
int ATAN_Table[33] =
{
58,331,653,976,1298,1617,1934,2247,2555,2860,3159,3453,3742,4025,4301,4572,
4836,5093,5344,5588,5826,6057,6282,6500,6712,6917,7116,7310,7497,7679,7855,8026,8192
};
void Atan_interp(void) //ArcTan interpolation
{
indexx = A/1024;//(A>>10 );
A = (A - (indexx<<10));
interp = ATAN_Table[indexx+1] - ATAN_Table[indexx];
interp = interp*A;
A = interp/1024;
A = A + ATAN_Table[indexx];
}
void Enc_Process(void)
{
if (Sin >= 0)
{
S = 1;
if (Cos >= 0) {
C = 1;
if (Sin < Cos) {
octant = 0;
X = Sin;
Y = Cos;
XY = 32767*X;
XY = XY/Y;
A = XY;
Atan_interp();
B = A + 0;
}
else {
octant = 1;
X = Cos;
Y = Sin;
XY = 32767*X;
XY = XY/Y;
A = XY;
Atan_interp();
B = 16383 - A;
}
}
else { //Sin>0 e Cos <0
C = 0;
if (Sin <= -Cos) {
octant = 3;
X = Sin;
Y = -Cos;
XY = 32767*X;
XY = XY/Y;
A = XY;
Atan_interp();
B = 32767 - A;
}
else {
octant = 2;
X = -Cos;
Y = Sin;
XY = 32767*X;
XY = XY/Y;
A = XY;
Atan_interp();
B = A + 16383;
}
}
}
else {
S = 0;
if (Cos >= 0) { //Sin<0 e Cos>0
C = 1;
if (-Sin <= Cos) {
octant = 7;
X = -Sin;
Y = Cos;
XY = 32767*X;
XY = XY/Y;
A = XY;
Atan_interp();
B = 32767 + 32767 - A;
}
else {
octant = 6;
X = Cos;
Y = -Sin;
XY = 32767*X;
XY = XY/Y;
A = XY;
Atan_interp();
B = 32767 + 16383 + A;
}
}
else { //Sin<0 e Cos<00
C = 0;
if (-Sin < -Cos) {
octant = 4;
X = -Sin;
Y = -Cos;
XY = 32767*X;
XY = XY/Y;
A = XY;
Atan_interp();
B = 32767 + A;
}
else {
octant = 5;
X = -Cos;
Y = -Sin;
XY = 32767*X;
XY = XY/Y;
A = XY;
Atan_interp();
B = 32767 + 16383 - A;
}
}
}
//A = Octant separated angle
//B = Entire period angle
//S/C = Sin/Cos signal Sa/Ca= Sin/Cos signal before
//Quadrature Counter
if (S != Sa) {
if (S != C) { thetaI--; Theta_Turns--; }
else { thetaI++; Theta_Turns++; }
}
if (C != Ca) {
if (S == C) { thetaI--; Theta_Turns--; }
else { thetaI++; Theta_Turns++; }
}
Sa = S;
Ca = C;
//thetaI = Quadrature Counter 4 counts/period, obviously -> (128(SKS36) * 4) = 512 counts/mechanical turn
if(thetaI>511) {
thetaI = 0;
Turns++;
}
else if (thetaI <0) {
thetaI = 511;
Turns--;
}
//Turns = Mechanical turns
//A = (A/512) + 64;
//thetaI = abs(thetaI)%512;
//thetaI = 0;
//A = -32760;
tets = (thetaI)/4;
B2 = ((B>>9) + tets*128 + octant*8)%16384;// /128 - B3; //B3 = Zeramento do Angulo
B3 = (B2*4)%16384;
theta2 = (float) B3;
theta_e= theta2/(2607.59458f); //Angulação elétrica de 0 a 2pi ; 1/4 de volta mecanica
theta2 = (float) B2;
theta = theta2;///(2607.59458f); //Angulação mecanica de 0 a 2pi ; 1 volta mecanica
//Calculo theta absoluto
tets = (Theta_Turns);///4;
if (tets < 0) {
tets = tets + 1;
tets = tets/4;
T = (tets*128);
theta2 = (float) (128-(B>>9))- octant*8;
AbsTheta2 = (float) T - theta2;// FALHA EM angulos negativos (B>>9) +Theta_Turns
}
else {
tets = tets/4;
T = (tets*128);
theta2 = (float) ((B>>9)+ octant*8);
AbsTheta2 = (float) T + theta2;// FALHA EM angulos negativos (B>>9) +Theta_Turns
}
//AbsTheta2 = AbsTheta2 + ((float) T2/(45.511110f));
//theta = ((float) thetaI)/4 + theta/128;
//DAC2->DHR12R1 = B2/4;//2048 + (int)id
//theta = 2.8125f*theta;
//theta = theta/360.0f;
//AbsTheta
//octant2 = octant;
}