venerdì 28 dicembre 2012

Correzione Nord Magnetico/Geografico su Android

All'interno delle Api del sistema operativo di Android ci sono anche delle funzioni abbastanza inusuali (per un linguaggio di programmazione standard), in particolare c'e' la possibilita' di effettuare il calcolo della declinazione magnetica ovvero della differenza tra la direzione del Nord magnetico e del Nord geografico

In Italia tale differenza e' abbastanza modesta e si puo' trascurare per praticamente tutte le attivita' amatoriali ma in alcuni paesi e' molto influente sulle misure

Per il calcolo della declinazione magnetica Android si basa sul modello matematico rintracciabile a questo link http://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml dove viene fornita anche cartografia a scala mondiale e software per ambiente desktop per il calcolo




Come si osserva per il calcolo e' necessario conoscere la posizione del luogo (in latitudine, longitudine ed altezza) e la data di osservazione; dati questi parametri il modello matematico fornisce la declinazione

Per implementare la funzione anche in Android si deve quindi attivare sia il SensorManager (per la bussola) che il LocationManager (per la posizione)...la data viene assunta quella del telefono

Di seguito si riporta un esempio completo di applicazione Android con correzione di Nord Magnetico/Geografico (la parte di codice che gestisce il Gps e' stata ripresa da qui)




ads
activity_compass.xml
----------------------------------------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Compass" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="35dp"
        android:gravity="right"
        android:text="n/a"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TextView01"
        android:layout_marginTop="29dp"
        android:layout_toRightOf="@+id/textView3"
        android:gravity="right"
        android:text="n/a"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView01"
        android:layout_alignBottom="@+id/TextView01"
        android:layout_alignParentLeft="true"
        android:text="Azimuth Magn.North:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:text="Magnetic Declination:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:text="Azimuth True North:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="34dp"
        android:gravity="right"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:text="--------"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:text="Waiting Gps Signal"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

----------------------------------------
Compass Manifest
----------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.compass"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.compass.Compass"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

----------------------------------------
Compass.java
----------------------------------------

package com.example.compass;

import java.text.DecimalFormat;
import java.util.List;


import android.hardware.GeomagneticField;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;

public class Compass extends Activity implements LocationListener{

private boolean sersorrunning;
private SensorManager mySensorManager;
private TextView azimuth;
private double lat;
private double lon;
private double alt;
private TextView az_corr;
private TextView gps;
private LocationManager lm;
private StringBuilder sb;
static final String tag = "Main"; // for Log
int noOfFixes = 0;
private TextView true_north;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compass);
azimuth = (TextView) findViewById(R.id.TextView01);
az_corr = (TextView)  findViewById(R.id.textView1);
true_north = (TextView)  findViewById(R.id.textView2);
gps = (TextView)  findViewById(R.id.textView4);

mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

lm = (LocationManager) getSystemService(LOCATION_SERVICE);

@SuppressWarnings("deprecation")
List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if(mySensors.size() > 0){
     mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
     sersorrunning = true;
     //Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
   
    }
    else{
     //Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
     sersorrunning = false;
     finish();
    }
}

protected void onResume() {
/*
* onResume is is always called after onStart, even if the app hasn't been
* paused

* add location listener and request updates every 1000ms or 10m
*/
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
super.onResume();
}

@Override
protected void onPause() {
/* GPS, as it turns out, consumes battery like crazy */
lm.removeUpdates(this);
super.onPause();
}

@Override
public void onLocationChanged(Location location) {
Log.v(tag, "Location Changed");

sb = new StringBuilder(512);

noOfFixes++;

/* display some of the data in the TextView */

sb.append("No. of Fixes: ");
sb.append(noOfFixes);
sb.append('\n');
sb.append('\n');

sb.append("Londitude: ");
sb.append(location.getLongitude());
sb.append('\n');

sb.append("Latitude: ");
sb.append(location.getLatitude());
sb.append('\n');

sb.append("Altitiude: ");
sb.append(location.getAltitude());
sb.append('\n');

sb.append("Accuracy: ");
sb.append(location.getAccuracy());
sb.append('\n');

sb.append("Timestamp: ");
sb.append(location.getTime());
sb.append('\n');

lat = location.getLatitude();
lon = location.getLongitude();
alt = location.getAltitude();

gps.setText(sb.toString());
}

@Override
public void onProviderDisabled(String provider) {
/* this is called if/when the GPS is disabled in settings */
Log.v(tag, "Disabled");

/* bring up the GPS settings */
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}

@Override
public void onProviderEnabled(String provider) {
Log.v(tag, "Enabled");
//Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status alters */
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Log.v(tag, "Status Changed: Out of Service");
//Toast.makeText(this, "Status Changed: Out of Service",
//Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v(tag, "Status Changed: Temporarily Unavailable");
//Toast.makeText(this, "Status Changed: Temporarily Unavailable",
//Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Log.v(tag, "Status Changed: Available");
//Toast.makeText(this, "Status Changed: Available",
//Toast.LENGTH_SHORT).show();
break;
}
}

@Override
protected void onStop() {
/* may as well just finish since saving the state is not important for this toy app */
finish();
super.onStop();
}
   


private SensorEventListener mySensorEventListener = new SensorEventListener(){

private float declinazione;

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
public void onSensorChanged(SensorEvent event) {

DecimalFormat df = new DecimalFormat("##.##");

azimuth.setText(df.format(event.values[0]));
 
GeomagneticField geoField = new GeomagneticField( 
  Double.valueOf(lat).floatValue(), 
  Double.valueOf(lon).floatValue(),
           Double.valueOf(alt).floatValue(),
           System.currentTimeMillis() );


if (noOfFixes > 0)
{
declinazione = geoField.getDeclination();
az_corr.setText(df.format(declinazione));
true_north.setText(df.format(event.values[0]+declinazione));
}
}

};

@Override
protected void onDestroy() {
super.onDestroy();

if(sersorrunning){
mySensorManager.unregisterListener(mySensorEventListener);
}
}

}


Nessun commento:

Posta un commento

FTP Bash script

 Uno script rubato su stack overflow per fare l'upload automatico su FTP senza criptazione   #!/bin/sh HOST='ftp.xxxxx.altervista.or...