car_fmw: smooth stop, small fixes
This commit is contained in:
+15
-6
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -10,7 +10,7 @@ fun main() {
|
||||
Leds.set(Leds.GREEN, true)
|
||||
|
||||
/* car task */
|
||||
Control.run()
|
||||
// Control.run()
|
||||
|
||||
/* Voyager */
|
||||
Voyager.run()
|
||||
|
||||
Reference in New Issue
Block a user