Skip to content

Commit ffbe093

Browse files
Merge pull request #5 from DarmawanRobotics/feat/touch-sticks
feat: any Android phone or tablet, with touch sticks (1.1.0)
2 parents b8e65ee + 2e8bb1f commit ffbe093

15 files changed

Lines changed: 234 additions & 46 deletions

File tree

‎README.md‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ so it drives a Pashupati robot too) and adds the X30's own parts.
1616
- The Skydroid H12's own sticks (through Skydroid's SDK, [below](#skydroid-h12)): left stick drives
1717
and strafes, right stick turns; key A switches AUTO / REMOTE, B is STOP, C / D and switch E set
1818
the speed level.
19-
- Or a gamepad (a Retroid, any Bluetooth pad), or touch sticks on the screen when there are no
20-
physical ones (any phone or tablet).
19+
- Or a gamepad (a Retroid, any Bluetooth pad), or touch sticks on the screen: the same APK runs
20+
on any Android 7+ phone or tablet ([below](#any-android-phone-or-tablet)).
2121
- AUTO / REMOTE, three speed levels, the X30's actions (stand up, sit down, stepping, gaits) as
2222
icons. **STOP** is the software e-stop, released by holding the button for a second.
2323

@@ -68,6 +68,24 @@ adb install -r build/app/outputs/flutter-apk/app-release.apk
6868
screen tap the version three times: the robot is `192.168.1.106:9870` (its navigation host), and
6969
the token is the robot's `X30_TELEOP_TOKEN` (`/etc/x30/navigation.env`).
7070

71+
## Any Android phone or tablet
72+
73+
The release APK runs on any Android device, not only the H12. Skydroid's SDK starts only on a
74+
Skydroid handheld (it recognizes them by model); anywhere else the app says *Not a Skydroid
75+
handheld* in the settings and drives with the touch sticks on the screen, or a gamepad.
76+
77+
Settings (gear), **Sticks**:
78+
79+
| Choice | Drives |
80+
|---|---|
81+
| **Auto** (default) | the H12's sticks, else a gamepad, else the touch sticks |
82+
| **H12** | only the H12's sticks; also starts Skydroid's SDK on a handheld whose model it does not recognize |
83+
| **Gamepad** | only a gamepad (Bluetooth or USB) |
84+
| **Touch** | only the touch sticks, even with a gamepad or the H12 there |
85+
86+
The left touch stick drives and strafes, the right one turns; release them and the robot stops.
87+
Hold the phone in landscape.
88+
7189
## Skydroid H12
7290

7391
The H12 sends its sticks over its own radio, not as an Android gamepad: only Skydroid's RC SDK

‎android/app/src/main/kotlin/com/darmawanrobotics/x30_controller/MainActivity.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ class MainActivity : FlutterActivity(), GamepadsCompatibleActivity {
2727
MethodChannel(messenger, "x30/skydroid").setMethodCallHandler { call, result ->
2828
when (call.method) {
2929
"available" -> result.success(SkydroidSticks.available())
30+
"device" -> result.success(SkydroidSticks.device())
3031
else -> result.notImplemented()
3132
}
3233
}
3334
// channel values as a list of ints, the link state as text
3435
EventChannel(messenger, "x30/skydroid/channels").setStreamHandler(
3536
object : EventChannel.StreamHandler {
3637
override fun onListen(arguments: Any?, events: EventChannel.EventSink) {
38+
// force: the H12's sticks chosen in the settings, whatever the model says
39+
val force = (arguments as? Map<*, *>)?.get("force") == true
3740
sticks.start(object : SkydroidSticks.Listener {
3841
override fun onChannels(channels: IntArray) {
3942
events.success(channels.toList())
@@ -42,7 +45,7 @@ class MainActivity : FlutterActivity(), GamepadsCompatibleActivity {
4245
override fun onState(state: String) {
4346
events.success(state)
4447
}
45-
})
48+
}, force)
4649
}
4750

4851
override fun onCancel(arguments: Any?) {

‎android/app/src/noskydroid/java/com/darmawanrobotics/x30_controller/SkydroidSticks.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ public static boolean available() {
2121
return false;
2222
}
2323

24-
public void start(Listener listener) {
24+
/** No SDK, no handheld to recognize. */
25+
public static String device() {
26+
return "none";
27+
}
28+
29+
public void start(Listener listener, boolean force) {
2530
listener.onState("no Skydroid SDK in this build");
2631
}
2732

‎android/app/src/skydroid/java/com/darmawanrobotics/x30_controller/SkydroidSticks.java‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.darmawanrobotics.x30_controller;
22

33
import android.content.Context;
4+
import android.os.Build;
45
import android.os.Handler;
56
import android.os.Looper;
67

@@ -12,6 +13,7 @@
1213
import com.skydroid.rcsdk.common.callback.KeyListener;
1314
import com.skydroid.rcsdk.common.error.SkyException;
1415
import com.skydroid.rcsdk.key.RemoteControllerKey;
16+
import com.skydroid.rcsdk.utils.RCSDKUtils;
1517

1618
import java.util.concurrent.Executors;
1719
import java.util.concurrent.ScheduledExecutorService;
@@ -22,6 +24,10 @@
2224
* The sticks, keys and switches of a Skydroid handheld (H12, H12 Pro, H16, ...) through
2325
* Skydroid's RC SDK (rcsdk-*.aar in android/app/libs). The H12 answers a request for its
2426
* channel values (asked every PERIOD_MS, one request at a time); the H16 pushes them.
27+
*
28+
* On any other device (a phone, a tablet) the SDK is never started: it knows the handhelds by
29+
* their model, and on an unknown one it would only fail. Every call into it is guarded, so a
30+
* problem of the SDK reports a state and never stops the app.
2531
*/
2632
public final class SkydroidSticks {
2733
/** Channel values (about 1050..1950, 1500 centred) and the link state, on the main thread. */
@@ -63,13 +69,42 @@ public SkydroidSticks(Context context) {
6369
this.context = context.getApplicationContext();
6470
}
6571

66-
/** True: this build has Skydroid's SDK. */
72+
/** True: this build has Skydroid's SDK and runs on a Skydroid handheld. */
6773
public static boolean available() {
68-
return true;
74+
try {
75+
return RCSDKUtils.getDeviceType() != DeviceType.UNKNOWN;
76+
} catch (Throwable error) {
77+
return false;
78+
}
79+
}
80+
81+
/** The handheld the SDK recognizes (H12, H12Pro, H16, ...), UNKNOWN on any other device. */
82+
public static String device() {
83+
try {
84+
return RCSDKUtils.getDeviceType().name();
85+
} catch (Throwable error) {
86+
return "UNKNOWN";
87+
}
6988
}
7089

71-
public synchronized void start(Listener listener) {
90+
/**
91+
* Read the sticks for listener. force: also on a model the SDK does not recognize (the H12's
92+
* sticks chosen in the app's settings), for a handheld with an unexpected model name.
93+
*/
94+
public synchronized void start(Listener listener, boolean force) {
7295
this.listener = listener;
96+
if (!force && !available()) {
97+
report("not a Skydroid handheld (" + Build.MODEL + ")");
98+
return;
99+
}
100+
try {
101+
connect();
102+
} catch (Throwable error) {
103+
report("failed: " + error);
104+
}
105+
}
106+
107+
private void connect() {
73108
if (!initialized) {
74109
initialized = true;
75110
RCSDKManager.INSTANCE.initSDK(context, new SDKManagerCallBack() {
@@ -100,16 +135,27 @@ public synchronized void stop() {
100135
poller.shutdownNow();
101136
poller = null;
102137
}
103-
KeyManager.INSTANCE.cancelListen(pushed);
138+
if (!initialized) return; // never started: nothing to release
139+
try {
140+
KeyManager.INSTANCE.cancelListen(pushed);
141+
RCSDKManager.INSTANCE.disconnectRC(); // else the port stays taken
142+
} catch (Throwable error) {
143+
// the app goes on; the SDK is started again with the next listener
144+
}
104145
reading = false;
105-
RCSDKManager.INSTANCE.disconnectRC(); // else the port stays taken
106146
}
107147

108148
private synchronized void read() {
109149
if (reading) return;
110150
reading = true;
111-
if (RCSDKManager.INSTANCE.getDeviceType() == DeviceType.H16) {
112-
KeyManager.INSTANCE.listen(RemoteControllerKey.INSTANCE.getKeyH16Channels(), pushed);
151+
try {
152+
if (RCSDKManager.INSTANCE.getDeviceType() == DeviceType.H16) {
153+
KeyManager.INSTANCE.listen(RemoteControllerKey.INSTANCE.getKeyH16Channels(), pushed);
154+
return;
155+
}
156+
} catch (Throwable error) {
157+
report("failed: " + error);
158+
reading = false;
113159
return;
114160
}
115161
poller = Executors.newSingleThreadScheduledExecutor();
@@ -121,7 +167,11 @@ private void ask() {
121167
long last = asked.get();
122168
if (last != 0 && now - last < STUCK_MS) return; // the previous answer is still coming
123169
asked.set(now);
124-
KeyManager.INSTANCE.get(RemoteControllerKey.INSTANCE.getKeyChannels(), answer);
170+
try {
171+
KeyManager.INSTANCE.get(RemoteControllerKey.INSTANCE.getKeyChannels(), answer);
172+
} catch (Throwable error) {
173+
asked.set(0); // a scheduled task that throws is never run again: keep asking
174+
}
125175
}
126176

127177
private void deliver(int[] channels) {

‎lib/input/skydroid_input.dart‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ class SkydroidInput extends ChangeNotifier {
3636
final _levels = StreamController<int>.broadcast();
3737
DateTime _last = DateTime.fromMillisecondsSinceEpoch(0);
3838

39-
/// This build has Skydroid's SDK.
39+
/// This build has Skydroid's SDK and runs on a Skydroid handheld.
4040
bool available = false;
4141

42+
/// The handheld the SDK found: H12, H12Pro, H16, ...; UNKNOWN on any other device (a phone, a
43+
/// tablet); none without the SDK in the build.
44+
String device = 'none';
45+
4246
/// The SDK's link to the handheld's controls: connecting, connected, failed: ..., disconnected.
4347
String state = '';
4448

@@ -54,16 +58,24 @@ class SkydroidInput extends ChangeNotifier {
5458
/// Channel values arrived in the last half second.
5559
bool get live => DateTime.now().difference(_last) < const Duration(milliseconds: 500);
5660

57-
Future<void> start() async {
61+
bool _forced = false;
62+
63+
/// Read the handheld's sticks when this is a Skydroid handheld. [force]: also when the SDK does
64+
/// not recognize the model (the H12's sticks chosen in the settings); never without the SDK.
65+
Future<void> start({bool force = false}) async {
5866
try {
5967
available = await _methods.invokeMethod<bool>('available') ?? false;
68+
device = await _methods.invokeMethod<String>('device') ?? 'none';
6069
} on PlatformException {
6170
available = false;
6271
} on MissingPluginException {
6372
available = false; // tests, desktop: no Android side
6473
}
65-
if (!available) return;
66-
_subscription ??= _events.receiveBroadcastStream().listen(
74+
if (!available && !(force && device != 'none')) return;
75+
if (_subscription != null && (_forced || !force)) return;
76+
await _subscription?.cancel();
77+
_forced = force;
78+
_subscription = _events.receiveBroadcastStream({'force': force}).listen(
6779
onEvent,
6880
onError: (Object error) {
6981
state = 'error: $error';

‎lib/input/sticks.dart‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class StickReading {
2727
}
2828

2929
/// The drive sticks of the handheld: the Skydroid H12's own (through its SDK), a gamepad (a
30-
/// Retroid, any Bluetooth pad), or the touch sticks on the screen, in that order: the first that
31-
/// is live drives.
30+
/// Retroid, any Bluetooth pad), or the touch sticks on the screen. In [InputMode.auto] the first
31+
/// that is live drives, in that order; otherwise only the one chosen in the settings.
3232
class Sticks extends ChangeNotifier {
3333
Sticks({required this.skydroid, required this.gamepad});
3434

@@ -48,12 +48,33 @@ class Sticks extends ChangeNotifier {
4848
notifyListeners();
4949
}
5050

51-
/// Something can drive: the H12's sticks, a gamepad, or the touch sticks when shown.
52-
bool any(AppSettings settings) => skydroid.live || gamepad.connected || settings.touchSticks;
51+
/// The source that drives now, under the input chosen in [settings]; none when the chosen
52+
/// sticks are not there.
53+
StickSource source(AppSettings settings) {
54+
switch (settings.input) {
55+
case InputMode.h12:
56+
return skydroid.live ? StickSource.skydroid : StickSource.none;
57+
case InputMode.gamepad:
58+
return gamepad.connected ? StickSource.gamepad : StickSource.none;
59+
case InputMode.touch:
60+
return StickSource.touch;
61+
case InputMode.auto:
62+
if (skydroid.live) return StickSource.skydroid;
63+
if (gamepad.connected) return StickSource.gamepad;
64+
return StickSource.touch;
65+
}
66+
}
67+
68+
/// Something can drive.
69+
bool any(AppSettings settings) => source(settings) != StickSource.none;
70+
71+
/// The touch sticks are on the screen.
72+
bool showTouch(AppSettings settings) => source(settings) == StickSource.touch;
5373

54-
/// The command of the live source now.
74+
/// The command of the source that drives now.
5575
StickReading read(AppSettings settings) {
56-
if (skydroid.live) {
76+
final from = source(settings);
77+
if (from == StickSource.skydroid) {
5778
final map = settings.h12Channels;
5879
final invert = settings.h12Invert;
5980
return StickReading(
@@ -63,7 +84,7 @@ class Sticks extends ChangeNotifier {
6384
GamepadInput.shaped(skydroid.axis(map[2], invert: invert[2])),
6485
);
6586
}
66-
if (gamepad.connected) {
87+
if (from == StickSource.gamepad) {
6788
return StickReading(
6889
StickSource.gamepad,
6990
GamepadInput.shaped(gamepad.axis(GamepadAxis.leftStickY)),
@@ -72,7 +93,7 @@ class Sticks extends ChangeNotifier {
7293
hold: !settings.deadman || gamepad.pressed(GamepadButton.rightBumper),
7394
);
7495
}
75-
if (settings.touchSticks) {
96+
if (from == StickSource.touch) {
7697
return StickReading(
7798
StickSource.touch,
7899
GamepadInput.shaped(_left.dy, 0.08),

‎lib/l10n.dart‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ const _en = <String, String>{
6868
'field_stale': 'No scan',
6969
'field_off': 'Field off',
7070
'health_issues': 'Needs attention',
71-
'no_gamepad': 'No sticks: turn on the touch sticks in the settings',
71+
'no_gamepad': 'No gamepad: connect one or choose other sticks in the settings',
72+
'no_h12': 'The H12 sticks do not answer: choose other sticks in the settings',
7273
'speed_toast': 'Speed {n}: {name}',
7374
'speed_1': 'Slow',
7475
'speed_2': 'Normal',
@@ -273,10 +274,19 @@ const _en = <String, String>{
273274
'camera_next': 'Visible / thermal camera',
274275
'stick_drive': 'Drive',
275276
'stick_turn': 'Turn',
276-
'touch_sticks': 'Sticks on the screen when there are no physical ones',
277+
'input': 'Sticks',
278+
'input_auto': 'Auto',
279+
'input_h12': 'H12',
280+
'input_gamepad': 'Gamepad',
281+
'input_touch': 'Touch',
282+
'input_hint_auto': 'The H12 sticks, else a gamepad, else the sticks on the screen',
283+
'input_hint_h12': 'Only the sticks of the Skydroid H12',
284+
'input_hint_gamepad': 'Only a gamepad (Bluetooth or USB)',
285+
'input_hint_touch': 'Sticks on the screen, on any phone or tablet',
277286
'h12_sticks': 'Skydroid H12 sticks',
278287
'h12_state': 'H12: {state}',
279288
'h12_none': 'This build has no Skydroid SDK (README, "Skydroid H12")',
289+
'h12_not_here': 'Not a Skydroid handheld: the touch sticks or a gamepad drive',
280290
'h12_channel': 'Channel',
281291
'h12_forward': 'Forward',
282292
'h12_right': 'Strafe right',
@@ -337,7 +347,8 @@ const _id = <String, String>{
337347
'field_stale': 'Scan basi',
338348
'field_off': 'Field mati',
339349
'health_issues': 'Perlu dicek',
340-
'no_gamepad': 'Tidak ada stik: nyalakan stik layar di pengaturan',
350+
'no_gamepad': 'Tidak ada gamepad: sambungkan atau pilih stik lain di pengaturan',
351+
'no_h12': 'Stik H12 tidak menjawab: pilih stik lain di pengaturan',
341352
'speed_toast': 'Kecepatan {n}: {name}',
342353
'speed_1': 'Pelan',
343354
'speed_2': 'Sedang',
@@ -538,10 +549,19 @@ const _id = <String, String>{
538549
'camera_next': 'Kamera visual / termal',
539550
'stick_drive': 'Jalan',
540551
'stick_turn': 'Putar',
541-
'touch_sticks': 'Stik di layar bila tidak ada stik fisik',
552+
'input': 'Stik',
553+
'input_auto': 'Otomatis',
554+
'input_h12': 'H12',
555+
'input_gamepad': 'Gamepad',
556+
'input_touch': 'Layar',
557+
'input_hint_auto': 'Stik H12, kalau tidak ada gamepad, kalau tidak ada stik di layar',
558+
'input_hint_h12': 'Hanya stik Skydroid H12',
559+
'input_hint_gamepad': 'Hanya gamepad (Bluetooth atau USB)',
560+
'input_hint_touch': 'Stik di layar, di HP atau tablet apa saja',
542561
'h12_sticks': 'Stik Skydroid H12',
543562
'h12_state': 'H12: {state}',
544563
'h12_none': 'Build ini tanpa SDK Skydroid (README, "Skydroid H12")',
564+
'h12_not_here': 'Bukan handheld Skydroid: stik layar atau gamepad yang mengemudi',
545565
'h12_channel': 'Channel',
546566
'h12_forward': 'Maju',
547567
'h12_right': 'Geser kanan',

‎lib/main.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ Future<void> main() async {
2525
gamepad: GamepadInput()..start(),
2626
video: WebRtcVideo(),
2727
);
28-
unawaited(services.skydroid.start()); // the H12's own sticks, when the SDK is built in
28+
// the H12's own sticks, when the SDK is built in and this is a Skydroid handheld; chosen in the
29+
// settings, also when the SDK does not know the handheld's model
30+
void startSkydroid() =>
31+
unawaited(services.skydroid.start(force: services.settings.input == InputMode.h12));
32+
startSkydroid();
33+
services.settings.addListener(startSkydroid);
2934
runApp(TeleopApp(services: services));
3035
}
3136

‎lib/screens/connect_screen.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import '../widgets/hud.dart';
1010
import 'drive_screen.dart';
1111
import 'settings_screen.dart';
1212

13-
const appVersion = '1.0.2'; // the version in pubspec.yaml (test/version_test.dart)
13+
const appVersion = '1.1.0'; // the version in pubspec.yaml (test/version_test.dart)
1414

1515
/// Robot picker: the main robot from the settings plus every robot that answers on the LAN.
1616
class ConnectScreen extends StatefulWidget {

0 commit comments

Comments
 (0)