martedì 17 gennaio 2017

Grafico realtime dellaccelerometro su Android

Un esempio su come visualizzare dati realtime dell'accelerometro su Android


E' stata impiegata la libreria GraphView (per aggiungere la libreria, invece di usare Gradle, ho scaricato il file .jar, ho creato una directory libs nel progetto con Project Clean finale)

--------------------------------
package com.luca_innocenti.wristcoach;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.MessageEvent;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.DataPointInterface;
import com.jjoe64.graphview.series.LineGraphSeries;


public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private String receivedMessage = null;
    private static final String TAG = "WearMainActivity";

    private SensorManager sensorManager;
    private long lastUpdate;
    private double graph2LastXValue = 5d;
    private LineGraphSeries<DataPointInterface> mSeries;
    private LineGraphSeries<DataPointInterface> mSeries2;
    private LineGraphSeries<DataPointInterface> mSeries3;


    private double[] linear_acceleration = new double[3];
    private double[]  gravity = new double[3];


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
        MessageReceiver messageReceiver = new MessageReceiver();
        LocalBroadcastManager.getInstance(getApplication()).registerReceiver(messageReceiver, messageFilter);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        lastUpdate = System.currentTimeMillis();

        GraphView graph = (GraphView) findViewById(R.id.graph);

        mSeries = new LineGraphSeries<>();
        mSeries2 = new LineGraphSeries<>();
        mSeries3 = new LineGraphSeries<>();



        mSeries.setTitle("AX");
        mSeries.setColor(Color.GREEN);
        //mSeries.setDrawDataPoints(true);        //mSeries.setDataPointsRadius(10);        mSeries.setThickness(2);

        mSeries2.setTitle("AY");
        mSeries2.setColor(Color.RED);
        //mSeries2.setDrawDataPoints(true);        //mSeries2.setDataPointsRadius(10);        mSeries2.setThickness(2);

        mSeries2.setTitle("AZ");
        mSeries2.setColor(Color.RED);
        //mSeries2.setDrawDataPoints(true);        //mSeries2.setDataPointsRadius(10);        mSeries2.setThickness(2);

        graph.addSeries(mSeries);
        graph.addSeries(mSeries2);
        graph.addSeries(mSeries3);
        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setMinX(0);
        graph.getViewport().setMaxX(40);

        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMinY(-7);
        graph.getViewport().setMaxY(7);

        graph.getLegendRenderer().setVisible(true);
        graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);


    }

    public class MessageReceiver extends BroadcastReceiver {
        private static final String TAG = "MessageReceiver";

        @Override        public void onReceive(Context context, Intent intent) {

            receivedMessage = intent.getStringExtra("message");
            Log.d("MessageReceiver", "onReceive() receivedMessage = " + receivedMessage);

            if (receivedMessage != null) {
                // Display message in UI                //message = receivedMessage;                Log.d("MessageReceiver", "receivedMessage =" + receivedMessage);
            } else {
                receivedMessage = "No Message";
                Log.d("MessageReceiver", "receivedMessage = No Message");
            }

        }
    }


    @Override    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            getAccelerometer(event);
        }

    }

    @Override    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        //To change body of implemented methods use File | Settings | File Templates.    }

    private void getAccelerometer(SensorEvent event) {
        float[] values = event.values;
        // Movement        float x = values[0];
        float y = values[1];
        float z = values[2];
        final double alpha = 0.8;

        // Isolate the force of gravity with the low-pass filter.        gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
        gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
        gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

        // Remove the gravity contribution with the high-pass filter.        linear_acceleration[0] = event.values[0] - gravity[0];
        linear_acceleration[1] = event.values[1] - gravity[1];
        linear_acceleration[2] = event.values[2] - gravity[2];

        String strTmp = "Acc.\n"                + " X: " + linear_acceleration[0] + "\n"                + " Y: " + linear_acceleration[1] + "\n"                + " Z: " + linear_acceleration[2];
        Log.d(TAG,strTmp);
        Double risultante = Math.sqrt(((linear_acceleration[0]*linear_acceleration[0])+(linear_acceleration[1]*linear_acceleration[1])+(linear_acceleration[1]*linear_acceleration[1])));

        long actualTime = System.currentTimeMillis();
        graph2LastXValue += 1d;
        mSeries.appendData(new DataPoint(graph2LastXValue, linear_acceleration[0]), true, 100);
        mSeries2.appendData(new DataPoint(graph2LastXValue, linear_acceleration[1]), true, 100);
        mSeries3.appendData(new DataPoint(graph2LastXValue, linear_acceleration[2]), true, 100);


        Log.d("luca",Double.toString(risultante));

    }



    @Override    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_FASTEST);
    }
    @Override    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }
}

Nessun commento:

Posta un commento

Debugger integrato ESP32S3

Aggiornamento In realta' il Jtag USB funziona anche sui moduli cinesi Il problema risiede  nell'ID USB della porta Jtag. Nel modulo...