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.RouteExecutor
import carControl.RouteExecutorImpl.MoveDirection 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. * Created by user on 7/27/16.
*/ */
class Car constructor(val routeExecutor: RouteExecutor, val controller: Control) { class Car constructor(val routeExecutor: RouteExecutor, val controller: Control) {
val velocityMove = 0.3278
val velocityRotation = 12.3
//position //position
var x: Double var x: Double
var y: Double var y: Double
@@ -32,15 +33,15 @@ class Car constructor(val routeExecutor: RouteExecutor, val controller: Control)
val deltaSeconds = delta.toDouble() / 1000 val deltaSeconds = delta.toDouble() / 1000
when (moveDirection) { when (moveDirection) {
MoveDirection.FORWARD -> { MoveDirection.FORWARD -> {
this.x += this.velocityMove * deltaSeconds * Math.cos(this.angle * Math.PI / 180); this.x += MOVE_VELOCITY * deltaSeconds * Math.cos(this.angle * Math.PI / 180);
this.y += this.velocityMove * deltaSeconds * Math.sin(this.angle * Math.PI / 180); this.y += MOVE_VELOCITY * deltaSeconds * Math.sin(this.angle * Math.PI / 180);
} }
MoveDirection.BACKWARD -> { MoveDirection.BACKWARD -> {
this.x -= this.velocityMove * deltaSeconds * Math.cos(this.angle * Math.PI / 180); this.x -= MOVE_VELOCITY * deltaSeconds * Math.cos(this.angle * Math.PI / 180);
this.y -= this.velocityMove * deltaSeconds * Math.sin(this.angle * Math.PI / 180); this.y -= MOVE_VELOCITY * deltaSeconds * Math.sin(this.angle * Math.PI / 180);
} }
MoveDirection.LEFT -> this.angle += velocityRotation * deltaSeconds MoveDirection.LEFT -> this.angle += ROTATION_VELOCITY * deltaSeconds
MoveDirection.RIGHT -> this.angle -= velocityRotation * deltaSeconds MoveDirection.RIGHT -> this.angle -= ROTATION_VELOCITY * deltaSeconds
else -> { else -> {
} }
@@ -64,9 +65,9 @@ class Car constructor(val routeExecutor: RouteExecutor, val controller: Control)
} }
if (moveDirection != MoveDirection.STOP) { if (moveDirection != MoveDirection.STOP) {
if (moveDirection == MoveDirection.FORWARD || moveDirection == MoveDirection.BACKWARD) { if (moveDirection == MoveDirection.FORWARD || moveDirection == MoveDirection.BACKWARD) {
controller.delay(getTimeForMoving(value, velocityMove), callBack) controller.delay(getTimeForMoving(value, MOVE_VELOCITY), callBack)
} else { } 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 val resultBytes = arrayListOf<Byte>()
private var callback: (bytes: ByteArray) -> Unit = {} 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) println("write: " + bytes)
val bytesTemp = bytes writeStream.write(js("new Buffer(bytes)"))
writeStream.write(js("new Buffer(bytesTemp)"))
} }
fun writeToFile(byte: Byte) { fun sendBytes(byte: Byte) {
writeToFile(ByteArray(1, { idx -> byte })) sendBytes(ByteArray(1, { idx -> byte }))
} }
fun initStreams(pathToFile: String) { fun initStreams(pathToFile: String) {
@@ -56,20 +58,29 @@ class McTransport() {
//need first ${protoHeaderLength} bytes - header //need first ${protoHeaderLength} bytes - header
return -1 return -1
} }
var res = 0 return decodeInt(resultBytes.toByteArray())
for (i in 0..protoHeaderLength - 1) { }
val curByte = resultBytes.get(i)
val curBytePositive: Int = if (curByte < 0) { private fun encodeInt(i: Int): ByteArray {
256 + curByte val result = ByteArray(4)
} else { result[0] = i.shr(24).toByte()
curByte.toInt() result[1] = i.shr(16).toByte()
} result[2] = i.shr(8).toByte()
res += curBytePositive * Math.pow(2.0, 24.0 - i * 8).toInt() result[3] = i.toByte()
}
return res 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() val mcTransport = McTransport()
+2 -2
View File
@@ -32,7 +32,7 @@ class MicroController private constructor() {
fun start() { fun start() {
connectToServer(config.getCarIp(), serverPort) connectToServer(config.getCarIp(), carServerPort)
mcTransport.setCallBack { bytes -> mcTransport.setCallBack { bytes ->
println("read: " + bytes.toString()) println("read: " + bytes.toString())
@@ -96,7 +96,7 @@ class MicroController private constructor() {
} }
fun connectToServer(thisIp: String, thisPort: Int) { 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()) val bytes = ByteArray(requestObject.getSizeNoTag())
requestObject.writeTo(CodedOutputStream(bytes)) requestObject.writeTo(CodedOutputStream(bytes))
net.sendRequest(js("new Buffer(bytes)"), "/connect", { resultData -> net.sendRequest(js("new Buffer(bytes)"), "/connect", { resultData ->
+3 -1
View File
@@ -30,7 +30,9 @@ class Udev {
fun isOurMcDevice(device: dynamic): Boolean { fun isOurMcDevice(device: dynamic): Boolean {
val microController = MicroController.instance 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() { fun disconnectDevice() {
@@ -14,27 +14,27 @@ class ControlImpl : Control {
override fun moveCarLeft() { override fun moveCarLeft() {
println("move left") println("move left")
mcTransport.writeToFile(4) mcTransport.sendBytes(4)
} }
override fun moveCarRight() { override fun moveCarRight() {
println("move rigth") println("move rigth")
mcTransport.writeToFile(3) mcTransport.sendBytes(3)
} }
override fun moveCarForward() { override fun moveCarForward() {
println("move forward") println("move forward")
mcTransport.writeToFile(1) mcTransport.sendBytes(1)
} }
override fun moveCarBackward() { override fun moveCarBackward() {
println("move backward") println("move backward")
mcTransport.writeToFile(2) mcTransport.sendBytes(2)
} }
override fun stopCar() { override fun stopCar() {
println("stopped") println("stopped")
mcTransport.writeToFile(0) mcTransport.sendBytes(0)
} }
override fun delay(ms: Int, callBack: () -> Unit) { override fun delay(ms: Int, callBack: () -> Unit) {
@@ -10,14 +10,12 @@ class RouteExecutorToUsb : RouteExecutor {
override fun executeRoute(route: RouteRequest) { override fun executeRoute(route: RouteRequest) {
println("Execute Route:") println("Execute Route:")
val size = encodeInt(route.getSizeNoTag())
val routeBytes = encodeProtoBuf(route) val routeBytes = encodeProtoBuf(route)
mcTransport.setCallBack { bytes -> mcTransport.setCallBack { bytes ->
println("Read $bytes; decoded: ${decodeInt(bytes)}") println("Read $bytes;")
} }
mcTransport.writeToFile(size) mcTransport.sendBytes(routeBytes)
mcTransport.writeToFile(routeBytes)
} }
} }
@@ -26,7 +26,7 @@ class LoadBin : AbstractHandler {
val message = fromServerObjectBuilder.build() val message = fromServerObjectBuilder.build()
message.mergeFrom(CodedInputStream(data)) message.mergeFrom(CodedInputStream(data))
val responseMessage = toServerObjectBuilder.build() val responseMessage = toServerObjectBuilder.build()
mcTransport.writeToFile(message.data) mcTransport.sendBytes(message.data)
val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000" val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000"
exec(stFlashCommand, { err, stdOutRes, stdErrRes -> exec(stFlashCommand, { err, stdOutRes, stdErrRes ->
val resultCode = if (err != null) 15 else 0 val resultCode = if (err != null) 15 else 0
@@ -1,9 +1,11 @@
package net.server.handlers.main package net.server.handlers.main
import net.server.handlers.AbstractHandler
import CodedInputStream import CodedInputStream
import MicroController
import RouteRequest
import RouteResponse
import encodeProtoBuf import encodeProtoBuf
import mcTransport import net.server.handlers.AbstractHandler
/** /**
* Created by user on 7/28/16. * Created by user on 7/28/16.