|
|
@ -1,20 +1,18 @@
|
|
|
|
package com.rookiedev.hexapod
|
|
|
|
package com.rookiedev.hexapod
|
|
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.content.res.Resources
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.util.TypedValue
|
|
|
|
|
|
|
|
import android.view.*
|
|
|
|
import android.view.*
|
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.ImageView
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived
|
|
|
|
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlin.math.PI
|
|
|
|
import kotlin.math.PI
|
|
|
|
import kotlin.math.atan2
|
|
|
|
import kotlin.math.atan2
|
|
|
|
import kotlin.math.pow
|
|
|
|
import kotlin.math.pow
|
|
|
|
import kotlin.math.sqrt
|
|
|
|
import kotlin.math.sqrt
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -71,32 +69,28 @@ class ControlActivity : AppCompatActivity() {
|
|
|
|
private var radius = 0f
|
|
|
|
private var radius = 0f
|
|
|
|
|
|
|
|
|
|
|
|
private var tcpClient: TCPClient? = null
|
|
|
|
private var tcpClient: TCPClient? = null
|
|
|
|
|
|
|
|
private var ip:String = ""
|
|
|
|
|
|
|
|
private var port = 0
|
|
|
|
|
|
|
|
|
|
|
|
private val scope = CoroutineScope(Job() + Dispatchers.IO)
|
|
|
|
private val scope = CoroutineScope(Job() + Dispatchers.IO)
|
|
|
|
|
|
|
|
|
|
|
|
private var currentState: String = "standby"
|
|
|
|
private var currentState: String = "standby"
|
|
|
|
|
|
|
|
|
|
|
|
// private val lock = ReentrantLock()
|
|
|
|
|
|
|
|
// private val waitLock = lock.newCondition()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_control)
|
|
|
|
setContentView(R.layout.activity_control)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val myIntent = intent // gets the previously created intent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ip = myIntent.getStringExtra("ip").toString()
|
|
|
|
|
|
|
|
port = myIntent.getStringExtra("port").toString().toInt()
|
|
|
|
|
|
|
|
|
|
|
|
controlWindowInsets(true)
|
|
|
|
controlWindowInsets(true)
|
|
|
|
|
|
|
|
|
|
|
|
val controlCircle = findViewById<ImageView>(R.id.control_image)
|
|
|
|
val controlCircle = findViewById<ImageView>(R.id.control_image)
|
|
|
|
|
|
|
|
|
|
|
|
val dip = 32f
|
|
|
|
|
|
|
|
val r: Resources = resources
|
|
|
|
|
|
|
|
this.pxMargin = TypedValue.applyDimension(
|
|
|
|
|
|
|
|
TypedValue.COMPLEX_UNIT_DIP,
|
|
|
|
|
|
|
|
dip,
|
|
|
|
|
|
|
|
r.displayMetrics
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val vto: ViewTreeObserver = controlCircle.viewTreeObserver
|
|
|
|
val vto: ViewTreeObserver = controlCircle.viewTreeObserver
|
|
|
|
vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
|
|
|
|
vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
|
|
|
|
override fun onPreDraw(): Boolean {
|
|
|
|
override fun onPreDraw(): Boolean {
|
|
|
@ -132,7 +126,7 @@ class ControlActivity : AppCompatActivity() {
|
|
|
|
currentState = "standby"
|
|
|
|
currentState = "standby"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (length >= radius / 3 && length < 2 * radius / 3) {
|
|
|
|
} else if (length >= radius / 3 && length < 2 * radius / 3) {
|
|
|
|
var angle = atan2(coorY, coorX)
|
|
|
|
val angle = atan2(coorY, coorX)
|
|
|
|
if (angle > -PI / 4 && angle <= PI / 4) {
|
|
|
|
if (angle > -PI / 4 && angle <= PI / 4) {
|
|
|
|
if (currentState != "shiftright") {
|
|
|
|
if (currentState != "shiftright") {
|
|
|
|
println("Move right")
|
|
|
|
println("Move right")
|
|
|
@ -159,7 +153,7 @@ class ControlActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (length >= 2 * radius / 3 && length < radius) {
|
|
|
|
} else if (length >= 2 * radius / 3 && length < radius) {
|
|
|
|
var angle = atan2(coorY, coorX)
|
|
|
|
val angle = atan2(coorY, coorX)
|
|
|
|
if (angle > -PI / 4 && angle <= PI / 4) {
|
|
|
|
if (angle > -PI / 4 && angle <= PI / 4) {
|
|
|
|
if (currentState != "rightturn") {
|
|
|
|
if (currentState != "rightturn") {
|
|
|
|
println("Turn right")
|
|
|
|
println("Turn right")
|
|
|
@ -190,7 +184,7 @@ class ControlActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
this.tcpClient = TCPClient(this, "192.168.1.202", 1234, object : OnMessageReceived {
|
|
|
|
this.tcpClient = TCPClient(this, ip, port, object : OnMessageReceived {
|
|
|
|
override fun messageReceived(message: String?) {
|
|
|
|
override fun messageReceived(message: String?) {
|
|
|
|
if (message == null) {
|
|
|
|
if (message == null) {
|
|
|
|
// alertDialog(DISCONNECTED)
|
|
|
|
// alertDialog(DISCONNECTED)
|
|
|
|