From c5e27c2ca76c963cb7ce8a0585f66c7226fac3d3 Mon Sep 17 00:00:00 2001 From: Omer Schloss Date: Thu, 24 Jun 2021 19:08:17 +0300 Subject: [PATCH] joystick working --- .../flightgearcontrollerapp/model/FGModel.kt | 11 ++-------- .../model/TelnetClient.kt | 6 +++--- .../view_model/FGViewModel.kt | 12 +++-------- .../view_model/VMJoyStick.kt | 17 +++++++++++++++ .../flightgearcontrollerapp/views/JoyStick.kt | 21 ++++++++++--------- .../views/MainActivity.kt | 11 ++-------- app/src/main/res/layout/activity_main.xml | 20 ++++-------------- app/src/main/res/layout/joystick.xml | 17 +++++++++++++++ 8 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 app/src/main/java/com/example/flightgearcontrollerapp/view_model/VMJoyStick.kt create mode 100644 app/src/main/res/layout/joystick.xml diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/model/FGModel.kt b/app/src/main/java/com/example/flightgearcontrollerapp/model/FGModel.kt index 5d1639a..33998e0 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/model/FGModel.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/model/FGModel.kt @@ -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) } diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/model/TelnetClient.kt b/app/src/main/java/com/example/flightgearcontrollerapp/model/TelnetClient.kt index 4342ac8..d412632 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/model/TelnetClient.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/model/TelnetClient.kt @@ -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() } } diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/view_model/FGViewModel.kt b/app/src/main/java/com/example/flightgearcontrollerapp/view_model/FGViewModel.kt index 710b66a..d2aac9c 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/view_model/FGViewModel.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/view_model/FGViewModel.kt @@ -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) - } } diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/view_model/VMJoyStick.kt b/app/src/main/java/com/example/flightgearcontrollerapp/view_model/VMJoyStick.kt new file mode 100644 index 0000000..db54b42 --- /dev/null +++ b/app/src/main/java/com/example/flightgearcontrollerapp/view_model/VMJoyStick.kt @@ -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) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/views/JoyStick.kt b/app/src/main/java/com/example/flightgearcontrollerapp/views/JoyStick.kt index 240805a..ddea8e7 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/views/JoyStick.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/views/JoyStick.kt @@ -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) } \ No newline at end of file diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/views/MainActivity.kt b/app/src/main/java/com/example/flightgearcontrollerapp/views/MainActivity.kt index 40dc63b..228e0a1 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/views/MainActivity.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/views/MainActivity.kt @@ -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() { } } } - - - - } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9c8fd30..95119c3 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -127,26 +127,14 @@ android:background="@color/black_60" android:baselineAligned="false"> - + android:layout_gravity="center" + layout="@layout/joystick" /> - - + + + \ No newline at end of file