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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
| /* task1: 避障 task2: 包含中断的跟踪 状态一: 20cm 跟踪, 10cm 后退 状态二: 20cm 跟踪. task3: 跟着光强的方向走 */ #define task2
//standard C library #include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> #include <stdint.h> //like int16_t; #include <stdbool.h>
//else #include "io.h" #include "system.h" #include "altera_avalon_pio_regs.h" #include <altera_avalon_performance_counter.h>
//what? #include "sys/alt_irq.h" #include "alt_types.h"
typedef signed char char_8; typedef unsigned char uchar_8; typedef signed short short_16; typedef unsigned short ushort_16; typedef signed long long_32; typedef unsigned long ulong_32; typedef long long long_64; typedef unsigned long long ulong_64;
/*PWM信号输出,控制马达*/
//first mortor #define PWM_EN PWM0_BASE #define PWM_PERIOD PWM0_BASE+1 #define PWM_DUTY1 PWM0_BASE+2 #define PWM_DUTY2 PWM0_BASE+3 #define PWM_PHASE1 PWM0_BASE+4 #define PWM_PHASE2 PWM0_BASE+5
//仿造PWM0函数写下的PWM2宏定义以及函数 #define PWM_EN1 PWM2_BASE #define PWM_PERIOD1 PWM2_BASE+1 #define PWM_DUTY11 PWM2_BASE+2 #define PWM_DUTY21 PWM2_BASE+3 #define PWM_PHASE11 PWM2_BASE+4 #define PWM_PHASE21 PWM2_BASE+5
void motor_setting(unsigned long phase1, unsigned long duty1, unsigned long phase2, unsigned long duty2, unsigned long period,unsigned long enable);
void motor_setting1(unsigned long phase1, unsigned long duty1, unsigned long phase2, unsigned long duty2, unsigned long period,unsigned long enable);
//中断 int a = 1;
volatile static char key = 0;
unsigned int nirq_isr_context; void nIRQ_Initial(void);//记得在main中initial void isr_function(void);//记得声明在前面...
void nIRQ_Initial(void) { //todo: set up interrupt function IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_BASE,3);//3 = 1 | 2,表示1和2输入时都有反应
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_BASE, 0x1);//这句话是清空中断状态
alt_ic_isr_register( 0, 1,isr_function, 0, 0 );//声明中断函数
} void isr_function(void) { a = !a; //这句话是用来测试是否进入中断的... printf("cs - %d\n",a); IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_BASE, 0x1); } //超声波操作函数,Address是传入地址,x是对应命令 unsigned int light(long Address,long x) { unsigned int i; unsigned char res_l, res_h; Uart_send_byte(Address, 0xe8); for (i = 0; i < 2000; i++) ; Uart_send_byte(Address, 0x02); for (i = 0; i < 2000; i++) ; Uart_send_byte(Address,x);//修改的函数.. res_h = Uart_receive_byte(Address); for (i = 0; i < 1000; i++); res_l = Uart_receive_byte(Address);
return (((unsigned int) res_h) << 8) + res_l; }
/* 马达驱动的参数 1,2是对右边马达,phase是初始相位,duty是占空比,都得/period enable是对于两个端口的使能 */ unsigned long phase1=0x000, phase2=0x000, phase3=0x000, phase4=0x000; unsigned long duty1=0x0000, duty2=0x1000, duty3=0x0000, duty4=0x0000; unsigned long period=0x186A,enable=0x3;
/* 封装四个方向函数 up和down在for循环十次后基本能前进后退..调整了一定参数 避障中left和right函数先后退一步然后前进(也是一个for(10)) 跟踪中的直接左右拐就行..for(5) */ void up() { phase1=0x000, phase2=0x000, phase3=0x000, phase4=0x000; duty1=0x0000, duty2=0x1000, duty3=0x0000, duty4=0x1000; period=0x186A,enable=0x3; motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); usleep(8000);
} void down() { phase1=0x000, phase2=0x000, phase3=0x000, phase4=0x000; duty1=0x1000, duty2=0x0000, duty3=0x1000, duty4=0x0000; period=0x186A,enable=0x3; motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); usleep(20000);
} #ifdef task1 void le() { down(); phase1=0x000, phase2=0x000, phase3=0x000, phase4=0x000; duty1=0x0000, duty2=0x1000, duty3=0x1000, duty4=0x0000; period=0x186A,enable=0x3; motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); usleep(5000);
}
void ri() { down(); phase1=0x000, phase2=0x000, phase3=0x000, phase4=0x000; duty1=0x1000, duty2=0x0000, duty3=0x0000, duty4=0x1000; period=0x186A,enable=0x3; motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); usleep(5000); } #else void le() { phase1=0x000, phase2=0x000, phase3=0x000, phase4=0x000; duty1=0x0000, duty2=0x1000, duty3=0x1000, duty4=0x0000; period=0x186A,enable=0x3; motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); usleep(5000);
}
void ri() { phase1=0x000, phase2=0x000, phase3=0x000, phase4=0x000; duty1=0x1000, duty2=0x0000, duty3=0x0000, duty4=0x1000; period=0x186A,enable=0x3; motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); usleep(5000); } #endif
//避障中查询距离是否过近 int ck(unsigned int dis) { //if(dis > 2900000) return 0; if(dis < 200000) return 0; return 1; } //如果已经超过范围 int ck1(int dis) { if(dis > 300000 && dis < 2900000) return 1; return 0; } int main() { printf("HELLO WORLD\n");//hello world DEBUG大法好 nIRQ_Initial();//中断初始化函数 //默认马达初始化为0 motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); //PID积分参数 int tot = 0,tot1 = 0; /* 跟踪参数 第一个是最远有效距离66.6666cm 第二个是跟踪距离 第三个是后退距离 */ int meter = 666666; int meter02 = 200000; int meter03 = 100000; int p; // cycle //pid系数 double k = 0.3 ,j = 0.05,d = 0; //这里是先调了一个直线的PID,然后转化成有角度后改的一个参量 int clk11; //pace
while(1){
#ifdef task3 //光敏测距 ultrasound_init(NEW_ULTRASOUND0_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND1_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND2_BASE);//重置测位函数 int clk = light(NEW_ULTRASOUND0_BASE,0xa0) ; printf("1 => %d.%dm\t", clk ); usleep(10000); int clk1 = light(NEW_ULTRASOUND1_BASE,0xa0) ; printf("2 => %d.%dm\t", clk1 ); usleep(10000); int clk2 = light(NEW_ULTRASOUND2_BASE,0xa0) ; printf("3 => %d.%dm\n", clk2 ); usleep(10000);
#define run for(p = 0;p < 5;p++) //选取光最强的方向 if (clk1 >= clk && clk1 >= clk2) { clk11 = clk1; }//don' rotate else if(clk >= clk1 && clk >= clk2) { clk11 = clk; run le(); }//left rotate else if(clk2 >= clk && clk2 >= clk1) { clk11 = clk2; run ri(); }//right rotate //go go go if(clk11 < 200 )//光太弱停止..参数应该适当调大的.. { tot = tot1 = 0; duty1=0x0000, duty2=0x0000; duty3=0x0000, duty4=0x0000; } else//PID向前跑 { printf("up\n"); d = clk11 ; duty1=0x0000, duty2=0x1000; duty3=0x0000, duty4=0x1000; tot += d; tot1 = 0; } motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); int rtime = (int)(100 * d );//这里乘了一个d之后记得强制转换,double有毒 if(rtime <= 0) rtime = 0;//小于零有毒..这两个有可能保持后退状态 usleep(rtime); #endif #ifdef task21 //测试只能前进的跟踪 ultrasound_init(NEW_ULTRASOUND0_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND1_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND2_BASE);//重置测位函数 int clk = light(NEW_ULTRASOUND0_BASE,0x1e)*170 ; printf("1 => %d.%dm\t", clk / 1000000 , clk % 1000000); usleep(10000); int clk1 = light(NEW_ULTRASOUND1_BASE,0x1e)*170 ; printf("2 => %d.%dm\t", clk1 / 1000000 , clk1 % 1000000); usleep(10000); int clk2 = light(NEW_ULTRASOUND2_BASE,0x1e)*170 ; printf("3 => %d.%dm\n", clk2 / 1000000 , clk2 % 1000000); usleep(10000);
#define run for(p = 0;p < 5;p++)
if (clk1 <= clk && clk1 <= clk2) { clk11 = clk1; }//don' rotate else if(clk <= clk1 && clk <= clk2) { clk11 = clk; run le(); }//left rotate else if(clk2 <= clk && clk2 <= clk1) { clk11 = clk2; run ri(); }//right rotate //go go go if(clk11 > meter ) { tot = 0; duty1=0x0000, duty2=0x0000; duty3=0x0000, duty4=0x0000; } else if(clk11 > meter02) { printf("up\n"); d = clk11 - meter02; duty1=0x0000, duty2=0x1000; duty3=0x0000, duty4=0x1000; tot += d; tot1 = 0; } motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); int rtime = (int)(d * k + tot * j ); if(rtime <= 0) rtime = 0; usleep(rtime);
#endif
//get the distance #ifdef task2 //测试跟踪 if(a) { ultrasound_init(NEW_ULTRASOUND0_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND1_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND2_BASE);//重置测位函数 //用前文自己写的3m内测速函数 int clk = light(NEW_ULTRASOUND0_BASE,0x1e)*170 ; printf("1 => %d.%dm\t", clk / 1000000 , clk % 1000000); usleep(10000);//记得usleep,因为每个测距有个时间,不然很容易死循环.. int clk1 = light(NEW_ULTRASOUND1_BASE,0x1e)*170 ; printf("2 => %d.%dm\t", clk1 / 1000000 , clk1 % 1000000); usleep(10000); int clk2 = light(NEW_ULTRASOUND2_BASE,0x1e)*170 ; printf("3 => %d.%dm\n", clk2 / 1000000 , clk2 % 1000000); usleep(10000);
#define run for(p = 0;p < 5;p++)//啊这个只是懒得写.... //选取距离最小的方向 if (clk1 <= clk && clk1 <= clk2) { clk11 = clk1; }//don' rotate else if(clk <= clk1 && clk <= clk2) { clk11 = clk; run le(); }//left rotate else if(clk2 <= clk && clk2 <= clk1) { clk11 = clk2; run ri(); }//right rotate //go go go if(clk11 > meter || tot1)//保证只后退一步....(这个是一开始一直后退的修复) { tot = tot1 = 0; duty1=0x0000, duty2=0x0000; duty3=0x0000, duty4=0x0000; } else if( clk11 < meter03)//后退 { printf("down\n"); d = meter03 - clk11; duty1=0x1000, duty2=0x0000; duty3=0x1000, duty4=0x0000; tot1 += d; tot = 0;
} else if(clk11 > meter02)//前进,PID中P,I记录 { printf("up\n"); d = clk11 - meter02; duty1=0x0000, duty2=0x1000; duty3=0x0000, duty4=0x1000; tot += d; tot1 = 0; } motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); int rtime = (int)(d * k + (tot + tot1) * j );//同上 if(rtime <= 0) rtime = 0; usleep(rtime); } else {//只能前进,相对好控制一些 ultrasound_init(NEW_ULTRASOUND0_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND1_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND2_BASE);//重置测位函数 int clk = light(NEW_ULTRASOUND0_BASE,0x1e)*170 ; printf("1 => %d.%dm\t", clk / 1000000 , clk % 1000000); usleep(10000); int clk1 = light(NEW_ULTRASOUND1_BASE,0x1e)*170 ; printf("2 => %d.%dm\t", clk1 / 1000000 , clk1 % 1000000); usleep(10000); int clk2 = light(NEW_ULTRASOUND2_BASE,0x1e)*170 ; printf("3 => %d.%dm\n", clk2 / 1000000 , clk2 % 1000000); usleep(10000);
#define run for(p = 0;p < 5;p++)
if (clk1 <= clk && clk1 <= clk2) { clk11 = clk1; }//don' rotate else if(clk <= clk1 && clk <= clk2) { clk11 = clk; run le(); }//left rotate else if(clk2 <= clk && clk2 <= clk1) { clk11 = clk2; run ri(); }//right rotate //go go go if(clk11 > meter ) { tot = 0; duty1=0x0000, duty2=0x0000; duty3=0x0000, duty4=0x0000; } else if(clk11 > meter02) { printf("up\n"); d = clk11 - meter02; duty1=0x0000, duty2=0x1000; duty3=0x0000, duty4=0x1000; tot += d; tot1 = 0; } motor_setting(phase1,duty1,phase2,duty2,period,enable); motor_setting1(phase3,duty3,phase4,duty4,period,enable); int rtime = (int)(d * k + tot * j ); if(rtime <= 0) rtime = 0; usleep(rtime); }
#endif
#ifdef task1 // hide somethingelse #define run for(p = 0;p < 10;p++) ultrasound_init(NEW_ULTRASOUND0_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND1_BASE);//重置测位函数 ultrasound_init(NEW_ULTRASOUND2_BASE);//重置测位函数 int clk = light(NEW_ULTRASOUND0_BASE,0x1e)*170 ; printf("1 => %d.%dm\t", clk / 1000000 , clk % 1000000); usleep(10000); int clk1 = light(NEW_ULTRASOUND1_BASE,0x1e)*170 ; printf("2 => %d.%dm\t", clk1 / 1000000 , clk1 % 1000000); usleep(10000); int clk2 = light(NEW_ULTRASOUND2_BASE,0x1e)*170 ; printf("3 => %d.%dm\n", clk2 / 1000000 , clk2 % 1000000); usleep(10000);
//暴力枚举大法好= =(可惜忘了拍xiaoyi的列表...) //每次做一件事就行..因为循环过来可以继续下一个任务. if(ck(clk) && ck(clk1) && ck(clk2) ) { run up(); } else if(ck(clk) && ck(clk1) && !ck(clk2)) { run le(); } else if(ck(clk) && !ck(clk1) && ck(clk2)) { run down(); run ri(); } else if(ck(clk) && !ck(clk1) && !ck(clk2)) { run le(); } else if(!ck(clk) && !ck(clk1) && !ck(clk2)) { run down(); } else if(!ck(clk) && !ck(clk1) && ck(clk2)) { run ri(); } else if(!ck(clk) && ck(clk1) && !ck(clk2)) { run down(); run ri(); } else if(!ck(clk) && ck(clk1) && ck(clk2)) { run ri(); }
printf("%d\t %d\t %d\t %d\n",duty1,duty2,duty3,duty4);
#endif } return 0; }
void motor_setting(unsigned long phase1, unsigned long duty1, unsigned long phase2, unsigned long duty2, unsigned long period,unsigned long enable) { unsigned long * pwm_en= (unsigned long *)PWM_EN; unsigned long * pwm_period=(unsigned long *)PWM_PERIOD; unsigned long * pwm_phase1=(unsigned long *)PWM_PHASE1; unsigned long * pwm_phase2=(unsigned long *)PWM_PHASE2; unsigned long * pwm_duty1=(unsigned long *)PWM_DUTY1; unsigned long * pwm_duty2=(unsigned long *)PWM_DUTY2;
* pwm_en=enable; * pwm_period=period; * pwm_phase1=phase1; * pwm_phase2=phase2; * pwm_duty1=duty1; * pwm_duty2=duty2; }
void motor_setting1(unsigned long phase1, unsigned long duty1, unsigned long phase2, unsigned long duty2, unsigned long period,unsigned long enable) { unsigned long * pwm_en= (unsigned long *)PWM_EN1; unsigned long * pwm_period=(unsigned long *)PWM_PERIOD1; unsigned long * pwm_phase1=(unsigned long *)PWM_PHASE11; unsigned long * pwm_phase2=(unsigned long *)PWM_PHASE21; unsigned long * pwm_duty1=(unsigned long *)PWM_DUTY11; unsigned long * pwm_duty2=(unsigned long *)PWM_DUTY21;
* pwm_en=enable; * pwm_period=period; * pwm_phase1=phase1; * pwm_phase2=phase2; * pwm_duty1=duty1; * pwm_duty2=duty2;
}
|