car_fmw: distance and degree metrics, drive functions

This commit is contained in:
e5l
2016-08-25 15:18:24 +03:00
parent 1370952a95
commit 27321af56d
5 changed files with 30 additions and 37 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ object Control {
val time = times[i] val time = times[i]
val direction = directions[i] val direction = directions[i]
Engine.drive(direction) Engine.go(direction)
Time.wait(time) Time.wait(time)
Engine.stop() Engine.stop()
+5 -32
View File
@@ -1,45 +1,18 @@
object Voyager { object Voyager {
val VELOCITY_DRIVE: Double = 0.05 // centimeter in millisecond
val VEL0CITY_ROTATE: Double = 0.05 // degree in millisecond
val SEGMENT_SIZE: Int = 30 // centimeter
val MAX_ANGLE: Int = 180 // degree val MAX_ANGLE: Int = 180 // degree
val ENGINE_DELAY: Int = 500 // milisecond val SEGMENT_SIZE: Int = 30 // centimeter
fun run() { fun run() {
while (true) { while (true) {
var distance = getSmoothDistance(0) var distance = Sonar.getSmoothDistance(0)
while (distance == -1 || distance > 4 * SEGMENT_SIZE) { while (distance == -1 || distance > 4 * SEGMENT_SIZE) {
drive(RouteType.FORWARD, SEGMENT_SIZE) Engine.drive(RouteType.FORWARD.id, SEGMENT_SIZE)
distance = getSmoothDistance(0) distance = Sonar.getSmoothDistance(0)
} }
rotate(45) Engine.rotate(RouteType.LEFT.id, 45)
} }
} }
private fun rotate(degree: Int) {
val duration = (degree.toDouble() / VEL0CITY_ROTATE).toInt()
Engine.left()
Time.wait(duration)
smoothStop()
}
private fun drive(direction: RouteType, distance: Int) {
val duration = (distance.toDouble() / VELOCITY_DRIVE).toInt()
Engine.drive(direction.id)
Time.wait(duration)
smoothStop()
}
private fun getSmoothDistance(degree: Int): Int {
Sonar.getDistance(180)
return Sonar.getDistance(degree)
}
private fun smoothStop() {
Engine.stop()
Time.wait(ENGINE_DELAY)
}
} }
+16 -1
View File
@@ -14,6 +14,9 @@ enum class RouteType(val id: Int) {
} }
object Engine { object Engine {
val VELOCITY_DRIVE: Double = 0.05 // centimeter in millisecond
val VEL0CITY_ROTATE: Double = 0.05 // degree in millisecond
fun init() { fun init() {
car_engine_init() car_engine_init()
} }
@@ -38,7 +41,7 @@ object Engine {
car_engine_turn_right() car_engine_turn_right()
} }
fun drive(direction: Int) { fun go(direction: Int) {
when (direction) { when (direction) {
RouteType.FORWARD.id -> forward() RouteType.FORWARD.id -> forward()
RouteType.BACKWARD.id -> backward() RouteType.BACKWARD.id -> backward()
@@ -46,4 +49,16 @@ object Engine {
RouteType.RIGHT.id -> right() RouteType.RIGHT.id -> right()
} }
} }
fun drive(direction: Int, distance: Int) {
go(direction)
Time.wait((distance.toDouble() / VELOCITY_DRIVE).toInt())
stop()
}
fun rotate(direction: Int, degree: Int) {
go(direction)
Time.wait((degree.toDouble() / VEL0CITY_ROTATE).toInt())
stop()
}
} }
+6 -1
View File
@@ -1,4 +1,3 @@
external fun car_sonar_init() external fun car_sonar_init()
external fun car_sonar_get_dist(degree: Byte): Short external fun car_sonar_get_dist(degree: Byte): Short
@@ -8,4 +7,10 @@ object Sonar {
} }
fun getDistance(degree: Int): Int = car_sonar_get_dist(degree.toByte()).toInt() fun getDistance(degree: Int): Int = car_sonar_get_dist(degree.toByte()).toInt()
fun getSmoothDistance(degree: Int): Int {
Sonar.getDistance(180)
return Sonar.getDistance(degree)
}
} }
+2 -2
View File
@@ -10,8 +10,8 @@ fun main() {
Leds.set(Leds.GREEN, true) Leds.set(Leds.GREEN, true)
/* car task */ /* car task */
// Control.run() Control.run()
/* Voyager */ /* Voyager */
Voyager.run() // Voyager.run()
} }