Skip to content

Commit

Permalink
detect motion type based on attitude data
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiGong-dev committed Dec 12, 2021
1 parent b646d1c commit 420a595
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class _MyHomePageState extends State<MyHomePage> {
EventChannel('com.huigong.headmotion/attitude');

String _sensorAvailable = "Unknown";
Map _currentAttitude = {"pitch": 0.0, "roll": 0.0, "yaw": 0.0};
String _lastMotionType = "still";
String _currentMotionType = "still";
double _attitudePitchReading = 0;
double _attitudeRollReading = 0;
double _attitudeYawReading = 0;
Expand All @@ -68,21 +71,87 @@ class _MyHomePageState extends State<MyHomePage> {
_sensorAvailable = available.toString();
});
} on PlatformException catch (e) {
print(e);
debugPrint(e.toString());
}
}

_startReading() {
attitudeSubscription =
attitudeChannel.receiveBroadcastStream().listen((event) {
setState(() {
_currentAttitude = {
"pitch": event["pitch"],
"roll": event["roll"],
"yaw": event["yaw"]
};
_currentMotionType = _getMotionType(_currentAttitude);
_attitudePitchReading = event["pitch"];
_attitudeRollReading = event["roll"];
_attitudeYawReading = event["yaw"];
});
});
}

// _updateMotionType() {
// _lastMotionType = _currentMotionType;
// }

double _max(Map attitudeMap) {
double pitch = attitudeMap["pitch"];
double roll = attitudeMap["roll"];
double yaw = attitudeMap["yaw"];

if (pitch.abs() >= roll.abs()) {
if (pitch.abs() >= yaw.abs()) {
return pitch;
} else {
return yaw;
}
} else {
if (roll.abs() >= yaw.abs()) {
return roll;
} else {
return yaw;
}
}
}

String _getMotionType(Map attitudeMap) {
double? pitch = attitudeMap["pitch"];
double? roll = attitudeMap["roll"];
double? yaw = attitudeMap["yaw"];

double maxValue = _max(attitudeMap);

if (maxValue.abs() < 0.5) {
return "still";
}
if (maxValue == roll) {
if (maxValue.isNegative) {
return "tilt left";
} else {
return "tilt right";
}
}

if (maxValue == pitch) {
if (maxValue.isNegative) {
return "tilt forward";
} else {
return "tilt backward";
}
}

if (maxValue == yaw) {
if (maxValue.isNegative) {
return "look right";
} else {
return "look left";
}
}
return "still";
}

_stopReading() {
setState(() {
_attitudePitchReading = 0;
Expand Down Expand Up @@ -119,7 +188,11 @@ class _MyHomePageState extends State<MyHomePage> {
Pitch: $_attitudePitchReading
Roll: $_attitudeRollReading
Yaw: $_attitudeYawReading
type: $_currentMotionType
'''),
if (_currentMotionType != _lastMotionType &&
_currentMotionType != "still")
Text("motion type: $_currentMotionType"),
if (_sensorAvailable == 'true' &&
_attitudeRollReading == 0 &&
_attitudePitchReading == 0 &&
Expand Down

0 comments on commit 420a595

Please sign in to comment.