car_fmw: Voyager velocity
This commit is contained in:
+13
-7
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
object Voyager {
|
object Voyager {
|
||||||
val VELOCITY_DRIVE: Double = 0.03 // centimeter in millisecond
|
val VELOCITY_DRIVE: Double = 0.05 // centimeter in millisecond
|
||||||
val VEL0CITY_ROUTATE: Double = 0.015 // degree in millisecond
|
val VEL0CITY_ROTATE: Double = 0.05 // degree in millisecond
|
||||||
val SEGMENT_SIZE: Int = 30 // centimeter
|
val SEGMENT_SIZE: Int = 30 // centimeter
|
||||||
|
|
||||||
val MAX_ANGLE: Int = 180 // degree
|
val MAX_ANGLE: Int = 180 // degree
|
||||||
@@ -9,20 +9,21 @@ object Voyager {
|
|||||||
|
|
||||||
fun run() {
|
fun run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
var distance = Sonar.getDistance(0)
|
var distance = getSmoothDistance(0)
|
||||||
while (distance == -1 || distance > SEGMENT_SIZE) {
|
while (distance == -1 || distance > 4 * SEGMENT_SIZE) {
|
||||||
drive(RouteType.FORWARD, SEGMENT_SIZE)
|
drive(RouteType.FORWARD, SEGMENT_SIZE)
|
||||||
distance = Sonar.getDistance(0)
|
distance = getSmoothDistance(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
rotate(Random.getInt() % MAX_ANGLE)
|
rotate(45)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun rotate(degree: Int) {
|
private fun rotate(degree: Int) {
|
||||||
val duration = (degree.toDouble() / VELOCITY_DRIVE).toInt()
|
val duration = (degree.toDouble() / VEL0CITY_ROTATE).toInt()
|
||||||
Engine.left()
|
Engine.left()
|
||||||
Time.wait(duration)
|
Time.wait(duration)
|
||||||
|
smoothStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drive(direction: RouteType, distance: Int) {
|
private fun drive(direction: RouteType, distance: Int) {
|
||||||
@@ -32,6 +33,11 @@ object Voyager {
|
|||||||
smoothStop()
|
smoothStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getSmoothDistance(degree: Int): Int {
|
||||||
|
Sonar.getDistance(180)
|
||||||
|
return Sonar.getDistance(degree)
|
||||||
|
}
|
||||||
|
|
||||||
private fun smoothStop() {
|
private fun smoothStop() {
|
||||||
Engine.stop()
|
Engine.stop()
|
||||||
Time.wait(ENGINE_DELAY)
|
Time.wait(ENGINE_DELAY)
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ void car_engine_backward(void)
|
|||||||
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_RWHEEL_BKWD_PIN);
|
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_RWHEEL_BKWD_PIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
void car_engine_turn_left(void)
|
void car_engine_turn_right(void)
|
||||||
{
|
{
|
||||||
car_engine_stop();
|
car_engine_stop();
|
||||||
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_ENABLE_PIN);
|
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_ENABLE_PIN);
|
||||||
@@ -64,7 +64,7 @@ void car_engine_turn_left(void)
|
|||||||
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_RWHEEL_FWD_PIN);
|
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_RWHEEL_FWD_PIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
void car_engine_turn_right(void)
|
void car_engine_turn_left(void)
|
||||||
{
|
{
|
||||||
car_engine_stop();
|
car_engine_stop();
|
||||||
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_ENABLE_PIN);
|
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_ENABLE_PIN);
|
||||||
|
|||||||
Reference in New Issue
Block a user