猫兔0 发表于 2019-12-24 19:37 #include <reg52.h> #define Left_moto2_pwm P0_4 //接驱动模块ENA 左后轮 #define Right_moto2_pwm P0_5 //接驱动模块ENB 右后轮 #define Left_moto_pwm P1_4 //接驱动模块ENA 左前轮 #define Right_moto_pwm P1_5 //接驱动模块ENB 右前轮 #define uchar unsigned char #define uint unsigned int sbit P0_4=P0^4; //定义P0_4 sbit P0_5=P0^5; //定义P0_5 sbit P1_4=P1^4; //定义P1_4 sbit P1_5=P1^5; //定义P1_5 sbit IN1 = P1^2; //为1 左电机反转 前轮 sbit IN2 = P1^3; //为1 左电机正转 前轮 sbit IN3 = P1^6; //为1 右电机正转 前轮 sbit IN4 = P1^7; //为1 右电机反转 前轮 sbit IN5 = P0^2; //为1 左电机反转 后轮 sbit IN6 = P0^3; //为1 左电机正转 后轮 sbit IN7 = P0^6; //为1 右电机正转 后轮 sbit IN8 = P0^7; //为1 右电机反转 后轮 bit Right_moto_stop=1; bit Left_moto_stop =1; int pwm=1; #define left_motor_go IN1 = 0, IN2 = 1//左电机正传 #define left_motor_back IN1 = 1, IN2 = 0//左电机反转 #define right_motor_go IN3 = 0, IN4 = 1//右电机正传 #define right_motor_back IN3 = 1, IN4 = 0//右电机反转 #define left_motor2_go IN5 = 0, IN6 = 1//左电机正传 #define left_motor2_back IN5 = 1, IN6 = 0//左电机反转 #define right_motor2_go IN7 = 0, IN8 = 1//右电机正传 #define right_motor2_back IN7 = 1, IN8 = 0//右电机反转 unsigned char pwm_val_left =0;//变量定义 unsigned char push_val_left =0;// 左电机占空比N/10 unsigned char pwm_val_right =0; unsigned char push_val_right=0;// 右电机占空比N/10 void delay(uint z) { uint x,y; for(x = z; x > 0; x--) for(y = 114; y > 0 ; y--); } void UART_INIT() //蓝牙初始化 { SM0 = 0; SM1 = 1;//串口工作方式1 REN = 1;//允许串口接收 EA = 1;//开总中断 ES = 1;//开串口中断 TMOD = 0x20;//8位自动重装模式 TH1 = 0xfd; TL1 = 0xfd;//9600波特率 TR1 = 1;//启动定时器1 } void run(void) //pwm调速函数 { push_val_left =pwm; //PWM 调节参数1-10 push_val_right =pwm; //PWM 调节参数1-10 if(pwm==10) pwm=0; if(pwm==0&&pwm<0) pwm=0; } void pwm_out_left_moto(void) //左侧电机调速,调节push_val_left的值改变电机转速,占空比 { if(Left_moto_stop) { if(pwm_val_left<=push_val_left) { Left_moto_pwm=1; Left_moto2_pwm=1; } else { Left_moto_pwm=0;Left_moto2_pwm=0; } if(pwm_val_left>=10) pwm_val_left=0; } else { Left_moto_pwm=0;Left_moto2_pwm=0; } } void pwm_out_right_moto(void) //右侧电机调速 { if(Right_moto_stop) { if(pwm_val_right<=push_val_right) { Right_moto_pwm=1; Right_moto2_pwm=1; } else {Right_moto_pwm=0; Right_moto2_pwm=0;} if(pwm_val_right>=10) pwm_val_right=0; } else {Right_moto_pwm=0;Right_moto2_pwm=0; } } void timer0()interrupt 1 using 2 //TIMER0中断服务子函数产生PWM信号// { TH0=0XF8; //1Ms定时 TL0=0X30; pwm_val_left++; pwm_val_right++; pwm_out_left_moto(); pwm_out_right_moto(); } void forward() //小车前进// { ET0 = 1; run(); //pwm 程序 left_motor_go; //左电机前进 right_motor_go; //右电机前进 left_motor2_go; //左电机前进 后轮 right_motor2_go; //右电机前进 后轮 } void left_go() //左转 { ET0 = 1; run(); left_motor_back; right_motor_go; left_motor2_back; right_motor2_go; delay(700); forward(); } void right_go() //右转 { ET0 = 1; run(); delay(50); right_motor_back; left_motor_go; right_motor2_back; left_motor2_go; delay(700); forward(); } void left() //小车左转圈 { ET0 = 1; run(); delay(50); right_motor_go; // 右电机前进 left_motor_back; // 左电机后退 right_motor2_go; // 右电机前进 left_motor2_back; // 左电机后退 } void right() //小车右转圈 { ET0 = 1; run(); delay(50); left_motor_go; right_motor_back; left_motor2_go; right_motor2_back; } void back() //小车后退 { ET0 = 1; run(); left_motor_back; right_motor_back; left_motor2_back; right_motor2_back; } void stop() //小车停止 { ET0 = 0; P1=0; P0=0; } void UART_SER() interrupt 4 //串口中断 { if(RI) { RI = 0; //清除接收标志 switch(SBUF) { case 'g': forward(); break;//前进 case 'b': back(); break;//后退 case 'l': left(); break;//左转圈 case 'r': right(); break;//右转圈 case 's': stop(); break;//停止 case 'z': left_go(); break;//左转行驶 case 'y': right_go(); break;//右转行驶 case 'p': pwm++;break; //加速 case 'c': pwm--;break; //减速 } } } void main() { TMOD=0X01; TH0= 0XF8; //1ms定时 TL0= 0X30; TR0= 1; ET0= 1; EA = 1; UART_INIT();//串口初始化 while(1); } |
建议先把转弯拉出来单独调试,然后打包成函数。 |
可能是电机安装和驱动方向错误,拿起来悬空看看先排除一下是不是这个问题 |
如果要实现转弯的话 前轮一个轮子不动 一个轮子按照你转弯的方向转动就好 |
验证是不是抵消的方法建议就先动一边的轮子,然后看下能不能动 |
liuyongjun000a 发表于 2019-12-24 21:34 3楼是对的,建议你换几个摩擦力小的轮子,放在光滑的平面上看效果。 |
liuyongjun000a 发表于 2019-12-24 22:03 我的小车能够单独的转圈也能够单独的直走,我是想只发送一次指令让它先转圈然后再直走。但是它不转圈然后就直走了,你可能没明白我的意思。 |
Li5151 发表于 2019-12-24 21:42 你去家附近找找小孩可以驱动的大点的电动四驱车试试就知了,一个轮驱动,车子是向驱动方向,同边双驱动也是向驱动方向。错开一前一后,驱动相反,才会转圈。 |
小车如不出现打滑的情况下,是不会动的,相互抵制了。应该是右电机向前加力,左电机空不加电就行了。或者右前通电+左后通电=左前+右后,四个双双单边通电,玩个电动玩具的一看就知道,要不不动,就不就乱动。 |
liuyongjun000a 发表于 2019-12-24 21:34 不会动??相互抵制了??麻烦能具体解释一下吗?? |
liuyongjun000a 发表于 2019-12-24 21:34 为啥是不会动的啊?相互抵制了??麻烦能具体解释一下吗? |
这个你得看全部程序,这样没办法回答你啊 |