|
IARfor STM32平衡车源程序
- /****************************************************************************/
- //车轮直径 6.7cm 周长21cm 电机转一圈接收到1496个脉冲
- //则一个脉冲对应0.14mm
- /****************************************************************************/
- #include "bsp.h"
- extern uint16_t Speed_Left,Speed_Right;
- uint16_t a;
- uint16_t b=0;
- float situ_pitch;
- int16_t situ_gyroy;//全局变量以便直立PID使用
- //串口1发送1个字符
- //c:要发送的字符
- void usart1_send_char(u8 c)
- {
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕
- USART_SendData(USART1,c);
- }
- //传送数据给匿名四轴上位机软件(V2.6版本)
- //fun:功能字. 0XA0~0XAF
- //data:数据缓存区,最多28字节!!
- //len:data区有效数据个数
- void usart1_niming_report(u8 fun,u8*data,u8 len)
- {
- u8 send_buf[32];
- u8 i;
- if(len>28)return; //最多28字节数据
- send_buf[len+3]=0; //校验数置零
- send_buf[0]=0X88; //帧头
- send_buf[1]=fun; //功能字
- send_buf[2]=len; //数据长度
- for(i=0;i<len;i++)send_buf[3+i]=data[i]; //复制数据
- for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i]; //计算校验和
- for(i=0;i<len+4;i++)usart1_send_char(send_buf[i]); //发送数据到串口1
- }
- //发送加速度传感器数据和陀螺仪数据
- //aacx,aacy,aacz:x,y,z三个方向上面的加速度值
- //gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值
- void mpu6050_send_data(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw,uint16_t Left,uint16_t Right)
- {
- u8 tbuf[22];
- tbuf[0]=(aacx>>8)&0XFF;
- tbuf[1]=aacx&0XFF;
- tbuf[2]=(aacy>>8)&0XFF;
- tbuf[3]=aacy&0XFF;
- tbuf[4]=(aacz>>8)&0XFF;
- tbuf[5]=aacz&0XFF;
- tbuf[6]=(gyrox>>8)&0XFF;
- tbuf[7]=gyrox&0XFF;
- tbuf[8]=(gyroy>>8)&0XFF;
- tbuf[9]=gyroy&0XFF;
- tbuf[10]=(gyroz>>8)&0XFF;
- tbuf[11]=gyroz&0XFF;
-
- tbuf[12]=(roll>>8)&0XFF;
- tbuf[13]=roll&0XFF;
- tbuf[14]=(pitch>>8)&0XFF;
- tbuf[15]=pitch&0XFF;
- tbuf[16]=(yaw>>8)&0XFF;
- tbuf[17]=yaw&0XFF;
-
- tbuf[18]=(Left>>8)&0XFF;
- tbuf[19]=Left&0XFF;
- tbuf[20]=(Right>>8)&0XFF;
- tbuf[21]=Right&0XFF;
-
- usart1_niming_report(0XA1,tbuf,22);//自定义帧,0XA1
- }
- //发送左右电机速度
- //left right左右电机速度值
- void motor_send_speed(uint16_t left,uint16_t right)
- {
- u8 tbuf[4];
- tbuf[0]=(left>>8)&0XFF;
- tbuf[1]=left&0XFF;
- tbuf[2]=(right>>8)&0XFF;
- tbuf[3]=right&0XFF;
- usart1_niming_report(0XA1,tbuf,4);//自定义帧,0XA1
- }
- //通过串口1上报结算后的姿态数据给电脑
- //aacx,aacy,aacz:x,y,z三个方向上面的加速度值
- //gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值
- //roll:横滚角.单位0.01度。 -18000 -> 18000 对应 -180.00 -> 180.00度
- //pitch:俯仰角.单位 0.01度。-9000 - 9000 对应 -90.00 -> 90.00 度
- //yaw:航向角.单位为0.1度 0 -> 3600 对应 0 -> 360.0度
- void usart1_report_imu(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw)
- {
- u8 tbuf[28];
- u8 i;
- for(i=0;i<28;i++)tbuf[i]=0;//清0
- tbuf[0]=(aacx>>8)&0XFF;
- tbuf[1]=aacx&0XFF;
- tbuf[2]=(aacy>>8)&0XFF;
- tbuf[3]=aacy&0XFF;
- tbuf[4]=(aacz>>8)&0XFF;
- tbuf[5]=aacz&0XFF;
- tbuf[6]=(gyrox>>8)&0XFF;
- tbuf[7]=gyrox&0XFF;
- tbuf[8]=(gyroy>>8)&0XFF;
- tbuf[9]=gyroy&0XFF;
- tbuf[10]=(gyroz>>8)&0XFF;
- tbuf[11]=gyroz&0XFF;
- tbuf[18]=(roll>>8)&0XFF;
- tbuf[19]=roll&0XFF;
- tbuf[20]=(pitch>>8)&0XFF;
- tbuf[21]=pitch&0XFF;
- tbuf[22]=(yaw>>8)&0XFF;
- tbuf[23]=yaw&0XFF;
-
- usart1_niming_report(0XAF,tbuf,28);//飞控显示帧,0XAF
- }
- /*********************************************************************************************************************/
-
- int main(void)
- {
- /******************************************************************************/
- // 变量声明
- /******************************************************************************/
- GPIO_InitTypeDef GPIOB_InitStructure;
- GPIO_InitTypeDef GPIOC_InitStructure;
- float pitch,roll,yaw; //欧拉角
- short aacx,aacy,aacz; //加速度传感器原始数据
- short gyrox,gyroy,gyroz; //陀螺仪原始数据
- // short temp; //温度
-
- /******************************************************************************/
- // 模块初始化
- /******************************************************************************/
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化
- delay_init(); //延时初始化
- All_GPIO_Init();
- OLED_Init(); //OLED初始化
- OLED_Clear(); //OLED清屏
- MPU_Init(); //初始化MPU6050
- TIM3_Cap_Init(); //左编码器初始化
- TIM4_Cap_Init(); //右编码器初始化
- Motor_Left_Init(100); //左电机初始化
- Motor_Right_Init(100); //右电机初始化
- TIM1_Int_Init(10); //定时器初始化 定时10ms
-
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
- RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
- RCC_APB2Periph_GPIOE, ENABLE); //使能A,B,C,D,E端口时钟
- /***************************************灯的IO口*****************************************/
- GPIOC_InitStructure.GPIO_Pin = GPIO_Pin_13;
- GPIOC_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIOC_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
- GPIO_Init(GPIOC, &GPIOC_InitStructure); //初始化GPIOC13
-
- /**************************************电机的IO口****************************************/
- GPIOB_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_8|GPIO_Pin_9;
- GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIOB_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
- GPIO_Init(GPIOB, &GPIOB_InitStructure); //初始化B0 B1 B8 B9
-
-
- PCout(13)=0;//亮灯
- PBout(0)=1;
- PBout(1)=0;//A端 右轮
- PBout(8)=1;
- PBout(9)=0;//B端 左轮
- /******************************************************************************/
- // 主程序
- /******************************************************************************/
-
- while(mpu_dmp_init());
- while(1)
- {
- if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
- {
- // temp=MPU_Get_Temperature(); //得到温度值
-
- MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度传感器数据
- MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺仪数据
- situ_pitch=pitch;
- situ_gyroy=gyroy;
- mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)((pitch+4)*100),(int)(yaw*10),Speed_Left*10,Speed_Right*10);//用自定义帧发送加速度和陀螺仪原始数据
- // usart1_report_imu(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
- // OLED_ShowNum(0,0,(int)(pitch*100),10,16);
-
-
- Motor_Right_Duty(PID_Stand(situ_pitch,situ_gyroy));
- Motor_Left_Duty(PID_Stand(situ_pitch,situ_gyroy));
-
- }
- }
-
-
-
-
-
-
-
- }
-
-
-
-
- /******************************************************************************/
- // 定时器2中断服务程序
- /******************************************************************************/
- void TIM1_UP_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM1,TIM_IT_Update)!=RESET)
- {
- TIM_ClearFlag(TIM1,TIM_IT_Update);//清中断标志位
- Speed_Left = Read_TIM3_Encoder()*14;//单位:cm/s * 10
- Speed_Right = Read_TIM4_Encoder()*14;//单位:cm/s * 10
-
- // Motor_Right_Duty(PID_RIGHT(50)/14);
- // Motor_Left_Duty(PID_LEFT(0)/14);
- // motor_send_speed(Speed_Left,Speed_Right);
- TIM4->CNT=0;
- TIM3->CNT=0;
-
-
- }
- }
复制代码
|
评分
-
查看全部评分
|