sensor - Measuing car rotation with Android phone -


i have found many threads how handle device' rotation,orientation motion , position sensors. create app use in car, first measure rotation degree of car. put phone phone case , example when turn left car see car' turning degree on phone.

is possible magnetic , accelero meter?

i post code first think okay. (let's hold phone "portait" mode not landscape first)

private static sensormanager sensorservice;  //magnetic private sensor msensor;  //accelerometer private sensor gsensor;  private float[] mvaluesmagnet      = new float[3]; private float[] mvaluesaccel       = new float[3]; private float[] mvaluesorientation = new float[3];  private float[] mrotationmatrix    = new float[9];    @override public void oncreate(bundle savedinstancestate) { nf.setmaximumfractiondigits(2); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); sensorservice = (sensormanager) getsystemservice(context.sensor_service); this.msensor = sensorservice.getdefaultsensor(sensor.type_magnetic_field); this.gsensor = sensorservice.getdefaultsensor(sensor.type_accelerometer);  sensorservice.registerlistener(this, gsensor, sensormanager.sensor_delay_normal); sensorservice.registerlistener(this, msensor, sensormanager.sensor_delay_normal); }  @override public void onaccuracychanged(sensor arg0, int arg1) { }  @override public void onsensorchanged(sensorevent event) { switch (event.sensor.gettype()) {            case sensor.type_accelerometer:  system.arraycopy(event.values, 0, mvaluesaccel, 0, 3);  break;   case sensor.type_magnetic_field:  system.arraycopy(event.values, 0, mvaluesmagnet, 0, 3);  break;  }  sensormanager.getrotationmatrix(mrotationmatrix, null, mvaluesaccel, mvaluesmagnet); sensormanager.getorientation(mrotationmatrix, mvaluesorientation);   //        double azimuth = math.todegrees(mvaluesorientation[0]);    //azimuth,     rotation around z axis. //        double pitch   = math.todegrees(mvaluesorientation[1]);    // pitch, rotation around x axis. double roll    = math.todegrees(mvaluesorientation[2]);    //roll, rotation around y axis.  //normalize //        azimuth = azimuth>0?azimuth:azimuth+360; roll = roll>0?roll:roll+360;   string txt = "roll= "+math.round(roll); ((edittext)findviewbyid(r.id.szog)).settext(txt); } 

questions: - how accurate app in car? (what can more accurate?) - should when hold phone @ "landscape" mode? roll orientation still okay?

please note first try there do! first want see how can achive that

thanks!

if had android device set compass, there arrow pointed magnetic north. measuring change in direction of arrow, measure rotation of car.

to better indication of orientation of android device, use sensor.type_gravity , sensor.type_magnetic_field, instead of sensor.type_accelerometer , sensor.type_magnetic_field. better because sensor.type_gravity tries filter out effects due movement of device.

if android device lying flat on surface, direction of magnetic north defined azimuth comes out of sensormanager.getorientation(...). when it's standing or when it's on side it's bit more complicated. however, suggest starting device lying flat first because that's easiest case, , can progress more difficult cases.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -