综合技术
登录
直播中
杨兢兢
7年用户
204经验值
私信
关注
[问答]
红外光循迹+PID控制电机的小车有什么心得分享吗
开启该帖子的消息推送
电机
PID
想学习做一个红外光循迹黑白线来控制的小车,pid控制电机的方法
有没有大神能够分享一下的心得,资料的
回帖
(6)
张婷婷
2019-1-25 08:44:30
帮顶
帮顶
举报
李劲草
2019-1-25 08:50:08
#include
#define SPEEDMAX 1000
***it ENA = P1^2;
***it CS = P1^3;
***it CLK = P1^4;
***it DO = P1^5;
unsigned char seg_data[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //¹²Òõ¼«ÊýÂë¹Ü¶ÎÖµ
unsigned char bit_tab[4]={0xfe,0xfd,0xfb,0xf7}; //λѡ¿ØÖÆλ
unsigned int mod[4]={1000,100,10,1}; //È¡Ä£¿ØÖÆÁ¿£¬ÎªÁË·½±ã¼ÆËãÊ®½øÖÆÊýµÄÿһλÊý×Ö
float s1,s2,s3,s4,s5; //Ëæ»ú¶¨ÒåµÄËٶȱäÁ¿£¬±¸ÓÃ
unsigned int rate; //δʹÓÃ
unsigned int timer0_cnt; //¶¨Ê±Æ÷0¶¨Ê±ÖжϼÆÊý£¬ÎªÁË¿ØÖÆÕ¼¿Õ±ÈʹÓÃ
int button_set_data; //°´¼üÉèÖÃËÙ¶ÈÖµ
unsigned char button; //°´¼üÖµ
unsigned char flag; //±ê־λ£¬Ä¿Ç°Î´Ê¹ÓÃ
unsigned char timer2_cnt; //¶¨Ê±Æ÷2¼ÆÊýÆ÷£¬ÓÃÀ´¼Æʱ500ms
unsigned char pot; //»¬¶¯±ä×èÆ÷µçѹֵ
unsigned int motor_plus; //1sÄÚ£¬µç»ú±àÂëÆ÷Âö³å¸öÊý
unsigned int driver_data; //Õ¼¿Õ±ÈÇý¶¯µ÷Õû
unsigned int pot_data; //»¬¶¯±ä×èÆ÷µçѹֵ¶ÔÓ¦µÄתËÙ
unsigned int speed; //µç»úתËÙ
int pid_val; //PID¿ØÖÆÊä³öÖµ
typedef struct
{
unsigned int set_point;
float P;
float I;
float D;
unsigned int last_error;
unsigned int prev_error;
unsigned int sum_error;
}
PID; //PIDËã·¨½á¹¹Ìå
PID motor_pid; //¶¨Òåµç»úPID¿ØÖƱäÁ¿
//PID¼ÆËã
int PidCalculate(PID *ppid, int next_point) //PID¼ÆË㺯Êý
{
float derror,error;
float val;
error=ppid->set_point-next_point; //µ±Ç°Ê±¿Ì²îÖµ
ppid->sum_error+=error; //»ý·ÖÖµ£¬»ý·ÖÖµ¾ÍÊDzîÖµµÄÀÛ¼Ó
derror=ppid->last_error - ppid->prev_error; //΢·ÖÖµ£¬Î¢·Ö¾ÍÊǵ±Ç°Ê±¿ÌµÄ²îÖµ-ǰһʱ¿ÌµÄ²îÖµ
ppid->prev_error=ppid->last_error; //´æ´¢µ±Ç°Ê±¿ÌµÄ²îÖµ£¬±ä³Éǰһʱ¿Ì²îÖµ£¬ÎªÏ´μÆËã×ö×¼±¸
ppid->last_error=error;
val=ppid->P*error + ppid->I * ppid->sum_error + ppid->D*derror;
return (int)val;
}
void PidInit (PID *ppid) //PID³õʼ»¯º¯Êý
{
ppid->set_point=0;
ppid->P=0.6;
ppid->I=1;
ppid->D=0.2;
}
void delay(unsigned int x) //ÑÓʱº¯Êý
{
unsigned int i;
for(i=0;i
}
unsigned char AD0831_ReadChar(void) //ADC¶ÁÈ¡Êý¾Ý
{
unsigned char val;
unsigned char i;
val=0;
CS=0;
for(i=0;i<9;i++)
{
CLK=1;
delay(1);
CLK=0;
val<<=1;
val|=DO;
}
CS=1;
delay(1);
return val;
}
void main(void)
{
unsigned char i,j;
IT0=1;
EX0=1;
TMOD=0X12;
TH0=256-70;
TL0=256-70;
ET0=1;
EA=1;
TR0=1;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
ET1=1;
TR1=1;
P1=0x00;
DO=1;
PidInit(&motor_pid);
while(1)
{
P1|=0x01;
j++;
if(j==100)
{
pot = AD0831_ReadChar();
pot_data=(unsigned int)(((float)(pot)/256)*1000);
j=0;
}
button=P3;
button=button & 0x03;
if(button!=0x03)
{
delay(10);
button=P3;
button=button & 0x03;
if(button!=0x03)
{
if(button==0x02)
{
button_set_data=button_set_data+100;
}
if(button==0x01)
{
button_set_data=button_set_data-100;
}
if(button_set_data>1000)
button_set_data=1000;
if(button_set_data<0)
button_set_data=0;
}
while(button!=0x03)
{
button=P3;
button=button & 0x03;
}
}
for(i=0;i<4;i++)
{
P0=seg_data[(speed/mod
)%10];
P2=bit_tab
;
delay(100);
P2=0xff;
}
motor_pid.set_point=pot_data;
}
}
void timer0() interrupt 1
{
timer0_cnt++;
if(timer0_cnt==1000)
{
timer0_cnt=0;
}
if(timer0_cnt
{
ENA=1;
}
else
{
ENA=0;
}
}
void int0() interrupt 0
{
motor_plus++;
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
timer2_cnt++;
if(timer2_cnt==10)
{
timer2_cnt=0;
speed=((float)(motor_plus)*(float)60)/(float)24.0;
motor_plus=0;
pid_val=PidCalculate(&motor_pid,speed);
if(pid_val>1000)
pid_val=1000;
if(pid_val<0)
pid_val=0;
driver_data=(unsigned int)pid_val;
}
}
#include
#define SPEEDMAX 1000
***it ENA = P1^2;
***it CS = P1^3;
***it CLK = P1^4;
***it DO = P1^5;
unsigned char seg_data[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //¹²Òõ¼«ÊýÂë¹Ü¶ÎÖµ
unsigned char bit_tab[4]={0xfe,0xfd,0xfb,0xf7}; //λѡ¿ØÖÆλ
unsigned int mod[4]={1000,100,10,1}; //È¡Ä£¿ØÖÆÁ¿£¬ÎªÁË·½±ã¼ÆËãÊ®½øÖÆÊýµÄÿһλÊý×Ö
float s1,s2,s3,s4,s5; //Ëæ»ú¶¨ÒåµÄËٶȱäÁ¿£¬±¸ÓÃ
unsigned int rate; //δʹÓÃ
unsigned int timer0_cnt; //¶¨Ê±Æ÷0¶¨Ê±ÖжϼÆÊý£¬ÎªÁË¿ØÖÆÕ¼¿Õ±ÈʹÓÃ
int button_set_data; //°´¼üÉèÖÃËÙ¶ÈÖµ
unsigned char button; //°´¼üÖµ
unsigned char flag; //±ê־λ£¬Ä¿Ç°Î´Ê¹ÓÃ
unsigned char timer2_cnt; //¶¨Ê±Æ÷2¼ÆÊýÆ÷£¬ÓÃÀ´¼Æʱ500ms
unsigned char pot; //»¬¶¯±ä×èÆ÷µçѹֵ
unsigned int motor_plus; //1sÄÚ£¬µç»ú±àÂëÆ÷Âö³å¸öÊý
unsigned int driver_data; //Õ¼¿Õ±ÈÇý¶¯µ÷Õû
unsigned int pot_data; //»¬¶¯±ä×èÆ÷µçѹֵ¶ÔÓ¦µÄתËÙ
unsigned int speed; //µç»úתËÙ
int pid_val; //PID¿ØÖÆÊä³öÖµ
typedef struct
{
unsigned int set_point;
float P;
float I;
float D;
unsigned int last_error;
unsigned int prev_error;
unsigned int sum_error;
}
PID; //PIDËã·¨½á¹¹Ìå
PID motor_pid; //¶¨Òåµç»úPID¿ØÖƱäÁ¿
//PID¼ÆËã
int PidCalculate(PID *ppid, int next_point) //PID¼ÆË㺯Êý
{
float derror,error;
float val;
error=ppid->set_point-next_point; //µ±Ç°Ê±¿Ì²îÖµ
ppid->sum_error+=error; //»ý·ÖÖµ£¬»ý·ÖÖµ¾ÍÊDzîÖµµÄÀÛ¼Ó
derror=ppid->last_error - ppid->prev_error; //΢·ÖÖµ£¬Î¢·Ö¾ÍÊǵ±Ç°Ê±¿ÌµÄ²îÖµ-ǰһʱ¿ÌµÄ²îÖµ
ppid->prev_error=ppid->last_error; //´æ´¢µ±Ç°Ê±¿ÌµÄ²îÖµ£¬±ä³Éǰһʱ¿Ì²îÖµ£¬ÎªÏ´μÆËã×ö×¼±¸
ppid->last_error=error;
val=ppid->P*error + ppid->I * ppid->sum_error + ppid->D*derror;
return (int)val;
}
void PidInit (PID *ppid) //PID³õʼ»¯º¯Êý
{
ppid->set_point=0;
ppid->P=0.6;
ppid->I=1;
ppid->D=0.2;
}
void delay(unsigned int x) //ÑÓʱº¯Êý
{
unsigned int i;
for(i=0;i
}
unsigned char AD0831_ReadChar(void) //ADC¶ÁÈ¡Êý¾Ý
{
unsigned char val;
unsigned char i;
val=0;
CS=0;
for(i=0;i<9;i++)
{
CLK=1;
delay(1);
CLK=0;
val<<=1;
val|=DO;
}
CS=1;
delay(1);
return val;
}
void main(void)
{
unsigned char i,j;
IT0=1;
EX0=1;
TMOD=0X12;
TH0=256-70;
TL0=256-70;
ET0=1;
EA=1;
TR0=1;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
ET1=1;
TR1=1;
P1=0x00;
DO=1;
PidInit(&motor_pid);
while(1)
{
P1|=0x01;
j++;
if(j==100)
{
pot = AD0831_ReadChar();
pot_data=(unsigned int)(((float)(pot)/256)*1000);
j=0;
}
button=P3;
button=button & 0x03;
if(button!=0x03)
{
delay(10);
button=P3;
button=button & 0x03;
if(button!=0x03)
{
if(button==0x02)
{
button_set_data=button_set_data+100;
}
if(button==0x01)
{
button_set_data=button_set_data-100;
}
if(button_set_data>1000)
button_set_data=1000;
if(button_set_data<0)
button_set_data=0;
}
while(button!=0x03)
{
button=P3;
button=button & 0x03;
}
}
for(i=0;i<4;i++)
{
P0=seg_data[(speed/mod
)%10];
P2=bit_tab
;
delay(100);
P2=0xff;
}
motor_pid.set_point=pot_data;
}
}
void timer0() interrupt 1
{
timer0_cnt++;
if(timer0_cnt==1000)
{
timer0_cnt=0;
}
if(timer0_cnt
{
ENA=1;
}
else
{
ENA=0;
}
}
void int0() interrupt 0
{
motor_plus++;
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
timer2_cnt++;
if(timer2_cnt==10)
{
timer2_cnt=0;
speed=((float)(motor_plus)*(float)60)/(float)24.0;
motor_plus=0;
pid_val=PidCalculate(&motor_pid,speed);
if(pid_val>1000)
pid_val=1000;
if(pid_val<0)
pid_val=0;
driver_data=(unsigned int)pid_val;
}
}
举报
李秀梅
2019-1-25 08:57:06
大佬,有没有stm32的?
大佬,有没有stm32的?
举报
陈平
2019-1-25 09:10:33
想要stm32的,红外光循迹还能走十字路口的
想要stm32的,红外光循迹还能走十字路口的
举报
更多回帖
rotate(-90deg);
回复
相关问答
电机
PID
请问有没有大神做过stm32的
红外光
循迹
的智能
小车
?求分享
2019-01-18
2038
怎样去设计一种高可靠
小车
红外光
循迹
威廉希尔官方网站 ?
2021-05-12
1196
求
小车
红外
循迹
基本
控制
实物图和线路图?
2020-11-09
1704
请问
循迹
避障当中的四路
循迹
模块与
红外
寻
光
&避障模块
有
什么区别?
2019-02-01
10254
怎么实现智能
小车
红外
循迹
的设计?
2022-01-18
1172
求简单
红外
循迹
小车
制作程序与仿真图
2014-05-29
4759
红外光
的基本原理解析
2020-12-15
1669
寻找能反射
红外光
的材料
2016-06-01
7618
怎么实现基于STM32的智能
循迹
往返
小车
设计?
2021-11-02
2114
如何利用STC89c52rc单片机实现
红外
循迹
小车
的设计?
2021-10-20
1693
发帖
登录/注册
20万+
工程师都在用,
免费
PCB检查工具
无需安装、支持浏览器和手机在线查看、实时共享
查看
点击登录
登录更多精彩功能!
英国威廉希尔公司网站
william hill官网 版块
小组
免费开发板试用
ebook
直播
搜索
登录
×
20
完善资料,
赚取积分