-
Notifications
You must be signed in to change notification settings - Fork 814
feat: implementation of the Compass page #2710
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0c29ce8
implementation of the Compass page
Yugesh-Kumar-S 05d2830
made suggested changes
Yugesh-Kumar-S 3a57a4f
updated ios permission
Yugesh-Kumar-S 31a2469
Merge branch 'flutter' into Compass
Yugesh-Kumar-S 5e1a7aa
Changed Sourcery's suggestion
Yugesh-Kumar-S abca6c0
Merge branch 'flutter' into Compass
Yugesh-Kumar-S 40a85f2
refactoring and ui fixes
Yugesh-Kumar-S 180bada
Merge branch 'flutter' into Compass
Yugesh-Kumar-S d0dbcae
Merge branch 'flutter' into Compass
Yugesh-Kumar-S 15fdded
Merge branch 'flutter' of https://github.com/Yugesh-Kumar-S/pslab-and…
Yugesh-Kumar-S 55edff0
resolved conflicts and moved strings in localization files
Yugesh-Kumar-S ad3c4d0
Merge branch 'Compass' of https://github.com/Yugesh-Kumar-S/pslab-and…
Yugesh-Kumar-S 77db994
fixes
Yugesh-Kumar-S 731d9f7
Merge branch 'flutter' into Compass
Yugesh-Kumar-S 9daf137
Update app_en.arb
Yugesh-Kumar-S 0e83553
Merge branch 'flutter' into Compass
Yugesh-Kumar-S cd7b2f4
Merge branch 'flutter' into Compass
Yugesh-Kumar-S f0caedf
Merge branch 'flutter' into Compass
Yugesh-Kumar-S 4130bef
Merge branch 'flutter' into Compass
Yugesh-Kumar-S 4fbd6fe
Merge branch 'flutter' into Compass
marcnause 1123bba
Merge branch 'flutter' into Compass
marcnause 3bae6e7
Merge branch 'flutter' into Compass
Yugesh-Kumar-S 5d776f9
Merge branch 'flutter' into Compass
marcnause File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import 'dart:async'; | ||
import 'dart:math'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:sensors_plus/sensors_plus.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:pslab/others/logger_service.dart'; | ||
|
||
import '../constants.dart'; | ||
|
||
class CompassProvider extends ChangeNotifier { | ||
MagnetometerEvent _magnetometerEvent = | ||
MagnetometerEvent(0, 0, 0, DateTime.now()); | ||
AccelerometerEvent _accelerometerEvent = | ||
AccelerometerEvent(0, 0, 0, DateTime.now()); | ||
StreamSubscription? _magnetometerSubscription; | ||
StreamSubscription? _accelerometerSubscription; | ||
String _selectedAxis = 'X'; | ||
double _currentDegree = 0.0; | ||
int _direction = 0; | ||
double _smoothedHeading = 0.0; | ||
|
||
MagnetometerEvent get magnetometerEvent => _magnetometerEvent; | ||
AccelerometerEvent get accelerometerEvent => _accelerometerEvent; | ||
String get selectedAxis => _selectedAxis; | ||
double get currentDegree => _currentDegree; | ||
int get direction => _direction; | ||
double get smoothedHeading => _smoothedHeading; | ||
|
||
void initializeSensors() { | ||
_magnetometerSubscription = magnetometerEventStream().listen( | ||
(event) { | ||
_magnetometerEvent = event; | ||
_updateCompassDirection(); | ||
notifyListeners(); | ||
}, | ||
onError: (error) { | ||
logger.e("$magnetometerError: $error"); | ||
}, | ||
cancelOnError: true, | ||
); | ||
|
||
_accelerometerSubscription = accelerometerEventStream().listen( | ||
(event) { | ||
_accelerometerEvent = event; | ||
_updateCompassDirection(); | ||
notifyListeners(); | ||
}, | ||
onError: (error) { | ||
logger.e("$accelerometerError: $error"); | ||
}, | ||
cancelOnError: true, | ||
); | ||
} | ||
|
||
void disposeSensors() { | ||
_magnetometerSubscription?.cancel(); | ||
_accelerometerSubscription?.cancel(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
disposeSensors(); | ||
super.dispose(); | ||
} | ||
|
||
void _updateCompassDirection() { | ||
double radians = _getRadiansForAxis(_selectedAxis); | ||
double degrees = radians * (180 / pi); | ||
if (degrees < 0) { | ||
degrees += 360; | ||
} | ||
|
||
degrees = (degrees - 90) % 360; | ||
if (degrees < 0) { | ||
degrees += 360; | ||
} | ||
|
||
const double alpha = 0.45; | ||
double angleDiff = degrees - _smoothedHeading; | ||
if (angleDiff > 180) { | ||
angleDiff -= 360; | ||
} else if (angleDiff < -180) { | ||
angleDiff += 360; | ||
} | ||
_smoothedHeading = _smoothedHeading + alpha * angleDiff; | ||
if (_smoothedHeading >= 360) { | ||
_smoothedHeading -= 360; | ||
} else if (_smoothedHeading < 0) { | ||
_smoothedHeading += 360; | ||
} | ||
switch (_selectedAxis) { | ||
case 'X': | ||
_currentDegree = -(_smoothedHeading * pi / 180); | ||
break; | ||
case 'Y': | ||
_currentDegree = ((_smoothedHeading - 10) * pi / 180); | ||
break; | ||
case 'Z': | ||
_currentDegree = -((_smoothedHeading + 90) * pi / 180); | ||
break; | ||
} | ||
} | ||
|
||
double _getRadiansForAxis(String axis) { | ||
double ax = _accelerometerEvent.x; | ||
double ay = _accelerometerEvent.y; | ||
double az = _accelerometerEvent.z; | ||
double mx = _magnetometerEvent.x; | ||
double my = _magnetometerEvent.y; | ||
double mz = _magnetometerEvent.z; | ||
|
||
double pitch = atan2(ay, sqrt(ax * ax + az * az)); | ||
double roll = atan2(-ax, az); | ||
|
||
double xH = mx * cos(pitch) + mz * sin(pitch); | ||
double yH = mx * sin(roll) * sin(pitch) + | ||
my * cos(roll) - | ||
mz * sin(roll) * cos(pitch); | ||
double zH = -mx * cos(roll) * sin(pitch) + | ||
my * sin(roll) + | ||
mz * cos(roll) * cos(pitch); | ||
|
||
switch (axis) { | ||
case 'X': | ||
return atan2(yH, xH); | ||
case 'Y': | ||
return atan2(-xH, zH); | ||
case 'Z': | ||
return atan2(yH, -zH); | ||
default: | ||
return atan2(yH, xH); | ||
} | ||
} | ||
|
||
double getDegreeForAxis(String axis) { | ||
double radians = _getRadiansForAxis(axis); | ||
double degree = radians * (180 / pi); | ||
|
||
switch (axis) { | ||
case 'X': | ||
degree = (degree - 90) % 360; | ||
break; | ||
case 'Y': | ||
degree = (-degree + 100) % 360; | ||
break; | ||
case 'Z': | ||
degree = (degree + 90) % 360; | ||
break; | ||
} | ||
|
||
return degree < 0 ? degree + 360 : degree; | ||
} | ||
|
||
void onAxisSelected(String axis) { | ||
_selectedAxis = axis; | ||
switch (axis) { | ||
case 'X': | ||
_direction = 0; | ||
break; | ||
case 'Y': | ||
_direction = 1; | ||
break; | ||
case 'Z': | ||
_direction = 2; | ||
break; | ||
} | ||
notifyListeners(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.