android - dynamically updating position of an object using accelerometer and gyroscope -


i beginner in android programming. below program updating current position of object using accelerometer , gyroscope readings.my objective update object's position on screen. output of program displays object , doesn't update it's position. can please suggest me going wrong.

mainactivity.java

public class mainactivity extends activity implements sensoreventlistener {

    customdrawableview mcustomdrawableview = null;     shapedrawable mdrawable = new shapedrawable();      int update_threshold=20;     sensor accelerometer,gyroscope;     sensormanager sm,sm1;     textview acceleration,rotation,c_x,c_y;     long mlastupdate;      public static int x;     public static int y;       double gravity_thresh = 12;      double one_step_freeze = 0;     double sensor_interval = 0.02;     double step_interval_thresh = 0.4;     double init_x = 1;     double init_y = 0;     double init_theta = 0;     static int acce_filter_data_min_time = 20; // 1000ms     long lastsaved = system.currenttimemillis();     double step_counter = 0;     double current_x,current_y;       double one_step_happend = 0;     double accumulated_rad = 0;     double trace_x = init_x;     double trace_x_particle = init_x;     double trace_y = init_y;     double trace_y_particle = init_y;     double last_x = init_x;     double last_y = init_y;     double last_theta = init_theta;      double z_gy;      float acc_z,acc_x,acc_y;     sensoreventlistener msensorlisteneracc,msensorlistenergyro;     @override     protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);           //setcontentview(r.layout.activity_main);          sm=(sensormanager)getsystemservice(sensor_service);          sm1=(sensormanager)getsystemservice(sensor_service);           mcustomdrawableview = new customdrawableview(this);          setcontentview(mcustomdrawableview);           accelerometer=sm.getdefaultsensor(sensor.type_accelerometer);          gyroscope=sm1.getdefaultsensor(sensor.type_gyroscope);          sm.registerlistener(this, accelerometer,     sensormanager.sensor_delay_normal );                sm1.registerlistener(this,gyroscope,sensormanager.sensor_delay_normal);     //acceleration=(textview)findviewbyid(r.id.acceleration);     //rotation=(textview)findviewbyid(r.id.rotation);    // c_x=(textview)findviewbyid(r.id.c_x);     //c_y=(textview)findviewbyid(r.id.c_y); } /*@override protected void onresume() {     super.onresume();     //sm.registerlistener(this, accelerometer, sensormanager.sensor_delay_fastest);     sm.registerlistener(msensorlisteneracc, accelerometer, sensormanager.sensor_delay_fastest);     sm1.registerlistener(msensorlistenergyro, gyroscope,sensormanager.sensor_delay_fastest);     //mlastupdate=system.currenttimemillis(); }  @override protected void onpause() {     sm.unregisterlistener(this); }*/     @override public void onsensorchanged(sensorevent event) {   /*  long actualtime = system.currenttimemillis();     if(actualtime-mlastupdate > update_threshold){         mlastupdate=actualtime;      }*/     if (event.sensor.gettype() == sensor.type_gyroscope) {          /*rotation.settext("xg: " + event.values[0] +                 "\nyg: " + event.values[1] +                 "\nzg: " + event.values[2]);*/         z_gy= event.values[2];       }      if (event.sensor.gettype() == sensor.type_accelerometer) {           /* acceleration.settext("x: " + event.values[0] +                 "\ny: " + event.values[1] +                 "\nz: " + event.values[2]);*/         acc_x=event.values[1];         acc_y=event.values[2];         acc_z = event.values[2];         system.out.println("acc_z:" + float.tostring(acc_z));         if (acc_z > gravity_thresh && one_step_freeze == 0) {             system.out.println("entering1oop1");             one_step_happend = 1;             step_counter = step_counter + 1;             system.out.println("stepcounter: " + double.tostring(step_counter) + "==========================");             one_step_freeze = step_interval_thresh / sensor_interval;             /*system.out.println("onestepfreeze_in_firstloop_aft_div: " + double.tostring(one_step_freeze));             system.out.println("exitingloop1");*/         }          if (one_step_freeze > 0) {                 system.out.println("stepcounterin 2 loop: " + double.tostring(step_counter) + "==========================");                 if ((system.currenttimemillis() - lastsaved) > acce_filter_data_min_time) {                     lastsaved = system.currenttimemillis();                     one_step_freeze = one_step_freeze - 1;                    /* system.out.println("onestepfreeze2:" + double.tostring(one_step_freeze));                     system.out.println("exitin 2loop");*/                  }             }         if(one_step_happend==1){            // system.out.println("entered 3 loop:");             last_theta=accumulated_rad+last_theta;            // system.out.println("last_theta value: "+last_theta);             current_x=last_x+stride_length*math.cos(last_theta);             system.out.println("current_x value: " + current_x);             current_y=last_y+stride_length*math.sin(last_theta);             //system.out.println("current_y value: " + current_y);            // system.out.println("processing c_x");             //c_x.settext("c_x:" + current_x);           //  system.out.println("processing c_y");             //c_y.settext("c_y:" + current_y);             last_x = current_x;            // system.out.println("last_x value: " + last_x);              last_y = current_y;            // system.out.println("last_y value: "+last_y);             accumulated_rad = 0;             one_step_happend = 0;          }else{             accumulated_rad= accumulated_rad + z_gy * sensor_interval;             system.out.println("accumulatedrad: "+accumulated_rad);         }         }            /*if ((acc_z > gravity_thresh) && (one_step_freeze == 0)) {            one_step_happend = 1;            step_counter = step_counter + 1;            system.out.println("stepcounter: " + double.tostring(step_counter) + "==========================");            one_step_freeze = step_interval_thresh / sensor_interval;            //system.out.println("onestepfreeze2:"+double.tostring(one_step_freeze)+"==========================");                /*spcounter.settext((int) step_counter);        }*/        /*if (one_step_freeze > 0) {            if ((system.currenttimemillis() - lastsaved) > acce_filter_data_min_time) {                lastsaved = system.currenttimemillis();                one_step_freeze = one_step_freeze - 1;                system.out.println("onestepfreeze2:" + double.tostring(one_step_freeze));            }        }        if (one_step_happend == 1) {             last_theta = accumulated_rad + last_theta;            double current_x = last_x + stride_length * math.cos(last_theta);            double current_y = last_y + stride_length * math.sin(last_theta);            c_x.settext((int) current_x);            c_y.settext((int)current_y);                 /*trace_x = [trace_x current_x];                 trace_y = [trace_y current_y];            last_x = current_x;            //system.out.println("last_x"+last_x);            last_y = current_y;            //system.out.println("last_y"+last_y);            accumulated_rad = 0;            one_step_happend = 0;        } *//*else {            accumulated_rad = accumulated_rad + z_gy * sensor_interval;            //system.out.println(accumulated_rad);               /*  calculate(accumulated_rad);         }    } 

*/

} @override protected void onresume() {     super.onresume();     // register class listener accelerometer sensor     sm.registerlistener(this, sm.getdefaultsensor(sensor.type_accelerometer),             sensormanager.sensor_delay_normal);     // ...and orientation sensor     sm1.registerlistener(this, sm1.getdefaultsensor(sensor.type_orientation),             sensormanager.sensor_delay_normal); }  @override protected void onstop() {     // unregister listener     sm.unregisterlistener(this);     super.onstop(); } @override public void onaccuracychanged(sensor sensor, int accuracy) {  } public class customdrawableview extends view {     static final int width = 50;     static final int height = 50;      public customdrawableview(context context)     {         super(context);          mdrawable = new shapedrawable(new ovalshape());         mdrawable.getpaint().setcolor(0xff74ac23);         mdrawable.setbounds((int) current_x, (int) current_y, (int) current_x + width, (int) current_y + height);         //mdrawable.setbounds((int)acc_x, (int)acc_y, (int)acc_x + width,(int) acc_y + height);     }      protected void ondraw(canvas canvas)     {        /* x= (int)current_x;         y= (int)current_y;*/         rectf oval = new rectf(mainactivity.x, mainactivity.y, mainactivity.x + width, mainactivity.y                 + height); // set bounds of rectangle         paint p = new paint(); // set paint options         p.setcolor(color.blue);         canvas.drawoval(oval, p);         invalidate();     } } 

}


Comments