car_fmw: smooth stop, small fixes

This commit is contained in:
e5l
2016-08-24 11:55:19 +03:00
parent cfe38a5b64
commit 234c05c120
3 changed files with 18 additions and 9 deletions
+15 -6
View File
@@ -1,9 +1,12 @@
object Voyager {
val VELOCITY_DRIVE: Double = 0.03 // centimeter in millisecond
val VEL0CITY_ROUTATE: Double = 0.001 // degree in millisecond
val VEL0CITY_ROUTATE: Double = 0.015 // degree in millisecond
val SEGMENT_SIZE: Int = 30 // centimeter
val MAX_ANGLE: Int = 180 // degree
val ENGINE_DELAY: Int = 500 // milisecond
fun run() {
while (true) {
var distance = Sonar.getDistance(0)
@@ -12,19 +15,25 @@ object Voyager {
distance = Sonar.getDistance(0)
}
rotate(Random.getInt())
rotate(Random.getInt() % MAX_ANGLE)
}
}
private fun rotate(degree: Int) {
val time = (degree.toDouble() / VELOCITY_DRIVE).toInt()
val duration = (degree.toDouble() / VELOCITY_DRIVE).toInt()
Engine.left()
Time.wait(time)
Time.wait(duration)
}
private fun drive(direction: RouteType, distance: Int) {
val time = (distance.toDouble() / VELOCITY_DRIVE).toInt()
val duration = (distance.toDouble() / VELOCITY_DRIVE).toInt()
Engine.drive(direction.id)
Time.wait(time)
Time.wait(duration)
smoothStop()
}
private fun smoothStop() {
Engine.stop()
Time.wait(ENGINE_DELAY)
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
external fun car_random_int(): Int
external fun car_random_get_int(): Int
object Random {
fun getInt(): Int = car_random_int()
fun getInt(): Int = car_random_get_int()
}
+1 -1
View File
@@ -10,7 +10,7 @@ fun main() {
Leds.set(Leds.GREEN, true)
/* car task */
Control.run()
// Control.run()
/* Voyager */
Voyager.run()