kramann.info
© Guido Kramann

Login: Passwort:










kramann.info
© Guido Kramann

Login: Passwort:




Allgemeines Vorgehen bei der Programmentwicklung mit Sensoren

(EN google-translate)

(PL google-translate)

Anders als bei einer selbst geschriebenen Funktion ist bei der Verwendung einer Sensor Hardware nicht ganz sicher vorher zu sehen, welche Werte dieser liefern wird. Dabei spielt nicht nur der Zusammenhang zwischen Sensorwert und Sensorinput eine Rolle, sondern auch das zeitliche Übertragungsverhalten der Werte und die Überlagerung von Störsignalen.

Bevor man ein komplettes Programm auf der Grundlage von Sensordaten schreibt, sollte man zuerst den Sensor ausgiebig testen.

Dies geht am besten, wenn man die Sensordaten einliest und die empfangenen Werte zyklisch als Text ausgibt.

In Android Processing bietet die Bibliothek Ketai zu jedem denkbaren Sensor ein entsprechendes Beispielprogramm.

Nachfolgend beispielsweise der Originalsketch zum Testen des Beschleunigungssensors: Die Bibliothek beinhaltet als Kommentare die Möglichkeit viele weitere Sensoren zu aktivieren und sich anzeigen zu lassen.

/**
 * <p>Ketai Sensor Library for Android: http://Ketai.org</p>
 *
 * <p>KetaiSensor Features:
 * <ul>
 * <li>handles incoming Sensor Events</li>
 * <li>Includes Accelerometer, Magnetometer, Gyroscope, GPS, Light, Proximity</li>
 * <li>Use KetaiNFC for Near Field Communication</li>
 * </ul>
 * <p>Updated: 2017-08-29 Daniel Sauter/j.duran</p>
 */

import ketai.sensors.*;

KetaiSensor sensor;
float accelerometerX, accelerometerY, accelerometerZ;

void setup()
{
  fullScreen();  
  sensor = new KetaiSensor(this);
  sensor.start();
  orientation(LANDSCAPE);
  textAlign(CENTER, CENTER);
  textSize(displayDensity * 36);
}

void draw()
{
  background(78, 93, 75);
  text("Accelerometer: 
" +
    "x: " + nfp(accelerometerX, 1, 3) + "
" +
    "y: " + nfp(accelerometerY, 1, 3) + "
" +
    "z: " + nfp(accelerometerZ, 1, 3), 0, 0, width, height);
}

void onAccelerometerEvent(float x, float y, float z)
{
  accelerometerX = x;
  accelerometerY = y;
  accelerometerZ = z;
}

/*
	available sensors/methods

 * void onSensorEvent(SensorEvent e) - raw android sensor event <br />
 * void onAccelerometerEvent(float x, float y, float z, long a, int b): x,y,z force in m/s^2, a=timestamp(nanos), b=accuracy
 * void onAccelerometerEvent(float x, float y, float z):  x,y,z force in m/s2
 * void onOrientationEvent(float x, float y, flaot z, long a, int b):  x,y,z rotation in degrees, a=timestamp(nanos), b=accuracy
 * void onOrientationEvent(float x, float y, float z) : x,y,z rotation in degrees
 * void onMagneticFieldEvent(float x, float y, float z, long a, int b) : x,y,z geomag field in uT, a=timestamp(nanos), b=accuracy
 * void onMagneticFieldEvent(float x, float y, float z): x,y,z geomagnetic field in uT
 * void onGyroscopeEvent(float x, float y, float z, long a, int b):x,y,z rotation in rads/sec, a=timestamp(nanos), b=accuracy
 * void onGyroscopeEvent(float x, float y, float z): x,y,z rotation in rads/sec
 * void onGravityEvent(float x, float y, float z, long a, int b): x,y,z force of gravity in m/s^2, a=timestamp(nanos), b=accuracy
 * void onGravityEvent(float x, float y, float z): x,y,z rotation in m/s^s
 * void onProximityEvent(float d, long a, int b): d distance from sensor (typically 0,1), a=timestamp(nanos), b=accuracy
 * void onProximityEvent(float d): d distance from sensor (typically 0,1)
 * void onLightEvent(float d, long a, int b): d illumination from sensor in lx
 * void onLightEvent(float d): d illumination from sensor in lx
 * void onPressureEvent(float p, long a, int b): p ambient pressure in hPa or mbar, a=timestamp(nanos), b=accuracy
 * void onPressureEvent(float p): p ambient pressure in hPa or mbar
 * void onTemperatureEvent(float t, long a, int b): t temperature in degrees in degrees Celsius, a=timestamp(nanos), a=timestamp(nanos), b=accuracy
 * void onTemperatureEvent(float t): t temperature in degrees in degrees Celsius
 * void onLinearAccelerationEvent(float x, float y, float z, long a, int b): x,y,z acceleration force in m/s^2, minus gravity, a=timestamp(nanos), b=accuracy
 * void onLinearAccelerationEvent(float x, float y, float z): x,y,z acceleration force in m/s^2, minus gravity
 * void onRotationVectorEvent(float x, float y, float z, long a, int b): x,y,z rotation vector values, a=timestamp(nanos), b=accuracy
 * void onRotationVectorEvent(float x, float y, float z):x,y,z rotation vector values
 * void onAmibentTemperatureEvent(float t): same as temp above (newer API)
 * void onRelativeHumidityEvent(float h): h ambient humidity in percentage
  * void onSignificantMotionEvent(): trigger for when significant motion has occurred
 * void onStepDetectorEvent(): called on every step detected
 * void onStepCounterEvent(float s): s is the step count since device reboot, is called on new step
 * void onGeomagneticRotationVectorEvent(float x, float y, float z):
 * void onGameRotationEvent(float x, float y, float z):
 * void onHeartRateEvent(float r): returns current heart rate in bpm
*/

Code 0-1: Accelerometer

Verwendung von Sensoren mit Arduino

Bei Arduino Programmen und Schaltungen ist es ganz ähnlich: Zuerst sollte man mit einem kleinen Programm und einer kleinen Schaltung beginnen, bei der Sensordaten empfangen und auf dem Terminal am PC ausgegeben werden. Erst danach sollte man nach und nach den Rest des Programms entwickeln und unbedingt auch Zwischenschritte immer wieder testen.

Das folgende Arduino-Programm ist völlig ausreichend, um Sensorwerte anzusehen: Hier muß der Sensor an den Analog Eingang A0 angeschlossen sein.

int sensor=0;
void setup() 
{
     Serial.begin(9600);
}

void loop() 
{
     sensor = analogRead(0);
     Serial.println(sensor);
     delay(200);
}

Code 0-2: sensorread

Sensor Curcuit (example with Sharp IR distance sensor)

Bild 0-1: Sensor Curcuit (example with Sharp IR distance sensor)