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 CodedOutputStream
import Exceptions.InactiveCarException import Exceptions.InactiveCarException
import Exceptions.SonarDataException
import RouteMetricRequest import RouteMetricRequest
import SonarRequest import SonarRequest
import algorithm.geometry.Angle 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)) // 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 var requiredAngles = defaultAngles
protected enum class CarState { protected enum class CarState {
WALL, WALL,
INNER, INNER,
OUTER OUTER
} }
private var iterationCounter = 0 private var iterationCounter = 0
protected fun getData(angles: Array<Angle>): IntArray { protected fun getData(angles: Array<Angle>): IntArray {
@@ -115,10 +118,16 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
this.requiredAngles = defaultAngles this.requiredAngles = defaultAngles
val state = getCarState(anglesDistances) ?: return val command: RouteMetricRequest
val command = getCommand(anglesDistances, state) val state: CarState
try {
if (command == null) { 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 return
} }
@@ -207,8 +216,8 @@ abstract class AbstractAlgorithm(val thisCar: Car, val exchanger: Exchanger<IntA
Logger.log("=== Finished rollback ===") Logger.log("=== Finished rollback ===")
} }
protected abstract fun getCarState(anglesDistances: Map<Angle, AngleData>): CarState? protected abstract fun getCarState(anglesDistances: Map<Angle, AngleData>): CarState
protected abstract fun getCommand(anglesDistances: Map<Angle, AngleData>, state: CarState): RouteMetricRequest? protected abstract fun getCommand(anglesDistances: Map<Angle, AngleData>, state: CarState): RouteMetricRequest
protected abstract fun afterGetCommand(route: RouteMetricRequest) protected abstract fun afterGetCommand(route: RouteMetricRequest)
abstract fun isCompleted(): Boolean abstract fun isCompleted(): Boolean
@@ -1,5 +1,6 @@
package algorithm package algorithm
import Exceptions.SonarDataException
import Logger.log import Logger.log
import RouteMetricRequest import RouteMetricRequest
import SonarRequest 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 dist0 = anglesDistances[Angle(0)]!!
val dist70 = anglesDistances[Angle(70)]!! val dist70 = anglesDistances[Angle(70)]!!
val dist80 = anglesDistances[Angle(80)]!! 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") log("Found to many -1 in angle distances, falling back")
rollback() rollback()
//todo Теоретически такая ситуация может быть валидной, если сразу после внутреннего угла идёт внешний //todo Теоретически такая ситуация может быть валидной, если сразу после внутреннего угла идёт внешний
return null throw SonarDataException()
} }
val average = (anglesDistances.values val average = (anglesDistances.values