سلام
من ماژول GY-271 رو خریدم و با بدبختی راه اندازیش کردم
ولی اطلاعاتی که الان بهم میده x,y,z درست نیست
توی اینترنت گشتم نوشته بود باید با فرمول
angle= atan2((double)yo,(double)xo) * (180 / 3.14159265) + 180; // angle in degrees
زاویه رو بدست بیاری که وقتی از همین فرمول استفاده می کنم زاویه رو در حدود 2200 تا 2800 نشون میده
مشکل از کجاست؟
اینم کد کدویژن c
من ماژول GY-271 رو خریدم و با بدبختی راه اندازیش کردم
ولی اطلاعاتی که الان بهم میده x,y,z درست نیست
توی اینترنت گشتم نوشته بود باید با فرمول
angle= atan2((double)yo,(double)xo) * (180 / 3.14159265) + 180; // angle in degrees
زاویه رو بدست بیاری که وقتی از همین فرمول استفاده می کنم زاویه رو در حدود 2200 تا 2800 نشون میده
مشکل از کجاست؟
اینم کد کدویژن c
کد:
while (1)
{
i2c_start();
delay_ms(10);
i2c_write(0x3C); //write to HMC
delay_ms(10);
i2c_write(0x02); //mode register
delay_ms(10);
i2c_write(0x00); //continuous measurement mode
delay_ms(10);
i2c_stop();
//must read all six registers plus one to move the pointer back to 0x03
i2c_start();
delay_ms(10);
i2c_write(0x3D); //read from HMC
delay_ms(10);
xh = i2c_read(1); //x high byte
delay_ms(10);
//printf(" %d", xh);
xl = i2c_read(1); //x low byte
delay_ms(10);
xo = xl|(xh << 8);
//printf("x=%4ld, ", xo);
yh = i2c_read(1); //y high byte
delay_ms(10);
//printf(" %d ", yh);
yl = i2c_read(1); //y low byte
delay_ms(10);
yo = yl|(yh << 8);
//printf("y=%4ld, ", yo);
zh = i2c_read(1);
delay_ms(10); //z high byte
//printf(" %d ", zh);
zl = i2c_read(1); //z low byte
delay_ms(10);
zo = zl|(zh << 8);
//printf("z=%ld \r\n", zo);
i2c_write(0x3D); //must reach 0x09 to go back to 0x03
delay_ms(10);
i2c_stop();
angle= atan2((double)yo,(double)xo) * (180 / 3.14159265) + 180; // angle in degrees
angle*=10;
sprintf(str,"x:%d ,y:%d,z:%d , angel:%d \r\n",xo,yo,zo,angle);
puts(str);
delay_ms(300);
}







دیدگاه