Download presentation
Presentation is loading. Please wait.
1
Android sensors
2
List of sensors Make new app. In onCreate add Nexus One Sensors
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> list = sensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor s : list) { Log.e("SENSORS","name: "+s.getName()); } Nexus One Sensors BMA150 3-axis Accelerometer AK axis Magnetic field sensor AK8973 Orientation sensor CM3602 Proximity sensor CM3602 Light sensor Linear Acceleration Sensor Rotation Vector Sensor Nexus S Sensors KR3DM 3-axis Accelerometer AK axis Magnetic field sensor AK8973 Orientation sensor GP2A Proximity sensor GP2A Light sensor Linear Acceleration Sensor Rotation Vector Sensor K3G Gyroscope sensor Gravity Sensor API also supports pressure sensor (to help determine altitude)
3
compass New app: Compass Add member variables
SensorManager sensorManager; Sensor compassSensor; MyCompassListener myCompassListener = new MyCompassListener(); // we need to make this
4
MyCompassListener Make class Let eclipse add unimplemented functions
class MyCompassListener implements SensorEventListener {}; Let eclipse add unimplemented functions In onSensorChanged(SensorEvent event), add Log.e("compass","x: "+event.values[0]+"y: "+event.values[1]);
5
Register our sensor listener
In onCreate, get sensor and register listener sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> list = sensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor s : list) { Log.e("SENSORS","name: "+s.getName()+" tpye: "+s.getType()); if (s.getType()==Sensor.TYPE_MAGNETIC_FIELD) { Log.e("debuginfo","registering lin accel"); compassSensor = s; sensorManager.registerListener(myCompassListener, s, sensorManager.SENSOR_DELAY_FASTEST); } Run and see compass values
6
unregister and reregister
Sensors will keep on creating events, so we must stop the events when the app is not active In main activity @Override public void onPause() { super.onPause(); sensorManager.unregisterListener(myCompassListener); } public void onResume() { super.onResume(); sensorManager.registerListener(myCompassListener, compassSensor, sensorManager.SENSOR_DELAY_FASTEST);
7
Draw compass Recall simple drawing on a canvas lecture
In graphical editor, add “view” object. Save Go to xml and make the just made entry be of type edu.udel.eleg454.MyView instead of View This requires replacing view and /view Make new class MyView that extends view Add public MyView(Context context, AttributeSet attrs) { super(context, attrs); } public MyView(Context context) { super(context);
8
In MyView, add member variables
public float x=0; public float y=1; float scale; In onDraw, add Paint paint = new Paint(); paint.setDither(true); paint.setColor(Color.RED); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(2); Now draw the compass scale = (float) Math.sqrt(x*x+y*y); // normalize x = x/scale*50; y = y/scale*50; canvas.drawLine(100,100,100+y,100+x, paint);
9
In Compass (our main activity class)
In onCreate, get the view view = (MyView)findViewById(R.id.View01); In onSensorChanged(SensorEvent event), add view.x = event.values[0]; view.y = event.values[1]; view.invalidate(); // forces redraw run
10
More sensors - Proximity
Make new app (or maybe canalized Compass) Add member variables SensorManager sensorManager; Sensor sensor; MySensorListener mySensorListener = new MySensorListener (); // we need to make this In onCreate() add, if (s.getType()==Sensor.TYPE_PROXIMITY) { Log.e("debuginfo","registering lin accel"); compassSensor = s; sensorManager.registerListener(myCompassListener, s, sensorManager.SENSOR_DELAY_FASTEST); } Make MySensorListener as we made MyCompassListener In onSensorChanged add if (event.sensor.getType()==Sensor.TYPE_PROXIMITY) { Log.e("Proximity ","x: "+event.values[0]); Run Try with light sensor Sensor.TYPE_LIGHT
11
Linear Accelerometer Add local class
Make a linear accelerometer listener Register listener Add member variable MyLinearAccelerationListener myLinearAccelerationListener = new MyLinearAccelerationListener(); Add local class class MyLinearAccelerationListener implements SensorEventListener { }; Let eclipse add unimplemented methods Add class member variables double[] linear_acceleration = new double[3]; double[] location = new double[3]; double[] velocity = new double[3]; long lastTimeStamp = 0; double timeDif; In onSensorChanged(SensorEvent event) // make sure arg is called event if (event.sensor.getType()==10) { if (lastTimeStamp>0) { // to do: add a jetPlayer-based sounds timeDif = ((double)(event.timestamp - lastTimeStamp))/1e9; velocity[0] = velocity[0] + timeDif*event.values[0]; velocity[1] = velocity[1] + timeDif*event.values[1]; velocity[2] = velocity[2] + timeDif*event.values[2]; location[0] = location[0] + timeDif*velocity[0]; location[1] = location[1] + timeDif*velocity[1]; location[2] = location[2] + timeDif*velocity[2]; locationView.setText("location x: "+((double)((int)(location[0]*10.0)))/10.0+ " y: "+ ((double)((int)(location[1]*10.0)))/10.0+" z: "+((double)((int)(location[2]*10.0)))/10.0); velocityView.setText("velocity x: "+((double)((int)(velocity[0]*10.0)))/10.0+ " y: "+ ((double)((int)(velocity[1]*10.0)))/10.0+" z: "+((double)((int)(velocity[2]*10.0)))/10.0); } lastTimeStamp = event.timestamp;
12
accelerometer linear_acceleration[0] = event.values[0] - gravity[0];
Challenge: remove gravity from measurement 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]; scale = Math.sqrt(gravity[0]*gravity[0] + gravity[1]*gravity[1] + gravity[2]*gravity[2])/SensorManager.GRAVITY_EARTH; gravity[0] = gravity[0]/scale; gravity[1] = gravity[1]/scale; gravity[2] = gravity[2]/scale; linear_acceleration[0] = event.values[0] - gravity[0]; linear_acceleration[1] = event.values[1] - gravity[1]; linear_acceleration[2] = event.values[2] - gravity[2]; SAME as linear accelerometer
13
todo Rotation sensor or gyroscope Gesture recognition
Show opengl image that rotates as phone rotates E.g., see navigator Allow phone to work as joystick Gesture recognition Hidden markov models Location tracking Keep history Gryoscope (to track gravity) Statistical signal processing
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.