Jump to content
View in the app

A better way to browse. Learn more.

Tinkerunity

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Für Acceleromter Bricklet 2.0 Pitch and Roll berechnen in openHAB

Featured Replies

Geschrieben

Hallo,

ich habe den Accelerometer Bricklet 2.0.
Im BrickViewer werden zusätzlich zu den 3 Beschleunigungswerten auch die Werte für Pitch and Roll angezeigt.

In openHAB sind jedoch nur die 3 Beschleunigungswerte als Items verfügbar.

Nach Recherchen im Internet habe ich dann Formeln (Eulersche Winkel) zur Berechnung der Werte Pitch and Roll gefunden.

Auch einen Codeschnipsel zur Berechnung habe ich unter https://wiki.dfrobot.com/How_to_Use_a_T ... lt_Sensing gefunden:

int x, y, z;                        //three axis acceleration data
double roll = 0.00, pitch = 0.00;       //Roll & Pitch are the angles which rotate by the axis X and y

void RP_calculate(){
  double x_Buff = float(x);
  double y_Buff = float(y);
  double z_Buff = float(z);
  roll = atan2(y_Buff , z_Buff) * 57.3;
  pitch = atan2((- x_Buff) , sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3;
}

Dies könnte die richtige Berechnung sein.

Nur leider lässt sich dieser Code nicht in eine Rule unter openHAB einbauen, da es offensichtlich unterschiedliche Sprachen sind.
Hat jemand eine Lösung, wie ich den Code auf openHAB umschreiben könnte bzw. wie ich die Werte Pitch and Roll berechnen lassen kann?

Vielen Dank 
Thomas

Geschrieben

Moin,

Wenn du dir Items für die drei Beschleunigungswerte anlegst kannst du dann diese Rule benutzen:

import java.lang.Math

rule "calc pitch"
when
    Item JpE_AccelerationX changed
then
    val x = (JpE_AccelerationX.state as Number).doubleValue
    val y = (JpE_AccelerationY.state as Number).doubleValue
    val z = (JpE_AccelerationZ.state as Number).doubleValue

    val pitch = Math.round(Math.atan(x / Math.sqrt(y * y + z * z)) * 180 / Math.PI)
    val roll =  Math.round(Math.atan(y / Math.sqrt(x * x + z * z)) * 180 / Math.PI)
    logInfo("calc pitch", "pitch: " + pitch + "°")
    logInfo("calc pitch", "roll: " + roll + "°")
end

(JpE musst du durch die UID von deinem Bricklet ersetzen, wenn du die Standard-Itemnamen verwendest, wenn du eigene Itemnamen benutzt, dann musst du JpE_AccelerationX,Y und Z komplett ersetzen)

Die Umrechnung habe ich aus dem Brick-Viewer-Quellcode entnommen, das ist ja aber bis auf Vorzeichen identisch zu dem C-Code den du gefunden hast.

Die Regel schreibt den Pitch- und Roll-Wert in das Log, du kannst aber auch ein Item dafür anlegen und die logInfo-Zeile durch

Pitch.setState(new DecimalType(pitch))

ersetzen (Pitch mit großem P ist der Item-Name)

Geschrieben
  • Autor

Hallo,

vielen, vielen Dank.👍

Obiger Code hat auf Anhieb funktioniert.

Super wie schnell und kompetent einem hier geholfen wird...

Grüße
Thomas

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.