BT connection improved. Requstes to enable BT. 5 speed implementation.

UI improvements
This commit is contained in:
estevez 2017-12-27 00:41:25 +02:00
parent bfaffdc0c4
commit 47373c317a
8 changed files with 182 additions and 93 deletions

View File

@ -2,7 +2,7 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/VaporizR2.iml" filepath="$PROJECT_DIR$/VaporizR2.iml" /> <module fileurl="file://$PROJECT_DIR$/Android.iml" filepath="$PROJECT_DIR$/Android.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
</modules> </modules>
</component> </component>

2
Android/.idea/vcs.xml generated
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component> </component>
</project> </project>

View File

@ -6,8 +6,8 @@ android {
applicationId "yehor.vialov.vaporizr2" applicationId "yehor.vialov.vaporizr2"
minSdkVersion 25 minSdkVersion 25
targetSdkVersion 26 targetSdkVersion 26
versionCode 1 versionCode 2
versionName "1.0" versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {

View File

@ -29,9 +29,9 @@ public class MainActivity extends AppCompatActivity {
SeekBar motorB; SeekBar motorB;
TextView txtA; TextView txtA;
TextView txtB; 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_FULL_BACKWARD_COMMAND = 0;
private final static int MOTOR_STOP_COMMAND = 2; private final static int MOTOR_STOP_COMMAND = 5;
ImageView imEngine; ImageView imEngine;
@Override @Override
@ -41,10 +41,10 @@ public class MainActivity extends AppCompatActivity {
txtStatus = (TextView) findViewById(R.id.txtStatus); txtStatus = (TextView) findViewById(R.id.txtStatus);
motorA = (SeekBar)findViewById(R.id.motorB); motorA = (SeekBar)findViewById(R.id.motorA);
motorA.setProgress(MOTOR_STOP_COMMAND); motorA.setProgress(MOTOR_STOP_COMMAND);
motorA.setMax(MOTOR_FULL_FORWARD_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.setProgress(MOTOR_STOP_COMMAND);
motorB.setMax(MOTOR_FULL_FORWARD_COMMAND); motorB.setMax(MOTOR_FULL_FORWARD_COMMAND);
motorA.setEnabled(false); motorA.setEnabled(false);
@ -134,22 +134,19 @@ public class MainActivity extends AppCompatActivity {
private void connectToCar() { private void connectToCar() {
new Thread() { new Thread() {
public void run() { public void run() {
boolean enableBTRequested = false;
while (btSocket==null || !btSocket.isConnected()) { while (btSocket==null || !btSocket.isConnected()) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
txtStatus.setText("Connecting..."); txtStatus.setText("Bluetooth is not supported");
imEngine.setVisibility(View.VISIBLE); imEngine.setVisibility(View.VISIBLE);
} }
}); });
} else {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (!adapter.isEnabled() && !enableBTRequested) {
if (adapter == null) {
// Device does not support Bluetooth
finish(); //exit
}
if (!adapter.isEnabled()) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -158,9 +155,18 @@ public class MainActivity extends AppCompatActivity {
} }
}); });
//make sure the device's bluetooth is enabled //make sure the device's bluetooth is enabled
enableBTRequested = true;
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT); startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
} else { } else if (adapter.isEnabled()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
txtStatus.setText("Connecting...");
imEngine.setVisibility(View.VISIBLE);
}
});
enableBTRequested = false;
final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection 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 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 BluetoothDevice device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired
@ -206,6 +212,7 @@ public class MainActivity extends AppCompatActivity {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
try { try {
Thread.sleep(5000); Thread.sleep(5000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -217,8 +224,26 @@ public class MainActivity extends AppCompatActivity {
} }
private void sendCommand() { private void sendCommand() {
String m1 = Integer.toString(motorA.getProgress()); int motorASpeed, motorBSpeed;
String m2 = Integer.toString(motorB.getProgress()); 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); txtA.setText(m1);
txtB.setText(m2); txtB.setText(m2);
try { try {

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<size
android:height="80dp"
android:width="20dp" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:drawable="@android:drawable/ic_notification_overlay"/>
</layer-list>

View File

@ -7,45 +7,41 @@
tools:context="yehor.vialov.vaporizr2.MainActivity"> tools:context="yehor.vialov.vaporizr2.MainActivity">
<SeekBar <SeekBar
android:id="@+id/motorA" android:id="@+id/motorB"
style="@style/Widget.AppCompat.SeekBar.Discrete" style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="0dp" android:layout_width="232dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:max="500" android:max="500"
android:min="0" android:min="0"
android:progress="255" android:progress="255"
android:rotation="270" android:rotation="270"
android:thumb="@android:drawable/ic_notification_overlay" android:thumb="@drawable/thumb_image"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<SeekBar <SeekBar
android:id="@+id/motorB" android:id="@+id/motorA"
style="@style/Widget.AppCompat.SeekBar.Discrete" style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="0dp" android:layout_width="232dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:max="500" android:max="500"
android:min="0" android:min="0"
android:progress="255" android:progress="255"
android:rotation="270" android:rotation="270"
android:thumb="@android:drawable/ic_notification_overlay" android:thumb="@drawable/thumb_image"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextView <TextView
android:id="@+id/txtA" android:id="@+id/txtB"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
@ -55,7 +51,7 @@
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent" />
<TextView <TextView
android:id="@+id/txtB" android:id="@+id/txtA"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"

View File

@ -14,61 +14,80 @@ const int offsetA = 1;
const int offsetB = -1; const int offsetB = -1;
SoftwareSerial BTSerial(3, 2); // RX | TX SoftwareSerial BTSerial(3, 2); // RX | TX
String reader; Motor motorA = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY); Motor motorB = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
void setup() void setup()
{ {
//Serial.begin(9600); //Serial.begin(9600);
//Serial.println("Ready:"); //Serial.println("Ready:");
BTSerial.begin(9600); // HC-05 default speed in AT command more BTSerial.begin(9600); // HC-05 default speed in AT command more
brake(motor1, motor2);
} }
void loop() void loop()
{ {
char m1 = '9'; char motorADirection = 'f';
char m2 = '9'; char motorBDirection = 'f';
char motorASpeed = '0';
char motorBSpeed = '0';
int motorAVelocity = 0;
int motorBVelocity = 0;
//Serial.println("There is "+String(BTSerial.available())+" available"); //Serial.println("There is "+String(BTSerial.available())+" available");
if (BTSerial.available()==2) { if (BTSerial.available() == 4) {
m1 = BTSerial.read(); motorADirection = BTSerial.read();
m2 = BTSerial.read(); motorASpeed = BTSerial.read();
//Serial.println("Receved: "+String(m1)+";"+String(m2)); motorBDirection = BTSerial.read();
switch (m1) { motorBSpeed = BTSerial.read();
//Serial.println("Receved: "+String(motorADirection)+String(motorASpeed)+String(motorBDirection)+String(motorBSpeed));
switch (motorASpeed) {
case '0': case '0':
motor1.drive(-255); motorAVelocity = 0;
break; break;
case '1': case '1':
motor1.drive(-170); motorAVelocity = 100;
break; break;
case '2': case '2':
motor1.drive(0); motorAVelocity = 140;
break; break;
case '3': case '3':
motor1.drive(170); motorAVelocity = 180;
break; break;
case '4': case '4':
motor1.drive(255); motorAVelocity = 200;
break;
case '5':
motorAVelocity = 225;
break; break;
} }
switch (m2) { switch (motorBSpeed) {
case '0': case '0':
motor2.drive(-255); motorBVelocity = 0;
break; break;
case '1': case '1':
motor2.drive(-170); motorBVelocity = 100;
break; break;
case '2': case '2':
motor2.drive(0); motorBVelocity = 140;
break; break;
case '3': case '3':
motor2.drive(170); motorBVelocity = 180;
break; break;
case '4': case '4':
motor2.drive(255); motorBVelocity = 200;
break;
case '5':
motorBVelocity = 225;
break; break;
} }
if (motorADirection == 'b') {
motorAVelocity = 0 - motorAVelocity;
}
if (motorBDirection == 'b') {
motorBVelocity = 0 - motorBVelocity;
}
//Serial.println("A: "+String(motorAVelocity)+"; B: "+String(motorBVelocity));
motorA.drive(motorAVelocity);
motorB.drive(motorBVelocity);
} }
} }

View File

@ -0,0 +1,34 @@
#include <SparkFun_TB6612.h>
#include <SoftwareSerial.h>
#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;
}
}