2

我正在使用 android 传感器数据过滤。同时,我不擅长JAVA。它需要将“FixFilter”类与启动 ENS492SensorsActivity 的主要源代码集成。在这种方法中,我的目标是过滤取自加速度计和方向传感器的数据。acx 是在 X 方向上从加速度计获取的数据,而 orx 是从 X 方向上的方向数据中获取的数据。

public float FixFilter (double acx, float orx)
{

orx = 0.75*(orx) + 0.25*(acx);
return orx;

}


public class Ens492SenorsActivity extends Activity implements SensorListener
{
    final String tag = "IBMEyes";
    SensorManager sm = null;

    TextView xViewA = null;
    TextView yViewA = null;
    TextView zViewA = null;
    TextView xViewO = null;
    TextView yViewO = null;
    TextView zViewO = null;

    //for gps
    TextView xView1 = null;
    TextView yView1 = null;
    TextView zView1 = null;


    private LocationManager locManager;
    private LocationListener locListener = new MyLocationListener();



    private boolean gps_enabled = false;
    private boolean network_enabled = false;
    private Timer mTimer = new Timer();
    private int REFRESH_TIME = 1000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    setContentView(R.layout.main);
    xViewA = (TextView) findViewById(R.id.xbox);
    yViewA = (TextView) findViewById(R.id.ybox);
    zViewA = (TextView) findViewById(R.id.zbox);
    xViewO = (TextView) findViewById(R.id.xboxo);
    yViewO = (TextView) findViewById(R.id.yboxo);
    zViewO = (TextView) findViewById(R.id.zboxo);



    xView1 = (TextView) findViewById(R.id.xbox1);
    yView1 = (TextView) findViewById(R.id.ybox1);
    zView1 = (TextView) findViewById(R.id.zbox1);

    locManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
    init_arr();
}
public static void connect(String url)
{

    HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet(url); 

    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
        // Examine the response status
        Log.i("Praeda",response.getStatusLine().toString());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release

        if (entity != null) {

            // A Simple JSON Response Read
            InputStream instream = entity.getContent();
            String result= convertStreamToString(instream);
            // now you have the string representation of the HTML request
            instream.close();
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
}
private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
public void ShowMap(View v)
{
    String uri = String.format("geo:%f,%f", alig.getLatitude(), alig.getLongitude());
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    this.startActivity(intent);
}
public double DoruksKalman (double v1, double v2, double v3,double v4,double v5,double v6,double v7,double v8,double v9,double v10 )
{
    double result = 0;
    //result = 10 / ( 1/ v1 + 1/ v2 +1/ v3 +1/ v4 +1/ v5 +1/ v6 +1/ v7 +1/ v8 +1/ v9 +1/ v10);
    result = (v1 + v2 + v3 + v4 +v5 + v6 +v7 + v8 +v9 + v10)/10;
    return result;
}

public float FixFilter (double acx, float orx)
{

    orx = 0.75*(orx) + 0.25*(acx);
    return orx;

}


public void SendToServer(View v)
{
    Log.i(getClass().getSimpleName(), "send  task - start");
    try{
        connect("http://192.168.1.117/ens4912/process.php?ip=&lat=" +
                DoruksKalman( 
                        alig.getLatitude(), alig.getLatitude(),alig.getLatitude(),
                        alig.getLatitude(),alig.getLatitude(),alig.getLatitude(),
                        alig.getLatitude(),alig.getLatitude(),alig.getLatitude(),
                        alig.getLatitude())+ 
        "&lng="+
        DoruksKalman(alig.getLongitude(), alig.getLongitude(),alig.getLongitude(), alig.getLongitude(), 
                alig.getLongitude(), alig.getLongitude(), alig.getLongitude(),
                alig.getLongitude(), alig.getLongitude(), alig.getLongitude())+
        "&alt="+
        DoruksKalman(alig.getAltitude(),alig.getAltitude(),alig.getAltitude(),
                alig.getAltitude(),alig.getAltitude(),alig.getAltitude(),
                alig.getAltitude(),alig.getAltitude(),alig.getAltitude(),alig.getAltitude())+
        "&accel="+
        DoruksKalman(acx_arr[0], acx_arr[1],acx_arr[2],acx_arr[3],acx_arr[4],
                acx_arr[5],acx_arr[6],acx_arr[7],
                acx_arr[8],acx_arr[9])+
        "&mag="+
        DoruksKalman(orx_arr[0], orx_arr[1], orx_arr[2], orx_arr[3],
                orx_arr[4],orx_arr[5],orx_arr[6],
                orx_arr[7],orx_arr[8],orx_arr[9])
        );//"http://www.google.com");
    }
    catch (Exception e)
    {
        double zero =0.0;
        e.printStackTrace();
         connect("http://192.168.1.117/ens4912/process.php?ip=&lat=" +
                 zero+ 
                    "&lng="+
                    zero+
                    "&alt="+
                    zero+
                    "&accel="+
        DoruksKalman(acx_arr[0], acx_arr[1],acx_arr[2],acx_arr[3],acx_arr[4],
                acx_arr[5],acx_arr[6],acx_arr[7],
                acx_arr[8],acx_arr[9])+
        "&mag="+
        DoruksKalman(orx_arr[0], orx_arr[1], orx_arr[2], orx_arr[3],
                orx_arr[4],orx_arr[5],orx_arr[6],
                orx_arr[7],orx_arr[8],orx_arr[9])
                    );
    }
    Log.i(getClass().getSimpleName(), "send  task - end");
}

float  orx;
float  ory;
float  orz;
float acx;
float acy;
float acz;

float [] acx_arr = new float[10];
float [] acy_arr = new float[10];
float [] acz_arr = new float[10];

float [] orx_arr = new float[10];
float [] ory_arr = new float[10];
float [] orz_arr = new float[10];

private void init_arr()
{
    for(int i = 0;i <10;i++)
    {
        acx_arr[i] = 1.0f;
         acy_arr[i] = 1.0f;
        acz_arr[i] = 1.0f;

        orx_arr[i] = 1.0f;
         ory_arr[i] = 1.0f;
         orz_arr[i] = 1.0f;
    }

}

int sensorReadCount = 0;
private final float NOISE = (float) 2.0;
private float mLastX, mLastY, mLastZ;
private boolean mInitialized = false;



public void onSensorChanged(int sensor, float[] values) {
    Boolean writeIt = false;
    synchronized (this) {
        //Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
        if (sensor == SensorManager.SENSOR_ORIENTATION) {
            xViewO.setText("Orientation X: " + 
                    DoruksKalman(orx_arr[0], orx_arr[1],orx_arr[2],orx_arr[3],orx_arr[4],
                            orx_arr[5],orx_arr[6],orx_arr[7],
                            orx_arr[8],orx_arr[9]));
            yViewO.setText("Orientation Y: " +
                            DoruksKalman(ory_arr[0], ory_arr[1],ory_arr[2],ory_arr[3],ory_arr[4],
                    ory_arr[5],ory_arr[6],ory_arr[7],
                    ory_arr[8],ory_arr[9]));
            zViewO.setText("Orientation Z: " + 
                    DoruksKalman(orz_arr[0], orz_arr[1],orz_arr[2],orz_arr[3],orz_arr[4],
                            orz_arr[5],orz_arr[6],orz_arr[7],
                            orx_arr[8],orx_arr[9]));
            orx = values[0];
            ory = values[1];
            orz = values[2];

            orx_arr[sensorReadCount%10] = orx;
            ory_arr[sensorReadCount%10] = ory;
            orz_arr[sensorReadCount%10] = orz;
        }
        if (sensor == SensorManager.SENSOR_ACCELEROMETER) {



            acx = values[0];
            acy = values[1];
            acz = values[2];

            if (!mInitialized) {
                mLastX = acx;
                mLastY = acy;
                mLastZ = acz;
                mInitialized = true;
                }
            else {
                float deltaX = Math.abs(mLastX - acx);
                float deltaY = Math.abs(mLastY - acy);
                float deltaZ = Math.abs(mLastZ - acz);
                if (deltaX < NOISE) deltaX = (float)0.0;
                if (deltaY < NOISE) deltaY = (float)0.0;
                if (deltaZ < NOISE) deltaZ = (float)0.0;
                mLastX = acx;
                mLastY = acy;
                mLastZ = acz;
                if (deltaX > deltaY) {
                //iv.setImageResource(R.drawable.horizontal);
                    writeIt = false;
                } else if (deltaY > deltaX) {
                //iv.setImageResource(R.drawable.vertical);
                    writeIt = true;
                } else {
                //iv.setVisibility(View.INVISIBLE);
                    writeIt = false;
                }
            }
            xViewA.setText("Accel X: " + acx);
            yViewA.setText("Accel Y: " + acy);
            zViewA.setText("Accel Z: " + acz);
            /*
            xViewA.setText("Accel X: " + 
                    DoruksKalman(acx_arr[0], acx_arr[1],acx_arr[2],acx_arr[3],acx_arr[4],
                            acx_arr[5],acx_arr[6],acx_arr[7],
                            acx_arr[8],acx_arr[9]));
            yViewA.setText("Accel Y: " + 
                    DoruksKalman(acy_arr[0], acy_arr[1],acy_arr[2],acy_arr[3],acy_arr[4],
                            acy_arr[5],acy_arr[6],acy_arr[7],
                            acy_arr[8],acy_arr[9]));
            zViewA.setText("Accel Z: " +
                    DoruksKalman(acz_arr[0], acz_arr[1],acz_arr[2],acz_arr[3],acz_arr[4],
                            acz_arr[5],acz_arr[6],acz_arr[7],
                            acz_arr[8],acz_arr[9]));

            */
            acx_arr[sensorReadCount%10] = acx;
            acy_arr[sensorReadCount%10] = acy;
            acz_arr[sensorReadCount%10] = acz;


        }            
    }
    try
    {
        if(sensorReadCount % 10 == 0 && writeIt)// sensorReadCount <= 60 * 10 /* write for 10 minutes*/ )
        {
                WriteToTXT();
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    sensorReadCount++;
}
 public Boolean IsItBumb()
 {
     return true;
 }

public void onAccuracyChanged(int sensor, int accuracy) {
    Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);

}
int id = 0;
private void WriteToTXT()
{
    id++;
    // TO DO 
    // Buray\FD dolduracaks\FDn\FDz

    //sat\FDr sat\FDr
    //id - lat - lon -alt - magx - magy - magz - accx - accy -accz - time

    double lat;
    double lon;
    double alt;
    double speed;
    try
    {
         lat = alig.getLatitude();
         lon = alig.getLongitude();
         alt = alig.getAltitude();
         speed = alig.getSpeed();
    }
    catch( Exception e)
    {
        lat = 0.0;
        lon = 0.0;
        alt = 0.0;
        speed = 0.0;
    }



    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String currentDateandTime = sdf.format(new Date());

    String fileName = "doruks_txt";
    /*
    String sBody = id + " " + lat +  " " +  lon +  " " + alt + " " +  orx + " " +  ory + " " +
            orz + " " +  acx + " " +  acy + " " +  acz + " " + speed + " " +  currentDateandTime;
    */
    String sBody = lat +  "|" +  lon +  "|" + alt + " " +  orx + " " +  ory + " " +
            orz + " " +  acx + " " +  acy + " " +  acz + " " + speed + " " +  currentDateandTime + "|" +  id;

    generateNoteOnSD(fileName, sBody);
    appendLog(sBody);
}
public void appendLog(String text)
{       
   File logFile = new File("sdcard/mylog.file");
   if (!logFile.exists())
   {
      try
      {
         logFile.createNewFile();
      } 
      catch (IOException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   try
   {
      //BufferedWriter for performance, true to set append to file flag
      BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
      buf.append(text);
      buf.newLine();
      buf.close();
   }
   catch (IOException e)
   {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
}

public void generateNoteOnSD(String sFileName, String sBody)
{
    try
    {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) 
        {
            root.mkdirs();
        }

        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();

        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    }
}   


@Override
protected void onResume() {
    super.onResume();
    sm.registerListener(this, 
            SensorManager.SENSOR_ORIENTATION |
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onStop() {
    sm.unregisterListener(this);
    super.onStop();
}    

    public Location alig = null;
    class MyLocationListener implements LocationListener {

        public Location gLoc  = null;
        public Location mLoc = null;
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                synchronized (this) {
                // This needs to stop getting the location data and save the battery power.
                    //locManager.removeUpdates(locListener); 

                    alig = location;
                    String longitude = "Longitude: " + location.getLongitude();
                    String latitude = "Latitude: " + location.getLatitude();
                    String altitiude = "Altitiude: " + location.getAltitude();
                    String accuracy = "Accuracy: " + location.getAccuracy();
                    String time = "Time: " + location.getTime();

                    //editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + time);
                    //progress.setVisibility(View.GONE);

                    xView1.setText("X: " + longitude);
                    yView1.setText("Y: " + latitude);
                    zView1.setText("Z: " + altitiude);
                }
            }           
        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }
}
4

0 回答 0