Is it possible to send values of the gyroscope and a seekbar simultaneously via bluetooth?

Asked

Viewed 128 times

1

Hello, everybody!

I’m trying to control a Drone (Hubsan X4) via smartphone sensors. But I can’t send the collected values of the gyroscope and a seekbar simultaneously. The seekbar represents speed (Throttle) and the gyroscope is the directional control of the drone.

I can only send one of the data (or gyroscope or seekbar), when I implement the two codes bluetooth sends only the data of the gyroscope that is in onSensorChange() and the programming of seekbar is in onCreate().

Could someone help me with this?

Thank you for your attention!

  • Post the code to facilitate. An observation: I do not know if it applies to your situation, I’ve had this problem but with Arduino and RXTX communication. The solution was to send the sensor data one at a time, the time difference was small which was "almost simultaneous".

  • Hi @Daniela, I’m making some changes to the code and I plan to test this week yet. As soon as I finish I will post saying whether I succeeded or not. Thanks in advance for your help!

1 answer

1

@Daniela, I was sending as you suggested (one at a time), but as the sending of the sensor data was on onSensorChange() and the sending of the seekbar data was on onCreate() it was not possible to send them simultaneously. As I am still new to this type of development I did not know that I could not put everything in onOnSensorChange(). But I can :) Finally, I concatenated all the values in a string, converted to an array bytes and now it is working perfectly :) See the code:

@Override
public void onSensorChanged(SensorEvent event) {

    byte[] vetor;
    String eixoX, eixoY, eixoZ; // em GRAUS.
    String pacote = "";

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        gravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        geomagnetic = event.values;
    if (gravity != null && geomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean sucesso = SensorManager.getRotationMatrix(R, I, gravity,
                geomagnetic);
        if (sucesso) {
            float orientation[] = new float[3];
            orientation = SensorManager.getOrientation(R, orientation);

            eixoX = (int) Math.toDegrees(orientation[0]);
            eixoY = (int) Math.toDegrees(orientation[1]);
            eixoZ = (int) Math.toDegrees(orientation[2]);

            // Envia para o arduíno
            if (mConnectedThread != null) {
                try {

                    // INÍCIO - SEEKBAR

                    seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                            // velocidade.setText(progresso);

                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {

                        }

                        @Override
                        public void onProgressChanged(SeekBar seekBar,
                                int progressValue, boolean fromUser) {
                            // sensorStop = true;
                            velocidade.setText(Integer
                                    .toString(progressValue));

                        }

                    });

                    // FIM - SEEKBAR

                    // Velocidade
                    pacote += "V" + velocidade.getText() + "|";

                    // Envia 
                    pacote += "X" + eixoX + "|";

                    // Envia Y
                    pacote += "Y" + eixoY + "|";

                    // Envia 
                    pacote += "Z" + eixoZ + "|";

                    mConnectedThread.write(pacote.getBytes());
                    Thread.sleep(20);

                } catch (Exception ex) {
                    Toast.makeText(this,
                            "Erro ao enviar os dados via bluetooth",
                            Toast.LENGTH_LONG);
                }

            }

        }

    }

}@Override
public void onSensorChanged(SensorEvent event) {

    byte[] vetor;
    int eixoX, eixoY, eixoZ; // em GRAUS.
    String valorX, valorY, valorZ; // em BYTES.
    String pacote = "";

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        gravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        geomagnetic = event.values;
    if (gravity != null && geomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean sucesso = SensorManager.getRotationMatrix(R, I, gravity,
                geomagnetic);
        if (sucesso) {
            float orientation[] = new float[3];
            orientation = SensorManager.getOrientation(R, orientation);

            eixoX = (int) Math.toDegrees(orientation[0]);
            eixoY = (int) Math.toDegrees(orientation[1]);
            eixoZ = (int) Math.toDegrees(orientation[2]);
            xCoor.setText("X: " + eixoX);
            yCoor.setText("Y: " + eixoY);
            zCoor.setText("Z: " + eixoZ);

            // Envia para o arduíno
            if (mConnectedThread != null) {
                try {

                    // INÍCIO - SEEKBAR

                    seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                            // velocidade.setText(progresso);

                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {

                        }

                        @Override
                        public void onProgressChanged(SeekBar seekBar,
                                int progressValue, boolean fromUser) {
                            // sensorStop = true;
                            velocidade.setText(Integer
                                    .toString(progressValue));

                        }

                    });

                    // FIM - SEEKBAR

                    // Velocidade
                    pacote += "V" + velocidade.getText() + "|";

                    // Envia X
                    //valorX = Integer.toString(eixoX * 256 / 360);
                    //pacote += "X" + valorX + "|";

                    // Envia Y
                    valorY = Integer.toString(eixoY * 256 / 360);
                    pacote += "Y" + valorY + "|";

                    // Envia Z
                    valorZ = Integer.toString(eixoZ * 256 / 360);
                    if (Integer.parseInt(valorZ) > 50)
                        valorZ = Integer.toString(50);
                    else if (Integer.parseInt(valorZ) < 0)
                        valorZ = Integer.toString(0);

                    pacote += "Z" + valorZ + "|";

                    mConnectedThread.write(pacote.getBytes());
                    Thread.sleep(20);

                } catch (Exception ex) {
                    Toast.makeText(this,
                            "Erro ao enviar os dados via bluetooth",
                            Toast.LENGTH_LONG);
                }

            }

        }

    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.