我自己做过的,可以用单片机源程序如下:
- #include"stm32f10x.h"
- #include"delay.h"
- #include"usart.h"
- #include"sys.h"
- #include"encoder.h"
- #include"pwm.h"
- #include"mpu6050.h"
- #include"inv_mpu.h"
- #include"usart2.h"
- #include"oled.h"
- #include"ascii.h"
- #include"pid.h"
- int main()
- {
- u8 oled_flag=0;//oled相关
- u8 temp;
-
- delay_init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- uart_init(115200);//usart
- Usart2_Init(9600);
-
-
-
- ENCODER_Init();
- I2C2_Init();//oled
-
- Pwm_Init(7200,0);//moter
- MPU_Init();
- while(mpu_dmp_init());
- A8_Motor_STNBY();
- TIM1_Int(5000,71);
- OLED_Init();
- OLED_Clear();
- OLED_ShowHanZi(0,0,1);
- OLED_ShowHanZi(16,0,2);
- OLED_ShowChar(32,0,':',16);
- OLED_ShowChar(56,0,'%',16);
-
- OLED_ShowHanZi(64,0,1);
- OLED_ShowHanZi(80,0,2);
- OLED_ShowChar(96,0,':',16);
- OLED_ShowChar(120,0,'%',16);
- OLED_ShowString(0,2,"ROLL:",16);
- OLED_ShowString(0,6,"L:",16);
- OLED_ShowString(64,6,"R:",16);
-
- OLED_ShowString(0,4,"H:",16);
- OLED_ShowString(64,4,"us",16);
- OLED_ShowChar(88,4,'G',16);
- OLED_ShowString(88,2,"SMD:",16);
- while(1)
- {
-
-
-
-
-
- Decide();
- if(oled_flag%5==0)//防止OLED占用过长时间
- {
- OLED_ShowChar(96,4,sr04_gate_val/1000%10+'0',16);
- OLED_ShowChar(104,4,sr04_gate_val/100%10+'0',16);
- OLED_ShowChar(112,4,sr04_gate_val/10%10+'0',16);
- OLED_ShowChar(120,4,sr04_gate_val%10+'0',16);
- if(Motor_A>=0)
- {
- if(Motor_A>7199)Motor_A=7199;
- OLED_ShowChar(104,0,(u32)Motor_A*100/7200/10%10+'0',16);//电机a和b的pwm显示
- OLED_ShowChar(112,0,(u32)Motor_A*100/7200%10+'0',16);
- }else
- {
- if(Motor_A<-7199)Motor_A=-7199;
- OLED_ShowChar(104,0,(u32)(-Motor_A)*100/7200/10%10+'0',16);//电机a和b的pwm显示
- OLED_ShowChar(112,0,(u32)(-Motor_A)*100/7200%10+'0',16);
- }
- if(Motor_B>=0)
- {
- if(Motor_B>7199)Motor_B=7199;
- OLED_ShowChar(40,0,(u32)Motor_B*100/7200/10%10+'0',16);
- OLED_ShowChar(48,0,(u32)Motor_B*100/7200%10+'0',16);
- }else
- {
- if(Motor_B<-7199)Motor_B=-7199;
- OLED_ShowChar(40,0,(u32)(-Motor_B)*100/7200/10%10+'0',16);
- OLED_ShowChar(48,0,(u32)(-Motor_B)*100/7200%10+'0',16);
- }
- if(roll<0)
- {
- OLED_ShowChar(40,2,'-',16);
- temp=-roll; //尽量不要修改roll值,会对pwm造成影响
- }
- else
- {
- temp=roll;
- OLED_ShowChar(40,2,'+',16);
- }
- OLED_ShowChar(48,2,(u8)temp/10+'0',16);
- OLED_ShowChar(56,2,(u8)temp%10+'0',16);
- OLED_ShowHanZi(64,2,3);
- if(Encode_left<0)
- {
- OLED_ShowChar(16,6,'-',16);
- OLED_ShowChar(24,6,(-Encode_left)/100%10+'0',16);
- OLED_ShowChar(32,6,(-Encode_left)/10%10+'0',16);
- OLED_ShowChar(40,6,(-Encode_left)%10+'0',16);
- }else
- {
- OLED_ShowChar(16,6,'+',16);
- OLED_ShowChar(24,6,(Encode_left)/100%10+'0',16);
- OLED_ShowChar(32,6,(Encode_left)/10%10+'0',16);
- OLED_ShowChar(40,6,(Encode_left)%10+'0',16);
- }
- if(Encode_right<0)
- {
- OLED_ShowChar(80,6,'-',16);
- OLED_ShowChar(88,6,(-Encode_right)/100%10+'0',16);
- OLED_ShowChar(96,6,(-Encode_right)/10%10+'0',16);
- OLED_ShowChar(104,6,(-Encode_right)%10+'0',16);
- }else
- {
- OLED_ShowChar(80,6,'+',16);
- OLED_ShowChar(88,6,(Encode_right)/100%10+'0',16);
- OLED_ShowChar(96,6,(Encode_right)/10%10+'0',16);
- OLED_ShowChar(104,6,(Encode_right)%10+'0',16);
- }
-
-
- if(oled_flag==5)oled_flag=0;
-
- }
- oled_flag++;
-
- }
- }
复制代码
所有资料51hei提供下载:
平衡小车工程.rar
(362.65 KB, 下载次数: 10)
|