changeset 162:ed9dcbb8df43 main

Finish up the cone-bot 1.0 s/w
author Bob Cook <bob@bobcookdev.com>
date Sat, 10 Nov 2012 08:09:50 -0800
parents 24c77b4f9c9d
children 13020c7be732
files main/robots/cone-bot/main.cpp
diffstat 1 files changed, 35 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/main/robots/cone-bot/main.cpp	Sun Nov 04 21:18:29 2012 -0800
+++ b/main/robots/cone-bot/main.cpp	Sat Nov 10 08:09:50 2012 -0800
@@ -123,6 +123,30 @@
 
 // ----------------------------------------------------------------------------------------
 
+static void delay_until_next_action()
+{
+    //--    Delay between 25 and 200 seconds.
+
+    uint32_t delay_deadline = ( random() % 200 ) + 25;
+    printf_P( PSTR("delay until next action: %ld seconds\n"), delay_deadline );
+
+    //--    Our clock is ~100 Hz e.g. 100 counts per second. So scale and set the time
+    //      of expiry to be in the future of the current time.
+
+    delay_deadline *= 100;
+    delay_deadline += tickcount();
+
+    //--    Loop while we are still waiting. We can safely sleep the CPU, as the clock
+    //      interrupt will wake us up.
+
+    while ( delay_deadline > tickcount() )
+    {
+        sleep_mode();
+    }
+}
+
+// ----------------------------------------------------------------------------------------
+
 static void action_move_fwd()
 {
     motor_m1_fwd( 120 );
@@ -243,7 +267,7 @@
     //--    Initialize the hardware and send a "hello we started" message.
 
     hw_init();
-    printf_P( PSTR("hello world\n") );
+    printf_P( PSTR("hello world, i am a cone-bot...\n") );
 
     //--    Retract the cone position, give the servo time to react.
 
@@ -251,82 +275,27 @@
     spinwait_delay_ms( 1000 );
     servo_set_off();
 
-    //--    Main.
-
-#if 1
-    for ( uint8_t i = 0; i < 20; ++i )
-    {
-//        uint8_t delayUntilAction = static_cast< uint8_t >( random() % 25 );
-//        printf_P( PSTR("delay = %d "), delayUntilAction );
-//        print_action( choose_next_action() );
-//        printf_P( PSTR("\n") );
+    //--    First action doesn't start for 5 seconds.
 
-        uint8_t action = choose_next_action();
-        do_action( action );
-
-        spinwait_delay_ms( 5000 );
-    }
+    printf_P( PSTR("cone-bot starting in 5 seconds...\n") );
+    spinwait_delay_ms( 5000 );
 
-    for ( ;; )
-    {
-        action_blink();
-        spinwait_delay_ms( 5000 );
-    }
-#endif
+    //--    Main.
 
     for ( ;; )
     {
-        action_blink();
-
-        spinwait_delay_ms( 500 );
-
-//        action_cone_up_down();
+        delay_until_next_action();
 
-//        spinwait_delay_ms( 500 );
-
-        motor_m1_fwd( 120 );
-        motor_m2_fwd( 120 );
-        spinwait_delay_ms( 2000 );
+        uint8_t action = choose_next_action();
 
-//        motor_set_off();
-//        spinwait_delay_ms( 500 );
+        printf_P( PSTR("next action: ") );
+        print_action( action );
+        putchar( '\n' );
 
-//        motor_m1_rev( 85 );
-//        spinwait_delay_ms( 2000 );
-//        motor_m1_stop();
+        do_action( action );
 
-//        motor_m2_rev( 85 );
-//        spinwait_delay_ms( 2000 );
-
-        motor_set_off();
-        spinwait_delay_ms( 500 );
     }
 
-
-#if 0
-    do_diagnostics();
-
-    motor_m1_fwd( 20 );
-    motor_m2_rev( 90 );
-
-    for ( ;; )
-    {
-        for ( uint8_t i = 20; i <= 39; ++i )
-        {
-            OCR1A = i;
-            spinwait_delay_ms( 500 );
-        }
-
-        led_one_on();
-        spinwait_delay_ms( 500 );
-        led_one_off();
-        spinwait_delay_ms( 500 );
-    }
-#endif
-
-    cli();
-    sleep_cpu();
-
     return 0;
 }