2012-11-07 14 views
5

Androidで走行速度センサーTYPE_LINEAR_ACCELEROMETERを計算しようとしましたが、結果は非常に不正確です。計算された平均加速度は常に正の値であるため、増加し続けます。ここに私の手順とコードがありますので、3軸加速度計のデータとコードが間違っている場所から正しい計算方法を教えてください。 、私はxの加速度値を取得していますアンドロイドで加速度センサーを使って走行速度を計算する方法

y、加速を結果として生じるz方向

a1 = sqrt(x*x + y*y + z*z)5加速測定値の

平均:私は何

がある

Avg(4) = `(a0 + a1 + a2 + a3 + a4)/5` 

時間デルタ:

Tdelta = (time of Avg(4)) - (time of Avg(0)) 

当初V(0)0あり、その後V(0)が故に、以前に計算された速度、次のとおりです。

V(1) = V(0) + at = 0 + Avg(1) 
V(2) = V(1) + at = V(1) + Avg(2) 
V(n) = V(n-1) + at = V(n-1) + Avg(n) 

これは私がベロシティ値を取得しています方法ですが、それは正しい速度ではありません。私を案内してください。

これはコードです:

public class TestCalculateVelocityActivity extends Activity implements OnClickListener, SensorEventListener { 

    final String TAG = getClass().getName().toString(); 
    SensorManager mSensorManager; 
    Sensor mAccelerometer; 
    TableLayout accTable; 
    TextView accl, spd, spd_kmph; 
    Button btnStart, btnStop, btnClear; 
    Timer updateTimer; 
    float []linearAcceleration = new float[3]; 
    Velocity velocity; 
    Handler handler; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     initSensor(); 

     accTable =(TableLayout)findViewById(R.id.country_table); 

     //accl = (TextView)findViewById(R.id.accl); 
     spd = (TextView)findViewById(R.id.spd); 
     spd_kmph = (TextView)findViewById(R.id.spd_kmph); 

     btnStart = (Button)findViewById(R.id.buttonStart); 
     btnStart.setOnClickListener(this); 
     btnStop = (Button)findViewById(R.id.buttonStop); 
     btnStop.setOnClickListener(this); 
     btnClear= (Button)findViewById(R.id.buttonClear); 
     btnClear.setOnClickListener(this); 
    } 

    private void initSensor() { 
     mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 
     mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); 
     if(mAccelerometer == null) { 
      Toast.makeText(this, "Accelerometer sensor not available", Toast.LENGTH_SHORT).show(); 
      finish(); 
     } 
    } 

    void fillTable(float values[]) { 

     float[] val = values; 
     TableRow row; 
     TextView t1, t2, t3; 
     //Converting to dip unit 
     int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
       (float) 1, getResources().getDisplayMetrics()); 

     //for (int current = 0; current < CountriesList.abbreviations.length; current++) { 
     row = new TableRow(this); 

     t1 = new TextView(this); 
     t1.setTextColor(Color.WHITE); 
     t1.setBackgroundColor(Color.GRAY); 
     t2 = new TextView(this); 
     t2.setTextColor(Color.WHITE); 
     t2.setBackgroundColor(Color.LTGRAY); 
     t3 = new TextView(this); 
     t3.setTextColor(Color.WHITE); 
     t3.setBackgroundColor(Color.GRAY); 

     t1.setText(""+val[0]); 
     t2.setText(""+val[1]); 
     t3.setText(""+val[2]); 

     t1.setTypeface(null, 1); 
     t2.setTypeface(null, 1); 
     t3.setTypeface(null, 1); 

     t1.setTextSize(15); 
     t2.setTextSize(15); 
     t3.setTextSize(15); 

     t1.setWidth(150 * dip); 
     t2.setWidth(150 * dip); 
     t3.setWidth(150 * dip); 
     t1.setPadding(20*dip, 0, 0, 0); 
     row.addView(t1); 
     row.addView(t2); 
     row.addView(t3); 

     accTable.addView(row, new TableLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    } 

    public void onClick(View v) { 

     if(v == btnStart) { 
      mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); 
      velocity = new Velocity(); 
      updateTimer = new Timer("velocityUpdate"); 
      handler = new Handler(); 
      updateTimer.scheduleAtFixedRate(new TimerTask() { 
       public void run() { 
        calculateAndUpdate(); 
       } 
      }, 0, 1200); 
     }else if(v == btnStop) { 
      mSensorManager.unregisterListener(this); 

      displayVelocityValues(); 
      displayVelocityTable(); 
      velocity = null; 
      handler = null; 
      updateTimer.cancel(); 


     } else if(v == btnClear) { 
      accTable.removeAllViews(); 
     } 
    } 

    private void displayVelocityTable() { 
     try { 
      accTable.removeAllViews(); 
      double[] vl = velocity.getVlArray(); 
      for(int i = 0; i<vl.length; i++) { 
       /*Log.d(TAG, "v = " + vl[i] + "mps, "+(vl[i] * 3.6)+ " kmph");*/ 


       //float[] val = values; 
       TableRow row; 
       TextView t1, t2; 
       //Converting to dip unit 
       int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
         (float) 1, getResources().getDisplayMetrics()); 

       //for (int current = 0; current < CountriesList.abbreviations.length; current++) { 
       row = new TableRow(this); 

       t1 = new TextView(this); 
       t1.setTextColor(Color.WHITE); 
       t1.setBackgroundColor(Color.GRAY); 
       t2 = new TextView(this); 
       t2.setTextColor(Color.WHITE); 
       t2.setBackgroundColor(Color.LTGRAY); 


       t1.setText(""+vl[i]); 
       t2.setText(""+(vl[i] * 3.6)); 


       t1.setTypeface(null, 1); 
       t2.setTypeface(null, 1); 


       t1.setTextSize(15); 
       t2.setTextSize(15); 

       t1.setWidth(200 * dip); 
       t2.setWidth(200 * dip); 

       t1.setPadding(20*dip, 0, 0, 0); 
       row.addView(t1); 
       row.addView(t2); 


       accTable.addView(row, new TableLayout.LayoutParams(
         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      } 
     } catch(NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void displayVelocityValues() { 
     try { 
      double[] vl = velocity.getVlArray(); 
      for(int i = 0; i<vl.length; i++) { 
       Log.d(TAG, "v = " + vl[i] + "mps, "+(vl[i] * 3.6)+ " kmph"); 
      } 
     } catch(NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void calculateAndUpdate() { 

     final double vel = velocity.getVelocity(linearAcceleration, System.currentTimeMillis()); 
     final double velKmph = vel * 3.6; 
     //spd.setText("v = "+ velKmph + " kmph"); 

     handler.post(new Runnable() { 
      public void run() { 

       //Log.d(getClass().getName().toString(), "Setting velocity = " + velKmph+ " kmph"); 
       spd.setText("v = "+ vel + " mps"); 
       spd_kmph.setText("v = "+ velKmph + " kmph"); 
      } 
     }); 
    } 



    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 

     linearAcceleration[0] = event.values[0]; 
     linearAcceleration[1] = event.values[1]; 
     linearAcceleration[2] = event.values[2];   

     fillTable(linearAcceleration); 
    } 
} 



public class Velocity { 

    private final String TAG = getClass().getName().toString(); 
    int sampleCounter = 0; 
    final int totalSamples = 5; 
    long time0, nAccel; 
    static int i=0; 
    double aDelT0 = 0, v0 = 0, v = 0; 

    final int totalVelocityValues = 1000; 
    double []velocityValues = new double[totalVelocityValues]; 

    //float []linearAcceleration = new float[3]; 

    //final int totalAccl = 5; 
    double []accel = new double[totalSamples]; 

    private double getAvg(double[] a) { 
     double total = 0; 
     for(int i = 0; i<a.length; i++) 
      total = total + a[i]; 
     return (total/a.length); 
    } 

    private double getAcceleration(float[] linearAcceleration) { 
     return Math.sqrt(Math.pow(linearAcceleration[0], 2) + Math.pow(linearAcceleration[0], 2) + Math.pow(linearAcceleration[0], 2)); 
    } 

    public double getVelocity(float[] linearAcceleration, long time1) { 

     //this.linearAcceleration = linearAcceleration; 

     try { 
      if(sampleCounter < (totalSamples-1)) { 
       if(sampleCounter == 0) 
        time0 = time1; 
       accel[sampleCounter] = getAcceleration(linearAcceleration);  
       sampleCounter++;  
      } else if(sampleCounter == (totalSamples-1)) { 
       accel[sampleCounter] = getAcceleration(linearAcceleration); 

       double avgAccel = getAvg(accel); 
       long timeDelta = ((time1 - time0)/1000); 
       double aDelT1 = (avgAccel * timeDelta); 
       Log.d(TAG, "aDelT1 = "+avgAccel +" * "+timeDelta + " = "+aDelT1); 

       v = calculateVelovity(aDelT1); 
       if(i !=totalVelocityValues) { 
        velocityValues[i]=v; 
        i++; 
       } else { 
        for(int j=0;j<(totalVelocityValues-1);j++) 
         velocityValues[j]=velocityValues[j+1]; 
        velocityValues[totalVelocityValues -1]=v; 
       } 
       sampleCounter = 0; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return v; 
    } 

    private double calculateVelovity(double aDelT1) { 
     double v = v0 + (aDelT1 - aDelT0); 
     Log.d(TAG, "v = "+v0+ "+ ("+aDelT1+" - "+aDelT0+") = "+v); 
     v0 = v; 
     aDelT0 = aDelT1; 
     return v; 
    } 



    public double[] getVlArray() { 
     return velocityValues; 
    } 

}

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/spd" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dip" 
     android:layout_weight="1" 
     android:background="@android:color/darker_gray" 
     android:gravity="center_vertical|center_horizontal" 
     android:text="speed (kmph)" 
     android:textColor="@android:color/white" 
     android:textSize="20dip" 
     android:textStyle="bold" 
     android:typeface="sans" /> 

    <TextView 
     android:id="@+id/spd_kmph" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dip" 
     android:layout_weight="1" 
     android:background="@android:color/darker_gray" 
     android:gravity="center_vertical|center_horizontal" 
     android:text="ooooo" 
     android:textColor="@android:color/white" 
     android:textSize="20dip" 
     android:textStyle="bold" 
     android:typeface="sans" /> 


    <TextView 
     android:text="Acceleration Data" 
      android:textColor="@android:color/white" 
      android:gravity="center_vertical|center_horizontal" 
      android:textSize="20dip" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:layout_marginBottom="5dip" 
      android:typeface="sans" 
      android:layout_weight="1" 
      android:background="@android:color/darker_gray"/> 


</LinearLayout> 

<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button android:id="@+id/buttonStart" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_weight="1" 
     android:text="Start" /> 

    <Button android:id="@+id/buttonClear" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_weight="1" 
     android:text="Clear" /> 

    <Button android:id="@+id/buttonStop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_weight="1" 
     android:text="Stop" /> 

</LinearLayout> 

<RelativeLayout android:id="@+id/rl_country_heading" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/darker_gray"> 

    <TextView android:id="@+id/tv_11" 
     android:layout_width="70dip" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="X" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/white" 
     android:textSize="18dip"> 
    </TextView> 

    <TextView android:id="@+id/tv_12" 
     android:layout_width="150dip" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Y" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/white" 
     android:textSize="18dip" 
     android:layout_toRightOf="@+id/tv_11"> 
    </TextView> 

    <TextView android:id="@+id/tv_13" 
     android:layout_width="150dip" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Z" 
     android:textStyle="normal|bold" 
     android:textColor="@android:color/white" 
     android:textSize="18dip" 
     android:layout_toRightOf="@+id/tv_12"> 
    </TextView> 
</RelativeLayout> 

<LinearLayout android:id="@+id/ll_country" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 

    <ScrollView android:id="@+id/ScrollView11" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true"> 

     <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dip"> 

      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:stretchColumns="0,1" 
       android:id="@+id/country_table" 
       android:background="@android:color/black"> 
      </TableLayout> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

+0

こんにちは@Gaurav gpsを使用するのではなく、加速度計を使用して速度を取得する方法を理解しましたか?あなたはいくつかの場所で同じ問題に直面しているいくつかのコードやサンプルを私が持っている場合私はGPSが利用できないときに速度を得ることができません – prasanthMurugan

答えて

1

あなたがgetAccelerationと加速度の合計大きさを働いて、それは常に正となるように、すべての方向情報を失います。

あなたはジャイロを使用する必要があるので、あなたは方向を取る必要があり、それは電話が捻ってターンすると変化します。

一般的な電話機のセンサの精度は非常に優れていますが、コードを正しく取得しても、非常に迅速に精度が失われます。

実行速度をgpsで見たい場合....

編集。

あなたの重力を考慮に入れるのを忘れてしまった。あなたは重力の影響を取り除かなければなりません。

+0

私は正常にGPSを使用して速度を持っている。 GPSデータが利用できない状況を会計処理するために加速度計を使用して計算したいと思います。 Sensor.TYPE_LINEAR_ACCELERATIONは、重力を除く加速度値を示します。 携帯電話で走っている間、センサーはあまりにも多くのショックやジャークを感知します。これらの条件を考慮して、実際の走行速度を計算することは可能ですか? センサー。TYPE_GYROSCOPEは回転率を示します。 GYROSCOPEまたはACCELEROMETERを使用して加速度の方向(正または負)を取得するためのサンプルコードがあります。 – Gaurav