car_srv: angles fix, conditions fix

This commit is contained in:
e5l
2016-09-01 11:17:06 +03:00
parent b3f5d67172
commit b0155cf914
8 changed files with 29 additions and 43 deletions
@@ -18,16 +18,16 @@ class CarController(var car: Car) {
var position = Pair(0.0, 0.0)
private set
private val WALL_DISTANCE = 50
private val CHARGE_CORRECTION = 1.0 //0.85
private var angle = 0.0
fun moveTo(to: Pair<Double, Double>) {
val driveAngle = (Math.toDegrees(Math.atan2(to.first, to.second)) + 360) % 360
rotateOn(angleDistance(angle, driveAngle))
drive(Direction.FORWARD, Math.max(0.0, distance(position, to)).toInt() - WALL_DISTANCE)
drive(Direction.FORWARD, 1)
position = to
position = Pair(position.first + Math.cos(Math.toRadians(angle)),
position.second + Math.sin(Math.toRadians(angle)))
}
fun rotateOn(target: Double) {
@@ -58,7 +58,7 @@ class CarController(var car: Car) {
private fun drive(direction: Direction, distance: Int) {
val request = RouteMetricRequest.BuilderRouteMetricRequest(intArrayOf((distance * CHARGE_CORRECTION).toInt()), intArrayOf(direction.id)).build()
val data = CarClient.serialize(request.getSizeNoTag(), { i -> request.writeTo(i) })
CarClient.sendRequest(car, CarClient.Request.ROUTE_METRIC, data).get()
val response = CarClient.sendRequest(car, CarClient.Request.ROUTE_METRIC, data).get()
}
}
@@ -2,24 +2,18 @@ package RoomScanner
class RoomScanner(val controller: CarController) : Thread() {
private val points = mutableListOf<Pair<Double, Double>>()
private val MAX_VALID_DISTANCE = 100
override fun run() {
println("Car connected ${controller.car.host}")
var i = 1
while (true) {
step()
if (i % 10 == 0) {
println(plot(points))
}
i++
println(plot(points))
}
}
private fun step() {
val iterationPoints = scan()
points.addAll(iterationPoints.filter { it.first > 0 && it.second > 0 })
points.addAll(iterationPoints.filter { it.first > 0 || it.second > 0 })
val target = iterationPoints.filter { it.first > 0 && it.second > 0 }.maxBy { distance(controller.position, it) }
target ?: return
@@ -38,14 +32,12 @@ class RoomScanner(val controller: CarController) : Thread() {
}
val metrics = mutableListOf<List<Double>>()
for (i in intArrayOf(0, 90, 180, 270)) {
for (i in intArrayOf(0, 180)) {
controller.rotateOn(i.toDouble())
metrics.add(controller.scan(horizon))
}
val points = merge(metrics.reversed(), distance).reversed()
controller.rotateOn(0.0)
return points.mapIndexed { i: Int, d: Double -> controller.convertToPoint((i * 5).toDouble(), d) }.toMutableList()
}
@@ -112,10 +112,7 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
this.requiredAngles = defaultAngles
val state = getCarState(anglesDistances)
if (state == null) {
return
}
val state = getCarState(anglesDistances) ?: return
val command = getCommand(anglesDistances, state)
if (command == null) {
+1 -3
View File
@@ -7,11 +7,9 @@ import algorithm.geometry.Angle
import algorithm.geometry.Line
import algorithm.geometry.Point
import algorithm.geometry.Wall
import objects.Car
import java.util.*
object RoomModel {
val walls = arrayListOf<Wall>(Wall(Angle(0)))
val walls = arrayListOf(Wall(Angle(0)))
var finished = false
val linesModel = listOf(Line(0.0, 1.0, -300.0),
@@ -9,5 +9,5 @@ fun Double.gt(other: Double): Boolean {
}
fun Double.eq(other: Double): Boolean {
return Math.abs(this - other) < Util.eps;
return Math.abs(this - other) < Util.eps
}
@@ -22,10 +22,10 @@ class Line(var A: Double, var B: Double, var C: Double) {
}
fun normalize() {
val div = Math.sqrt(A * A + B * B);
A /= div;
B /= div;
C /= div;
val div = Math.sqrt(A * A + B * B)
A /= div
B /= div
C /= div
if (A.eq(0.0)) {
if (B.lt(0.0)) {
@@ -51,7 +51,7 @@ class Line(var A: Double, var B: Double, var C: Double) {
override fun equals(other: Any?): Boolean {
if (other == null) return false
if (!(other is Line)) return false
if (other !is Line) return false
Logger.log("Comparing lines: ")
Logger.log(" this = ${this.toString()}")
@@ -15,8 +15,8 @@ class Vector constructor(var x: Double, var y: Double) {
}
fun angleTo(other: Vector): Angle {
var sp = scalarProduct(other)
var cosinus = sp / length() / other.length()
val sp = scalarProduct(other)
val cosinus = sp / length() / other.length()
return Angle(Math.acos(cosinus))
}
+12 -13
View File
@@ -1,7 +1,6 @@
package algorithm.geometry
import java.util.*
import algorithm.geometry.Vector
data class Wall(val wallAngleOX: Angle,
val rawPoints: ArrayList<Point> = arrayListOf<Point>(),
@@ -64,10 +63,10 @@ data class Wall(val wallAngleOX: Angle,
var sumx = 0.0
var sumy = 0.0
var sumx2 = 0.0
for (pt in rawPoints) {
sumx += pt.x
sumx2 += pt.x * pt.x
sumy += pt.y
for ((x2, y2) in rawPoints) {
sumx += x2
sumx2 += x2 * x2
sumy += y2
n++
}
val xbar = sumx / n
@@ -77,10 +76,10 @@ data class Wall(val wallAngleOX: Angle,
var xxbar = 0.0
var yybar = 0.0
var xybar = 0.0
for (pt in rawPoints) {
xxbar += (pt.x - xbar) * (pt.x - xbar)
yybar += (pt.y - ybar) * (pt.y - ybar)
xybar += (pt.x - xbar) * (pt.y - ybar)
for ((x1, y1) in rawPoints) {
xxbar += (x1 - xbar) * (x1 - xbar)
yybar += (y1 - ybar) * (y1 - ybar)
xybar += (x1 - xbar) * (y1 - ybar)
}
var beta1 = xybar / xxbar
@@ -90,9 +89,9 @@ data class Wall(val wallAngleOX: Angle,
val df = n - 2
var rss = 0.0 // residual sum of squares
var ssr = 0.0 // regression sum of squares
for (pt in rawPoints) {
val fit = beta1 * pt.x + beta0
rss += (fit - pt.y) * (fit - pt.y)
for ((x, y) in rawPoints) {
val fit = beta1 * x + beta0
rss += (fit - y) * (fit - y)
ssr += (fit - ybar) * (fit - ybar)
}
@@ -118,7 +117,7 @@ data class Wall(val wallAngleOX: Angle,
override fun equals(other: Any?): Boolean {
if (other == null) return false
if (!(other is Wall)) return false
if (other !is Wall) return false
return line.equals(other.line)
}
}