now room configurations can read from files

This commit is contained in:
MaximZaitsev
2016-09-05 15:12:46 +03:00
parent 23caaf6ef9
commit 92836b838f
6 changed files with 128 additions and 99 deletions
+16 -6
View File
@@ -20,11 +20,11 @@ fun main(args: Array<String>) {
println("incorrect config format!") println("incorrect config format!")
js("process.exit(1)") js("process.exit(1)")
} }
val clArgs = js("process.argv") val clArgs = js("process.argv")
var runAsEmulator = false var runAsEmulator = false
var runTests = false var runTests = false
var nextSeed = false var nextSeed = false
var room: Room = Room.testRoom1()
for (arg in clArgs) { for (arg in clArgs) {
if (nextSeed) { if (nextSeed) {
Rng.SEED = parseInt(arg, 10).toLong() Rng.SEED = parseInt(arg, 10).toLong()
@@ -34,17 +34,27 @@ fun main(args: Array<String>) {
when (arg) { when (arg) {
"-t" -> runTests = true "-t" -> runTests = true
"-e" -> runAsEmulator = true "-e" -> runAsEmulator = true
"-tr1" -> Room.walls = Room.testRoom1() "-tr1" -> room = Room.testRoom1()
"-tr2" -> Room.walls = Room.testRoom2() "-tr2" -> room = Room.testRoom2()
"-tr3" -> Room.walls = Room.testRoom3() "-tr3" -> room = Room.testRoom3()
"-r" -> Room.randomOn() "-r" -> Rng.ADD_RANDOM = true
"-s" -> nextSeed = true "-s" -> nextSeed = true
} }
if (arg.toString().contains("--room=")) {
val pathToFile = arg.toString().split("=")[1].replace("\"", "")
val data: String = fs.readFileSync(pathToFile, "utf8")
val roomOrNull = Room.roomFromString(data)
if (roomOrNull == null) {
println("error parsing string $data \nto room")
return
}
room = roomOrNull
}
} }
val carController = val carController =
if (runAsEmulator) { if (runAsEmulator) {
McState.instance.connect("") McState.instance.connect("")
ControllerEmulator() ControllerEmulator(room)
} else { } else {
ControllerToUsb() ControllerToUsb()
} }
+1 -1
View File
@@ -6,7 +6,7 @@ fun runTests() {
} }
private fun testCarEmulator() { private fun testCarEmulator() {
val controller = ControllerEmulator() val controller = ControllerEmulator(Room.testRoom1())
var sonarRequest = SonarRequest.BuilderSonarRequest(IntArray(2, { x -> x * 90 }), IntArray(0), 0, SonarRequest.Smoothing.NONE).build() var sonarRequest = SonarRequest.BuilderSonarRequest(IntArray(2, { x -> x * 90 }), IntArray(0), 0, SonarRequest.Smoothing.NONE).build()
var sonarResponse = SonarResponse.BuilderSonarResponse(IntArray(0)).build() var sonarResponse = SonarResponse.BuilderSonarResponse(IntArray(0)).build()
controller.executeRequestSensorData(sonarRequest, { bytes -> controller.executeRequestSensorData(sonarRequest, { bytes ->
@@ -4,8 +4,8 @@ import CarState
import RouteMetricRequest import RouteMetricRequest
import RouteRequest import RouteRequest
import RouteResponse import RouteResponse
import SonarRequest
import SonarExploreAngleRequest import SonarExploreAngleRequest
import SonarRequest
import SonarResponse import SonarResponse
import control.Controller import control.Controller
import encodeProtoBuf import encodeProtoBuf
@@ -14,7 +14,7 @@ import geometry.Vector
import room.Room import room.Room
import kotlin.Pair import kotlin.Pair
class ControllerEmulator : Controller { class ControllerEmulator(val room: Room) : Controller {
private val MOVE_VELOCITY = 0.05//sm/ms private val MOVE_VELOCITY = 0.05//sm/ms
private val ROTATION_VELOCITY = 0.05//degrees/ms private val ROTATION_VELOCITY = 0.05//degrees/ms
@@ -111,7 +111,7 @@ class ControllerEmulator : Controller {
if (distance == -1) { if (distance == -1) {
distances.add(distance) distances.add(distance)
} else { } else {
val delta = if (Room.ADD_RANDOM) Rng.nextInt(-2, 2) else 0//return one of -2 -1 0 1 or 2 val delta = if (Rng.ADD_RANDOM) Rng.nextInt(-2, 2) else 0//return one of -2 -1 0 1 or 2
distances.add(distance + delta) distances.add(distance + delta)
} }
} }
@@ -122,7 +122,7 @@ class ControllerEmulator : Controller {
private fun getDistance(xSensor0: Int, ySensor0: Int, sensorLine: Line, sensorVector: Vector): Int { private fun getDistance(xSensor0: Int, ySensor0: Int, sensorLine: Line, sensorVector: Vector): Int {
var result = Int.MAX_VALUE var result = Int.MAX_VALUE
for (wall in Room.walls) { for (wall in room.walls) {
val wallLine = wall.line val wallLine = wall.line
val slope = sensorLine.A * wallLine.B - sensorLine.B * wallLine.A val slope = sensorLine.A * wallLine.B - sensorLine.B * wallLine.A
if (Math.abs(slope) < 0.01) { if (Math.abs(slope) < 0.01) {
@@ -148,9 +148,6 @@ class ControllerEmulator : Controller {
result = currentDistance result = currentDistance
} }
} }
// if (result < 5 || result > 500) {
// return -1
// }
return result return result
} }
@@ -163,14 +160,6 @@ class ControllerEmulator : Controller {
return angleTmp % 360 return angleTmp % 360
} }
//return random int from array
private fun getRandomIntFrom(values: IntArray): Int {
val randomValue = (Math.random() * 1000).toInt()//value in [0,999]
val randomArrayIdx = randomValue * values.size / 1000//index in [0, values.size-1]
return values[randomArrayIdx]
}
fun executeCommand(commands: List<Pair<MoveDirection, Double>>, currentCommandIdx: Int, callBack: (ByteArray) -> Unit) { fun executeCommand(commands: List<Pair<MoveDirection, Double>>, currentCommandIdx: Int, callBack: (ByteArray) -> Unit) {
if (currentCommandIdx == commands.size) { if (currentCommandIdx == commands.size) {
val responseMessage = RouteResponse.BuilderRouteResponse(0).build() val responseMessage = RouteResponse.BuilderRouteResponse(0).build()
@@ -182,7 +171,7 @@ class ControllerEmulator : Controller {
//refresh car state //refresh car state
val carInstance = CarState.instance val carInstance = CarState.instance
val commandDistance = currentCommand.second val commandDistance = currentCommand.second
val coef = if (Room.ADD_RANDOM) Rng.nextInt(900, 1100).toDouble() / 1000.0 else 1.0 val coef = if (Rng.ADD_RANDOM) Rng.nextInt(900, 1100).toDouble() / 1000.0 else 1.0
val commandDistanceIncludeRandom = (commandDistance * coef).toInt() val commandDistanceIncludeRandom = (commandDistance * coef).toInt()
when (currentCommand.first) { when (currentCommand.first) {
MoveDirection.FORWARD -> carInstance.moving(commandDistanceIncludeRandom) MoveDirection.FORWARD -> carInstance.moving(commandDistanceIncludeRandom)
@@ -7,6 +7,8 @@ object Rng {
val c = 1013904223L val c = 1013904223L
val mod = 2147483648L val mod = 2147483648L
var ADD_RANDOM = false
fun abs(value: Long): Long { fun abs(value: Long): Long {
if (value < 0) if (value < 0)
return -value return -value
+72 -75
View File
@@ -2,86 +2,83 @@ package room
import geometry.Line import geometry.Line
object Room { class Room() {
val walls = arrayListOf<Wall>()
var walls = testRoom1() constructor(walls: Collection<Wall>) : this() {
var ADD_RANDOM = false this.walls.addAll(walls)
/*
_________
| |
| |
|________|
*/
fun randomOn() {
ADD_RANDOM = true
} }
fun testRoom1(): List<Wall> { companion object {
val upLine = Line(0.0, 1.0, -300.0) fun roomFromString(roomString: String): Room? {
val leftLine = Line(1.0, 0.0, 150.0) val result = Room()
val bottomLine = Line(0.0, 1.0, 45.0) roomString.split("\n").forEach { wallString ->
val rightLine = Line(1.0, 0.0, -200.0) if (!wallString.equals("")) {
// val wall = Wall.wallFromString(wallString) ?: return null
val walls = listOf<Wall>(Wall(upLine, 200, -150, 300, 300), result.walls.add(wall)
Wall(leftLine, -150, -150, 300, -45), }
Wall(bottomLine, -150, 200, -45, -45), }
Wall(rightLine, 200, 200, -45, 300) return result
) }
return walls
fun testRoom1(): Room {
val upLine = Line(0.0, 1.0, -300.0)
val leftLine = Line(1.0, 0.0, 150.0)
val bottomLine = Line(0.0, 1.0, 45.0)
val rightLine = Line(1.0, 0.0, -200.0)
val walls = listOf(Wall(upLine, 200, -150, 300, 300),
Wall(leftLine, -150, -150, 300, -45),
Wall(bottomLine, -150, 200, -45, -45),
Wall(rightLine, 200, 200, -45, 300)
)
return Room(walls)
}
fun testRoom2(): Room {
val upLine = Line(0.0, 1.0, -300.0)
val leftLine = Line(1.0, 0.0, 150.0)
val bottomLine = Line(0.0, 1.0, 45.0)
val rightLine = Line(1.0, 0.0, -200.0)
val bottomLine2 = Line(0.0, 1.0, -100.0)
val rightLine2 = Line(1.0, 0.0, -300.0)
val walls = listOf(Wall(upLine, 300, -150, 300, 300),
Wall(leftLine, -150, -150, 300, -45),
Wall(bottomLine, -150, 200, -45, -45),
Wall(rightLine, 200, 200, -45, 100),
Wall(bottomLine2, 200, 300, 100, 100),
Wall(rightLine2, 300, 300, 100, 300)
)
return Room(walls)
}
fun testRoom3(): Room {
val upLine = Line(0.0, 1.0, -300.0)
val leftLine = Line(1.0, 0.0, 150.0)
val bottomLine = Line(0.0, 1.0, 20.0)
val rightLine = Line(-1.2, 1.0, 140.0)
val rightLine2 = Line(1.0, 0.0, -200.0)
val walls = listOf(Wall(upLine, 200, -150, 300, 300),
Wall(leftLine, -150, -150, 300, -20),
Wall(bottomLine, -150, 100, -20, -20),
Wall(rightLine, 100, 200, -20, 100),
Wall(rightLine2, 200, 200, 100, 300)
)
return Room(walls)
}
} }
fun testRoom2(): List<Wall> { fun roomToString(): String {
/* var result = ""
________ walls.forEach {
| | result += it.wallToString() + "\n"
| ___| }
|___| return result
*/
val upLine = Line(0.0, 1.0, -300.0)
val leftLine = Line(1.0, 0.0, 150.0)
val bottomLine = Line(0.0, 1.0, 45.0)
val rightLine = Line(1.0, 0.0, -200.0)
val bottomLine2 = Line(0.0, 1.0, -100.0)
val rightLine2 = Line(1.0, 0.0, -300.0)
val walls = listOf<Wall>(Wall(upLine, 300, -150, 300, 300),
Wall(leftLine, -150, -150, 300, -45),
Wall(bottomLine, -150, 200, -45, -45),
Wall(rightLine, 200, 200, -45, 100),
Wall(bottomLine2, 200, 300, 100, 100),
Wall(rightLine2, 300, 300, 100, 300)
)
return walls
} }
fun testRoom3(): List<Wall> {
/*
_______
| |
|____/
*/
val upLine = Line(0.0, 1.0, -300.0)
val leftLine = Line(1.0, 0.0, 150.0)
val bottomLine = Line(0.0, 1.0, 20.0)
val rightLine = Line(-1.2, 1.0, 140.0)
val rightLine2 = Line(1.0, 0.0, -200.0)
val walls = listOf<Wall>(Wall(upLine, 200, -150, 300, 300),
Wall(leftLine, -150, -150, 300, -20),
Wall(bottomLine, -150, 100, -20, -20),
Wall(rightLine, 100, 200, -20, 100),
Wall(rightLine2, 200, 200, 100, 300)
)
return walls
}
} }
+32 -1
View File
@@ -4,4 +4,35 @@ import geometry.Line
class Wall constructor(val line: Line, val xFrom: Int, val xTo: Int, val yFrom: Int, val yTo: Int) { class Wall constructor(val line: Line, val xFrom: Int, val xTo: Int, val yFrom: Int, val yTo: Int) {
} companion object {
fun wallFromString(wall: String): Wall? {
val points = wall.replace(" ", "").split(",")
var xFrom = 0
var xTo = 0
var yTo = 0
var yFrom = 0
points.forEach {
val keyValue = it.split(":")
if (keyValue.size != 2) {
return null
}
try {
when (keyValue[0]) {
"xFrom" -> xFrom = parseInt(keyValue[1])
"yFrom" -> yFrom = parseInt(keyValue[1])
"xTo" -> xTo = parseInt(keyValue[1])
"yTo" -> yTo = parseInt(keyValue[1])
}
} catch (e: Exception) {
return null
}
}
val line = Line((yFrom - yTo).toDouble(), (xTo - xFrom).toDouble(), (xFrom * yTo - yFrom * xTo).toDouble())
return Wall(line, xFrom, xTo, yFrom, yTo)
}
}
fun wallToString(): String {
return "xFrom: $xFrom, yFrom: $yFrom, xTo: $xTo, yTo: $yTo"
}
}