diff --git a/car_srv/kotlinSrv/src/Main.kt b/car_srv/kotlinSrv/src/Main.kt index 9c0644d4a77..3b233abcfc5 100644 --- a/car_srv/kotlinSrv/src/Main.kt +++ b/car_srv/kotlinSrv/src/Main.kt @@ -1,5 +1,6 @@ import control.car.ControllerToUsb import control.emulator.ControllerEmulator +import control.emulator.Rng import net.Client import net.server.handlers.AbstractHandler import net.server.handlers.debug.Memory @@ -22,13 +23,21 @@ fun main(args: Array) { val clArgs = js("process.argv") var runAsEmulator = false var runTests = false + var nextSeed = false for (arg in clArgs) { + if (nextSeed) { + Rng.SEED = parseInt(arg, 10).toLong() + Rng.curState = Rng.SEED.toLong() + nextSeed = false + } when (arg) { "-t" -> runTests = true "-e" -> runAsEmulator = true "-tr1" -> Room.walls = Room.testRoom1() "-tr2" -> Room.walls = Room.testRoom2() "-tr3" -> Room.walls = Room.testRoom3() + "-r" -> Room.randomOn() + "-s" -> nextSeed = true } } val carController = diff --git a/car_srv/kotlinSrv/src/control/emulator/ControllerEmulator.kt b/car_srv/kotlinSrv/src/control/emulator/ControllerEmulator.kt index ff08ede84bd..fc447cc5499 100644 --- a/car_srv/kotlinSrv/src/control/emulator/ControllerEmulator.kt +++ b/car_srv/kotlinSrv/src/control/emulator/ControllerEmulator.kt @@ -18,8 +18,6 @@ class ControllerEmulator : Controller { private val MOVE_VELOCITY = 0.05//sm/ms private val ROTATION_VELOCITY = 0.05//degrees/ms - private val ADD_RANDOM = false - enum class MoveDirection { LEFT, RIGHT, @@ -113,7 +111,7 @@ class ControllerEmulator : Controller { if (distance == -1) { distances.add(distance) } else { - val delta = if (ADD_RANDOM) getRandomIntFrom(IntArray(5, { x -> x - 2 })) else 0//return one of -2 -1 0 1 or 2 + val delta = if (Room.ADD_RANDOM) Rng.nextInt(-2, 2) else 0//return one of -2 -1 0 1 or 2 distances.add(distance + delta) } } @@ -184,8 +182,8 @@ class ControllerEmulator : Controller { //refresh car state val carInstance = CarState.instance val commandDistance = currentCommand.second - val delta = if (ADD_RANDOM) Math.random() * 0.2 + 0.9 else 1.0// delta in [0.9, 1.1) - val commandDistanceIncludeRandom = (commandDistance * delta).toInt() + val coef = if (Room.ADD_RANDOM) Rng.nextInt(900, 1100).toDouble() / 1000.0 else 1.0 + val commandDistanceIncludeRandom = (commandDistance * coef).toInt() when (currentCommand.first) { MoveDirection.FORWARD -> carInstance.moving(commandDistanceIncludeRandom) MoveDirection.BACKWARD -> carInstance.moving(commandDistanceIncludeRandom) diff --git a/car_srv/kotlinSrv/src/control/emulator/Rng.kt b/car_srv/kotlinSrv/src/control/emulator/Rng.kt new file mode 100644 index 00000000000..eebb6980f5f --- /dev/null +++ b/car_srv/kotlinSrv/src/control/emulator/Rng.kt @@ -0,0 +1,29 @@ +package control.emulator + +object Rng { + var SEED = 55L + var curState = SEED + val a = 1664525L + val c = 1013904223L + val mod = 2147483648L + + fun abs(value: Long): Long { + if (value < 0) + return -value + return value + } + + fun nextInt(): Int { + val res = curState + curState = abs(a * curState + c) % mod + return (res % mod).toInt() + } + + fun nextInt(min_val: Int, max_val: Int): Int { + val rand = nextInt() + val size = max_val - min_val + 1 + val randInRange = rand % size + val res = randInRange + min_val + return res + } +} \ No newline at end of file diff --git a/car_srv/kotlinSrv/src/room/Room.kt b/car_srv/kotlinSrv/src/room/Room.kt index 95cfaf0111d..60f8b28f8ba 100644 --- a/car_srv/kotlinSrv/src/room/Room.kt +++ b/car_srv/kotlinSrv/src/room/Room.kt @@ -6,6 +6,7 @@ object Room { var walls = testRoom1() + var ADD_RANDOM = false /* _________ @@ -15,6 +16,10 @@ object Room { */ + fun randomOn() { + ADD_RANDOM = true + } + fun testRoom1(): List { val upLine = Line(0.0, 1.0, -300.0) val leftLine = Line(1.0, 0.0, 150.0)