智能循迹小车详细源代码程序MSPID
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
巡线车程序(完整版)
1 #ifndef _Macro.h_
2 #define _Macro.h_
3 #include
4 #include
5 #define uchar unsigned char
6 #define uint unsigned int
7 #define one 11.11
8 #define LMAX 1999
9 #define RMAX 3999
10 #define CPU_F ((double)8000000)
11 #define delay_us(x)
__delay_cycles((long)(CPU_F*(double)x/1000000.0))
12
13 #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
14 #define PC 20 // 比例放大系数
15 #define IC 0 //积分放大系数
16 #define DC 85 //大系数
17 #define LEFTOUT TACCR1
18 #define RIGHTOUT TACCR2
19 #define SensorIn P5IN
20 #define F 5000//5000hz
21 #define Period (8000000/F)
22 #define EnableLeftPos P3OUT|=BIT1
23 #define UnenableLeftPos P3OUT&=~BIT1
24
25 #define EnableLeftNeg P3OUT|=BIT0
26 #define UnenableLeftNeg P3OUT&=~BIT0
27
28 #define EnableRightPos P3OUT|=BIT2
29 #define UnenableRightPos P3OUT&=~BIT2
30
31 #define EnableRightNeg P3OUT|=BIT3
32 #define UnenableRightNeg P3OUT&=~BIT3
33
34 #define Basic_Left 100//百分之八十
35 #define Basic_Right 100//Basic_Left
36 #define MAX (100)
37 #define MIN (-100)
38 #define foreward 1
39 #define backward 0
40 #define max_speed 100
41 #define min_speed -100
42 #define key 0
43
44
45 #define left_1 1
46 #define left_2 2
47 #define left_3 3
48 #define left_4 4
49 #define left_5 5
50 #define left_6 6
51 #define left_7 7//右直角
52
53 #define right_1 -1
54 #define right_2 -2
55 #define right_3 -3
56 #define right_4 -4
57 #define right_5 -5
58 #define right_6 -6
59 #define right_7 -7//左直角
60 #endif
[cpp]view plaincopy
61 #include "Macro.h"
62 #include "sensor.h"
63 void Motorstop()
64 {
65 LEFTOUT=0;
66 RIGHTOUT=0;
67 }
68 void MotorLeft(int speed,int direction)
69 {
70 if(speed>max_speed)speed=max_speed;
71 if(direction==backward)//反转
72 {
73 EnableLeftNeg;
74 UnenableLeftPos;
75 }
76 else if(direction==foreward)//正转
77 {
78 EnableLeftPos;
79 UnenableLeftNeg;
80 }
81 LEFTOUT=Period/100*speed;
82 }
83 void MotorRight(int speed,int direction)
84 {
85 if(speed>max_speed)speed=max_speed;
86
87 if(direction==backward)//反转
88 {
89 EnableRightNeg;
90 UnenableRightPos;
91 }
92 else if(direction==foreward)//正转
93 {
94 EnableRightPos;
95 UnenableRightNeg;
96 }
97 RIGHTOUT=Period/100*speed;
98 }
99 void MotorDrive(int PIDout)
100 {
101 int speedleft,speedright;
102 speedleft=Basic_Left PIDout;
103 speedright=Basic_Right-PIDout;
104
105 if(speedleft<0)
106 MotorLeft(speedleft,backward);//反转
107 else MotorLeft(speedleft,foreward);//正转108
109 if(speedright<0)
110 MotorRight(speedright,backward);//反转111 else MotorRight(speedright,foreward);//正转112 }
113 void Rangle(float angle)
114 {
115 // TBCTL|=TBCLR;
116 TBCCR1=LMAX (unsigned int)(angle*one);