speedometer problem

Put your problem here if it does not fit any of the other categories.

speedometer problem

Postby ashwani » Wed Mar 16, 2011 6:45 am

i am working on a speedometer application that uses accelerometer to get the speed. actually i am taking the sqrt of x,y,z coordinates to get the speed and then converting it into mph. i am using sensorsimulator, the problem here is that it dosen't updates or display speed change with changes in accelerometer value change. here is the source code:
Code: Select all
    package com.example.speedo;

    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;

    import org.openintents.sensorsimulator.hardware.Sensor;
    import org.openintents.sensorsimulator.hardware.SensorEvent;
    import org.openintents.sensorsimulator.hardware.SensorEventListener;
    import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.Log;
    import android.widget.TextView;

    public class Speedometer extends Activity implements SensorEventListener{
       /** Called when the activity is first created. */
       SensorManagerSimulator sensorManager;
       TextView textView;

       float appliedAcceleration = 0;
       float currentAcceleration = 0;
       float velocity = 0;
       Date lastUpdate;
       double calibration;
       boolean sensorrunning;
       Handler handler = new Handler();

       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          textView = (TextView) findViewById(R.id.myTextView);
          lastUpdate = new Date(System.currentTimeMillis());
          sensorManager = SensorManagerSimulator.getSystemService(this,
                SENSOR_SERVICE);
          sensorManager.registerListener(this,
                      sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                      SensorManagerSimulator.SENSOR_DELAY_NORMAL);
          sensorManager.connectSimulator();
          calibration = calibration - Double.NaN;
          sensorrunning = true;
          Timer updateTimer = new Timer("velocityUpdate");
          updateTimer.scheduleAtFixedRate(new TimerTask() {
             public void run() {
                updateGUI();
                Log.v((String)textView.getText(), "G");
             }
          }, 0, 1000);
       }

       private void updateGUI() {
          // Convert from m/s to mph
          final double mph = (Math.round(100 * velocity / 1.6 * 3.6)) / 100;
          // Update the GUI
          handler.post(new Runnable() {
             public void run() {
                Log.v((String)textView.getText(), "F");
                textView.setText(String.valueOf(mph) + "mph");
             }
          });
       }

       private void updateVelocity() {
          // Calculate how long this acceleration has been applied.
          Log.v("Status", "B");
          Date timeNow = new Date(System.currentTimeMillis());
          long timeDelta = timeNow.getTime() - lastUpdate.getTime();
          lastUpdate.setTime(timeNow.getTime());
          Log.v("Status", "C");
          // Calculate the change in velocity at the
          // current acceleration since the last update.
          float deltaVelocity = appliedAcceleration * (timeDelta / 1000);
          appliedAcceleration = currentAcceleration;
          // Add the velocity change to the current velocity.
          velocity += deltaVelocity;
          Log.v("Status", "D");
          // Convert from meters per second to miles per hour.
          double mph = (Math.round(velocity / 1.6 * 3.6));
          textView.setText(String.valueOf(mph) + "mph");
          Log.v("Status", "E");
       }   
       
       @Override
       public void onAccuracyChanged(Sensor arg0, int arg1) {
          // TODO Auto-generated method stub
       }   
       @Override
       public void onSensorChanged(SensorEvent event) {
          double x = event.values[0];
          double y = event.values[1];
          double z = event.values[2];
          double a = -1
                * Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
          if (calibration == Double.NaN){
             calibration = a;
             Log.v("Status", "NAN");
          }
          else {
             updateVelocity();
             currentAcceleration = (float) a;
             Log.v("Status", "A");
          }
       }
       @Override
        protected void onResume() {
          super.onResume();
          sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManagerSimulator.SENSOR_DELAY_NORMAL);
          sensorrunning=true;
        }
       
        /** Stop the updates when Activity is paused */
        @Override
        protected void onPause() {
          super.onPause();
          sensorManager.unregisterListener(this);
        }   
        @Override
        protected void onDestroy(){
          super.onDestroy();
          if(sensorrunning){
             sensorManager.unregisterListener(this);
          }
       }
    }


i had also created another version of this app to test it on real device but its not working on device also. please help.
ashwani
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 10, 2011 12:43 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: mobiforms and 12 guests