Skip to content

Commit

Permalink
Merge branch 'master' into feature/videoCapture
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv2295 authored Sep 16, 2021
2 parents 2988579 + ac40bc3 commit 17a9172
Show file tree
Hide file tree
Showing 93 changed files with 3,178 additions and 1,469 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will check the python code using black

name: Python style check

on:
push:
branches: [ master ]
paths:
- 'controller/**'
- 'policy/**'
- '.github/workflows/python.yml'
pull_request:
branches: [ master ]
paths:
- 'controller/**'
- 'policy/**'
- '.github/workflows/python.yml'

jobs:
style-check:
name: Python style check
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up python
uses: actions/setup-python@v2
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install 'black[jupyter]'
- name: Check style
run: |
black --check .
59 changes: 46 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,74 @@
1. Submit an issue describing the changes you want to implement. If it's only minor changes/bug-fixes, you can skip to step 3.
2. After the scope was discussed in the issue, assign it to yourself. It should show up in the "To do" column in the OpenBot project.
3. Fork the project and clone it locally:

`git clone https://github.com/<user_id>/OpenBot.git`

4. Create a branch:

`git checkout -b <branch-name>`
`git checkout -b <branch-name>`

where `<branch-name>` concisely describes the scope of the work.

5. Do the work, write good commit messages, push your branch to the forked repository:
```

```bash
git add <modified file>
git commit -m <meaningful description>
git push --set-upstream origin <branch-name>
```

6. Create a [pull request](https://github.com/intel-isl/OpenBot/pulls) in GitHub and link the issue to it. It should show up in the "In progress" column in the OpenBot project.
7. Work on any code review feedback you may receive and push it to your fork. The pull request gets updated automatically.
8. Get a cold drink of your choice to reward yourself for making the world a better place.

## Guidelines

- Use same style and formatting as rest of code.
- For the Android code you can run:
1. `./gradlew checkStyle` --> returns java files with incorrect style.
2. `./gradlew applyStyle` --> applies neccessary style changes to all java files.
- For the Arduino and Python code, just try to blend in.
- Use same style and formatting as rest of code.
- For the Java (Android) and Python code see [below](#Formatting).
- For any other code, just try to blend in.
- Update documentation associated with code changes you made.
- If you want to include 3rd party dependencies, please discuss this in the issue first.
- If you want to include 3rd party dependencies, please discuss this in the issue first.
- Pull requests should implement single features with as few changes as possible.
- Make sure you don't include temporary or binary files (the gitignores should mostly take care of this).
- Rebase/merge master into your branch before you submit the pull request.
- If possible, test your code on Windows, Linux and OSX.

## Formatting

### Java

We use a gradle script for formatting java code. Make sure you are in the `android` directory.

You can check your code with:

```bash
./gradlew checkStyle
```

You can apply the style to all files by running:

```bash
./gradlew applyStyle
```

### Python

We use [black](https://pypi.org/project/black/) for formatting python code.

You can check your code in the current directory with:

```bash
black --check .
```

You can apply the style to all files in the current directory by running:

```bash
black .
```

## Further resources

If you are looking for more information about contributing to open-source projects, here are two good references:

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ OpenBot leverages smartphones as brains for low-cost robots. We have designed a
- Build your own [Robot Body](body/README.md)
- Flash the [Arduino Firmware](firmware/README.md)
- Install the [Android Apps](android/README.md)
- Control the robot via [Python](python/README.md)
- Try the [Python Controller](controller/README.md)
- Train your own [Driving Policy](policy/README.md)

## Get the source code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,11 @@ protected void handleDriveCommand(Control control) {
vehicle.setControl(control);
float left = vehicle.getLeftSpeed();
float right = vehicle.getRightSpeed();
binding.controllerContainer.controlInfo.setText(
String.format(Locale.US, "%.0f,%.0f", left, right));
requireActivity()
.runOnUiThread(
() ->
binding.controllerContainer.controlInfo.setText(
String.format(Locale.US, "%.0f,%.0f", left, right)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private void handlePhoneControllerEvents() {
switch (commandType) {
case Constants.CMD_DRIVE:
JSONObject driveValue = event.getJSONObject("driveCmd");

vehicle.setControl(
new Control(
Float.parseFloat(driveValue.getString("l")),
Expand Down
124 changes: 124 additions & 0 deletions android/app/src/main/java/org/openbot/env/ControllerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.openbot.env;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.preference.PreferenceManager;
import org.openbot.utils.ConnectionUtils;

public class ControllerConfig {
private static ControllerConfig _controllerConfig;
SharedPreferences preferences;

enum VIDEO_SERVER_TYPE {
WEBRTC,
RTSP
}

private final boolean isMirrored = true;
private final boolean mute = true;

private String currentServerType;

public static ControllerConfig getInstance() {
if (_controllerConfig == null) {
synchronized (PhoneController.class) {
if (_controllerConfig == null) _controllerConfig = new ControllerConfig();
}
}

return _controllerConfig;
}

void init(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
currentServerType = get("video_server", "WEBRTC");
monitorSettingUpdates();
}

private void set(String name, String value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(name, value);
editor.apply();
}

private String get(String name, String defaultValue) {
try {
return preferences.getString(name, defaultValue);
} catch (ClassCastException e) {
return defaultValue;
}
}

private Boolean getBoolean(String name, Boolean defaultValue) {
try {
return preferences.getBoolean(name, defaultValue);
} catch (ClassCastException e) {
return defaultValue;
}
}

private void setBoolean(String name, boolean value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(name, value);
editor.apply();
}

// specific settings
public boolean isMirrored() {
return getBoolean("MIRRORED", true);
}

public void setMirrored(boolean mirrored) {
setBoolean("MIRRORED", mirrored);
}

public boolean isMute() {
return getBoolean("MUTE", true);
}

public void setMute(boolean mute) {
setBoolean("MUTE", mute);
}

public String getVideoServerType() {
return get("video_server", "WEBRTC");
}

public void setVideoServerType(String type) {
set("video_server", type);
}

private void monitorSettingUpdates() {
ControllerToBotEventBus.subscribe(
this.getClass().getSimpleName(),
event -> {
String commandType = event.getString("command");

switch (commandType) {
case "TOGGLE_MIRROR":
setMirrored(!isMirrored());

// inform the controller of current state
BotToControllerEventBus.emitEvent(
ConnectionUtils.createStatus("TOGGLE_MIRROR", isMirrored()));
break;

case "TOGGLE_SOUND":
setMute(!isMute());

// inform the controller of current state
BotToControllerEventBus.emitEvent(
ConnectionUtils.createStatus("TOGGLE_SOUND", isMute()));
break;
}
},
error -> {
Log.d(null, "Error occurred in monitorConnection: " + error);
},
event ->
event.has("command")
&& (event.getString("command").contains("TOGGLE_MIRROR")
|| event.getString("command").contains("TOGGLE_SOUND")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.reactivex.rxjava3.subjects.PublishSubject;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public final class ControllerToBotEventBus {
Expand All @@ -17,7 +18,15 @@ private ControllerToBotEventBus() {}

private static final PublishSubject<JSONObject> subject = PublishSubject.create();

public static void emitEvent(JSONObject event) {
public static void emitEvent(String event) {
try {
emitEvent(new JSONObject(event));
} catch (JSONException e) {
e.printStackTrace();
}
}

private static void emitEvent(JSONObject event) {
subject.onNext(event);
}

Expand Down
7 changes: 6 additions & 1 deletion android/app/src/main/java/org/openbot/env/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Build;
import android.view.Surface;
import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -227,7 +228,11 @@ public static Matrix getTransformationMatrix(
}

public static int getScreenOrientation(Activity activity) {
switch (activity.getDisplay().getRotation()) {
int rotation =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
? activity.getDisplay().getRotation()
: activity.getWindowManager().getDefaultDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_270:
return 270;
case Surface.ROTATION_180:
Expand Down
14 changes: 2 additions & 12 deletions android/app/src/main/java/org/openbot/env/NearbyConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import java.util.Timer;
import java.util.TimerTask;
import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;
import org.openbot.OpenBotApplication;
import org.openbot.utils.ConnectionUtils;
import timber.log.Timber;
Expand Down Expand Up @@ -156,11 +154,7 @@ public void onConnectionResult(@NonNull String endpointId, ConnectionResolution

pairedDeviceEndpointId = endpointId;
isConnected = true;
try {
ControllerToBotEventBus.emitEvent(new JSONObject("{command: \"CONNECTED\"}"));
} catch (JSONException e) {
e.printStackTrace();
}
ControllerToBotEventBus.emitEvent("{command: \"CONNECTED\"}");
} else {
Timber.i("onConnectionResult: connection failed");
isConnected = false;
Expand All @@ -181,11 +175,7 @@ public void onDisconnected(@NonNull String endpointId) {
Toast.LENGTH_LONG)
.show();
Timber.i("onDisconnected: disconnected from the opponent");
try {
ControllerToBotEventBus.emitEvent(new JSONObject("{command: \"DISCONNECTED\"}"));
} catch (JSONException e) {
e.printStackTrace();
}
ControllerToBotEventBus.emitEvent("{command: \"DISCONNECTED\"}");
}
};

Expand Down
Loading

0 comments on commit 17a9172

Please sign in to comment.