joystick working

This commit is contained in:
Omer Schloss 2021-06-24 19:08:17 +03:00
parent f39dd6f61a
commit c5e27c2ca7
8 changed files with 59 additions and 56 deletions

View File

@ -4,8 +4,6 @@ package com.example.flightgearcontrollerapp.model
class FGModel {
private val telnetClient = TelnetClient()
private var isConnected =false
private var isExceptionOccurred = false
private var isTouchingJoystick = false
fun connect(ipAddress: String, portAddress: String): Boolean {
var res = false
@ -17,14 +15,9 @@ class FGModel {
throw RuntimeException(e)
}
}
val h = Thread.UncaughtExceptionHandler { _, _ -> isExceptionOccurred = true;res= false }
t.uncaughtExceptionHandler = h
t.start()
try {
t.join()
if (!isExceptionOccurred) {
this.isTouchingJoystick = false
}
} catch (e: Exception) {
e.printStackTrace()
return false
@ -50,13 +43,13 @@ class FGModel {
}
}
fun setAileron(fl: Float) {
fun setAileron(fl: Double) {
if(isConnected) {
telnetClient.updateAileron(fl)
}
}
fun setElevator(fl: Float) {
fun setElevator(fl: Double) {
if(isConnected) {
telnetClient.updateElevator(fl)
}

View File

@ -15,7 +15,7 @@ class TelnetClient{
fun connect(ipAddress: String, portAddress: Int): Boolean {
try {
client = Socket()
client.connect(InetSocketAddress(ipAddress, portAddress),5000)
client.connect(InetSocketAddress(ipAddress, portAddress),2000)
output = PrintWriter(client.getOutputStream(), true)
executor = Executors.newSingleThreadExecutor()
return true
@ -41,11 +41,11 @@ class TelnetClient{
executor.execute { output.print("set /controls/flight/rudder $fl\r\n");output.flush() }
}
fun updateAileron(fl: Float) {
fun updateAileron(fl: Double) {
executor.execute { output.print("set /controls/flight/aileron $fl\r\n");output.flush() }
}
fun updateElevator(fl: Float) {
fun updateElevator(fl: Double) {
executor.execute { output.print("set /controls/flight/elevator $fl\r\n");output.flush() }
}

View File

@ -11,7 +11,9 @@ class FGViewModel : ViewModel() {
}
return false
}
fun getModel() : FGModel{
return model
}
fun disconnectFromFG() {
model.disconnect()
}
@ -23,12 +25,4 @@ class FGViewModel : ViewModel() {
fun setRudder(fl: Float) {
model.setRudder(fl)
}
fun setAileron(fl: Float) {
model.setAileron(fl)
}
fun setElevator(fl: Float) {
model.setElevator(fl)
}
}

View File

@ -0,0 +1,17 @@
package com.example.flightgearcontrollerapp.view_model
import com.example.flightgearcontrollerapp.model.FGModel
import java.lang.Math.toRadians
import kotlin.math.cos
import kotlin.math.sin
class VMJoyStick(m: FGModel) {
private val model = m
fun updateMove(angle: Double, strength: Float) {
val length = strength /100
val x = length*cos( toRadians(angle))
val y = length*sin( toRadians(angle))
model.setAileron(x)
model.setElevator(y)
}
}

View File

@ -1,16 +1,17 @@
package com.example.flightgearcontrollerapp.views
import android.util.Log
import com.example.flightgearcontrollerapp.model.FGModel
import com.example.flightgearcontrollerapp.view_model.VMJoyStick
import com.jackandphantom.joystickview.JoyStickView
class JoyStick(jsv: JoyStickView) {
/* private var joyStickView = jsv
class JoyStick(m: FGModel, joyStick: JoyStickView) {
private val viewModel = VMJoyStick(m)
init {
joyStickView.setOnMoveListener(object : JoyStickView.OnMov0eListener( run {
fun onMove(angle: Int, strength: Int) {
Log.i("TAG", "angle: $angle strength: $strength");
}
})
}*/
joyStick.setOnMoveListener { angle, strength -> viewModel.updateMove(angle, strength)
}
}
//viewModel.updateMove(0.0,0f)
}

View File

@ -13,7 +13,6 @@ import com.example.flightgearcontrollerapp.R
import com.example.flightgearcontrollerapp.R.id.*
import com.example.flightgearcontrollerapp.view_model.FGViewModel
import com.google.android.material.slider.Slider
import com.jackandphantom.joystickview.JoyStickView
class MainActivity : AppCompatActivity() {
@ -21,10 +20,12 @@ class MainActivity : AppCompatActivity() {
private val vmConnection = FGViewModel()
private lateinit var throttleSlider: Slider
private lateinit var rudderSlider: Slider
private lateinit var joyStick:JoyStick
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
joyStick = JoyStick(vmConnection.getModel(), findViewById(widget_joystick))
throttleSlider = findViewById(slider_throttle)
rudderSlider = findViewById(slider_rudder)
throttleSlider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
@ -35,7 +36,6 @@ class MainActivity : AppCompatActivity() {
vmConnection.setThrottle(slider.value)
}
})
throttleSlider.addOnChangeListener { _, value, _ ->
vmConnection.setThrottle(value)
}
@ -47,12 +47,9 @@ class MainActivity : AppCompatActivity() {
vmConnection.setRudder(slider.value)
}
})
rudderSlider.addOnChangeListener { _, value, _ ->
vmConnection.setRudder(value)
}
// var joyStick = JoyStick(findViewById(widget_joystick))
}
fun onClickConnect(view: View) {
@ -101,9 +98,5 @@ class MainActivity : AppCompatActivity() {
}
}
}
}

View File

@ -127,26 +127,14 @@
android:background="@color/black_60"
android:baselineAligned="false">
<RelativeLayout
<include
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="75"
android:layout_gravity="center">
android:layout_gravity="center"
layout="@layout/joystick" />
<com.jackandphantom.joystickview.JoyStickView
android:id="@+id/widget_joystick"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
app:innerCircleColor="#f6FFFFFF"
app:innerCircleImage="@drawable/ic_baseline_airplane_mode_active_24"
app:outerCircleBorderColor="#fff"
app:outerCircleBorderWidth="5"
app:outerCircleColor="@color/navy_blue_40"
app:shadowColor="#000"
app:shadowRadius="7" />
</RelativeLayout>
<LinearLayout
android:layout_height="match_parent"

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.jackandphantom.joystickview.JoyStickView
android:id="@+id/widget_joystick"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:innerCircleColor="#f6FFFFFF"
app:innerCircleImage="@drawable/ic_baseline_airplane_mode_active_24"
app:outerCircleBorderColor="#fff"
app:outerCircleBorderWidth="5"
app:outerCircleColor="@color/navy_blue_40"
app:shadowColor="#000"
app:shadowRadius="7" />
</LinearLayout>