|
各位,麻烦帮我看看为什么进行磁阻传感器的校准不行啊,校准出来的数据不稳定,我平行地放在桌面上,角度还是不断地在乱变,变化范围很大,不过当我旋转180度之后,角度显示就稳定了,不知道是什么原因?恳请帮忙啊!!
void main()
{
delay(200); //上电延时
LCD_init(); //初始化液晶
LCD_clear();
while(1) //循环
{
keyscan();
if(jiaozhun==0)
{
Init_HMC5883(); //初始化HMC5883L
HMC5883_Multiple_Read(MagAdd,MagREG_Add,&Magscal); //采集数据,计算校准后的三轴值
if(jiaozhun2==1)
CalcScale(); //算出校准后的Xscal、Yscal、Zscal值
angle= atan2((double)Magscal.y,(double)Magscal.x) * (180 / 3.14159265) + 180; // angle in degrees
diplay_yuan(); //显示航向角中文和画圆
RefreshScreen(); //显示scop[]数组里边的内容
ClearnGraph();
}
if(jiaozhun==1)
{
RegulateXY();
}
if(jiaozhun==2)
{
regulateZ();
}
if(jiaozhun==3)
{
jiaozhun=0;
jiaozhun2=1;
CalcOffsetSen(); //计算偏移量offset和灵敏度sen
}
}
}
idata MagnetData MagMax = {-0x7FFF, -0x7FFF, -0x7FFF};//最大值设成int最小值
idata MagnetData MagMin = {0x7FFF, 0x7FFF, 0x7FFF}; //最小值设成int最大值
//计算偏移量offset和灵敏度sen
void CalcOffsetSen(void)
{
Magoffset.x=-(MagMin.x+MagMax.x)/2; //用这个代替CalcOffsetScale(&MagMax,&MagMin);
Magsen.x=1;
Magoffset.y=-(MagMin.y+MagMax.y)/2;
Magsen.y=(MagMax.y-MagMin.y)/(MagMax.x-MagMin.x);
Magoffset.z=-(MagMin.z+MagMax.z)/2;
Magsen.z= (MagMax.z-MagMin.z)/(MagMax.x-MagMin.x);
}
//得到校准后的Xscal、Yscal、Zscal值
void CalcScale(void)
{
Magscal.x=((long)(Magscal.x+Magoffset.x)* (long)Magsen.x);//方法4,不要右移12
Magscal.y=((long)(Magscal.y+Magoffset.y)* (long)Magsen.y);
Magscal.z=((long)(Magscal.z+Magoffset.z)* (long)Magsen.z);
}
//获取磁阻水平时XY轴的最大、最小值,从而计算校准后X、Y、Z值
void RegulateXY(void)
{
MagnetData xdata Magtmp;
diplay_intstr(0,39,MagMax.y);
diplay_intstr(0,31,MagMax.x);
diplay_intstr(0,23,MagMin.y);
diplay_intstr(0,15,MagMin.x);
Draw_english_string(0,7,"regulate XY");
RefreshScreen(); //显示scop[]数组里边的内容
ClearnGraph();
HMC5883_SetMeasure();
if(s2==0)
{
delay(10);
if(s2==0)
{
jiaozhun=2;
while(!s2);
}
}
HMC5883_Multiple_Read(MagAdd,MagREG_Add,&Magtmp);
if(Magtmp.x > MagMax.x)
{
MagMax.x = Magtmp.x;
}
else if(Magtmp.x < MagMin.x)
{
MagMin.x = Magtmp.x;
}
if(Magtmp.y > MagMax.y)
{
MagMax.y = Magtmp.y;
}
else if(Magtmp.y < MagMin.y)
{
MagMin.y = Magtmp.y;
}
}
void regulateZ(void)
{
MagnetData xdata Magtmp;
diplay_intstr(0,23,MagMax.z);
diplay_intstr(0,15,MagMin.z);
Draw_english_string(0,7,"regulate z");
RefreshScreen(); //显示内容
ClearnGraph();
HMC5883_SetMeasure();
if(s2==0)
{
delay(10);
if(s2==0)
{
jiaozhun=3;
while(!s2);
}
}
HMC5883_Multiple_Read(MagAdd,MagREG_Add,&Magtmp);
if(Magtmp.z > MagMax.z)
{
MagMax.z = Magtmp.z;
}
else if(Magtmp.z < MagMin.z)
{
MagMin.z = Magtmp.z;
}
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|