car_fmw: sonar explore angle feature
This commit is contained in:
@@ -16,6 +16,7 @@ object Control {
|
|||||||
TaskRequest.Type.ROUTE.id -> routeTask()
|
TaskRequest.Type.ROUTE.id -> routeTask()
|
||||||
TaskRequest.Type.ROUTE_METRIC.id -> routeMetricTask()
|
TaskRequest.Type.ROUTE_METRIC.id -> routeMetricTask()
|
||||||
TaskRequest.Type.SONAR.id -> sonarTask()
|
TaskRequest.Type.SONAR.id -> sonarTask()
|
||||||
|
TaskRequest.Type.SONAR_EXPLORE.id -> sonarExploreTask()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +84,17 @@ object Control {
|
|||||||
Writer.writeSonar(response)
|
Writer.writeSonar(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun sonarExploreTask() {
|
||||||
|
val request = Reader.readSonarExplore()
|
||||||
|
|
||||||
|
val angle = request.angle
|
||||||
|
val window = request.windowSize
|
||||||
|
val result = Sonar.getRange(max(angle - window, 0), min(angle + window, 360))
|
||||||
|
|
||||||
|
val response = SonarExploreAngleResponse.BuilderSonarExploreAngleResponse(result).build()
|
||||||
|
Writer.writeSonarExplore(response)
|
||||||
|
}
|
||||||
|
|
||||||
private fun sonarMeasure(smoothing: SonarRequest.Smoothing, attempts: Int, angle: Int, windowSize: Int): Int {
|
private fun sonarMeasure(smoothing: SonarRequest.Smoothing, attempts: Int, angle: Int, windowSize: Int): Int {
|
||||||
val data = IntArray(attempts)
|
val data = IntArray(attempts)
|
||||||
var i = 0
|
var i = 0
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ object Reader {
|
|||||||
return SonarRequest.BuilderSonarRequest(IntArray(0), IntArray(0), 0, SonarRequest.Smoothing.NONE).parseFrom(stream).build()
|
return SonarRequest.BuilderSonarRequest(IntArray(0), IntArray(0), 0, SonarRequest.Smoothing.NONE).parseFrom(stream).build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun readSonarExplore(): SonarExploreAngleRequest {
|
||||||
|
val stream = getInputStream()
|
||||||
|
return SonarExploreAngleRequest.BuilderSonarExploreAngleRequest(0, 0, 0).parseFrom(stream).build()
|
||||||
|
}
|
||||||
|
|
||||||
private fun getInputStream(): CodedInputStream {
|
private fun getInputStream(): CodedInputStream {
|
||||||
val buffer = Connection.receiveByteArray()
|
val buffer = Connection.receiveByteArray()
|
||||||
return CodedInputStream(buffer)
|
return CodedInputStream(buffer)
|
||||||
@@ -49,6 +54,12 @@ object Writer {
|
|||||||
Connection.sendByteArray(stream.buffer)
|
Connection.sendByteArray(stream.buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun writeSonarExplore(response: SonarExploreAngleResponse) {
|
||||||
|
val stream = getOutputStream(response.getSizeNoTag())
|
||||||
|
response.writeTo(stream)
|
||||||
|
Connection.sendByteArray(stream.buffer)
|
||||||
|
}
|
||||||
|
|
||||||
private fun getOutputStream(size: Int): CodedOutputStream {
|
private fun getOutputStream(size: Int): CodedOutputStream {
|
||||||
val buffer = ByteArray(size)
|
val buffer = ByteArray(size)
|
||||||
return CodedOutputStream(buffer)
|
return CodedOutputStream(buffer)
|
||||||
|
|||||||
@@ -42,6 +42,17 @@ object Sonar {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getRange(start: Int, stop: Int): IntArray {
|
||||||
|
val result = IntArray(stop - start + 1)
|
||||||
|
var i = 0
|
||||||
|
while (i < stop - start + 1) {
|
||||||
|
result[i] = getDistance(start + i)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
private fun getOneOfRange(start: Int, stop: Int): Int {
|
private fun getOneOfRange(start: Int, stop: Int): Int {
|
||||||
var i = start
|
var i = start
|
||||||
var distance = getDistance(start)
|
var distance = getDistance(start)
|
||||||
@@ -58,18 +69,6 @@ object Sonar {
|
|||||||
return distance
|
return distance
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getRange(start: Int, stop: Int): IntArray {
|
|
||||||
val result = IntArray(stop - start + 1)
|
|
||||||
var i = 0
|
|
||||||
while (i < stop - start + 1) {
|
|
||||||
result[i] = getDistance(start + i)
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun calibrate() {
|
fun calibrate() {
|
||||||
getDistance(180)
|
getDistance(180)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,3 +2,11 @@
|
|||||||
fun positive(i: Int): Boolean {
|
fun positive(i: Int): Boolean {
|
||||||
return i > 0
|
return i > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun min(first: Int, second: Int): Int {
|
||||||
|
if (first < second) return first else return second
|
||||||
|
}
|
||||||
|
|
||||||
|
fun max(first: Int, second: Int): Int {
|
||||||
|
if (first > second) return first else return second
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,3 +16,12 @@ message SonarRequest {
|
|||||||
message SonarResponse {
|
message SonarResponse {
|
||||||
repeated int32 distances = 1;
|
repeated int32 distances = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message SonarExploreAngleRequest {
|
||||||
|
int32 angle = 1;
|
||||||
|
int32 windowSize = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message SonarExploreAngleResponse {
|
||||||
|
repeated int32 distances = 1;
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,5 +10,6 @@ message TaskRequest {
|
|||||||
ROUTE = 1;
|
ROUTE = 1;
|
||||||
SONAR = 2;
|
SONAR = 2;
|
||||||
ROUTE_METRIC = 3;
|
ROUTE_METRIC = 3;
|
||||||
|
SONAR_EXPLORE = 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user