-
Notifications
You must be signed in to change notification settings - Fork 38
/
高分辨率赛道采集.c
519 lines (432 loc) · 13.5 KB
/
高分辨率赛道采集.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include "gpio.h"
#include "common.h"
#include "uart.h"
#include "dma.h"
#include "i2c.h"
#include "ov7725.h"
#include "oled_spi.h"
#include "ftm.h"
#include "lptmr.h"
#define offset 77
void turn(int angel){
angel += offset;
int pwm = (int)((angel/90.0 + 0.5) * 500); //90度是1.5ms
//printf("turn:%d\r\n", pwm);
FTM_PWM_ChangeDuty(HW_FTM1, HW_FTM_CH0, pwm);
}
#define DRIVER_PWM_WIDTH 1000
void setSpeed(int spd);
void initDriver(){
printf("initPWM\r\n");
//for(int i=0;i<0;i++)
GPIO_QuickInit(HW_GPIOC, 0, kGPIO_Mode_OPP);
PCout(0)=1;
//使能INH
FTM_PWM_QuickInit(FTM1_CH0_PA12, kPWM_EdgeAligned, 50); //设置FTM,边沿对齐模式
turn(0);
//初始化舵机
FTM_PWM_QuickInit(FTM0_CH0_PC01, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH1_PC02, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH2_PC03, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH3_PC04, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
//初始化电机PWM输出
setSpeed(0);
}
void setLeftSpeed(int spd){
if(spd>0){
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH1, spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH0, 0);
}else
{
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH1, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH0, -spd);
}
}
void setRightSpeed(int spd){
if(spd>0){
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH2, spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH3, 0);
}else
{
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH2, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH3, -spd);
}
}
void setSpeed(int spd){
if(spd>0){
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH1, spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH0, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH2, spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH3, 0);
}else
{
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH1, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH0, -spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH2, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH3, -spd);
}
}
/* 请将I2C.H中的 I2C_GPIO_SIM 改为 1 */
// 改变图像大小
//0: 80x60
//1: 160x120
//2: 240x180
#define IMAGE_SIZE 1
#if (IMAGE_SIZE == 0)
#define OV7620_W (80)
#define OV7620_H (60)
#elif (IMAGE_SIZE == 1)
#define OV7620_W (160)
#define OV7620_H (120)
#elif (IMAGE_SIZE == 2)
#define OV7620_W (240)
#define OV7620_H (180)
#else
#error "Image Size Not Support!"
#endif
#define WIDTH OV7620_W
#define HEIGHT OV7620_H-1
// 图像内存池
uint8_t gCCD_RAM[(OV7620_H)*((OV7620_W/8)+1)]; //使用内部RAM
/* 行指针 */
uint8_t * gpHREF[OV7620_H+1];
/* 引脚定义 PCLK VSYNC HREF 接到同一个PORT上 */
#define BOARD_OV7620_PCLK_PORT HW_GPIOE
#define BOARD_OV7620_PCLK_PIN (8)
#define BOARD_OV7620_VSYNC_PORT HW_GPIOE
#define BOARD_OV7620_VSYNC_PIN (10)
#define BOARD_OV7620_HREF_PORT HW_GPIOE
#define BOARD_OV7620_HREF_PIN (9)
/*
摄像头数据引脚PTA8-PTA15 只能填入 0 8 16三个值
0 :PTA0-PTA7
8 :PTA8-PTA15
16:PTA16-PTA24
*/
#define BOARD_OV7620_DATA_OFFSET (0)
static void UserApp(uint32_t vcount);
//定义一帧结束后的用户函数
/* 状态机定义 */
typedef enum
{
TRANSFER_IN_PROCESS, //数据在处理
NEXT_FRAME, //下一帧数据
}OV7620_Status;
int SCCB_Init(uint32_t I2C_MAP)
{
int r;
uint32_t instance;
instance = I2C_QuickInit(I2C_MAP, 50*1000);
r = ov7725_probe(instance);
if(r)
{
return 1;
}
r = ov7725_set_image_size((ov7725_size)IMAGE_SIZE);
if(r)
{
printf("OV7725 set image error\r\n");
return 1;
}
return 0;
}
//行中断和场中断都使用PTE中断
void OV_ISR(uint32_t index)
{
static uint8_t status = TRANSFER_IN_PROCESS;
static uint32_t h_counter, v_counter;
// uint32_t i;
/* 行中断 */
if(index & (1 << BOARD_OV7620_HREF_PIN))
{
DMA_SetDestAddress(HW_DMA_CH2, (uint32_t)gpHREF[h_counter++]);
//i = DMA_GetMajorLoopCount(HW_DMA_CH2);
DMA_SetMajorLoopCounter(HW_DMA_CH2, (OV7620_W/8)+1);
DMA_EnableRequest(HW_DMA_CH2);
return;
}
/* 场中断 */
if(index & (1 << BOARD_OV7620_VSYNC_PIN))
{
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, false);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, false);
switch(status)
{
case TRANSFER_IN_PROCESS: //接受到一帧数据调用用户处理
UserApp(v_counter++);
//printf("i:%d %d\r\n", h_counter, i);
status = NEXT_FRAME;
h_counter = 0;
break;
case NEXT_FRAME: //等待下次传输
status = TRANSFER_IN_PROCESS;
break;
default:
break;
}
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, true);
PORTE->ISFR = 0xFFFFFFFF; //这里可以改PORTE
h_counter = 0;
return;
}
}
void initCamera(){
uint32_t i;
printf("OV7725 test\r\n");
//检测摄像头
if(SCCB_Init(I2C0_SCL_PB00_SDA_PB01))
{
printf("no ov7725device found!\r\n");
while(1);
}
printf("OV7620 setup complete\r\n");
//每行数据指针
for(i=0; i<OV7620_H+1; i++)
{
gpHREF[i] = (uint8_t*)&gCCD_RAM[i*OV7620_W/8];
}
DMA_InitTypeDef DMA_InitStruct1 = {0};
/* 场中断 行中断 像素中断 */
GPIO_QuickInit(BOARD_OV7620_PCLK_PORT, BOARD_OV7620_PCLK_PIN, kGPIO_Mode_IPD);
GPIO_QuickInit(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_Mode_IPD);
GPIO_QuickInit(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_Mode_IPD);
/* install callback */
GPIO_CallbackInstall(BOARD_OV7620_VSYNC_PORT, OV_ISR);
GPIO_ITDMAConfig(BOARD_OV7620_HREF_PORT, BOARD_OV7620_HREF_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_VSYNC_PORT, BOARD_OV7620_VSYNC_PIN, kGPIO_IT_FallingEdge, true);
GPIO_ITDMAConfig(BOARD_OV7620_PCLK_PORT, BOARD_OV7620_PCLK_PIN, kGPIO_DMA_RisingEdge, true);
/* 初始化数据端口 */
for(i=0;i<8;i++)
{
GPIO_QuickInit(HW_GPIOE, BOARD_OV7620_DATA_OFFSET+i, kGPIO_Mode_IFT);
}
//DMA配置
DMA_InitStruct1.chl = HW_DMA_CH2;
DMA_InitStruct1.chlTriggerSource = PORTE_DMAREQ; //这里可以改PORTE
DMA_InitStruct1.triggerSourceMode = kDMA_TriggerSource_Normal;
DMA_InitStruct1.minorLoopByteCnt = 1;
DMA_InitStruct1.majorLoopCnt = ((OV7620_W/8) +1);
DMA_InitStruct1.sAddr = (uint32_t)&PTE->PDIR + BOARD_OV7620_DATA_OFFSET/8; //这里可以改PTE
DMA_InitStruct1.sLastAddrAdj = 0;
DMA_InitStruct1.sAddrOffset = 0;
DMA_InitStruct1.sDataWidth = kDMA_DataWidthBit_8;
DMA_InitStruct1.sMod = kDMA_ModuloDisable;
DMA_InitStruct1.dAddr = (uint32_t)gpHREF[0];
DMA_InitStruct1.dLastAddrAdj = 0;
DMA_InitStruct1.dAddrOffset = 1;
DMA_InitStruct1.dDataWidth = kDMA_DataWidthBit_8;
DMA_InitStruct1.dMod = kDMA_ModuloDisable;
/* initialize DMA moudle */
DMA_Init(&DMA_InitStruct1);
}
bool printflag = false;
//串口接收中断
void UART_RX_ISR(uint16_t byteRec){
//打印整个图像
if(byteRec == ' ')printflag = true;
if(byteRec == 'a'){
turn(-30);
setSpeed(2000);
}
if(byteRec == 'd'){
turn(30);
setSpeed(2000);
}
if(byteRec == 'w'){
turn(0);
setSpeed(2000);
}
if(byteRec == 's'){
turn(0);
setSpeed(-2000);
}
}
// IMG
uint8_t IMG[OV7620_W][OV7620_H]; //使用内部RAM
int white[HEIGHT];
int whiteF[HEIGHT];
bool crossflag = false;
void findType(){
int y;
int x;
int lastwhite = 0;
for(y=0;y<HEIGHT;y++){
int whitedots=0;
for(x=0;x<WIDTH;x++){
if(IMG[x][y]==0)whitedots++;
}
white[y] = whitedots;
whiteF[y] = whitedots - lastwhite;
lastwhite = whitedots;
}
//统计白点个数和进行微分
//直道,左转弯,右转弯的微分在有效区域都是0~2,通常在1左右,一旦小于0就表示有其他的干扰存在
//十字路口微分在有效区域中有一段大于0和一段小于0,其他的值也是0~2
int crossstart = 0;
int crossend = 0;
for(y=HEIGHT;y>0;y--){
if(whiteF[y]<3&&whiteF[y]>-3)continue; //正常模式
if(whiteF[y]<-5){ //十字路口开始
crossstart = y;
while(whiteF[y]<5 && y)y--;//走道十字路口尾部
while(whiteF[y]>2 && y)y--;//十字路口结束
if(y<=0)break;
crossend = y;
break;
}
}
if( (crossstart-crossend>15) && crossend && white[(crossstart+crossend)/2] > 150){
crossflag = true;
//在十字起始位置从中心向两边搜索边界
crossstart+=5;
crossend-=5;
//printf("crossstart:%d,crossend:%d\r\n", crossstart, crossend);
int left1 = WIDTH/2;
int right1 = WIDTH/2;
while(left1){
if(IMG[left1][crossstart] == 1 && (IMG[left1+1][crossstart] == 0))break;
left1--;
}
while(right1 < WIDTH-1){
if(IMG[right1][crossstart] == 0 && (IMG[right1+1][crossstart] == 1))break;
right1++;
}
IMG[left1][crossstart] = 1;
IMG[right1][crossstart] = 1;
//在十字终止位置从中心向两边搜索边界
int left2;
int right2;
float k1 = 0;
float k2 = 0;
while(crossend){
left2 = WIDTH/2;
right2 = WIDTH/2;
while(left2){
if(IMG[left2][crossend] == 1 && (IMG[left2+1][crossend] == 0))break;
left2--;
}
while(right2 < WIDTH-1){
if(IMG[right2][crossend] == 0 && (IMG[right2+1][crossend] == 1))break;
right2++;
}
IMG[left2][crossend] = 2;
IMG[right2][crossend] = 2;
k1 = (float)(left2-left1)/(crossend-crossstart);
k2 = (float)(right2-right1)/(crossend-crossstart);
if(k1<0 && k2>0)break;
crossend--;
}
if(crossend){
//两边补线
for(int i=0;i<(crossstart-crossend) && crossend+i < HEIGHT-1;i++){
IMG[(int)(left2+k1*i)][crossend+i] = 2;
IMG[(int)(right2+k2*i)][crossend+i] = 2;
}
}
}else crossflag = false;
}
void findCenter();
#define DELTA_MAX 3
int average;
/* 接收完成一场后 用户处理函数 */
static void UserApp(uint32_t vcount)
{
for(int y=0;y<OV7620_H-1;y++)
for(int x=0;x<OV7620_W/8;x++)
for(int i=0; i<8; i++)
IMG[x*8+i][y] = (gpHREF[y][x+1]>>(7-i))%2;
//将图片从OV7620_H*OV7620_W/8映射到OV7620_H*OV7620_W
findType();
if(printflag){
printflag = false;
//打印出图像
//printf("start\r\n");
for(int y=0;y<OV7620_H-1;y++){
for(int x=0;x<OV7620_W;x++){
printf("%d", IMG[x][y]);
}
printf("\r\n");
}
}
findCenter();
//打印到屏幕上
for(int y=0;y<8;y++){
LED_WrCmd(0xb0 + y); //0xb0+0~7表示页0~7
LED_WrCmd(0x00); //0x00+0~16表示将128列分成16组其地址在某组中的第几列
LED_WrCmd(0x10); //0x10+0~16表示将128列分成16组其地址所在第几组
for(int x=0;x<80;x++){
uint8_t data = 0;
for(int i=0;i<8 && (y*8+i)*2<HEIGHT ;i++){
data += (IMG[x*2][(y*8+i)*2] > 0 | IMG[x*2+1][(y*8+i)*2] > 0)<<(i);
}
LED_WrDat(data);
}
}
if(crossflag)LED_P8x16Str(80, 0, "cross ");
else LED_P8x16Str(80, 0, "normal");
}
void findCenter(){
int y=HEIGHT;
int center = WIDTH/2; //赛道中心
int s = 0; //中心累积求和
int sum = 0; //中心数
int left;
int right;
int err = 0;
while(y){
left = center-1;
right = center+1;
while(left){
if(IMG[left][y])break;
left--;
}
while(right < WIDTH){
if(IMG[right][y])break;
right++;
}
center = (left+right)/2;
if(right-left<10){
err++;
break;
}
if(err>3)break;
s += center;
sum ++;
IMG[center][y] = 1;
y--;
}
if(sum>10){
average = s/sum;
}else{
//setSpeed(0);
//turn(0);
//DelayMs(100);
}
char buf[20]={0};
sprintf(buf, "a=%d ", average-80);
LED_P8x16Str(80, 1, buf);
sprintf(buf, "h=%d ", sum);
LED_P8x16Str(80, 2, buf);
}
int main(void)
{
DelayInit();
/* 打印串口及小灯 */
GPIO_QuickInit(HW_GPIOD, 10, kGPIO_Mode_OPP);
UART_QuickInit(UART0_RX_PB16_TX_PB17, 115200);
/* 注册中断回调函数 */
UART_CallbackRxInstall(HW_UART0, UART_RX_ISR);
/* 开启UART Rx中断 */
UART_ITDMAConfig(HW_UART0, kUART_IT_Rx, true);
initOLED();
initDriver();
initCamera();
while(1)
{
PDout(10) = !PDout(10);
DelayMs(500);
setSpeed(0);
turn(0);
}
}