simple refactoring

This commit is contained in:
MaximZaitsev
2016-08-18 14:05:41 +03:00
parent 83c3021799
commit 57e693542c
8 changed files with 58 additions and 44 deletions
+13 -12
View File
@@ -2,14 +2,15 @@ import carControl.Control
import carControl.RouteExecutor
import carControl.RouteExecutorImpl.MoveDirection
private val MOVE_VELOCITY = 0.3278
private val ROTATION_VELOCITY = 12.3
// TODO make Car class mutable state saving entity
// that almost doesn't have behavior
/**
* Created by user on 7/27/16.
*/
class Car constructor(val routeExecutor: RouteExecutor, val controller: Control) {
val velocityMove = 0.3278
val velocityRotation = 12.3
//position
var x: Double
var y: Double
@@ -32,15 +33,15 @@ class Car constructor(val routeExecutor: RouteExecutor, val controller: Control)
val deltaSeconds = delta.toDouble() / 1000
when (moveDirection) {
MoveDirection.FORWARD -> {
this.x += this.velocityMove * deltaSeconds * Math.cos(this.angle * Math.PI / 180);
this.y += this.velocityMove * deltaSeconds * Math.sin(this.angle * Math.PI / 180);
this.x += MOVE_VELOCITY * deltaSeconds * Math.cos(this.angle * Math.PI / 180);
this.y += MOVE_VELOCITY * deltaSeconds * Math.sin(this.angle * Math.PI / 180);
}
MoveDirection.BACKWARD -> {
this.x -= this.velocityMove * deltaSeconds * Math.cos(this.angle * Math.PI / 180);
this.y -= this.velocityMove * deltaSeconds * Math.sin(this.angle * Math.PI / 180);
this.x -= MOVE_VELOCITY * deltaSeconds * Math.cos(this.angle * Math.PI / 180);
this.y -= MOVE_VELOCITY * deltaSeconds * Math.sin(this.angle * Math.PI / 180);
}
MoveDirection.LEFT -> this.angle += velocityRotation * deltaSeconds
MoveDirection.RIGHT -> this.angle -= velocityRotation * deltaSeconds
MoveDirection.LEFT -> this.angle += ROTATION_VELOCITY * deltaSeconds
MoveDirection.RIGHT -> this.angle -= ROTATION_VELOCITY * deltaSeconds
else -> {
}
@@ -64,9 +65,9 @@ class Car constructor(val routeExecutor: RouteExecutor, val controller: Control)
}
if (moveDirection != MoveDirection.STOP) {
if (moveDirection == MoveDirection.FORWARD || moveDirection == MoveDirection.BACKWARD) {
controller.delay(getTimeForMoving(value, velocityMove), callBack)
controller.delay(getTimeForMoving(value, MOVE_VELOCITY), callBack)
} else {
controller.delay(getTimeForMoving(value, velocityRotation), callBack)
controller.delay(getTimeForMoving(value, ROTATION_VELOCITY), callBack)
}
}
}
+28 -17
View File
@@ -9,14 +9,16 @@ class McTransport() {
private val resultBytes = arrayListOf<Byte>()
private var callback: (bytes: ByteArray) -> Unit = {}
fun writeToFile(bytes: ByteArray) {
fun sendBytes(bytes: ByteArray) {
val bytesHeader = encodeInt(bytes.size)
println("write: " + bytesHeader)
writeStream.write(js("new Buffer(bytesHeader)"))
println("write: " + bytes)
val bytesTemp = bytes
writeStream.write(js("new Buffer(bytesTemp)"))
writeStream.write(js("new Buffer(bytes)"))
}
fun writeToFile(byte: Byte) {
writeToFile(ByteArray(1, { idx -> byte }))
fun sendBytes(byte: Byte) {
sendBytes(ByteArray(1, { idx -> byte }))
}
fun initStreams(pathToFile: String) {
@@ -56,20 +58,29 @@ class McTransport() {
//need first ${protoHeaderLength} bytes - header
return -1
}
var res = 0
for (i in 0..protoHeaderLength - 1) {
val curByte = resultBytes.get(i)
val curBytePositive: Int = if (curByte < 0) {
256 + curByte
} else {
curByte.toInt()
}
res += curBytePositive * Math.pow(2.0, 24.0 - i * 8).toInt()
}
return res
return decodeInt(resultBytes.toByteArray())
}
private fun encodeInt(i: Int): ByteArray {
val result = ByteArray(4)
result[0] = i.shr(24).toByte()
result[1] = i.shr(16).toByte()
result[2] = i.shr(8).toByte()
result[3] = i.toByte()
return result
}
private fun decodeInt(bytes: ByteArray): Int {
var result = 0
result += bytes[3]
result += bytes[2].toInt().shl(8)
result += bytes[1].toInt().shl(16)
result += bytes[0].toInt().shl(24)
return result
}
}
val protoHeaderLength: Int = 4
private val protoHeaderLength: Int = 4
val mcTransport = McTransport()
+2 -2
View File
@@ -32,7 +32,7 @@ class MicroController private constructor() {
fun start() {
connectToServer(config.getCarIp(), serverPort)
connectToServer(config.getCarIp(), carServerPort)
mcTransport.setCallBack { bytes ->
println("read: " + bytes.toString())
@@ -96,7 +96,7 @@ class MicroController private constructor() {
}
fun connectToServer(thisIp: String, thisPort: Int) {
val requestObject = ConnectionRequest.BuilderConnectionRequest(config.getCarIp().split(".").map { str -> parseInt(str, 10) }.toIntArray(), serverPort).build()
val requestObject = ConnectionRequest.BuilderConnectionRequest(config.getCarIp().split(".").map { str -> parseInt(str, 10) }.toIntArray(), carServerPort).build()
val bytes = ByteArray(requestObject.getSizeNoTag())
requestObject.writeTo(CodedOutputStream(bytes))
net.sendRequest(js("new Buffer(bytes)"), "/connect", { resultData ->
+3 -1
View File
@@ -30,7 +30,9 @@ class Udev {
fun isOurMcDevice(device: dynamic): Boolean {
val microController = MicroController.instance
return device.ID_VENDOR_ID == microController.vendorID && device.ID_MODEL_ID == microController.modelID && device.SUBSYSTEM == "tty"
return (device.ID_VENDOR_ID == microController.vendorID)
&& (device.ID_MODEL_ID == microController.modelID)
&& (device.SUBSYSTEM == "tty")
}
fun disconnectDevice() {
@@ -14,27 +14,27 @@ class ControlImpl : Control {
override fun moveCarLeft() {
println("move left")
mcTransport.writeToFile(4)
mcTransport.sendBytes(4)
}
override fun moveCarRight() {
println("move rigth")
mcTransport.writeToFile(3)
mcTransport.sendBytes(3)
}
override fun moveCarForward() {
println("move forward")
mcTransport.writeToFile(1)
mcTransport.sendBytes(1)
}
override fun moveCarBackward() {
println("move backward")
mcTransport.writeToFile(2)
mcTransport.sendBytes(2)
}
override fun stopCar() {
println("stopped")
mcTransport.writeToFile(0)
mcTransport.sendBytes(0)
}
override fun delay(ms: Int, callBack: () -> Unit) {
@@ -10,14 +10,12 @@ class RouteExecutorToUsb : RouteExecutor {
override fun executeRoute(route: RouteRequest) {
println("Execute Route:")
val size = encodeInt(route.getSizeNoTag())
val routeBytes = encodeProtoBuf(route)
mcTransport.setCallBack { bytes ->
println("Read $bytes; decoded: ${decodeInt(bytes)}")
println("Read $bytes;")
}
mcTransport.writeToFile(size)
mcTransport.writeToFile(routeBytes)
mcTransport.sendBytes(routeBytes)
}
}
@@ -26,7 +26,7 @@ class LoadBin : AbstractHandler {
val message = fromServerObjectBuilder.build()
message.mergeFrom(CodedInputStream(data))
val responseMessage = toServerObjectBuilder.build()
mcTransport.writeToFile(message.data)
mcTransport.sendBytes(message.data)
val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000"
exec(stFlashCommand, { err, stdOutRes, stdErrRes ->
val resultCode = if (err != null) 15 else 0
@@ -1,9 +1,11 @@
package net.server.handlers.main
import net.server.handlers.AbstractHandler
import CodedInputStream
import MicroController
import RouteRequest
import RouteResponse
import encodeProtoBuf
import mcTransport
import net.server.handlers.AbstractHandler
/**
* Created by user on 7/28/16.