蓝牙部分的改进
由于要进行PID参数的整定。需要进行PID的快捷调试,如果每次都通过刷写固件的方式进行,效率太低而且对芯片寿命也有影响。所以这里对蓝牙部分进行功能扩展,通过电脑蓝牙连接小车,进行PID的整定。
这里感谢xxJian,看了他的帖子感觉蓝牙调整PID写的非常棒,就借鉴过来,具体部分如下。
//xxJian code 2 begins
if(uart_receive>0x20) TuningPIDdata[0]=uart_receive;
//-------------------------------------------------------------
//Angle Bias
//update Angle Bias
if(TuningPIDdata[0]=='+'
&&TuningPIDdata[1]=='B'
&&TuningPIDdata[2]=='A')
{
AngleBias = AngleBias + 0.1;
while((USART3->SR&0X40)==0);
USART3->DR = 'A';
while((USART3->SR&0X40)==0);
USART3->DR = 'n';
while((USART3->SR&0X40)==0);
USART3->DR = 'g';
while((USART3->SR&0X40)==0);
USART3->DR = 'l';
while((USART3->SR&0X40)==0);
USART3->DR = 'e';
while((USART3->SR&0X40)==0);
USART3->DR = 'B';
while((USART3->SR&0X40)==0);
USART3->DR = 'i';
while((USART3->SR&0X40)==0);
USART3->DR = 'a';
while((USART3->SR&0X40)==0);
USART3->DR = 's';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
if(AngleBias<0)
{
while((USART3->SR&0X40)==0);
USART3->DR = '-';
}
while((USART3->SR&0X40)==0);
if(AngleBias<0)
USART3->DR = (int32_t)-AngleBias%10+'0';
else
USART3->DR = (int32_t)AngleBias%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
if(AngleBias<0)
USART3->DR = (uint32_t)(-AngleBias*10)%10+'0';
else
USART3->DR = (uint32_t)(AngleBias*10)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
if(TuningPIDdata[0]=='-'
&&TuningPIDdata[1]=='B'
&&TuningPIDdata[2]=='A')
{
AngleBias = AngleBias - 0.1;
while((USART3->SR&0X40)==0);
USART3->DR = 'A';
while((USART3->SR&0X40)==0);
USART3->DR = 'n';
while((USART3->SR&0X40)==0);
USART3->DR = 'g';
while((USART3->SR&0X40)==0);
USART3->DR = 'l';
while((USART3->SR&0X40)==0);
USART3->DR = 'e';
while((USART3->SR&0X40)==0);
USART3->DR = 'B';
while((USART3->SR&0X40)==0);
USART3->DR = 'i';
while((USART3->SR&0X40)==0);
USART3->DR = 'a';
while((USART3->SR&0X40)==0);
USART3->DR = 's';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
if(AngleBias<0)
{
while((USART3->SR&0X40)==0);
USART3->DR = '-';
}
while((USART3->SR&0X40)==0);
if(AngleBias<0)
USART3->DR = (int32_t)-AngleBias%10+'0';
else
USART3->DR = (int32_t)AngleBias%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
if(AngleBias<0)
USART3->DR = (uint32_t)(-AngleBias*10)%10+'0';
else
USART3->DR = (uint32_t)(AngleBias*10)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
//-------------------------------------------------------------
//Balance PID
//update Balance Kp
if(TuningPIDdata[0]=='+'
&&TuningPIDdata[1]=='p'
&&TuningPIDdata[2]=='B')
{
B_Kp = B_Kp + 0.1;
while((USART3->SR&0X40)==0);
USART3->DR = 'B';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'p';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = B_Kp/10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (int32_t)B_Kp%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Kp*10)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
if(TuningPIDdata[0]=='-'
&&TuningPIDdata[1]=='p'
&&TuningPIDdata[2]=='B')
{
B_Kp = B_Kp - 0.1;
while((USART3->SR&0X40)==0);
USART3->DR = 'B';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'p';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = B_Kp/10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (int32_t)B_Kp%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Kp*10)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
//update Balance Ki
if(TuningPIDdata[0]=='+'
&&TuningPIDdata[1]=='i'
&&TuningPIDdata[2]=='B')
{
B_Ki = B_Ki + 0.001;
while((USART3->SR&0X40)==0);
USART3->DR = 'B';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'i';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = '0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Ki*10)+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Ki*100)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Ki*1000)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
if(TuningPIDdata[0]=='-'
&&TuningPIDdata[1]=='i'
&&TuningPIDdata[2]=='B')
{
B_Ki = B_Ki - 0.001;
while((USART3->SR&0X40)==0);
USART3->DR = 'B';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'i';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = '0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Ki*10)+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Ki*100)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Ki*1000)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
//update Balance Kd
if(TuningPIDdata[0]=='+'
&&TuningPIDdata[1]=='d'
&&TuningPIDdata[2]=='B')
{
B_Kd = B_Kd + 0.001;
while((USART3->SR&0X40)==0);
USART3->DR = 'B';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'd';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = '0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Kd*10)+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Kd*100)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Kd*1000)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
if(TuningPIDdata[0]=='-'
&&TuningPIDdata[1]=='d'
&&TuningPIDdata[2]=='B')
{
B_Kd = B_Kd - 0.001;
while((USART3->SR&0X40)==0);
USART3->DR = 'B';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'd';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = '0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Kd*10)+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Kd*100)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(B_Kd*1000)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
//-------------------------------------------------------------
//Velocity PID
//update Velocity Kp
if(TuningPIDdata[0]=='+'
&&TuningPIDdata[1]=='p'
&&TuningPIDdata[2]=='V')
{
V_Kp = V_Kp + 0.1;
while((USART3->SR&0X40)==0);
USART3->DR = 'V';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'p';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = V_Kp/10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (int32_t)V_Kp%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(V_Kp*10)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
if(TuningPIDdata[0]=='-'
&&TuningPIDdata[1]=='p'
&&TuningPIDdata[2]=='V')
{
V_Kp = V_Kp - 0.1;
while((USART3->SR&0X40)==0);
USART3->DR = 'V';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'p';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = V_Kp/10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (int32_t)V_Kp%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(V_Kp*10)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
//update Velocity Ki
if(TuningPIDdata[0]=='+'
&&TuningPIDdata[1]=='i'
&&TuningPIDdata[2]=='V')
{
V_Ki = V_Ki + 0.001;
while((USART3->SR&0X40)==0);
USART3->DR = 'V';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'i';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = '0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(V_Ki*10)+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(V_Ki*100)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(V_Ki*1000)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
if(TuningPIDdata[0]=='-'
&&TuningPIDdata[1]=='i'
&&TuningPIDdata[2]=='V')
{
V_Ki = V_Ki - 0.001;
while((USART3->SR&0X40)==0);
USART3->DR = 'V';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'i';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = '0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(V_Ki*10)+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(V_Ki*100)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(V_Ki*1000)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
//-------------------------------------------------------------
//Turn PID
//update Turn Kp
if(TuningPIDdata[0]=='+'
&&TuningPIDdata[1]=='p'
&&TuningPIDdata[2]=='T')
{
T_Kp = T_Kp + 0.1;
while((USART3->SR&0X40)==0);
USART3->DR = 'T';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'p';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = T_Kp/10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (int32_t)T_Kp%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(T_Kp*10)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
if(TuningPIDdata[0]=='-'
&&TuningPIDdata[1]=='p'
&&TuningPIDdata[2]=='T')
{
T_Kp = T_Kp - 0.1;
while((USART3->SR&0X40)==0);
USART3->DR = 'T';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'p';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = T_Kp/10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (int32_t)T_Kp%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(T_Kp*10)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
//update Turn Kd
if(TuningPIDdata[0]=='+'
&&TuningPIDdata[1]=='d'
&&TuningPIDdata[2]=='T')
{
T_Kd = T_Kd + 0.001;
while((USART3->SR&0X40)==0);
USART3->DR = 'T';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'd';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = '0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(T_Kd*10)+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(T_Kd*100)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(T_Kd*1000)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
if(TuningPIDdata[0]=='-'
&&TuningPIDdata[1]=='d'
&&TuningPIDdata[2]=='T')
{
T_Kd = T_Kd - 0.001;
while((USART3->SR&0X40)==0);
USART3->DR = 'T';
while((USART3->SR&0X40)==0);
USART3->DR = '_';
while((USART3->SR&0X40)==0);
USART3->DR = 'K';
while((USART3->SR&0X40)==0);
USART3->DR = 'd';
while((USART3->SR&0X40)==0);
USART3->DR = '=';
while((USART3->SR&0X40)==0);
USART3->DR = '0';
while((USART3->SR&0X40)==0);
USART3->DR = '.';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(T_Kd*10)+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(T_Kd*100)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = (uint32_t)(T_Kd*1000)%10+'0';
while((USART3->SR&0X40)==0);
USART3->DR = '\n';
}
TuningPIDdata[4]=TuningPIDdata[3];
TuningPIDdata[3]=TuningPIDdata[2];
TuningPIDdata[2]=TuningPIDdata[1];
TuningPIDdata[1]=TuningPIDdata[0];
//xxJian code 2 ends
替换了80行以后的代码
好了,通过修改蓝牙部分的代码,实现了蓝牙整定PID的功能。
通过修改24L01的代码,实现了无线数据示波器显示。
下面就可以进行pid的整定了。
看网友的建议,直立环和速度环是相辅相成的,如果调试直立单纯通过通过保留直立环,而屏蔽了速度和转向环进行调试效果并不好,所以尝试带有速度环和直立环进行调试。
屏蔽方法截图
屏蔽后,添加了蓝牙部分的调试代码。这里借用xxJian兄的代码。感谢。
然后进行PID的调试,经过初步调试,发下视频。视频可以看到调试的还不是很完美,直立状态会晃,可以进行前进后退操作,此时进行左右转向时无效。
http://player.youku.com/player.php/sid/XMTM1MDI2Mzc4MA==/v.swf
视频地址:http://player.youku.com/player.php/sid/XMTM1MDI2Mzc4MA==/v.swf