car_fmw: distance and degree metrics, drive functions
This commit is contained in:
@@ -44,7 +44,7 @@ object Control {
|
||||
val time = times[i]
|
||||
val direction = directions[i]
|
||||
|
||||
Engine.drive(direction)
|
||||
Engine.go(direction)
|
||||
Time.wait(time)
|
||||
Engine.stop()
|
||||
|
||||
|
||||
+5
-32
@@ -1,45 +1,18 @@
|
||||
|
||||
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 ENGINE_DELAY: Int = 500 // milisecond
|
||||
val SEGMENT_SIZE: Int = 30 // centimeter
|
||||
|
||||
fun run() {
|
||||
while (true) {
|
||||
var distance = getSmoothDistance(0)
|
||||
var distance = Sonar.getSmoothDistance(0)
|
||||
while (distance == -1 || distance > 4 * SEGMENT_SIZE) {
|
||||
drive(RouteType.FORWARD, SEGMENT_SIZE)
|
||||
distance = getSmoothDistance(0)
|
||||
Engine.drive(RouteType.FORWARD.id, SEGMENT_SIZE)
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,9 @@ enum class RouteType(val id: Int) {
|
||||
}
|
||||
|
||||
object Engine {
|
||||
val VELOCITY_DRIVE: Double = 0.05 // centimeter in millisecond
|
||||
val VEL0CITY_ROTATE: Double = 0.05 // degree in millisecond
|
||||
|
||||
fun init() {
|
||||
car_engine_init()
|
||||
}
|
||||
@@ -38,7 +41,7 @@ object Engine {
|
||||
car_engine_turn_right()
|
||||
}
|
||||
|
||||
fun drive(direction: Int) {
|
||||
fun go(direction: Int) {
|
||||
when (direction) {
|
||||
RouteType.FORWARD.id -> forward()
|
||||
RouteType.BACKWARD.id -> backward()
|
||||
@@ -46,4 +49,16 @@ object Engine {
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
external fun car_sonar_init()
|
||||
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 getSmoothDistance(degree: Int): Int {
|
||||
Sonar.getDistance(180)
|
||||
return Sonar.getDistance(degree)
|
||||
}
|
||||
|
||||
}
|
||||
+2
-2
@@ -10,8 +10,8 @@ fun main() {
|
||||
Leds.set(Leds.GREEN, true)
|
||||
|
||||
/* car task */
|
||||
// Control.run()
|
||||
Control.run()
|
||||
|
||||
/* Voyager */
|
||||
Voyager.run()
|
||||
// Voyager.run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user