mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Switched to software rotation sensor
This commit is contained in:
1 parent
f96275a5bb
commit
8c221c6144
1 file changed
+17
-29
@@ -27,7 +27,6 @@ import android.hardware.SensorManager
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.Observer
|
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.navigation.fragment.navArgs
|
import androidx.navigation.fragment.navArgs
|
||||||
@@ -42,11 +41,11 @@ import com.rtbishop.look4sat.ui.MainActivity
|
|||||||
import com.rtbishop.look4sat.ui.SharedViewModel
|
import com.rtbishop.look4sat.ui.SharedViewModel
|
||||||
import com.rtbishop.look4sat.ui.adapters.TransmitterAdapter
|
import com.rtbishop.look4sat.ui.adapters.TransmitterAdapter
|
||||||
import com.rtbishop.look4sat.ui.views.PolarView
|
import com.rtbishop.look4sat.ui.views.PolarView
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import kotlin.math.round
|
||||||
|
|
||||||
class PolarViewFragment : Fragment(R.layout.fragment_polar_view), SensorEventListener {
|
class PolarViewFragment : Fragment(R.layout.fragment_polar_view), SensorEventListener {
|
||||||
|
|
||||||
@@ -60,9 +59,6 @@ class PolarViewFragment : Fragment(R.layout.fragment_polar_view), SensorEventLis
|
|||||||
private var polarView: PolarView? = null
|
private var polarView: PolarView? = null
|
||||||
private val args: PolarViewFragmentArgs by navArgs()
|
private val args: PolarViewFragmentArgs by navArgs()
|
||||||
private val transmitterAdapter = TransmitterAdapter()
|
private val transmitterAdapter = TransmitterAdapter()
|
||||||
private var lastAccData = FloatArray(3)
|
|
||||||
private var lastMagData = FloatArray(3)
|
|
||||||
private var refreshJob: Job? = null
|
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
@@ -81,11 +77,8 @@ class PolarViewFragment : Fragment(R.layout.fragment_polar_view), SensorEventLis
|
|||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
if (viewModel.getCompass()) {
|
if (viewModel.getCompass()) {
|
||||||
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER).also { accData ->
|
sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR).also { sensor ->
|
||||||
sensorManager.registerListener(this, accData, SensorManager.SENSOR_DELAY_GAME)
|
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)
|
||||||
}
|
|
||||||
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD).also { magData ->
|
|
||||||
sensorManager.registerListener(this, magData, SensorManager.SENSOR_DELAY_GAME)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,30 +93,26 @@ class PolarViewFragment : Fragment(R.layout.fragment_polar_view), SensorEventLis
|
|||||||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
|
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
|
||||||
|
|
||||||
override fun onSensorChanged(event: SensorEvent?) {
|
override fun onSensorChanged(event: SensorEvent?) {
|
||||||
event?.let {
|
event?.let { useSoftwareSensor(it) }
|
||||||
if (it.sensor.type == Sensor.TYPE_ACCELEROMETER) {
|
}
|
||||||
filterSensorData(event.values, lastAccData)
|
|
||||||
} else if (it.sensor.type == Sensor.TYPE_MAGNETIC_FIELD) {
|
|
||||||
filterSensorData(event.values, lastMagData)
|
|
||||||
}
|
|
||||||
|
|
||||||
val rotationMatrix = FloatArray(9)
|
private fun useSoftwareSensor(event: SensorEvent) {
|
||||||
if (SensorManager.getRotationMatrix(rotationMatrix, null, lastAccData, lastMagData)) {
|
val rotationValues = FloatArray(9)
|
||||||
val orientation = FloatArray(3)
|
SensorManager.getRotationMatrixFromVector(rotationValues, event.values)
|
||||||
SensorManager.getOrientation(rotationMatrix, orientation)
|
val orientationValues = FloatArray(3)
|
||||||
val degree = orientation[0] * 57.2957795f
|
SensorManager.getOrientation(rotationValues, orientationValues)
|
||||||
polarView?.rotation = -degree
|
val magneticAzimuth = ((orientationValues[0] * 57.2957795f) + 360f) % 360f
|
||||||
}
|
val roundedAzimuth = round(magneticAzimuth * 100) / 100
|
||||||
}
|
polarView?.rotation = -roundedAzimuth
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun filterSensorData(input: FloatArray, output: FloatArray) {
|
private fun filterSensorData(input: FloatArray, output: FloatArray) {
|
||||||
val filterStep = 0.05f
|
val filterStep = 1f
|
||||||
input.indices.forEach { output[it] = output[it] + filterStep * (input[it] - output[it]) }
|
input.indices.forEach { output[it] = output[it] + filterStep * (input[it] - output[it]) }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun observePasses() {
|
private fun observePasses() {
|
||||||
viewModel.getPassList().observe(viewLifecycleOwner, Observer { result ->
|
viewModel.getPassList().observe(viewLifecycleOwner, { result ->
|
||||||
when (result) {
|
when (result) {
|
||||||
is Result.Success -> {
|
is Result.Success -> {
|
||||||
val refreshRate = viewModel.getRefreshRate()
|
val refreshRate = viewModel.getRefreshRate()
|
||||||
@@ -139,7 +128,7 @@ class PolarViewFragment : Fragment(R.layout.fragment_polar_view), SensorEventLis
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun observeTransmitters() {
|
private fun observeTransmitters() {
|
||||||
viewModel.getTransmittersForSat(satPass.tle.catnum).observe(viewLifecycleOwner, Observer {
|
viewModel.getTransmittersForSat(satPass.tle.catnum).observe(viewLifecycleOwner, {
|
||||||
if (it.isNotEmpty()) {
|
if (it.isNotEmpty()) {
|
||||||
transmitterAdapter.setPredictor(satPass.predictor)
|
transmitterAdapter.setPredictor(satPass.predictor)
|
||||||
transmitterAdapter.setList(it)
|
transmitterAdapter.setList(it)
|
||||||
@@ -154,8 +143,7 @@ class PolarViewFragment : Fragment(R.layout.fragment_polar_view), SensorEventLis
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun refreshText(rate: Long) {
|
private fun refreshText(rate: Long) {
|
||||||
refreshJob?.cancel()
|
lifecycleScope.launch {
|
||||||
refreshJob = lifecycleScope.launch {
|
|
||||||
while (true) {
|
while (true) {
|
||||||
setPassText()
|
setPassText()
|
||||||
polarView?.invalidate()
|
polarView?.invalidate()
|
||||||
|
|||||||
Reference in new issue
Block a user