Skip to content

Commit 8433be5

Browse files
committed
Merge branch 'master' into feature/videoCapture
2 parents 2988579 + ac40bc3 commit 8433be5

File tree

93 files changed

+3230
-1504
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+3230
-1504
lines changed

.github/workflows/python.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This workflow will check the python code using black
2+
3+
name: Python style check
4+
5+
on:
6+
push:
7+
branches: [ master ]
8+
paths:
9+
- 'controller/**'
10+
- 'policy/**'
11+
- '.github/workflows/python.yml'
12+
pull_request:
13+
branches: [ master ]
14+
paths:
15+
- 'controller/**'
16+
- 'policy/**'
17+
- '.github/workflows/python.yml'
18+
19+
jobs:
20+
style-check:
21+
name: Python style check
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v2
26+
- name: Set up python
27+
uses: actions/setup-python@v2
28+
with:
29+
python-version: 3.7
30+
- name: Install dependencies
31+
run: |
32+
python -m pip install 'black[jupyter]'
33+
- name: Check style
34+
run: |
35+
black --check .

CONTRIBUTING.md

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,74 @@
1010
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.
1111
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.
1212
3. Fork the project and clone it locally:
13-
13+
1414
`git clone https://github.com/<user_id>/OpenBot.git`
1515

1616
4. Create a branch:
1717

18-
`git checkout -b <branch-name>`
19-
18+
`git checkout -b <branch-name>`
19+
2020
where `<branch-name>` concisely describes the scope of the work.
21-
21+
2222
5. Do the work, write good commit messages, push your branch to the forked repository:
23-
24-
```
23+
24+
```bash
2525
git add <modified file>
2626
git commit -m <meaningful description>
2727
git push --set-upstream origin <branch-name>
2828
```
29-
29+
3030
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.
3131
7. Work on any code review feedback you may receive and push it to your fork. The pull request gets updated automatically.
3232
8. Get a cold drink of your choice to reward yourself for making the world a better place.
3333

3434
## Guidelines
3535

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

46+
## Formatting
47+
48+
### Java
49+
50+
We use a gradle script for formatting java code. Make sure you are in the `android` directory.
51+
52+
You can check your code with:
53+
54+
```bash
55+
./gradlew checkStyle
56+
```
57+
58+
You can apply the style to all files by running:
59+
60+
```bash
61+
./gradlew applyStyle
62+
```
63+
64+
### Python
65+
66+
We use [black](https://pypi.org/project/black/) for formatting python code.
67+
68+
You can check your code in the current directory with:
69+
70+
```bash
71+
black --check .
72+
```
73+
74+
You can apply the style to all files in the current directory by running:
75+
76+
```bash
77+
black .
78+
```
79+
80+
## Further resources
4881

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ OpenBot leverages smartphones as brains for low-cost robots. We have designed a
3232
- Build your own [Robot Body](body/README.md)
3333
- Flash the [Arduino Firmware](firmware/README.md)
3434
- Install the [Android Apps](android/README.md)
35-
- Control the robot via [Python](python/README.md)
35+
- Try the [Python Controller](controller/README.md)
3636
- Train your own [Driving Policy](policy/README.md)
3737

3838
## Get the source code

android/app/src/main/java/org/openbot/autopilot/AutopilotFragment.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,11 @@ protected void handleDriveCommand(Control control) {
440440
vehicle.setControl(control);
441441
float left = vehicle.getLeftSpeed();
442442
float right = vehicle.getRightSpeed();
443-
binding.controllerContainer.controlInfo.setText(
444-
String.format(Locale.US, "%.0f,%.0f", left, right));
443+
requireActivity()
444+
.runOnUiThread(
445+
() ->
446+
binding.controllerContainer.controlInfo.setText(
447+
String.format(Locale.US, "%.0f,%.0f", left, right)));
445448
}
446449

447450
@Override

android/app/src/main/java/org/openbot/common/ControlsFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private void handlePhoneControllerEvents() {
168168
switch (commandType) {
169169
case Constants.CMD_DRIVE:
170170
JSONObject driveValue = event.getJSONObject("driveCmd");
171+
171172
vehicle.setControl(
172173
new Control(
173174
Float.parseFloat(driveValue.getString("l")),
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package org.openbot.env;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
import android.util.Log;
6+
import androidx.preference.PreferenceManager;
7+
import org.openbot.utils.ConnectionUtils;
8+
9+
public class ControllerConfig {
10+
private static ControllerConfig _controllerConfig;
11+
SharedPreferences preferences;
12+
13+
enum VIDEO_SERVER_TYPE {
14+
WEBRTC,
15+
RTSP
16+
}
17+
18+
private final boolean isMirrored = true;
19+
private final boolean mute = true;
20+
21+
private String currentServerType;
22+
23+
public static ControllerConfig getInstance() {
24+
if (_controllerConfig == null) {
25+
synchronized (PhoneController.class) {
26+
if (_controllerConfig == null) _controllerConfig = new ControllerConfig();
27+
}
28+
}
29+
30+
return _controllerConfig;
31+
}
32+
33+
void init(Context context) {
34+
preferences = PreferenceManager.getDefaultSharedPreferences(context);
35+
currentServerType = get("video_server", "WEBRTC");
36+
monitorSettingUpdates();
37+
}
38+
39+
private void set(String name, String value) {
40+
SharedPreferences.Editor editor = preferences.edit();
41+
editor.putString(name, value);
42+
editor.apply();
43+
}
44+
45+
private String get(String name, String defaultValue) {
46+
try {
47+
return preferences.getString(name, defaultValue);
48+
} catch (ClassCastException e) {
49+
return defaultValue;
50+
}
51+
}
52+
53+
private Boolean getBoolean(String name, Boolean defaultValue) {
54+
try {
55+
return preferences.getBoolean(name, defaultValue);
56+
} catch (ClassCastException e) {
57+
return defaultValue;
58+
}
59+
}
60+
61+
private void setBoolean(String name, boolean value) {
62+
SharedPreferences.Editor editor = preferences.edit();
63+
editor.putBoolean(name, value);
64+
editor.apply();
65+
}
66+
67+
// specific settings
68+
public boolean isMirrored() {
69+
return getBoolean("MIRRORED", true);
70+
}
71+
72+
public void setMirrored(boolean mirrored) {
73+
setBoolean("MIRRORED", mirrored);
74+
}
75+
76+
public boolean isMute() {
77+
return getBoolean("MUTE", true);
78+
}
79+
80+
public void setMute(boolean mute) {
81+
setBoolean("MUTE", mute);
82+
}
83+
84+
public String getVideoServerType() {
85+
return get("video_server", "WEBRTC");
86+
}
87+
88+
public void setVideoServerType(String type) {
89+
set("video_server", type);
90+
}
91+
92+
private void monitorSettingUpdates() {
93+
ControllerToBotEventBus.subscribe(
94+
this.getClass().getSimpleName(),
95+
event -> {
96+
String commandType = event.getString("command");
97+
98+
switch (commandType) {
99+
case "TOGGLE_MIRROR":
100+
setMirrored(!isMirrored());
101+
102+
// inform the controller of current state
103+
BotToControllerEventBus.emitEvent(
104+
ConnectionUtils.createStatus("TOGGLE_MIRROR", isMirrored()));
105+
break;
106+
107+
case "TOGGLE_SOUND":
108+
setMute(!isMute());
109+
110+
// inform the controller of current state
111+
BotToControllerEventBus.emitEvent(
112+
ConnectionUtils.createStatus("TOGGLE_SOUND", isMute()));
113+
break;
114+
}
115+
},
116+
error -> {
117+
Log.d(null, "Error occurred in monitorConnection: " + error);
118+
},
119+
event ->
120+
event.has("command")
121+
&& (event.getString("command").contains("TOGGLE_MIRROR")
122+
|| event.getString("command").contains("TOGGLE_SOUND")));
123+
}
124+
}

android/app/src/main/java/org/openbot/env/ControllerToBotEventBus.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.reactivex.rxjava3.subjects.PublishSubject;
88
import java.util.HashMap;
99
import java.util.Map;
10+
import org.json.JSONException;
1011
import org.json.JSONObject;
1112

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

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

20-
public static void emitEvent(JSONObject event) {
21+
public static void emitEvent(String event) {
22+
try {
23+
emitEvent(new JSONObject(event));
24+
} catch (JSONException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
private static void emitEvent(JSONObject event) {
2130
subject.onNext(event);
2231
}
2332

android/app/src/main/java/org/openbot/env/ImageUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.graphics.Bitmap;
2222
import android.graphics.Matrix;
2323
import android.graphics.RectF;
24+
import android.os.Build;
2425
import android.view.Surface;
2526
import java.io.File;
2627
import java.io.FileOutputStream;
@@ -227,7 +228,11 @@ public static Matrix getTransformationMatrix(
227228
}
228229

229230
public static int getScreenOrientation(Activity activity) {
230-
switch (activity.getDisplay().getRotation()) {
231+
int rotation =
232+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
233+
? activity.getDisplay().getRotation()
234+
: activity.getWindowManager().getDefaultDisplay().getRotation();
235+
switch (rotation) {
231236
case Surface.ROTATION_270:
232237
return 270;
233238
case Surface.ROTATION_180:

android/app/src/main/java/org/openbot/env/NearbyConnection.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
import java.util.Timer;
3838
import java.util.TimerTask;
3939
import org.jetbrains.annotations.NotNull;
40-
import org.json.JSONException;
41-
import org.json.JSONObject;
4240
import org.openbot.OpenBotApplication;
4341
import org.openbot.utils.ConnectionUtils;
4442
import timber.log.Timber;
@@ -156,11 +154,7 @@ public void onConnectionResult(@NonNull String endpointId, ConnectionResolution
156154

157155
pairedDeviceEndpointId = endpointId;
158156
isConnected = true;
159-
try {
160-
ControllerToBotEventBus.emitEvent(new JSONObject("{command: \"CONNECTED\"}"));
161-
} catch (JSONException e) {
162-
e.printStackTrace();
163-
}
157+
ControllerToBotEventBus.emitEvent("{command: \"CONNECTED\"}");
164158
} else {
165159
Timber.i("onConnectionResult: connection failed");
166160
isConnected = false;
@@ -181,11 +175,7 @@ public void onDisconnected(@NonNull String endpointId) {
181175
Toast.LENGTH_LONG)
182176
.show();
183177
Timber.i("onDisconnected: disconnected from the opponent");
184-
try {
185-
ControllerToBotEventBus.emitEvent(new JSONObject("{command: \"DISCONNECTED\"}"));
186-
} catch (JSONException e) {
187-
e.printStackTrace();
188-
}
178+
ControllerToBotEventBus.emitEvent("{command: \"DISCONNECTED\"}");
189179
}
190180
};
191181

0 commit comments

Comments
 (0)