1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-29 07:24:13 +01:00

AndroidGCS Controller: Use the controller mode preference to select mode1 versus mode2

This commit is contained in:
James Cotton 2012-08-30 11:00:50 -05:00
parent 3488f0d2a2
commit 0f29f6e403
2 changed files with 85 additions and 36 deletions

View File

@ -19,7 +19,7 @@
<item>Mode 2</item> <item>Mode 2</item>
</string-array> </string-array>
<string-array name="controllerTypeValues"> <string-array name="controllerTypeValues">
<item>0</item>
<item>1</item> <item>1</item>
<item>2</item>
</string-array> </string-array>
</resources> </resources>

View File

@ -34,7 +34,9 @@ import org.openpilot.uavtalk.UAVDataObject;
import org.openpilot.uavtalk.UAVObject; import org.openpilot.uavtalk.UAVObject;
import org.openpilot.uavtalk.UAVObjectField; import org.openpilot.uavtalk.UAVObjectField;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -76,7 +78,7 @@ public class Controller extends ObjectManagerActivity {
@Override @Override
public void update(Observable observable, Object data) { public void update(Observable observable, Object data) {
// Once we have updated settings we can active the GCS receiver mode // Once we have updated settings we can active the GCS receiver mode
Log.d(TAG,"Got update from settings"); if (DEBUG) Log.d(TAG,"Got update from settings");
UAVDataObject manualControlSettings = (UAVDataObject) objMngr.getObject("ManualControlSettings"); UAVDataObject manualControlSettings = (UAVDataObject) objMngr.getObject("ManualControlSettings");
if(manualControlSettings != null) { if(manualControlSettings != null) {
manualControlSettings.removeUpdatedObserver(this); manualControlSettings.removeUpdatedObserver(this);
@ -89,7 +91,27 @@ public class Controller extends ObjectManagerActivity {
void onOPConnected() { void onOPConnected() {
super.onOPConnected(); super.onOPConnected();
Log.d(TAG, "onOPConnected()"); if (DEBUG) Log.d(TAG, "onOPConnected()");
DualJoystickView joystick = (DualJoystickView) findViewById(R.id.dualjoystickView);
joystick.setMovementConstraint(JoystickView.CONSTRAIN_BOX);
joystick.setMovementRange((int)MOVEMENT_RANGE, (int)MOVEMENT_RANGE);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int mode = Integer.decode(prefs.getString("controller_type", "1"));
switch(mode) {
case 1:
if (DEBUG) Log.d(TAG, "Mode1 connected");
joystick.setOnJostickMovedListener(mode1_left, mode1_right);
break;
case 2:
if (DEBUG) Log.d(TAG, "Mode2 connected");
joystick.setOnJostickMovedListener(mode2_left, mode2_right);
break;
default:
Log.e(TAG, "Unknown controller type");
return;
}
// Subscribe to updates from ManualControlCommand and show the values for crude feedback // Subscribe to updates from ManualControlCommand and show the values for crude feedback
UAVDataObject manualControl = (UAVDataObject) objMngr.getObject("ManualControlCommand"); UAVDataObject manualControl = (UAVDataObject) objMngr.getObject("ManualControlCommand");
@ -100,39 +122,6 @@ public class Controller extends ObjectManagerActivity {
manualSettings.addUpdatedObserver(settingsUpdated); manualSettings.addUpdatedObserver(settingsUpdated);
manualSettings.updateRequested(); manualSettings.updateRequested();
final double MOVEMENT_RANGE = 50.0;
DualJoystickView joystick = (DualJoystickView) findViewById(R.id.dualjoystickView);
joystick.setMovementConstraint(JoystickView.CONSTRAIN_BOX);
joystick.setMovementRange((int)MOVEMENT_RANGE, (int)MOVEMENT_RANGE);
// Hardcode a Mode 1 listener for now
joystick.setOnJostickMovedListener(new JoystickMovedListener() {
@Override
public void OnMoved(int pan, int tilt) {
pitch = tilt / MOVEMENT_RANGE;
yaw = pan / MOVEMENT_RANGE;
leftJoystickHeld = true;
}
@Override
public void OnReleased() { leftJoystickHeld = false; throttle = -1; updated = true; }
@Override
public void OnReturnedToCenter() { }
}, new JoystickMovedListener() {
@Override
public void OnMoved(int pan, int tilt) {
throttle = (-tilt + (MOVEMENT_RANGE -5)) / (MOVEMENT_RANGE - 5);
throttle *= 0.5;
if (throttle < 0)
throttle = -1;
roll = pan / MOVEMENT_RANGE;
rightJoystickHeld = true;
}
@Override
public void OnReleased() { rightJoystickHeld = false; throttle = -1; updated = true; }
@Override
public void OnReturnedToCenter() { }
});
//! This timer task actually periodically sends updates to the UAV //! This timer task actually periodically sends updates to the UAV
TimerTask controllerTask = new TimerTask() { TimerTask controllerTask = new TimerTask() {
@Override @Override
@ -248,4 +237,64 @@ public class Controller extends ObjectManagerActivity {
return (float) (neutral + (neutral - CHANNEL_MIN) * in); return (float) (neutral + (neutral - CHANNEL_MIN) * in);
} }
private final JoystickMovedListener mode1_left = new JoystickMovedListener() {
@Override
public void OnMoved(int pan, int tilt) {
pitch = tilt / MOVEMENT_RANGE;
yaw = pan / MOVEMENT_RANGE;
leftJoystickHeld = true;
}
@Override
public void OnReleased() { leftJoystickHeld = false; throttle = -1; updated = true; }
@Override
public void OnReturnedToCenter() { }
};
private final JoystickMovedListener mode1_right = new JoystickMovedListener() {
@Override
public void OnMoved(int pan, int tilt) {
throttle = (-tilt + (MOVEMENT_RANGE -5)) / (MOVEMENT_RANGE - 5);
throttle *= 0.5;
if (throttle < 0)
throttle = -1;
roll = pan / MOVEMENT_RANGE;
rightJoystickHeld = true;
}
@Override
public void OnReleased() { rightJoystickHeld = false; throttle = -1; updated = true; }
@Override
public void OnReturnedToCenter() { }
};
private final JoystickMovedListener mode2_left = new JoystickMovedListener() {
@Override
public void OnMoved(int pan, int tilt) {
throttle = (-tilt + (MOVEMENT_RANGE -5)) / (MOVEMENT_RANGE - 5);
throttle *= 0.5;
if (throttle < 0)
throttle = -1;
yaw = pan / MOVEMENT_RANGE;
leftJoystickHeld = true;
}
@Override
public void OnReleased() { leftJoystickHeld = false; throttle = -1; updated = true; }
@Override
public void OnReturnedToCenter() { }
};
private final JoystickMovedListener mode2_right = new JoystickMovedListener() {
@Override
public void OnMoved(int pan, int tilt) {
pitch = tilt / MOVEMENT_RANGE;
roll = pan / MOVEMENT_RANGE;
rightJoystickHeld = true;
}
@Override
public void OnReleased() { rightJoystickHeld = false; throttle = -1; updated = true; }
@Override
public void OnReturnedToCenter() { }
};
final double MOVEMENT_RANGE = 50.0;
} }