diff --git a/car_fmw/src/Control.kt b/car_fmw/src/Control.kt index dd57ec69ced..914d846646a 100644 --- a/car_fmw/src/Control.kt +++ b/car_fmw/src/Control.kt @@ -1,11 +1,4 @@ -enum class RouteType(val id: Int) { - FORWARD(0), - BACKWARD(1), - LEFT(2), - RIGHT(3); -} - object Control { val BLINK_DURATION = 1000 @@ -51,15 +44,10 @@ object Control { val time = times[i] val direction = directions[i] - when (direction) { - RouteType.FORWARD.id -> Engine.forward() - RouteType.BACKWARD.id -> Engine.backward() - RouteType.LEFT.id -> Engine.left() - RouteType.RIGHT.id -> Engine.right() - } - + Engine.drive(direction) Time.wait(time) Engine.stop() + i++ } diff --git a/car_fmw/src/Voyager.kt b/car_fmw/src/Voyager.kt new file mode 100644 index 00000000000..cce3222a2b6 --- /dev/null +++ b/car_fmw/src/Voyager.kt @@ -0,0 +1,30 @@ + +object Voyager { + val VELOCITY_DRIVE: Double = 0.03 // centimeter in millisecond + val VEL0CITY_ROUTATE: Double = 0.001 // degree in millisecond + val SEGMENT_SIZE: Int = 30 // centimeter + + fun run() { + while (true) { + var distance = Sonar.getDistance(0) + while (distance == -1 || distance > SEGMENT_SIZE) { + drive(RouteType.FORWARD, SEGMENT_SIZE) + distance = Sonar.getDistance(0) + } + + rotate(Random.getInt()) + } + } + + private fun rotate(degree: Int) { + val time = (degree / VELOCITY_DRIVE).toInt() + Engine.left() + Time.wait(time) + } + + private fun drive(direction: RouteType, distance: Int) { + val time = (distance / VELOCITY_DRIVE).toInt() + Engine.drive(direction.id) + Time.wait(time) + } +} \ No newline at end of file diff --git a/car_fmw/src/include/Engine.kt b/car_fmw/src/include/Engine.kt index 18d7074b59e..4f5971fe860 100644 --- a/car_fmw/src/include/Engine.kt +++ b/car_fmw/src/include/Engine.kt @@ -6,6 +6,13 @@ external fun car_engine_backward() external fun car_engine_turn_left() external fun car_engine_turn_right() +enum class RouteType(val id: Int) { + FORWARD(0), + BACKWARD(1), + LEFT(2), + RIGHT(3); +} + object Engine { fun init() { car_engine_init() @@ -30,4 +37,13 @@ object Engine { fun right() { car_engine_turn_right() } + + fun drive(direction: Int) { + when (direction) { + RouteType.FORWARD.id -> forward() + RouteType.BACKWARD.id -> backward() + RouteType.LEFT.id -> left() + RouteType.RIGHT.id -> right() + } + } } \ No newline at end of file diff --git a/car_fmw/src/include/Random.kt b/car_fmw/src/include/Random.kt new file mode 100644 index 00000000000..9756c56f8ad --- /dev/null +++ b/car_fmw/src/include/Random.kt @@ -0,0 +1,6 @@ + +external fun car_random_int(): Int + +object Random { + fun getInt(): Int = car_random_int() +} diff --git a/car_fmw/src/main.kt b/car_fmw/src/main.kt index b7d22f07f44..ec66dda6298 100644 --- a/car_fmw/src/main.kt +++ b/car_fmw/src/main.kt @@ -11,4 +11,7 @@ fun main() { /* car task */ Control.run() + + /* Voyager */ + Voyager.run() } diff --git a/car_hw/src/car_random.c b/car_hw/src/car_random.c new file mode 100644 index 00000000000..3a6c5990ce2 --- /dev/null +++ b/car_hw/src/car_random.c @@ -0,0 +1,15 @@ + +#include + + +uint32_t prev_number = 0; + +#define MULTIPLIER 1664525 +#define INCREMENT 1013904223 +#define MODULE UINT32_MAX + +uint32_t car_random_get_int(void) +{ + prev_number = (prev_number * MULTIPLIER + INCREMENT) % MODULE; + return prev_number; +} diff --git a/car_hw/src/car_sonar.c b/car_hw/src/car_sonar.c index 6e5098d94ee..88ffb9ddbc6 100644 --- a/car_hw/src/car_sonar.c +++ b/car_hw/src/car_sonar.c @@ -13,9 +13,11 @@ static uint8_t sonar_degree(uint8_t degree) { - if (degree > MAX_DEGREE) + if (degree > MAX_DEGREE) degree = MAX_DEGREE; + degree = MAX_DEGREE - degree; + float factor = 1.0 * (BACK_ANGLE - FRONT_ANGLE) / MAX_DEGREE; return factor * degree + FRONT_ANGLE; } diff --git a/car_hw/src/include/car_random.h b/car_hw/src/include/car_random.h new file mode 100644 index 00000000000..d1c42195048 --- /dev/null +++ b/car_hw/src/include/car_random.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +uint32_t car_random_get_int(void);