Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use R1 button to discard logging #291

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions android/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ Simple UI for collection of data sets.

- **Model Resolution**: Used to switch between resolutions of images saved for training different models.

- **Save/Discard the Collected Data**: the data collection process can be controlled from the screen or remotely, for instance from a bluetooth controller. When using a bluetooth controller, you may:
- press the **A button** to **start** the data collection process
- press the **A button again** to **stop** data collection and save the collected data in a .zip file
- alternatively press the **R1 button** to **stop** data collection **without saving** the collected data (for instance because of an unexpected collision with the environment)
- remember to use the controller mapping fragment to ensure you are using the correct buttons.

### Controller Mapping

Simple UI to check the button and joystick mapping of a connected BT controller.
Expand Down
57 changes: 36 additions & 21 deletions android/app/src/main/java/org/openbot/logging/LoggerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class LoggerFragment extends CameraFragment {
protected String logFolder;

protected boolean loggingEnabled;
private boolean loggingCanceled;

private Matrix frameToCropTransform;
private Bitmap croppedBitmap;
private int sensorOrientation;
Expand Down Expand Up @@ -105,7 +107,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
Enums.SpeedMode.getByID(preferencesManager.getSpeedMode()))));

binding.loggerSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) -> setIsLoggingActive(isChecked));
(buttonView, isChecked) -> setLoggingActive(isChecked));

binding.cameraToggle.setOnClickListener(v -> toggleCamera());

Expand Down Expand Up @@ -271,10 +273,10 @@ private void startLogging() {
intentSensorService.putExtra("logFolder", logFolder + File.separator + "sensor_data");
requireActivity().startService(intentSensorService);
requireActivity().bindService(intentSensorService, sensorConnection, Context.BIND_AUTO_CREATE);
// Send current vehicle state to log
runInBackground(
() -> {
try {
// Send current vehicle state to log
TimeUnit.MILLISECONDS.sleep(500);
sendControlToSensorService();
sendIndicatorToSensorService();
Expand All @@ -284,40 +286,58 @@ private void startLogging() {
});
}

private void stopLogging() {
private void stopLogging(boolean isCancel) {
if (sensorConnection != null) requireActivity().unbindService(sensorConnection);
requireActivity().stopService(intentSensorService);

// Pack and upload the collected data
runInBackground(
() -> {
try {
String logZipFile = logFolder + ".zip";
// Zip the log folder and then delete it
File folder = new File(logFolder);
File zip = new File(logZipFile);
if (!isCancel) {
// Zip the log folder and then upload it
serverCommunication.upload(zip(folder));
}
TimeUnit.MILLISECONDS.sleep(500);
// These two lines below are messy and may cause bugs. needs to be looked into
ZipUtil.pack(folder, zip);
FileUtils.deleteQuietly(folder);
serverCommunication.upload(zip);
} catch (InterruptedException e) {
Timber.e(e, "Got interrupted.");
}
});
loggingEnabled = false;
}

private File zip(File folder) {
String zipFileName = folder + ".zip";
File zip = new File(zipFileName);
ZipUtil.pack(folder, zip);
return zip;
}

private void cancelLogging() {
loggingCanceled = true;
setLoggingActive(false);
audioPlayer.playFromString("Log deleted!");
}

protected void setIsLoggingActive(boolean loggingActive) {
if (loggingActive && !loggingEnabled) {
protected void toggleLogging() {
loggingCanceled = false;
setLoggingActive(!loggingEnabled);
audioPlayer.playLogging(voice, loggingEnabled);
}

protected void setLoggingActive(boolean enableLogging) {
if (enableLogging && !loggingEnabled) {
if (!PermissionUtils.hasLoggingPermissions(requireActivity())) {
requestPermissionLauncherLogging.launch(Constants.PERMISSIONS_LOGGING);
loggingEnabled = false;
} else {
startLogging();
loggingEnabled = true;
}
} else if (!loggingActive && loggingEnabled) {
stopLogging();
} else if (!enableLogging && loggingEnabled) {
stopLogging(loggingCanceled);
loggingEnabled = false;
}
BotToControllerEventBus.emitEvent(ConnectionUtils.createStatus("LOGS", loggingEnabled));
Expand All @@ -331,17 +351,12 @@ protected void setIsLoggingActive(boolean loggingActive) {
new ActivityResultContracts.RequestMultiplePermissions(),
result -> {
result.forEach((permission, granted) -> allGranted = allGranted && granted);
if (allGranted) setIsLoggingActive(true);
if (allGranted) setLoggingActive(true);
else {
PermissionUtils.showLoggingPermissionsToast(requireActivity());
}
});

protected void handleLogging() {
setIsLoggingActive(!loggingEnabled);
audioPlayer.playLogging(voice, loggingEnabled);
}

@Override
protected void processUSBData(String data) {
long timestamp = SystemClock.elapsedRealtimeNanos();
Expand Down Expand Up @@ -387,7 +402,7 @@ protected void processControllerKeyData(String commandType) {
break;

case Constants.CMD_LOGS:
handleLogging();
toggleLogging();
break;

case Constants.CMD_INDICATOR_LEFT:
Expand Down Expand Up @@ -417,7 +432,7 @@ protected void processControllerKeyData(String commandType) {
Enums.SpeedMode.getByID(preferencesManager.getSpeedMode())));
break;
case Constants.CMD_NETWORK:
// handleNetwork();
cancelLogging();
break;
}
}
Expand Down