Java:双数组中的乘数值

时间:2018-07-05 21:49:01

标签: java android arrays sqlite android-sensors

我正在开发一个Android应用,该应用会将传感器数据记录到SQLite数据库中。

过程如下:

  1. 将传感器中的所有值添加到双精度数组。每个尺寸都有一个双精度数组,例如对于加速度计,我有3个双精度数组(一个用于x值,一个用于y值,一个用于z值)。
  2. 当所有值都在double数组中时,请将double数组转换为字符串。
  3. 将字符串放入SQLite表中的相关列。

当我在每个双精度数组中都有相乘的值时,我遇到了一个问题。

下面是代码片段:

@Override
public void onSensorChanged(final SensorEvent sensorEvent) {

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        //Getting and displaying accelerometer data
        x = sensorEvent.values[0];
        y = sensorEvent.values[1];
        z = sensorEvent.values[2];
    }

    if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        //Getting and displaying gyroscope data
        x2 = sensorEvent.values[0];
        y2 = sensorEvent.values[1];
        z2 = sensorEvent.values[2];
    }

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        //Getting and displaying gyroscope data
        x3 = sensorEvent.values[0]; // x*sin(θ/2)
        y3 = sensorEvent.values[1]; // y*sin(θ/2)
        z3 = sensorEvent.values[2]; // z*sin(θ/2)
        cos3 = sensorEvent.values[3]; // cos(θ/2)
        accuracy3 = sensorEvent.values[4]; // estimated heading Accuracy (in radians) (-1 if unavailable)
    }

    //Start recording sensor data on click
    Button startButton = (Button) findViewById(R.id.buttonStart);
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Get label from EditText
            label = textLabel.getText().toString();

            statusText.setText("Wait");
            statusText.setTextColor(Color.RED);

            //Get current date and time
            currentTime = Calendar.getInstance().getTime();
            dateTime = currentTime.toString();

            flag = true; //flag set to true = record data
            samples = 0;
            startTime = System.currentTimeMillis();
        }
    });


    //Insert given number of sensor data samples into database
    if (samples < samples_total) {
        if (flag == true) {

            //Get current date and time
            currentTime = Calendar.getInstance().getTime();
            dateTime = currentTime.toString();

            //Add x,y,z values to arrays for sensors
            accDoubleArrayX[samples] = x;
            accDoubleArrayY[samples] = y;
            accDoubleArrayZ[samples] = z;

            gyroDoubleArrayX[samples] = x2;
            gyroDoubleArrayY[samples] = y2;
            gyroDoubleArrayZ[samples] = z2;

            rotationDoubleArrayX[samples] = x3;
            rotationDoubleArrayY[samples] = y3;
            rotationDoubleArrayZ[samples] = z3;
            rotationDoubleArrayCos[samples] = cos3;
            rotationDoubleArrayAccuracy[samples] = accuracy3;

            samples++;

        } else {
            //Log.d("", "samples < total but Flag is false - Data NOT inserted");
        }
    } else if (samples == samples_total) {
        durationTime = System.currentTimeMillis() - startTime;

        //Insert x,y,z values for sensors
        dbRef.insertData(dateTime, accDoubleArrayX, accDoubleArrayY, accDoubleArrayZ,
                gyroDoubleArrayX, gyroDoubleArrayY, gyroDoubleArrayZ,
                rotationDoubleArrayX, rotationDoubleArrayY, rotationDoubleArrayZ, rotationDoubleArrayCos, rotationDoubleArrayAccuracy,
                durationTime, label);

        Log.d("", "Data inserted!");
        Toast.makeText(this, "Data inserted!", Toast.LENGTH_LONG).show();

        //Stop recording when given number of samples inserted
        flag = false;
        samples++;

    } else {
        //Not in use
    }

}

还有insertData方法:

//Inserting data
public boolean insertData(String start_time,
                          double[] acc_x, double[] acc_y, double[] acc_z,
                          double[] gyro_x, double[] gyro_y, double[] gyro_z,
                          double[] rotation_x, double[] rotation_y, double[] rotation_z, double[] rotation_cos, double[] rotation_accuracy,
                          long duration_time, String label) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_REC_TIME, start_time);
    contentValues.put(COL_ACC_X, Arrays.toString(acc_x));
    contentValues.put(COL_ACC_Y, Arrays.toString(acc_y));
    contentValues.put(COL_ACC_Z, Arrays.toString(acc_z));
    contentValues.put(COL_GYRO_X, Arrays.toString(gyro_x));
    contentValues.put(COL_GYRO_Y, Arrays.toString(gyro_y));
    contentValues.put(COL_GYRO_Z, Arrays.toString(gyro_z));
    contentValues.put(COL_ROTATION_X, Arrays.toString(rotation_x));
    contentValues.put(COL_ROTATION_Y, Arrays.toString(rotation_y));
    contentValues.put(COL_ROTATION_Z, Arrays.toString(rotation_z));
    contentValues.put(COL_ROTATION_COS, Arrays.toString(rotation_cos));
    contentValues.put(COL_ROTATION_ACCURACY, Arrays.toString(rotation_accuracy));
    contentValues.put(COL_DURATION, String.valueOf(duration_time));
    contentValues.put(COL_LABEL, label);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}

此代码几乎可以按预期工作,但为我提供了倍增的值。数组中的值看起来像这样:

accDoubleArrayX: [2.013523578643799, 2.013523578643799, 2.013523578643799, 2.492363929748535, 2.492363929748535, 2.492363929748535, -7.771578788757324, -7.771578788757324, -7.771578788757324, 2.0913352966308594, 2.0913352966308594, 2.0913352966308594, 9.205705642700195, 9.205705642700195, 9.205705642700195, -2.649184226989746, -2.649184226989746, -2.649184226989746, 0.851138710975647, 0.851138710975647, 0.851138710975647, 1.4508862495422363, 1.4508862495422363, 1.4508862495422363, 3.1938650608062744, 3.1938650608062744, 3.1938650608062744, 1.7621324062347412, 1.7621324062347412, 1.7621324062347412, -5.260061264038086, -5.260061264038086]   
accDoubleArrayY: [5.456385612487793, 5.456385612487793, 5.456385612487793, 7.893682956695557, 7.893682956695557, 7.893682956695557, 3.6786909103393555, 3.6786909103393555, 3.6786909103393555, 2.8095955848693848, 2.8095955848693848, 2.8095955848693848, 6.449979305267334, 6.449979305267334, 6.449979305267334, 5.60602331161499, 5.60602331161499, 5.60602331161499, 5.076904773712158, 5.076904773712158, 5.076904773712158, -0.05985504388809204, -0.05985504388809204, -0.05985504388809204, 2.78086519241333, 2.78086519241333, 2.78086519241333, 6.861782073974609, 6.861782073974609, 6.861782073974609, 4.49870491027832, 4.49870491027832]    
accDoubleArrayZ: [18.986019134521484, 18.986019134521484, 18.986019134521484, 0.5770025849342346, 0.5770025849342346, 0.5770025849342346, 6.994660377502441, 6.994660377502441, 6.994660377502441, 13.217190742492676, 13.217190742492676, 13.217190742492676, 2.6240451335906982, 2.6240451335906982, 2.6240451335906982, -9.330204010009766, -9.330204010009766, -9.330204010009766, 9.876082420349121, 9.876082420349121, 9.876082420349121, 13.722367286682129, 13.722367286682129, 13.722367286682129, 4.235342979431152, 4.235342979431152, 4.235342979431152, -10.040084838867188, -10.040084838867188, -10.040084838867188, -1.8638859987258911, -1.8638859987258911]   

gyroDoubleArrayX: [-0.575242817401886, 0.14913702011108398, 0.14913702011108398, 0.14913702011108398, 2.141181468963623, 2.141181468963623, 2.141181468963623, -4.218447208404541, -4.218447208404541, -4.218447208404541, -0.030892670154571533, -0.030892670154571533, -0.030892670154571533, 5.554288864135742, 5.554288864135742, 5.554288864135742, -2.211488962173462, -2.211488962173462, -2.211488962173462, -5.499960422515869, -5.499960422515869, -5.499960422515869, -1.3646037578582764, -1.3646037578582764, -1.3646037578582764, 7.179882526397705, 7.179882526397705, 7.179882526397705, 1.353951096534729, 1.353951096534729, 1.353951096534729, -5.861084938049316]   
gyroDoubleArrayY: [-0.4165183901786804, -1.6266587972640991, -1.6266587972640991, -1.6266587972640991, -1.0279802083969116, -1.0279802083969116, -1.0279802083969116, 3.4663705825805664, 3.4663705825805664, 3.4663705825805664, -7.069095134735107, -7.069095134735107, -7.069095134735107, 0.3334277868270874, 0.3334277868270874, 0.3334277868270874, 2.221076488494873, 2.221076488494873, 2.221076488494873, -0.21944448351860046, -0.21944448351860046, -0.21944448351860046, -3.3257555961608887, -3.3257555961608887, -3.3257555961608887, 0.04580637067556381, 0.04580637067556381, 0.04580637067556381, 0.3419498801231384, 0.3419498801231384, 0.3419498801231384, 1.2857742309570312]  
gyroDoubleArrayZ: [0.0649811327457428, -0.18002969026565552, -0.18002969026565552, -0.18002969026565552, 0.2865561246871948, 0.2865561246871948, 0.2865561246871948, -0.7233145833015442, -0.7233145833015442, -0.7233145833015442, -0.6796387434005737, -0.6796387434005737, -0.6796387434005737, -1.1739214658737183, -1.1739214658737183, -1.1739214658737183, -3.5452001094818115, -3.5452001094818115, -3.5452001094818115, -0.014913702383637428, -0.014913702383637428, -0.014913702383637428, -0.6348975896835327, -0.6348975896835327, -0.6348975896835327, -0.9310411214828491, -0.9310411214828491, -0.9310411214828491, -2.379800796508789, -2.379800796508789, -2.379800796508789, -0.6317018270492554]    

rotationDoubleArrayX: [0.2823379933834076, 0.2823379933834076, 0.26849400997161865, 0.26849400997161865, 0.26849400997161865, 0.2820430099964142, 0.2820430099964142, 0.2820430099964142, 0.35002198815345764, 0.35002198815345764, 0.35002198815345764, 0.20867300033569336, 0.20867300033569336, 0.20867300033569336, 0.2366899996995926, 0.2366899996995926, 0.2366899996995926, 0.39944401383399963, 0.39944401383399963, 0.39944401383399963, 0.3354569971561432, 0.3354569971561432, 0.3354569971561432, 0.17095300555229187, 0.17095300555229187, 0.17095300555229187, 0.1272200047969818, 0.1272200047969818, 0.1272200047969818, 0.34220200777053833, 0.34220200777053833, 0.34220200777053833]    
rotationDoubleArrayY: [0.10564299672842026, 0.10564299672842026, 0.08853700011968613, 0.08853700011968613, 0.08853700011968613, 0.04211900010704994, 0.04211900010704994, 0.04211900010704994, 0.01869099959731102, 0.01869099959731102, 0.01869099959731102, 0.1025020033121109, 0.1025020033121109, 0.1025020033121109, -0.10204900056123734, -0.10204900056123734, -0.10204900056123734, -0.06824100017547607, -0.06824100017547607, -0.06824100017547607, 0.03044299967586994, 0.03044299967586994, 0.03044299967586994, 0.026799000799655914, 0.026799000799655914, 0.026799000799655914, -0.07169300317764282, -0.07169300317764282, -0.07169300317764282, -0.07498300075531006, -0.07498300075531006, -0.07498300075531006]  
rotationDoubleArrayZ: [0.19162000715732574, 0.19162000715732574, 0.18857499957084656, 0.18857499957084656, 0.18857499957084656, 0.16563500463962555, 0.16563500463962555, 0.16563500463962555, 0.15753500163555145, 0.15753500163555145, 0.15753500163555145, 0.16617600619792938, 0.16617600619792938, 0.16617600619792938, 0.0989999994635582, 0.0989999994635582, 0.0989999994635582, 0.0755779966711998, 0.0755779966711998, 0.0755779966711998, -0.011357000097632408, -0.011357000097632408, -0.011357000097632408, -0.0029909999575465918, -0.0029909999575465918, -0.0029909999575465918, -0.039792999625205994, -0.039792999625205994, -0.039792999625205994, -0.05465399846434593, -0.05465399846434593, -0.05465399846434593]    
rotationDoubleArrayCos: [0.9340260028839111, 0.9340260028839111, 0.9404839873313904, 0.9404839873313904, 0.9404839873313904, 0.94405597448349, 0.94405597448349, 0.94405597448349, 0.9232100248336792, 0.9232100248336792, 0.9232100248336792, 0.9582970142364502, 0.9582970142364502, 0.9582970142364502, 0.9611250162124634, 0.9611250162124634, 0.9611250162124634, 0.911083996295929, 0.911083996295929, 0.911083996295929, 0.9414939880371094, 0.9414939880371094, 0.9414939880371094, 0.9849100112915039, 0.9849100112915039, 0.9849100112915039, 0.9884790182113647, 0.9884790182113647, 0.9884790182113647, 0.9350330233573914, 0.9350330233573914, 0.9350330233573914] 
rotationDoubleArrayAccuracy: [246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0]   

如您所见,加速度计的前三个值相同,接下来的三个相同,依此类推。 对于陀螺仪:第一个值是唯一的,然后三个是相同的,接下来的三个是相同的,依此类推。 对于旋转矢量:前两个相同,然后下三个相同,然后下三个,依此类推。

对我来说,这真的很奇怪,我找不到解决此问题的方法。

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为每次接收到传感器事件时,您都填充所有3种事件类型的数组,而不是仅填充一种事件类型(已接收事件的类型)的数组。

为了避免这种情况,您需要为每种事件类型保留三个单独的计数器。

据我所知,您还想在所有示例数组都已完全填充后将其插入到DB中。因此,您还需要修改数据库插入条件。

因此,应按以下方式修改填充例程:

if (samples_acc < samples_total || samples_gyro < samples_total || samples_rotation < samples_total) 
                {
                            if (flag == true) 
                            {

                                //Get current date and time
                                currentTime = Calendar.getInstance().getTime();
                                dateTime = currentTime.toString();

                                //Add x,y,z values to arrays for sensors

                                if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER && samples_acc < samples_total) 
                                {
                                accDoubleArrayX[samples_acc] = x;
                                accDoubleArrayY[samples_acc] = y;
                                accDoubleArrayZ[samples_acc] = z;

                                samples_acc++;
                                }

                                if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE && samples_gyro < samples_total) 
                                {
                                gyroDoubleArrayX[samples_gyro] = x2;
                                gyroDoubleArrayY[samples_gyro] = y2;
                                gyroDoubleArrayZ[samples_gyro] = z2;

                                samples_gyro++;
                                }

                                if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR && samples_rotation < samples_total) 
                                {
                                rotationDoubleArrayX[samples_rotation] = x3;
                                rotationDoubleArrayY[samples_rotation] = y3;
                                rotationDoubleArrayZ[samples_rotation] = z3;
                                rotationDoubleArrayCos[samples_rotation] = cos3;
                                rotationDoubleArrayAccuracy[samples_rotation] = accuracy3;

                                samples_rotation++;
                                }

                            }//end if flag=true
                            else 
                            {
                            //Log.d("", "samples < total but Flag is false - Data NOT inserted");
                            }
            }//end if at least one of arrays is not filled yet

            //all arrays have been filled   
            else 
            {
                durationTime = System.currentTimeMillis() - startTime;

                //Insert x,y,z values for sensors
                dbRef.insertData(dateTime, accDoubleArrayX, accDoubleArrayY, accDoubleArrayZ,
                        gyroDoubleArrayX, gyroDoubleArrayY, gyroDoubleArrayZ,
                        rotationDoubleArrayX, rotationDoubleArrayY, rotationDoubleArrayZ, rotationDoubleArrayCos, rotationDoubleArrayAccuracy,
                        durationTime, label);

                Log.d("", "Data inserted!");
                Toast.makeText(this, "Data inserted!", Toast.LENGTH_LONG).show();

                //Stop recording when given number of samples inserted
                flag = false;
            }

哦,还有关于按钮的另一条注释:最好将其从onSensorChanged方法中移出,因为在每次收到新事件时都不必重新声明它