Files
picar/app/src/main/java/org/vato/carcontroller/SimpleController.java
2020-04-25 16:29:12 +09:30

134 lines
3.9 KiB
Java

package org.vato.carcontroller;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;
import com.google.protobuf.Empty;
import org.vato.carcontroller.LIDAR.LidarTrackingController;
import java.util.stream.Stream;
import io.grpc.stub.StreamObserver;
public class SimpleController extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
private SeekBar steeringSlider;
private SeekBar throttleSlider;
private Switch recordSwitch;
private Switch recordLidarSwitch;
private static PiLoader grpcController;
private PersonTrackingGrpc.PersonTrackingStub trackingStub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_controller);
steeringSlider = findViewById(R.id.steeringBar);
if (steeringSlider != null) {
steeringSlider.setOnSeekBarChangeListener(this);
}
throttleSlider = findViewById(R.id.throttleBar);
if (throttleSlider != null) {
throttleSlider.setOnSeekBarChangeListener(this);
}
recordSwitch = findViewById(R.id.recordSwitch);
recordLidarSwitch = findViewById(R.id.lidarSwitch);
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (grpcController == null) {
grpcController = new PiLoader(prefs.getString("host", "10.0.0.53"), Integer.parseInt(prefs.getString("port", "50051")));
}
// Should call the equivalent of the load method either here or in the loader.
grpcController.start();
}
@Override
protected void onPause() {
super.onPause();
grpcController.stop();
}
@Override
protected void onStop() {
super.onStop();
grpcController.stop();
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
// Call the gRPC method to change the throttle.
switch (seekBar.getId()) {
case R.id.steeringBar:
if (grpcController != null) {
grpcController.updateSteering(i);
}
break;
case R.id.throttleBar:
if (grpcController != null) {
grpcController.updateThrottle(i);
}
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Reset back to the middle, as this is how the controller will normally work.
// Check if this will actually still call the onProgressChanged listener method.
// Otherwise update the steering/throttle here as well.
steeringSlider.setProgress(50);
throttleSlider.setProgress(50);
}
public void saveRecording(View view) {
grpcController.saveRecording();
}
public void record(View view) {
grpcController.record(recordSwitch.isChecked());
}
public void recordLidar(View view) {
StreamObserver<Empty> response = new StreamObserver<Empty>() {
@Override
public void onNext(Empty value) {
Toast.makeText(getApplicationContext(), "Started Recording Lidar", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable t) {
Toast.makeText(getApplicationContext(), "Failed to set lidar recording", Toast.LENGTH_SHORT).show();
}
@Override
public void onCompleted() {
}
};
}
}