use of org.openbot.vehicle.Vehicle in project OpenBot by isl-org.
the class OpenBotApplication method onCreate.
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int baudRate = Integer.parseInt(sharedPreferences.getString("baud_rate", "115200"));
vehicle = new Vehicle(this, baudRate);
vehicle.connectUsb();
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree() {
@NonNull
@Override
protected String createStackElementTag(@NotNull StackTraceElement element) {
return super.createStackElementTag(element) + ":" + element.getLineNumber();
}
});
}
}
use of org.openbot.vehicle.Vehicle in project OpenBot by isl-org.
the class SettingsFragment method onCreatePreferences.
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
mViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
vehicle = mViewModel.getVehicle().getValue();
connection = findPreference("connection");
if (connection != null) {
connection.setTitle("No Device");
if (vehicle != null && vehicle.isUsbConnected()) {
connection.setChecked(true);
connection.setTitle(vehicle.getUsbConnection().getProductName());
} else {
connection.setTitle("No Device");
connection.setChecked(false);
}
connection.setOnPreferenceClickListener(preference -> {
Timber.d(String.valueOf(connection.isChecked()));
if (vehicle != null) {
if (connection.isChecked()) {
vehicle.connectUsb();
if (vehicle.isUsbConnected())
connection.setTitle(vehicle.getUsbConnection().getProductName());
else {
connection.setTitle("No Device");
connection.setChecked(false);
Toast.makeText(requireContext().getApplicationContext(), "Please check the USB connection.", Toast.LENGTH_SHORT).show();
}
} else {
vehicle.disconnectUsb();
connection.setTitle("No Device");
}
mViewModel.setUsbStatus(vehicle.isUsbConnected());
}
return true;
});
}
camera = findPreference("camera");
if (camera != null) {
camera.setChecked(PermissionUtils.hasCameraPermission(requireActivity()));
camera.setOnPreferenceChangeListener((preference, newValue) -> {
if (camera.isChecked())
startInstalledAppDetailsActivity(requireActivity());
else {
if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_CAMERA)) {
startInstalledAppDetailsActivity(requireActivity());
} else {
requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_CAMERA });
}
}
return false;
});
}
storage = findPreference("storage");
if (storage != null) {
storage.setChecked(PermissionUtils.hasStoragePermission(requireActivity()));
storage.setOnPreferenceChangeListener((preference, newValue) -> {
if (storage.isChecked())
startInstalledAppDetailsActivity(requireActivity());
else {
if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_STORAGE)) {
startInstalledAppDetailsActivity(requireActivity());
} else
requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_STORAGE });
}
return false;
});
}
location = findPreference("location");
if (location != null) {
location.setChecked(PermissionUtils.hasLocationPermission(requireActivity()));
location.setOnPreferenceChangeListener((preference, newValue) -> {
if (location.isChecked())
startInstalledAppDetailsActivity(requireActivity());
else {
if (!PermissionUtils.shouldShowRational(requireActivity(), PERMISSION_LOCATION)) {
startInstalledAppDetailsActivity(requireActivity());
} else
requestPermissionLauncher.launch(new String[] { PERMISSION_LOCATION });
}
return false;
});
}
mic = findPreference("mic");
if (mic != null) {
mic.setChecked(PermissionUtils.hasAudioPermission(requireActivity()));
mic.setOnPreferenceChangeListener((preference, newValue) -> {
if (mic.isChecked())
startInstalledAppDetailsActivity(requireActivity());
else {
if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_AUDIO)) {
startInstalledAppDetailsActivity(requireActivity());
} else
requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_AUDIO });
}
return false;
});
}
ListPreference streamMode = findPreference("video_server");
if (streamMode != null)
streamMode.setOnPreferenceChangeListener((preference, newValue) -> {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle(R.string.confirm_title);
builder.setMessage(R.string.stream_change_body);
builder.setPositiveButton("Yes", (dialog, id) -> {
streamMode.setValue(newValue.toString());
restartApp();
});
builder.setNegativeButton("Cancel", (dialog, id) -> streamMode.setValue(streamMode.getEntry().toString()));
AlertDialog dialog = builder.create();
dialog.show();
return false;
});
}
use of org.openbot.vehicle.Vehicle in project OpenBot by isl-org.
the class CameraActivity method onCreate.
@Override
protected void onCreate(final Bundle savedInstanceState) {
LOGGER.d("onCreate " + this);
super.onCreate(null);
context = getApplicationContext();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
vehicle = OpenBotApplication.vehicle;
phoneController = PhoneController.getInstance(this);
setContentView(R.layout.activity_camera);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
if (PermissionUtils.hasCameraPermission(this)) {
setFragment();
} else {
PermissionUtils.requestCameraPermission(this);
}
preferencesManager = new SharedPreferencesManager(this);
connectionSwitchCompat = findViewById(R.id.connection_switch);
threadsTextView = findViewById(R.id.threads);
plusImageView = findViewById(R.id.plus);
minusImageView = findViewById(R.id.minus);
networkSwitchCompat = findViewById(R.id.network_switch);
bottomSheetLayout = findViewById(R.id.bottom_sheet_layout);
gestureLayout = findViewById(R.id.gesture_layout);
sheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
bottomSheetArrowImageView = findViewById(R.id.bottom_sheet_arrow);
logSwitchCompat = findViewById(R.id.logger_switch);
cameraSwitchCompat = findViewById(R.id.camera_toggle_switch);
baudRateSpinner = findViewById(R.id.baud_rate_spinner);
ArrayAdapter<CharSequence> baudRateAdapter = ArrayAdapter.createFromResource(this, R.array.baud_rates, R.layout.spinner_item);
baudRateAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
baudRateSpinner.setAdapter(baudRateAdapter);
modelSpinner = findViewById(R.id.model_spinner);
masterList = FileUtils.loadConfigJSONFromAsset(this);
List<String> models = masterList.stream().filter(f -> f.pathType != Model.PATH_TYPE.URL).map(f -> FileUtils.nameWithoutExtension(f.name)).collect(Collectors.toList());
masterList.stream().filter(f -> f.name.contains(preferencesManager.getDefaultModel())).findFirst().ifPresent(f -> model = f);
modelAdapter = new ArrayAdapter<>(this, R.layout.spinner_item, new ArrayList<>(models));
modelAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
modelSpinner.setAdapter(modelAdapter);
deviceSpinner = findViewById(R.id.device_spinner);
ArrayAdapter<CharSequence> deviceAdapter = ArrayAdapter.createFromResource(this, R.array.devices, R.layout.spinner_item);
deviceAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
deviceSpinner.setAdapter(deviceAdapter);
logSpinner = findViewById(R.id.log_spinner);
ArrayAdapter<CharSequence> logAdapter = ArrayAdapter.createFromResource(this, R.array.log_settings, R.layout.spinner_item);
logAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
logSpinner.setAdapter(logAdapter);
controlModeSpinner = findViewById(R.id.control_mode_spinner);
ArrayAdapter<CharSequence> controlModeAdapter = ArrayAdapter.createFromResource(this, R.array.control_modes, R.layout.spinner_item);
controlModeAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
controlModeSpinner.setAdapter(controlModeAdapter);
driveModeSpinner = findViewById(R.id.drive_mode_spinner);
ArrayAdapter<CharSequence> driveModeAdapter = ArrayAdapter.createFromResource(this, R.array.drive_modes, R.layout.spinner_item);
driveModeAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
driveModeSpinner.setAdapter(driveModeAdapter);
speedModeSpinner = findViewById(R.id.speed_mode_spinner);
ArrayAdapter<CharSequence> controlAdapter = ArrayAdapter.createFromResource(this, R.array.speed_modes, R.layout.spinner_item);
controlAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
speedModeSpinner.setAdapter(controlAdapter);
ViewTreeObserver vto = gestureLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
gestureLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// int width = bottomSheetLayout.getMeasuredWidth();
int height = gestureLayout.getMeasuredHeight();
sheetBehavior.setPeekHeight(height);
}
});
sheetBehavior.setHideable(false);
sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch(newState) {
case BottomSheetBehavior.STATE_HIDDEN:
break;
case BottomSheetBehavior.STATE_EXPANDED:
bottomSheetArrowImageView.setImageResource(R.drawable.icn_chevron_down);
preferencesManager.setSheetExpanded(true);
break;
case BottomSheetBehavior.STATE_COLLAPSED:
bottomSheetArrowImageView.setImageResource(R.drawable.icn_chevron_up);
preferencesManager.setSheetExpanded(false);
break;
case BottomSheetBehavior.STATE_DRAGGING:
break;
case BottomSheetBehavior.STATE_SETTLING:
bottomSheetArrowImageView.setImageResource(R.drawable.icn_chevron_up);
break;
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
voltageTextView = findViewById(R.id.voltage_info);
speedTextView = findViewById(R.id.speed_info);
sonarTextView = findViewById(R.id.sonar_info);
frameValueTextView = findViewById(R.id.frame_info);
cropValueTextView = findViewById(R.id.crop_info);
inferenceTimeTextView = findViewById(R.id.inference_info);
controlValueTextView = findViewById(R.id.control_info);
connectionSwitchCompat.setOnCheckedChangeListener(this);
networkSwitchCompat.setOnCheckedChangeListener(this);
logSwitchCompat.setOnCheckedChangeListener(this);
cameraSwitchCompat.setOnCheckedChangeListener(this);
plusImageView.setOnClickListener(this);
minusImageView.setOnClickListener(this);
baudRateSpinner.setOnItemSelectedListener(this);
modelSpinner.setOnItemSelectedListener(this);
deviceSpinner.setOnItemSelectedListener(this);
logSpinner.setOnItemSelectedListener(this);
controlModeSpinner.setOnItemSelectedListener(this);
driveModeSpinner.setOnItemSelectedListener(this);
speedModeSpinner.setOnItemSelectedListener(this);
// Intent for sensor service
intentSensorService = new Intent(this, SensorService.class);
setInitialValues();
// Try to connect to serial device
toggleConnection(true);
localBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
switch(action) {
case Constants.USB_ACTION_DATA_RECEIVED:
long timestamp = SystemClock.elapsedRealtimeNanos();
String data = intent.getStringExtra("data");
char header = data.charAt(0);
String body = data.substring(1);
int type = -1;
switch(header) {
case 'f':
vehicle.processVehicleConfig(body);
break;
case 'v':
if (FormatUtils.isNumeric(body)) {
type = SensorService.MSG_VOLTAGE;
vehicle.setBatteryVoltage(Float.parseFloat(body));
runOnUiThread(() -> {
voltageTextView.setText(String.format(Locale.US, "%2.1f V", vehicle.getBatteryVoltage()));
});
} else {
String[] msgParts = body.split(":");
switch(msgParts[0]) {
case "min":
vehicle.setMinMotorVoltage(Float.parseFloat(msgParts[1]));
break;
case "low":
vehicle.setLowBatteryVoltage(Float.parseFloat(msgParts[1]));
break;
case "max":
vehicle.setMaxBatteryVoltage(Float.parseFloat(msgParts[1]));
break;
default:
Toast.makeText(getApplicationContext(), "Invalid voltage message received!", Toast.LENGTH_SHORT).show();
break;
}
}
break;
case 's':
if (FormatUtils.isNumeric(body)) {
type = SensorService.MSG_SONAR;
vehicle.setSonarReading(Float.parseFloat(body));
runOnUiThread(() -> {
sonarTextView.setText(String.format(Locale.US, "%3.0f cm", vehicle.getSonarReading()));
});
}
break;
case 'w':
String[] itemList = body.split(",");
if (itemList.length == 2 && FormatUtils.isNumeric(itemList[0]) && FormatUtils.isNumeric(itemList[1])) {
type = SensorService.MSG_WHEELS;
vehicle.setLeftWheelRpm(Float.parseFloat(itemList[0]));
vehicle.setRightWheelRpm(Float.parseFloat(itemList[1]));
runOnUiThread(() -> {
speedTextView.setText(String.format(Locale.US, "%3.0f,%3.0f rpm", vehicle.getLeftWheelRpm(), vehicle.getRightWheelRpm()));
});
}
break;
case 'b':
type = SensorService.MSG_BUMPER;
break;
}
if (type > 0)
sendVehicleDataToSensorService(timestamp, data, type);
break;
}
}
}
};
IntentFilter localIntentFilter = new IntentFilter();
localIntentFilter.addAction(Constants.USB_ACTION_DATA_RECEIVED);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.registerReceiver(localBroadcastReceiver, localIntentFilter);
vehicle.requestVehicleConfig();
}
Aggregations