-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.h
278 lines (230 loc) · 4.99 KB
/
scan.h
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef SCAN_H
#define SCAN_H
#include "global.h"
#include "motor.h"
#include "turn.h"
#include "tilt.h"
#include "color.h"
#include "fakedata.h"
uint8_t rgb[3];
int scanRed[NFACE * 8];
int scanGreen[NFACE * 8];
int scanBlue[NFACE * 8];
void ScanAway()
{
moveAbs(M_SCAN, 100, -340);
}
void ScanCal()
{
//根据实际测试,比 mindcuber的值略大
endstop(M_SCAN, 70);
moveRel(M_SCAN, 100, -100);
endstop(M_SCAN, 50, 500);
reset(M_SCAN);
ScanAway();
}
void calibrateRGB()
{
for (int i = 0; i < 3; i++)
{
if (rgb[i] > white_rgb[i])
{
rgb[i] = white_rgb[i];
}
rgb[i] = map(rgb[i], 0, white_rgb[i], 0, 255);
}
}
void ScanRGB(int face, int piece, uint8_t *rgb)
{
uint8_t c = 0;
#ifdef DEBUG
unsigned long start = millis();
Serial.println();
Serial.print(F(" scan pos before:"));
Serial.print(getPosition(M_SCAN));
Serial.print(F(" _pidOutput :"));
Serial.print(motors[M_SCAN]._pidOutput);
Serial.print(F(" turn pos:"));
Serial.print(getPosition(M_TURN));
Serial.println();
#endif
// colorSensor.getRGB(&rgb[0], &rgb[1], &rgb[2], &c);
// delay((256 - TCS34725_INTEGRATIONTIME_24MS) * 12 / 5 + 1);
float red, green, blue;
colorSensor.getRGB(&red, &green, &blue);
rgb[0] = uint8_t(red);
rgb[1] = uint8_t(green);
rgb[2] = uint8_t(blue);
calibrateRGB();
#ifdef DEBUG
Serial.print(F(" after :"));
Serial.print(getPosition(M_SCAN));
Serial.print(F(" _pidOutput :"));
Serial.print(motors[M_SCAN]._pidOutput);
Serial.print(F(" positions:"));
Serial.print(positions[M_SCAN]);
Serial.print(F(" turn pos:"));
Serial.print(getPosition(M_TURN));
Serial.print(F(" time:"));
Serial.print(millis() - start);
Serial.println();
#endif
cubeColors.setRGB(face, piece, rgb);
cubeColors.getClr(face, piece);
}
void ScanPiece(int face, int piece)
{
#ifdef DEBUG
Serial.print(F("ScanPiece; face:"));
Serial.print(face);
Serial.print(F(" piece:"));
Serial.print(piece);
#endif
// delay(101);
int32_t pos = getPosition(M_TURN);
if (scanOK)
{
int index = face * 9 + piece;
ScanRGB(face, piece, rgb);
uint8_t r1 = rgb[0];
uint8_t g1 = rgb[1];
uint8_t b1 = rgb[2];
}
int32_t offset = pos - getPosition(M_TURN);
#ifdef DEBUG
cubeColors.print(face);
Serial.println();
#endif
}
const int scanDelay = 300;
void ScanMiddle(int face)
{
#ifdef DEBUG
Serial.print(F("ScanMiddle; face:"));
Serial.println(face);
Serial.println();
start = millis();
#endif
moveAbs(M_SCAN, 100, T_SCNT, false);
waitForArrival(M_SCAN);
delay(100);
ScanPiece(face, 8);
}
void ScanCorner(int face, int piece)
{
start = millis();
#ifdef DEBUG
Serial.print(F("ScanCorner; face:"));
Serial.print(face);
Serial.print(F(" piece:"));
Serial.print(piece);
Serial.println();
#endif
if (scanOK)
{
start = millis();
moveAbs(M_SCAN, 100, T_SCNR, false);
Spin45(false);
#ifdef DEBUG
Serial.print(F("ScanCorner; wait scan"));
Serial.println();
#endif
waitForArrival(M_SCAN);
waitForArrival(M_TURN);
delay(100);
ScanPiece(face, piece);
}
else
{
Spin45();
}
}
void ScanEdge(int face, int piece)
{
#ifdef DEBUG
start = millis();
Serial.print(F("ScanEdge; face:"));
Serial.print(face);
Serial.print(F(" piece:"));
Serial.println(piece);
Serial.println();
#endif
if (scanOK)
{
start = millis();
moveAbs(M_SCAN, 100, T_SEDG, false);
Spin45(false);
#ifdef DEBUG
Serial.print(F("ScanEdge; wait scan"));
Serial.println();
#endif
waitForArrival(M_SCAN);
waitForArrival(M_TURN);
delay(100);
ScanPiece(face, piece);
}
else
{
Spin45();
}
}
void ScanFace(int face, int offset)
{
#ifdef DEBUG
Serial.print(F("ScanFace; face:"));
Serial.print(face);
Serial.print(F(" offset:"));
Serial.println(offset);
Serial.println();
#endif
TiltAway();
do
{
scanOK = true;
ScanMiddle(face);
brake(M_TILT);
start = millis();
for (int i = 0; i < 4; i++)
{
int p = offset;
ScanCorner(face, p);
p++;
ScanEdge(face, p);
offset = (offset + 2) & 7;
}
} while (!scanOK);
#ifdef DEBUG
Serial.println();
Serial.println();
#endif
}
// NXT
bool ScanCube()
{
ScanFace(3, 2);
cubeColors.print(3);
ScanAway();
Tilt(1);
ScanFace(2, 2);
cubeColors.print(2);
ScanAway();
Tilt(1);
ScanFace(1, 2);
cubeColors.print(1);
ScanAway();
Tilt(1);
ScanFace(0, 2);
cubeColors.print(0);
ScanAway();
Spin(1, 0);
delay(50);
Tilt(1);
ScanFace(4, 6);
cubeColors.print(4);
ScanAway();
Tilt(2);
ScanFace(5, 2);
cubeColors.print(5);
ScanAway();
}
#endif