This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to decoding Raw characetristic data from Nordic-Thingy:52(nRF6936) -- JAVA android

Hello,

I'm developping an android application for detecting falls.

I will use accelerometer's values for find the g constant.

But i recover bad values.

this is my code for decoding values:

private double ConvertShortQxxToDouble(int bitsRightPoint,short value)
    {

        value= shortToLittleEndian(value);

        if (value<0)
            value=Short.reverseBytes(value);



        return value/Math.pow(2,bitsRightPoint);
    }
    
    private static Short shortToLittleEndian(short n) {
        ByteBuffer bb = ByteBuffer.allocate(2);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.putShort( n);
        byte[] b =bb.array();
        return (short)((b[0]<<8)|(b[1]&0xFF)) ;
    }
    --------------------------------------------------------
[...]
    byte[] value= characteristic.getValue();
    if(currentUUID.equals(Constants.CHARACTERISTIC_ACCELEROMETER_GYROSCOPE_VECTOR_STATION))
        {
            /***
             * Accelerometer
             */
            short tmp=  (short)((value[0] << 8) | (value[1] & 0xFF));

            double acX= ConvertShortQxxToDouble(10,tmp);

            tmp=  (short)((value[2] << 8) | (value[3] & 0xFF));

            double acY= ConvertShortQxxToDouble(10,tmp);


            tmp=  (short)((value[4] << 8) | (value[5] & 0xFF));

            double acZ= ConvertShortQxxToDouble(10,tmp);
            //find variance 
            if(ct_accelerometer+1<=Constants.SAMPLE_ACCELEROMETER)
            {

                accelerometerSquared+= Math.pow(acX,2)+Math.pow(acY,2)+Math.pow(acZ,2);
                mu+=Math.sqrt( Math.pow(acX,2)+Math.pow(acY,2)+Math.pow(acZ,2));
                ct_accelerometer++;
            } else if (variance==0)
            {
                mu/=Constants.SAMPLE_ACCELEROMETER;
                variance= (accelerometerSquared/Constants.SAMPLE_ACCELEROMETER) - Math.pow(mu,2);
                calibration=(9.81/mu);
             
            }

          }
    
    
[...]

--------------------------------------------------------------------------------------------------------

Functions used:

n= sample rate

mu=Σ ( (x^2 + y^2 + z^2)^(1/2) ) /n

accSqared= Σ  (x^2 + y^2 + z^2)

variance= (accSquared / n) - mu^2

calibration = g/mu

---------------------------------------------------------------------------------------------------------------

the result is :  average: 7.07990718008141, variance : 4.173282193669863, error +- 2.042861276168762, callibration : 1.3856113859231693

exemples values:

res=(x^2 + y^2 +z^2)^(1/2)

acX -5.7509765625 
acY -2.5009765625 
acZ 0.994140625
res :6.349561462878678

acX -6.2509765625 
acY -3.0009765625
acZ 0.9951171875
res :7.005057211076619

acX -5.5009765625 
acY -0.2509765625 
acZ 0.9892578125 
res :5.594851507921449

acX -8.2509765625
acY -3.5009765625 
acZ 0.9951171875
res :9.018076809550722

acX -7.0009765625 
acY -2.0009765625 
acZ 1.0009765625
res :7.3497982360758005

acX -10.2509765625 
acY -4.0009765625 
acZ 0.986328125 
res :11.048220540375027

acX -8.2509765625 
acY -5.5009765625 
acZ 0.978515625 
res :9.964780489527493

acX -4.5009765625 
acY -2.5009765625 
acZ 0.99609375 
res :5.244604517133555

acX -7.2509765625 
acY -0.2509765625 
acZ 0.9814453125 
res :7.321399131742297

acX -7.7509765625 
acY -2.5009765625 
acZ 0.9814453125 
res :8.203399072337445

acX -8.5009765625 
acY -3.5009765625 
acZ 1.00390625 
res :9.24831158461574

acX -8.5009765625 
acY -5.0009765625 
acZ 0.9892578125 
res :9.91236601999951

How can i calibrate my device to get expected values from my device or what should i change to make it work.

Thank you for your time

Related