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

base auto stop bug fix #152

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions lib/widgets/joystick.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_joystick/flutter_joystick.dart';
import 'package:viam_sdk/viam_sdk.dart';
Expand All @@ -19,6 +21,7 @@ class ViamBaseJoystick extends StatefulWidget {
class _ViamBaseJoystickState extends State<ViamBaseJoystick> {
num y = 0;
num z = 0;
bool touchActive = false;

@override
Widget build(BuildContext context) {
Expand All @@ -28,12 +31,14 @@ class _ViamBaseJoystickState extends State<ViamBaseJoystick> {
const SizedBox(height: 16),
Joystick(
listener: callSetPower,
onStickDragEnd: () => callSetPower(StickDragDetails(0, 0)),
)
],
);
}

void callSetPower(StickDragDetails details) {
touchActive = (details.x != 0 && details.y != 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be || for the use case of when I'm fully horizontal (e.g. only fully right). Then X would be non-zero, but Y would be zero

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah you're totally right, great catch, thank you!

widget.base.setPower(
Vector3()..y = details.y * -1,
Vector3()..z = details.x * -1,
Expand All @@ -42,5 +47,19 @@ class _ViamBaseJoystickState extends State<ViamBaseJoystick> {
y = details.y * -100;
z = details.x * -100;
});

// When the client has spotty network conditions,
// the joystick has a tendency to cause the machine
// to hang on the most recent setPower call and keep moving
// after the touch has been released. This is a workaround that
// will work faster than the session being canceled,
// if network conditions allowed. The session being canceled
// will eventually stop the base if network conditions become too spotty.
Future.delayed(const Duration(milliseconds: 100)).then((_) async {
final isMoving = await widget.base.isMoving();
if (isMoving && !touchActive) {
unawaited(widget.base.stop());
}
});
}
}