BeamNG / remotecontrol

Remote Control App for BeamNG.drive
ISC License
44 stars 8 forks source link

Steering doesn't work sometimes #8

Closed jhasse closed 8 years ago

jhasse commented 8 years ago

Many users report that gas and brake work, but steering doesn't.

jhasse commented 8 years ago

Okay the reason it fails, is that magnet is a null vector when the device doesn't have a magnetic sensor. This results in normH being 0 here and thus getRotationMatrix returning false. Thus SensorManger.getOrientation never gets called in this function:

    // calculates orientation angles from accelerometer and magnetometer output
    public void calculateAccMagOrientation() {
        if (SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnet)) {
            SensorManager.getOrientation(rotationMatrix, accMagOrientation);
        }
    }

See https://github.com/BeamNG/remotecontrol/blob/77294470c5dcd6fd14343b11768765fcaa891f13/Android/Udpsteering/app/src/main/java/com/beamng/udpsteering/MainActivity.java#L276

MainActivity.java is only using accMagOrientation[1] here. getOrientation sets this to Math.asin(-rotationMatrix[7]) here. rotationMatrix[7] is set to

gravity[1] / Math.sqrt(gravity[0]*gravity[0]+gravity[1]*gravity[1]+gravity[2]*gravity[2])

here. gravity is accel in MainActivity.java.

To sum it up, the magnet sensor information isn't used at all. The calls to getRotationMatrix and getOrientation are unnecessary.