From 47373c317aa1c18f4d1e83bba4323c3fbcdf6373 Mon Sep 17 00:00:00 2001 From: estevez Date: Wed, 27 Dec 2017 00:41:25 +0200 Subject: [PATCH] BT connection improved. Requstes to enable BT. 5 speed implementation. UI improvements --- Android/.idea/modules.xml | 2 +- Android/.idea/vcs.xml | 2 +- Android/app/build.gradle | 4 +- .../yehor/vialov/vaporizr2/MainActivity.java | 133 +++++++++++------- .../app/src/main/res/drawable/thumb_image.xml | 15 ++ .../app/src/main/res/layout/activity_main.xml | 20 ++- Arduino/VaporizR2/VaporizR2.ino | 65 ++++++--- Arduino/motor_test/motor_test.ino | 34 +++++ 8 files changed, 182 insertions(+), 93 deletions(-) create mode 100644 Android/app/src/main/res/drawable/thumb_image.xml create mode 100644 Arduino/motor_test/motor_test.ino diff --git a/Android/.idea/modules.xml b/Android/.idea/modules.xml index 410ae68..983383f 100644 --- a/Android/.idea/modules.xml +++ b/Android/.idea/modules.xml @@ -2,7 +2,7 @@ - + diff --git a/Android/.idea/vcs.xml b/Android/.idea/vcs.xml index 94a25f7..6c0b863 100644 --- a/Android/.idea/vcs.xml +++ b/Android/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Android/app/build.gradle b/Android/app/build.gradle index 4066641..6b771eb 100644 --- a/Android/app/build.gradle +++ b/Android/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "yehor.vialov.vaporizr2" minSdkVersion 25 targetSdkVersion 26 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "1.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/Android/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java b/Android/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java index df55e97..9f2ea2d 100644 --- a/Android/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java +++ b/Android/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java @@ -29,9 +29,9 @@ public class MainActivity extends AppCompatActivity { SeekBar motorB; TextView txtA; TextView txtB; - private final static int MOTOR_FULL_FORWARD_COMMAND = 4; + private final static int MOTOR_FULL_FORWARD_COMMAND = 10; private final static int MOTOR_FULL_BACKWARD_COMMAND = 0; - private final static int MOTOR_STOP_COMMAND = 2; + private final static int MOTOR_STOP_COMMAND = 5; ImageView imEngine; @Override @@ -41,10 +41,10 @@ public class MainActivity extends AppCompatActivity { txtStatus = (TextView) findViewById(R.id.txtStatus); - motorA = (SeekBar)findViewById(R.id.motorB); + motorA = (SeekBar)findViewById(R.id.motorA); motorA.setProgress(MOTOR_STOP_COMMAND); motorA.setMax(MOTOR_FULL_FORWARD_COMMAND); - motorB = (SeekBar)findViewById(R.id.motorA); + motorB = (SeekBar)findViewById(R.id.motorB); motorB.setProgress(MOTOR_STOP_COMMAND); motorB.setMax(MOTOR_FULL_FORWARD_COMMAND); motorA.setEnabled(false); @@ -134,76 +134,83 @@ public class MainActivity extends AppCompatActivity { private void connectToCar() { new Thread() { public void run() { + boolean enableBTRequested = false; while (btSocket==null || !btSocket.isConnected()) { - runOnUiThread(new Runnable() { - @Override - public void run() { - txtStatus.setText("Connecting..."); - imEngine.setVisibility(View.VISIBLE); - } - }); - BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (adapter == null) { - // Device does not support Bluetooth - finish(); //exit - } - - if (!adapter.isEnabled()) { runOnUiThread(new Runnable() { @Override public void run() { - txtStatus.setText("Bluetooth is not enabled. Will retry in 5 sec."); + txtStatus.setText("Bluetooth is not supported"); imEngine.setVisibility(View.VISIBLE); } }); - //make sure the device's bluetooth is enabled - Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT); } else { - final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection - String mac = "98:D3:31:F5:2D:2F"; //my laptop's mac adress - BluetoothDevice device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired - // Get a BluetoothSocket to connect with the given BluetoothDevice - btSocket = null; - btOutStream = null; - try { - btSocket = device.createRfcommSocketToServiceRecord(SERIAL_UUID); - } catch (Exception e) { + if (!adapter.isEnabled() && !enableBTRequested) { runOnUiThread(new Runnable() { @Override public void run() { - txtStatus.setText("Error creating socket. Will retry in 5 sec."); + txtStatus.setText("Bluetooth is not enabled. Will retry in 5 sec."); imEngine.setVisibility(View.VISIBLE); } }); - } - - try { - btSocket.connect(); - btOutStream = btSocket.getOutputStream(); - - btConnected = true; + //make sure the device's bluetooth is enabled + enableBTRequested = true; + Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT); + } else if (adapter.isEnabled()) { runOnUiThread(new Runnable() { @Override public void run() { - txtStatus.setText("Connected"); - imEngine.setVisibility(View.GONE); - motorA.setEnabled(true); - motorB.setEnabled(true); - } - }); - } catch (Exception e) { - final Exception er = e; - runOnUiThread(new Runnable() { - @Override - public void run() { - txtStatus.setText("Connection error. Will retry in 5 sec."); + txtStatus.setText("Connecting..."); imEngine.setVisibility(View.VISIBLE); } - }); - e.printStackTrace(); + enableBTRequested = false; + final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection + String mac = "98:D3:31:F5:2D:2F"; //my laptop's mac adress + BluetoothDevice device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired + // Get a BluetoothSocket to connect with the given BluetoothDevice + btSocket = null; + btOutStream = null; + try { + btSocket = device.createRfcommSocketToServiceRecord(SERIAL_UUID); + } catch (Exception e) { + runOnUiThread(new Runnable() { + @Override + public void run() { + txtStatus.setText("Error creating socket. Will retry in 5 sec."); + imEngine.setVisibility(View.VISIBLE); + } + }); + } + + try { + btSocket.connect(); + btOutStream = btSocket.getOutputStream(); + + btConnected = true; + runOnUiThread(new Runnable() { + @Override + public void run() { + txtStatus.setText("Connected"); + imEngine.setVisibility(View.GONE); + motorA.setEnabled(true); + motorB.setEnabled(true); + } + }); + } catch (Exception e) { + final Exception er = e; + runOnUiThread(new Runnable() { + @Override + public void run() { + txtStatus.setText("Connection error. Will retry in 5 sec."); + imEngine.setVisibility(View.VISIBLE); + } + + }); + e.printStackTrace(); + } } } try { @@ -217,8 +224,26 @@ public class MainActivity extends AppCompatActivity { } private void sendCommand() { - String m1 = Integer.toString(motorA.getProgress()); - String m2 = Integer.toString(motorB.getProgress()); + int motorASpeed, motorBSpeed; + String m1, m2; + int mAProgress = motorA.getProgress(); + int mBProgress = motorB.getProgress(); + if (mAProgress < 5) { + m1 = "b"; + motorASpeed = 5 - mAProgress; + } else { + m1 = "f"; + motorASpeed = mAProgress - 5; + } + if (mBProgress < 5) { + m2 = "b"; + motorBSpeed = 5 - mBProgress; + } else { + m2 = "f"; + motorBSpeed = mBProgress - 5; + } + m1 += Integer.toString(motorASpeed); + m2 += Integer.toString(motorBSpeed); txtA.setText(m1); txtB.setText(m2); try { diff --git a/Android/app/src/main/res/drawable/thumb_image.xml b/Android/app/src/main/res/drawable/thumb_image.xml new file mode 100644 index 0000000..f620333 --- /dev/null +++ b/Android/app/src/main/res/drawable/thumb_image.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Android/app/src/main/res/layout/activity_main.xml b/Android/app/src/main/res/layout/activity_main.xml index f07e422..32357be 100644 --- a/Android/app/src/main/res/layout/activity_main.xml +++ b/Android/app/src/main/res/layout/activity_main.xml @@ -7,45 +7,41 @@ tools:context="yehor.vialov.vaporizr2.MainActivity"> + +#include + +#define AIN1 4 +#define BIN1 8 +#define AIN2 5 +#define BIN2 7 +#define PWMA 11 +#define PWMB 10 +#define STBY 6 + +const int offsetA = 1; +const int offsetB = -1; + +Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY); +Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY); + +void setup() +{ + Serial.begin(9600); + Serial.println("Ready:"); +} + +void loop() +{ + int v = 0; + while(v<=255) { + motor2.drive(v); + Serial.println("Speed: "+String(v)); + delay(100); + v = v+1; + } +}