Accessing the Accelerometer

Tutorials with advanced 'difficulty' and more Lines of Code.

is there a acclerometer physics library somewhere

Postby schtief » Sun Dec 21, 2008 2:08 pm

hi i'm searching for a physics library for th accelerometer, is there som code that calculates my chnaging positions?
schtief
Freshman
Freshman
 
Posts: 8
Joined: Fri Aug 29, 2008 2:13 pm

Top

Where to get the lib android.hard.Sensors -New to Android

Postby caldinay » Thu Feb 05, 2009 8:24 am

Didn't see it from the SDK.
Thx a lot.
caldinay
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Feb 05, 2009 8:20 am

Postby MrSnowflake » Thu Feb 05, 2009 12:57 pm

There isn't but you could do this yourself, though as it's dead reckoning, it won't be very accurate.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Sensors in sdk 1.0

Postby k2thedevil » Sat Feb 07, 2009 3:49 am

No tutorial on sensors in sdk 1.0... someone help!
Bug Out!
k2thedevil
Freshman
Freshman
 
Posts: 4
Joined: Fri Jan 30, 2009 9:46 pm

Postby mohamed.adel » Mon Feb 16, 2009 2:43 pm

I couldnt run this code

the error is Sensor is not resolved

any one help
mohamed.adel
Freshman
Freshman
 
Posts: 9
Joined: Mon Feb 16, 2009 9:14 am

Sensor is not resolved

Postby sebastian » Tue Feb 24, 2009 4:50 pm

Mohamed: seems that in the sdk Sensor class does not come in Android API, I solved it by copying code from API Demos at os.hardware.sensors package. Please check it out.
sebastian
Freshman
Freshman
 
Posts: 6
Joined: Fri Feb 20, 2009 10:47 am

Top

Postby mohamed.adel » Tue Feb 24, 2009 6:09 pm

thanks sebastian

I fix this poblem, but now I think I face another big problem that

I want to get speed by ACCELEROMETER, I tried alot of codes but they do not work well

so any one can help
mohamed.adel
Freshman
Freshman
 
Posts: 9
Joined: Mon Feb 16, 2009 9:14 am

Postby Drainman » Tue Feb 24, 2009 10:07 pm

I have this code that i made to test the accelerometer out. I'll just paste it here. Hope it helps someone.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.goodcausegames.android.sldroid;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.content.Context;
  9.  
  10. import android.hardware.SensorListener;
  11.  
  12. import android.hardware.SensorManager;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.widget.TextView;
  17.  
  18.  
  19.  
  20.  
  21.  
  22. public class sldroid extends Activity{
  23.  
  24.  
  25.  
  26.         private TextView mTxtView;
  27.  
  28.          
  29.  
  30.         private SensorManager mSensorManager;
  31.  
  32.        
  33.  
  34.         @Override
  35.  
  36.         protected void onCreate(Bundle savedInstanceState) {
  37.  
  38.                 // TODO Auto-generated method stub
  39.  
  40.                 super.onCreate(savedInstanceState);
  41.  
  42.                
  43.  
  44.                 mTxtView = new TextView(this);
  45.  
  46.                 setContentView(mTxtView);
  47.  
  48.                
  49.  
  50.                 mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  51.  
  52.         }
  53.  
  54.        
  55.  
  56.         public void updateTV(float p_x, float p_y, float p_z)
  57.  
  58.         {
  59.  
  60.                 mTxtView.setText("x: "+Math.round(p_x)+", y: "+Math.round(p_y)+", z: "+Math.round(p_z));
  61.  
  62.         }
  63.  
  64.        
  65.  
  66.         private final SensorListener mSensorListener = new SensorListener() {
  67.  
  68.                
  69.  
  70.                 public void onSensorChanged(int sensor, float[] values)
  71.  
  72.                 {
  73.  
  74.                         updateTV(values[SensorManager.DATA_X],
  75.  
  76.                                         values[SensorManager.DATA_Y],
  77.  
  78.                                         values[SensorManager.DATA_Z]);
  79.  
  80.                 }
  81.  
  82.                
  83.  
  84.                 public void onAccuracyChanged(int sensor, int accuracy) {}
  85.  
  86.         };
  87.  
  88.        
  89.  
  90.         @Override
  91.  
  92.         protected void onResume()
  93.  
  94.         {
  95.  
  96.                 super.onResume();
  97.  
  98.                 mSensorManager.registerListener(mSensorListener,
  99.  
  100.                                 SensorManager.SENSOR_ORIENTATION,
  101.  
  102.                                 SensorManager.SENSOR_DELAY_GAME);
  103.  
  104.         }
  105.  
  106.        
  107.  
  108.         @Override
  109.  
  110.         protected void onStop()
  111.  
  112.         {
  113.  
  114.                 mSensorManager.unregisterListener(mSensorListener);
  115.  
  116.                 super.onStop();
  117.  
  118.         }
  119.  
  120.  
  121.  
  122. }
  123.  
  124.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Drainman
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 24, 2009 10:03 pm
Location: sweden

Postby mohamed.adel » Wed Feb 25, 2009 8:43 am

hi guys,

what about using ACCELEROMETER to get speed, I do alot of trails, but the result was wrong :(

So any one can help

I got two Android books, I hope they will be helpfulllllll :)

http://rapidshare.com/files/201333683/P ... siderg.rar
http://rapidshare.com/files/202019050/M ... ok-DDU.rar

Good Luck
Mohamed Adel
mohamed.adel
Freshman
Freshman
 
Posts: 9
Joined: Mon Feb 16, 2009 9:14 am

Postby Drainman » Wed Feb 25, 2009 10:01 am

You cannot get speed from the accelerometer. The accelerometer is a sensor to get the angel of the phone it has nothing to do with forward speed other than the speed you can get of how fast it changes angel.
Drainman
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 24, 2009 10:03 pm
Location: sweden

Postby mohamed.adel » Sat Mar 14, 2009 9:41 am

Drainman wrote:You cannot get speed from the accelerometer. The accelerometer is a sensor to get the angel of the phone it has nothing to do with forward speed other than the speed you can get of how fast it changes angel.


Sorry Drainman, The accelerometer sensor is used to get the acceleration in 3D (x, y, z), and the orientation sensor is the one that is responsible for the angle of the phone

See that: http://developer.android.com/reference/ ... tener.html
mohamed.adel
Freshman
Freshman
 
Posts: 9
Joined: Mon Feb 16, 2009 9:14 am

Postby pizzlefou » Fri May 15, 2009 11:24 am

Hello everyone,

Well ive been reading the forum for quite a time now, and it's very usefull when you get started to android.
Im curently working on a application based on a home made widget (compass). Im confronted with a shit loads of problem but that's what programming is right :-) but i have a major ISSUE for like 2 days now with the the Orientation sensor.

Right, so, what i'm trying to do for now is to get the sensor working for a start. So ive created a small programm that just shows the X, Y and Z values corresponding to the sensor. But with the new Android 1.5 the SensorListener interface is now depecrated and we have to use a new interface SensorEvenListener. Also in the class SensorManager some updates has been done like the registerListener method that is no longer
public boolean registerListener (SensorListener listener, int sensors) but now
public boolean registerListener (SensorListener listener, int sensors, int rate)

So considering the only methods to get a sensor is by using the public Sensor getDefaultSensor (int type)
or the public List<Sensor> getSensorList (int type) it's like hard to get trough this.

So here is what ive been writing for my compass class (not the app that display the values of the sensors)


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.hardware.SensorManager;
  7. import android.os.Bundle;
  8. import android.widget.TextView;
  9.  
  10.  
  11. public class Boussole extends Activity implements SensorEventListener {
  12.    
  13.         float pitch = 0;
  14.         float roll = 0;
  15.         float heading = 0;
  16.         Compass compass;
  17.         SensorManager sensorManager;
  18.        
  19.         private void updateOrientation(float mRoll, float mPitch, float mHeading){
  20.                 roll = mRoll;
  21.                 pitch = mPitch;
  22.                 heading = mHeading;
  23.                 if (compass != null){
  24.                         compass.setBearing(heading);
  25.                         compass.setPitch(pitch);
  26.                         compass.setRoll(roll);
  27.                         compass.invalidate();
  28.                 }
  29.                
  30.         }
  31.        
  32.        
  33.        
  34.     @Override
  35.     public void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.main);
  38.         //setContentView(R.layout.secondview);
  39.         compass = (Compass)this.findViewById(R.id.compassView);
  40.         sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  41.       updateOrientation(0, 0, 0);
  42.         //SensorEvent event = null;
  43.         //getOrientation(event.values[SensorManager.DATA_X], event.values[SensorManager.DATA_Y], event.values[SensorManager.DATA_Z]);
  44.        
  45.         //afficher(event.values[SensorManager.DATA_X]);
  46.         //afficher(event.values[SensorManager.DATA_Y]);
  47.         //afficher(event.values[SensorManager.DATA_Z]);
  48.        
  49.     }
  50.    
  51.     protected void onResume()
  52.     {
  53.       super.onResume();
  54.       sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
  55.     }
  56.  
  57.     protected void onStop()
  58.     {
  59.       sensorManager.unregisterListener(this);
  60.       super.onStop();
  61.     }
  62.  
  63.  
  64.         @Override
  65.         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  66.         }
  67.  
  68.  
  69.  
  70.         @Override
  71.         public void onSensorChanged(SensorEvent event) {
  72.                 updateOrientation(event.values[SensorManager.DATA_X], event.values[SensorManager.DATA_Y], event.values[SensorManager.DATA_Z]);
  73.                
  74.         }
  75.        
  76.         /*public float[] getOrientation(float mRoll, float mPitch, float mHeading){
  77.                 float[] resultat = {0,0,0};
  78.                 resultat[1] = mRoll;
  79.                 resultat[2] = mPitch;
  80.                 resultat[3] = mHeading;
  81.                 return resultat;
  82.         }
  83.        
  84.         public void afficher(float valeur){
  85.                 TextView tv= new TextView(this);
  86.                 tv.setText(Float.toString(valeur));
  87.                 setContentView(tv);
  88.         }*/
  89. }
  90.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4



And when i launch my app, just nothing happens on the screen (black screen bleh :| ).
I would be very pleased if someone can tell me what's wrong in this code.

Cheers

Quentin
pizzlefou
Freshman
Freshman
 
Posts: 2
Joined: Fri May 15, 2009 11:06 am

Postby mako » Sun May 17, 2009 8:44 am

@drainman: thanks for submitting your code, it is useful and clear. but when studying the following code:

Drainman wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         protected void onResume()
  3.         {
  4.                 super.onResume();
  5.                 mSensorManager.registerListener(mSensorListener,
  6.                                 SensorManager.SENSOR_ORIENTATION,
  7.                                 SensorManager.SENSOR_DELAY_GAME);
  8.         }
  9.        
  10.         @Override
  11.         protected void onStop()
  12.         {
  13.                 mSensorManager.unregisterListener(mSensorListener);
  14.                 super.onStop();
  15.         }
  16.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

and thinking about the life cycle of an application I am wondering if it would not be better to unregister the listener in onPause() ?
mako
Freshman
Freshman
 
Posts: 9
Joined: Sun May 03, 2009 6:45 am

Postby fundurianfred » Wed May 20, 2009 3:15 am

Guys, I cant figure this accererometer out,

I build a game that control the direction of fish using accererometer

this is my code

public void onSensorChanged(int sensor, float[] values) {
if (sensor == SENSOR_ACCELEROMETER) {

float x = values[DATA_X];
float y = values[DATA_Y];
float z = values[DATA_Z];

myview1.fishmove(-x, -y);
}
}


What actually happen when the game goes into the real device is that the x and y will sometime be upsidedown, x become y, y become x, and sometimes the x will become -x, y will become -y

it just keep on flipping up down left right randomly, if someone know how to fix please help, I will be very grateful
fundurianfred
Freshman
Freshman
 
Posts: 7
Joined: Sun May 17, 2009 9:20 am

actions depending on the positions of the phone

Postby PI9 » Wed May 20, 2009 2:43 pm

Hello everyone !
I'm trying to use the accelerometer in order to :
- make the phone vibrate when it's horizontal, screen down
- make the voice of the person i'm calling go out by earpiece (in reference to route_ear_piece in audio manager) when the phone is vertical
- make the voice of the person i'm calling go out by spekaers so that everyone can hear the conversation (in reference to route_speaker in audio manager) when the phone is horizontal, screen up

I wrote a program but when I'm launching it on the devphone it "stops unexpectedly" ... Here it is :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class accel_vol extends Activity{
  3.  
  4.  
  5.  
  6.      private TextView mTxtView;
  7.  
  8.          private String status = null;
  9.  
  10.  
  11.  
  12.         private AudioManager mAudioManager;
  13.  
  14.     private SensorManager mSensorManager;
  15.  
  16.     private  int vol_max_speaker=mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
  17.  
  18.  
  19.  
  20.      @Override
  21.  
  22.         public void onCreate(Bundle savedInstanceState) {
  23.  
  24.           // TODO Auto-generated method stub
  25.  
  26.           super.onCreate(savedInstanceState);
  27.  
  28.          
  29.  
  30.           mTxtView = new TextView(this);
  31.  
  32.           setContentView(mTxtView);
  33.  
  34.           mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  35.  
  36.           mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  37.  
  38.          
  39.  
  40.  
  41.  
  42.  
  43.  
  44.      }
  45.  
  46.      
  47.  
  48.      public void updateTV(float p_x, float p_y, float p_z, String status2)
  49.  
  50.      {
  51.  
  52.                 status=positiontest(p_y);
  53.  
  54.           mTxtView.setText("x: "+Math.round(p_x)+", y: "+Math.round(p_y)+", z: "+Math.round(p_z)+"\n"+status);
  55.  
  56.          
  57.  
  58.      }
  59.  
  60.      
  61.  
  62.      private final SensorListener mSensorListener = new SensorListener() {
  63.  
  64.          
  65.  
  66.           public void onSensorChanged(int sensor, float[] values)
  67.  
  68.           {
  69.  
  70.                   status=positiontest(SensorManager.DATA_Y);
  71.  
  72.                updateTV(values[SensorManager.DATA_X],
  73.  
  74.                          values[SensorManager.DATA_Y],
  75.  
  76.                          values[SensorManager.DATA_Z],
  77.  
  78.                          status);
  79.  
  80.  
  81.  
  82.           }
  83.  
  84.          
  85.  
  86.           public void onAccuracyChanged(int sensor, int accuracy) {}
  87.  
  88.      };
  89.  
  90.      
  91.  
  92.     @Override
  93.  
  94.      protected void onResume()
  95.  
  96.      {
  97.  
  98.           super.onResume();
  99.  
  100.           mSensorManager.registerListener(mSensorListener,
  101.  
  102.                     SensorManager.SENSOR_ORIENTATION,
  103.  
  104.                     SensorManager.SENSOR_DELAY_GAME);
  105.  
  106.      }
  107.  
  108.      
  109.  
  110.      @Override
  111.  
  112.      protected void onStop()
  113.  
  114.      {
  115.  
  116.           mSensorManager.unregisterListener(mSensorListener);
  117.  
  118.          super.onStop();
  119.  
  120.      }
  121.  
  122.  
  123.  
  124.      private String positiontest(float y)
  125.  
  126.      {
  127.  
  128.  
  129.  
  130.                  
  131.  
  132.       if ((y>-20)&&(y<20))
  133.  
  134.         {
  135.  
  136.           mAudioManager.setSpeakerphoneOn(true);
  137.  
  138.           mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, vol_max_speaker, AudioManager.FLAG_SHOW_UI);
  139.  
  140.           mAudioManager.setRouting(AudioManager.MODE_CURRENT, AudioManager.ROUTE_SPEAKER, 1);
  141.  
  142.           return "Horizontal + Display Up";
  143.  
  144.         }
  145.  
  146.       if ((y<-70)&&(y>-110))
  147.  
  148.         {
  149.  
  150.           mAudioManager.setSpeakerphoneOn(true);
  151.  
  152.           mAudioManager.setRouting(AudioManager.MODE_CURRENT, AudioManager.ROUTE_EARPIECE, 1);
  153.  
  154.           mAudioManager.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL,AudioManager.ADJUST_LOWER,AudioManager.FLAG_SHOW_UI);
  155.  
  156.           return "Vertical";
  157.  
  158.         }
  159.  
  160.       if (((y>160)&&(y<180))||((y<-160)&&(y>=-180)))
  161.  
  162.         {
  163.  
  164.           mAudioManager.setSpeakerphoneOn(false);
  165.  
  166.           mAudioManager.setRouting(AudioManager.RINGER_MODE_VIBRATE, AudioManager.ROUTE_SPEAKER, 1);
  167.  
  168.           mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.FLAG_SHOW_UI);
  169.  
  170.           return "Horizontal + Display Down";
  171.  
  172.         }
  173.  
  174.      
  175.  
  176.       if ((y>-155)&&(y<-115))
  177.  
  178.         {
  179.  
  180.           mAudioManager.setSpeakerphoneOn(false);
  181.  
  182.           mAudioManager.setRouting(AudioManager.RINGER_MODE_VIBRATE, AudioManager.ROUTE_SPEAKER, 1);
  183.  
  184.           mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
  185.  
  186.                   return "Oblique + Display Down";
  187.  
  188.         }
  189.  
  190.      
  191.  
  192.       if ((y>-65)&&(y<-25))
  193.  
  194.         {
  195.  
  196.          
  197.  
  198.           mAudioManager.setSpeakerphoneOn(true);
  199.  
  200.           mAudioManager.setRouting(AudioManager.MODE_CURRENT, AudioManager.ROUTE_EARPIECE, 1);
  201.  
  202.           mAudioManager.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL,AudioManager.ADJUST_LOWER,AudioManager.FLAG_SHOW_UI);
  203.  
  204.                   return "Oblique + Display Up";
  205.  
  206.         }
  207.  
  208.      
  209.  
  210.       else
  211.  
  212.           return "Waiting for a known position...";      
  213.  
  214.      }
  215.  
  216.  
  217.  
  218.   }
Parsed in 0.050 seconds, using GeSHi 1.0.8.4



and the manifest file :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.      package="com.google.android.hello"
  6.  
  7.      android:versionCode="1"
  8.  
  9.      android:versionName="1.0.0">
  10.  
  11.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  12.  
  13.         <activity android:name=".accel_vol"
  14.  
  15.                  android:label="@string/app_name">
  16.  
  17.             <intent-filter>
  18.  
  19.                 <action android:name="android.intent.action.MAIN" />
  20.  
  21.                 <category android:name="android.intent.category.LAUNCHER" />
  22.  
  23.             </intent-filter>
  24.  
  25.         </activity>
  26.  
  27.     </application>
  28.  
  29.      <uses-permission android:name="android.permission.VIBRATE">
  30.  
  31.      </uses-permission>
  32.  
  33.      <uses-permission android:name="android.permission.WRITE_SETTINGS">
  34.  
  35.          </uses-permission>
  36.  
  37. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


I really hope someone will help me get through that. :cry:
Thanks in advance.
PI9
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Apr 06, 2009 8:40 am

Top
PreviousNext

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests