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">
<component name="ProjectModuleManager">
<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" />
</modules>
</component>

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

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

View File

@ -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 {

View File

@ -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 {

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

View File

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