add exception for try get new sonar data

This commit is contained in:
MaximZaitsev
2016-09-01 16:54:26 +03:00
parent 9bbeadd2d5
commit d293bb8b6b
3 changed files with 32 additions and 18 deletions
@@ -0,0 +1,4 @@
package Exceptions
class SonarDataException() : Exception() {
}
@@ -2,6 +2,7 @@ package algorithm
import CodedOutputStream
import Exceptions.InactiveCarException
import Exceptions.SonarDataException
import RouteMetricRequest
import SonarRequest
import algorithm.geometry.Angle
@@ -38,12 +39,14 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
// private val defaultAngles = arrayOf(Angle(0), Angle(60), Angle(70), Angle(80), Angle(90), Angle(100), Angle(110), Angle(120), Angle(180))
protected var requiredAngles = defaultAngles
protected enum class CarState {
WALL,
INNER,
OUTER
}
private var iterationCounter = 0
protected fun getData(angles: Array<Angle>): IntArray {
@@ -115,10 +118,16 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
this.requiredAngles = defaultAngles
val state = getCarState(anglesDistances) ?: return
val command = getCommand(anglesDistances, state)
if (command == null) {
val command: RouteMetricRequest
val state: CarState
try {
state = getCarState(anglesDistances)
command = getCommand(anglesDistances, state)
} catch (e: SonarDataException) {
Logger.log("iteration cancelled. need more data from sonar")
Logger.outdent()
Logger.log("============= FINISHING ITERATION ${iterationCounter} ============")
Logger.log("")
return
}
@@ -157,7 +166,7 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
private fun addToHistory(command: RouteMetricRequest) {
history.push(command)
while(history.size > historySize) {
while (history.size > historySize) {
history.removeAt(0)
}
}
@@ -186,10 +195,10 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
protected fun rollback() {
val lastCommand = popFromHistory()
val invertedCommand = inverseCommand(lastCommand)
Logger.log ("Rollback:")
Logger.log("Rollback:")
Logger.indent()
Logger.log ("Last command: ${lastCommand.toString()}")
Logger.log ("Inverted cmd: ${invertedCommand.toString()}")
Logger.log("Last command: ${lastCommand.toString()}")
Logger.log("Inverted cmd: ${invertedCommand.toString()}")
Logger.outdent()
moveCar(invertedCommand)
}
@@ -198,7 +207,7 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
Logger.log("=== Starting rollback for ${steps} steps ===")
Logger.indent()
var stepsRemaining = steps
while(stepsRemaining > 0 && history.size > 0) {
while (stepsRemaining > 0 && history.size > 0) {
Logger.log("Step: ${steps - stepsRemaining + 1}")
rollback()
stepsRemaining--
@@ -207,8 +216,8 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
Logger.log("=== Finished rollback ===")
}
protected abstract fun getCarState(anglesDistances: Map<Angle, AngleData>): CarState?
protected abstract fun getCommand(anglesDistances: Map<Angle, AngleData>, state: CarState): RouteMetricRequest?
protected abstract fun getCarState(anglesDistances: Map<Angle, AngleData>): CarState
protected abstract fun getCommand(anglesDistances: Map<Angle, AngleData>, state: CarState): RouteMetricRequest
protected abstract fun afterGetCommand(route: RouteMetricRequest)
abstract fun isCompleted(): Boolean
@@ -1,5 +1,6 @@
package algorithm
import Exceptions.SonarDataException
import Logger.log
import RouteMetricRequest
import SonarRequest
@@ -134,7 +135,7 @@ class RoomBypassingAlgorithm(thisCar: Car, exchanger: Exchanger<IntArray>) : Abs
}
}
override fun getCommand(anglesDistances: Map<Angle, AngleData>, state: CarState): RouteMetricRequest? {
override fun getCommand(anglesDistances: Map<Angle, AngleData>, state: CarState): RouteMetricRequest {
val dist0 = anglesDistances[Angle(0)]!!
val dist70 = anglesDistances[Angle(70)]!!
val dist80 = anglesDistances[Angle(80)]!!
@@ -151,7 +152,7 @@ class RoomBypassingAlgorithm(thisCar: Car, exchanger: Exchanger<IntArray>) : Abs
log("Found to many -1 in angle distances, falling back")
rollback()
//todo Теоретически такая ситуация может быть валидной, если сразу после внутреннего угла идёт внешний
return null
throw SonarDataException()
}
val average = (anglesDistances.values
@@ -233,7 +234,7 @@ class RoomBypassingAlgorithm(thisCar: Car, exchanger: Exchanger<IntArray>) : Abs
log("")
// Check if wall is too close or too far
log ("5. Check if we have to move closer or farther to the wall")
log("5. Check if we have to move closer or farther to the wall")
if (dist90.distance > DISTANCE_TO_WALL_UPPER_BOUND || dist90.distance < DISTANCE_TO_WALL_LOWER_BOUND) {
log("Flaw in distance to the parallel wall found, correcting")
return correctDistanceToParallelWall(anglesDistances, state)
@@ -242,21 +243,21 @@ class RoomBypassingAlgorithm(thisCar: Car, exchanger: Exchanger<IntArray>) : Abs
// Approaching inner corner and getting spurious reflection from 2 walls on 60;
// Just move forward to get closer to the corner;
log ("6. Check if we are detecting echo reflections approaching inner corner")
log("6. Check if we are detecting echo reflections approaching inner corner")
if (dist70.distance - dist0.distance > ECHO_REFLECTION_DIFF) {
log("Echo reflection detected, moving forward")
return moveForward(dist0.distance)
}
log ("")
log("")
// Approaching outer corner (parallel wall is ending soon, but not yet);
// Just move forward to get to the end of the wall
log ("7. Check if we are detecting far wall approaching outer corner")
log("7. Check if we are detecting far wall approaching outer corner")
if (dist80.distance == -1 || Math.abs(dist100.distance - dist80.distance) > ISOSCALENESS_MAX_DIFF) {
log("Approaching outer corner, moving forward")
return moveForward(dist0.distance)
}
log ("")
log("")
// default case: everything is ok, just move forward
log("8. Default case: moving forward")