Skip to content

Commit

Permalink
Released version.
Browse files Browse the repository at this point in the history
  • Loading branch information
vladd11 committed Apr 9, 2022
1 parent d09a7a0 commit 3dd04d1
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 204 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {

defaultConfig {
applicationId "com.vladd11.app.robot"
minSdk 28
targetSdk 31
minSdk 27
targetSdk 27
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation project(path: ':libusbcamera')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand All @@ -54,4 +55,6 @@ dependencies {
// RxJava is also required.
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'

//implementation 'com.github.vladd11:AndroidUSBCamera:7db7cc7e0e'
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.usb.host"/>

<application
android:networkSecurityConfig="@xml/net"
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/vladd11/app/robot/ConnectionManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vladd11.app.robot;


import android.hardware.camera2.CameraAccessException;
import android.location.Location;
import android.util.Log;

Expand Down Expand Up @@ -72,7 +73,11 @@ public void getLocationPoints(Location current, OnLocationPointsAvailableListene
public void onMessage(@NonNull WebSocket webSocket, @NonNull String text) {
if (text.equals("img")) {
frameTime = System.currentTimeMillis();
onFrameRequest.call(buffer -> webSocket.send(ByteString.of(buffer)));
try {
onFrameRequest.call(buffer -> webSocket.send(ByteString.of(buffer)));
} catch (CameraAccessException | InterruptedException e) {
e.printStackTrace();
}
} else if (text.startsWith("route")) {
final List<List<Double>> rawCoordinates = gson.fromJson(
text.replace("route ", ""),
Expand Down Expand Up @@ -105,11 +110,11 @@ public interface OnLocationPointsAvailableListener {
}

public interface OnImageRequestReceivedListener {
void call(ImageReceivedListener listener);
void call(ImageReceivedListener listener) throws CameraAccessException, InterruptedException;
}

public interface ImageReceivedListener {
void received(ByteBuffer buffer) throws InterruptedException;
void received(byte[] buffer) throws InterruptedException;
}

public interface OnConnectedListener {
Expand Down
36 changes: 25 additions & 11 deletions app/src/main/java/com/vladd11/app/robot/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.hardware.usb.UsbManager;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import android.view.View;
Expand Down Expand Up @@ -48,9 +49,22 @@ public class MainActivity extends AppCompatActivity implements PathFinder.PathFi
private TextView actionTextView;
private TextView headingTextView;
private TextView bearingTextView;
private TextView diffTextView;

private SimpleBluetoothDeviceInterface deviceInterface;

@Override
protected void onStart() {
super.onStart();
if (controller.mCameraHelper != null) controller.mCameraHelper.registerUSB();
}

@Override
protected void onStop() {
if (controller.mCameraHelper != null) controller.mCameraHelper.unregisterUSB();
super.onStop();
}

@SuppressLint("CheckResult")
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -62,6 +76,7 @@ protected void onCreate(Bundle savedInstanceState) {
actionTextView = findViewById(R.id.currentActionText);
headingTextView = findViewById(R.id.headingTextView);
bearingTextView = findViewById(R.id.bearingTextView);
diffTextView = findViewById(R.id.diffTextView);

final Compass compass = new Compass();
pathFinder = new PathFinder(this);
Expand Down Expand Up @@ -141,6 +156,7 @@ private void onConnected(BluetoothSerialDevice connectedDevice) {

@Override
public void whenActionChanged(Action action) {

if (deviceInterface != null) {
switch (action) {
case FORWARD:
Expand Down Expand Up @@ -215,23 +231,20 @@ public void whenLocationAccurate() {
connectionManager.connect(() -> {
try {
connectionManager.getLocationPoints(pathFinder.location, this::followPathOfPoints);
connectionManager.receiveImageRequests(listener -> {
try {
controller.send((buffer) -> {
Log.d(TAG, "received");
listener.received(buffer);
});
} catch (CameraAccessException e) {
e.printStackTrace();
}
}, (roadPosition, frameTime) -> {
connectionManager.receiveImageRequests(listener -> controller.send((buffer) -> {
Log.d(TAG, "received");
listener.received(buffer);
}), (roadPosition, frameTime) -> {
for (List<Double> cords : roadPosition) {
double x = cords.get(0) - 0.5f;
//double y = cords.get(1);

if (x < -0.2f || x > 0.2) {
if (pathFinder.isFrameValid(frameTime)) {
pathFinder.setAngleDiff((int) (x * 60));
int diff = (int) (x * 60);

diffTextView.setText(String.valueOf(diff));
pathFinder.setAngleDiff(diff);
}
}
}
Expand All @@ -244,6 +257,7 @@ public void whenLocationAccurate() {

@Override
protected void onDestroy() {
controller.stop();
connectionManager.disconnect();
super.onDestroy();
}
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/com/vladd11/app/robot/PathFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ public void start() {
public void run() {
if (isLocationAccurate && target != null) {
float relativeBearing = location.bearingTo(target) - heading;
if (relativeBearing < 0) {
relativeBearing = 360 + relativeBearing;
}

if (!(almostEqual(relativeBearing, 0, 7) || almostEqual(relativeBearing, 360, 7))) {
if (!(almostEqual(relativeBearing, 0, 14))) {
if (relativeBearing < 0) listener.whenActionChanged(Action.LEFT);
else listener.whenActionChanged(Action.RIGHT);
} else listener.whenActionChanged(Action.FORWARD);
Expand All @@ -65,7 +62,7 @@ public void onLocationResult(@NonNull LocationResult locationResult) {
}

if (target != null) {
if (location.distanceTo(target) > location.getAccuracy()) {
if (location.distanceTo(target) > Math.max(location.getAccuracy(), 5)) {
shouldForward = true;
} else nextPoint();

Expand Down Expand Up @@ -109,7 +106,7 @@ public int getAngle() {
}

public void setAngleDiff(int angleDiff) {
this.diff = angleDiff;
//this.diff = angleDiff;
}

@Override
Expand Down
Loading

0 comments on commit 3dd04d1

Please sign in to comment.