changeset 161:24c77b4f9c9d main

Cone-bot version 1.
author Bob Cook <bob@bobcookdev.com>
date Sun, 04 Nov 2012 21:18:29 -0800
parents a498438cc538
children ed9dcbb8df43
files main/robots/cone-bot/diag.cpp main/robots/cone-bot/functions.h main/robots/cone-bot/hw.cpp main/robots/cone-bot/jamfile main/robots/cone-bot/main.cpp main/robots/cone-bot/project_defs.h
diffstat 6 files changed, 1133 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cone-bot/diag.cpp	Sun Nov 04 21:18:29 2012 -0800
@@ -0,0 +1,215 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+// 
+//  Cone-bot, the wonderfully odd yet intriguing useless robot.
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+// 
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+// 
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#include <avr/interrupt.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/sleep.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "project_defs.h"
+
+#include "functions.h"
+
+// always after other includes
+#include "packages/avr/device/workaround34734.h"
+
+// ----------------------------------------------------------------------------------------
+
+static void test_motors()
+{
+    printf_P( PSTR("testing motors (q to exit) ") );
+    printf_P( PSTR("M1: +a/-s/z0  M2: +d/-f/c0\n\n") );
+
+    bool do_update = true;
+
+    int16_t m1 = 0;
+    int16_t m2 = 0;
+
+    for ( ;; )
+    {
+        int  c = getchar();
+
+        if ( c == 'q' ) break;
+
+        switch ( c )
+        {
+            case 'a':
+                if ( m1 < 255 ) ++m1;
+                do_update = true;
+                break;
+
+            case 's':
+                if ( m1 > -255 ) --m1;
+                do_update = true;
+                break;
+
+            case 'z':
+                m1 = 0;
+                do_update = true;
+                break;
+
+            case 'd':
+                if ( m2 < 255 ) ++m2;
+                do_update = true;
+                break;
+
+            case 'f':
+                if ( m2 > -255 ) --m2;
+                do_update = true;
+                break;
+
+            case 'c':
+                m2 = 0;
+                do_update = true;
+                break;
+        }
+
+        if ( do_update )
+        {
+            printf_P( PSTR("\rM1: %04d   M2: %04d"), m1, m2 );
+
+            do_update = false;
+
+            if ( m1 == 0 )
+            {
+                motor_m1_stop();
+            }
+            else if ( m1 > 0 )
+            {
+                motor_m1_fwd( static_cast< uint8_t >( m1 ) );
+            }
+            else
+            {
+                motor_m1_rev( static_cast< uint8_t >( -1 * m1 ) );
+            }
+
+            if ( m2 == 0 )
+            {
+                motor_m2_stop();
+            }
+            else if ( m2 > 0 )
+            {
+                motor_m2_fwd( static_cast< uint8_t >( m2 ) );
+            }
+            else
+            {
+                motor_m2_rev( static_cast< uint8_t >( -1 * m2 ) );
+            }
+        }
+    }
+
+    putchar( '\n' );
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void test_servo()
+{
+    printf_P( PSTR("testing servo (q to exit) ") );
+    printf_P( PSTR("+a/-s to set value\n\n") );
+
+    bool do_update = true;
+
+    uint8_t value = 20;
+
+    for ( ;; )
+    {
+        int  c = getchar();
+
+        if ( c == 'q' ) break;
+
+        switch ( c )
+        {
+            case 'a':
+                if ( value < 39 ) ++value;
+                do_update = true;
+                break;
+
+            case 's':
+                if ( value > 20 ) --value;
+                do_update = true;
+                break;
+        }
+
+        if ( do_update )
+        {
+            printf_P( PSTR("\rservo: %03d"), value );
+
+            do_update = false;
+
+            servo_set_position( value );
+        }
+    }
+
+    putchar( '\n' );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void do_diagnostics()
+{
+    for ( ;; )
+    {
+        printf_P( PSTR("system diagnostics\n\n") );
+
+        printf_P( PSTR("1. test motors\n") );
+        printf_P( PSTR("2. test servo\n") );
+        
+        bool did_cmd = false;
+
+        while ( ! did_cmd )
+        {
+            int c = getchar();
+
+            switch ( c )
+            {
+                case '1':
+                    test_motors();
+                    did_cmd = true;
+                    break;
+
+                case '2':
+                    test_servo();
+                    did_cmd = true;
+                    break;
+
+                default:
+                    break;
+            }
+        }
+
+        putchar( '\n' );
+    }
+}
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cone-bot/functions.h	Sun Nov 04 21:18:29 2012 -0800
@@ -0,0 +1,70 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+// 
+//  Cone-bot, the wonderfully odd yet intriguing useless robot.
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+// 
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+// 
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+void hw_init();
+
+// ----------------------------------------------------------------------------------------
+
+uint32_t tickcount();
+
+// ----------------------------------------------------------------------------------------
+
+void leds_off();
+
+void led_one_off();
+void led_one_on();
+void led_one_toggle();
+
+void led_two_off();
+void led_two_on();
+void led_two_toggle();
+
+// ----------------------------------------------------------------------------------------
+
+void servo_set_off();
+void servo_set_position( uint8_t value );
+
+// ----------------------------------------------------------------------------------------
+
+void motor_set_off();
+
+void motor_m1_stop();
+void motor_m1_fwd( uint8_t value );
+void motor_m1_rev( uint8_t value );
+
+void motor_m2_stop();
+void motor_m2_fwd( uint8_t value );
+void motor_m2_rev( uint8_t value );
+
+// ----------------------------------------------------------------------------------------
+
+void do_diagnostics();
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cone-bot/hw.cpp	Sun Nov 04 21:18:29 2012 -0800
@@ -0,0 +1,421 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+// 
+//  Cone-bot, the wonderfully odd yet intriguing useless robot.
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+// 
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+// 
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#include <avr/interrupt.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/sleep.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "project_defs.h"
+
+#include "functions.h"
+
+#include "packages/avr/device/int_helpers.h"
+#include "packages/avr/device/simavr_helpers.h"
+//#include "packages/avr/device/spinwait.h"
+
+#include "packages/avr/redir/redir.h"
+
+#include "packages/common/util/misc.h"
+
+// always after other includes
+#include "packages/avr/device/workaround34734.h"
+
+// ----------------------------------------------------------------------------------------
+
+bool g_motors_active;
+bool g_servo_active;
+
+uint32_t g_ticks;
+
+// ----------------------------------------------------------------------------------------
+//  Cone-bot is based on a Pololu Baby Orangutan 168; port/pin assignments:
+//
+//      PORTB.0 :
+//      PORTB.1 : motor control, m1-a
+//      PORTB.2 : motor control, m2-a
+//      PORTB.3 :
+//      PORTB.4 :
+//      PORTB.5 :
+//
+//      PORTC.0 : servo output
+//      PORTC.1 : led #1
+//      PORTC.2 : led #2
+//      PORTC.3 :
+//      PORTC.4 :
+//      PORTC.5 :
+//
+//      ADC6    :
+//      ADC7    :
+//
+//      PORTD.0 : serial rx
+//      PORTD.1 : serial tx
+//      PORTD.2 :
+//      PORTD.3 :
+//      PORTD.4 :
+//      PORTD.5 : motor control, m1-b
+//      PORTD.6 : motor control, m2-b
+//      PORTD.7 :
+//
+// ----------------------------------------------------------------------------------------
+
+SIMAVR_DECLARE_DEVICE_PARAMETERS();
+
+SIMAVR_VCD_TRACE_PORT_PIN( SERVO, PORTC, PC0 );
+SIMAVR_VCD_TRACE_PORT_PIN( M1_A,  PORTD, PD5 );
+SIMAVR_VCD_TRACE_PORT_PIN( M1_B,  PORTD, PD6 );
+SIMAVR_VCD_TRACE_PORT_PIN( M2_A,  PORTD, PD3 );
+SIMAVR_VCD_TRACE_PORT_PIN( M2_B,  PORTB, PB3 );
+
+// ----------------------------------------------------------------------------------------
+
+static void enable_hw_for_motors()
+{
+    if ( g_motors_active ) return;
+
+    //--    Timer0 is used for motor control m1 & m2.
+
+    TCCR0A = 0
+
+        | ( 0 << COM0A1 ) | ( 0 << COM0A0 )                 // OC0A disconnected
+        | ( 0 << COM0B1 ) | ( 0 << COM0B0 )                 // OC0B disconnected
+        | ( 1 << WGM01  ) | ( 1 << WGM00  )                 // Fast PWM mode (3)
+        ;
+
+    TCCR0B = 0
+
+        | ( 0 << FOC0A ) | ( 0 << FOC0B )                   // no force output compare
+        | ( 0 << WGM02 )                                    // Fast PWM mode (3)
+        | ( 0 << CS02  ) | ( 1 << CS01  ) | ( 0 << CS00 )   // clk/8
+        ;
+
+    OCR0A = 0;
+    OCR0B = 0;
+
+    TIMSK0 |= ( 1 << OCIE0B ) | ( 1 << OCIE0A ) | ( 1 << TOIE0 );
+
+    g_motors_active = true;
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void disable_hw_for_motors()
+{
+    if ( ! g_motors_active ) return;
+
+    TCCR0A = 0;
+    TCCR0B = 0;
+    TIMSK0 &= ~( ( 1 << OCIE0B ) | ( 1 << OCIE0A ) | ( 1 << TOIE0 ) );
+
+    PORTB &= ~( 1 << PB1 );
+    PORTD &= ~( 1 << PD5 );
+
+    PORTB &= ~( 1 << PB2 );
+    PORTD &= ~( 1 << PD6 );
+
+    g_motors_active = false;
+}
+
+// ----------------------------------------------------------------------------------------
+
+ISR( TIMER0_COMPA_vect )
+{
+    PORTB &= ~( 1 << PB1 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+ISR( TIMER0_COMPB_vect )
+{
+    PORTB &= ~( 1 << PB2 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+ISR( TIMER0_OVF_vect )
+{
+    PORTB |= ( 1 << PB1 );
+    PORTB |= ( 1 << PB2 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void enable_hw_for_servo()
+{
+    if ( g_servo_active ) return;
+
+    //--    Timer1 is used for servo control. Each count is 0.0000512 seconds (51.2 us)
+    //      so the normal servo range of 1.0 ms to 2.0 ms is 20 to 39.
+
+    TCCR1A = 0
+
+        | ( 0 << COM1A1 ) | ( 0 << COM1A0 )                 // OC1A disconnected
+        | ( 0 << COM1B1 ) | ( 0 << COM1B0 )                 // OC1B disconnected
+        | ( 0 << WGM11  ) | ( 1 << WGM10  )                 // Fast 8-bit PWM mode (5)
+        ;
+
+    TCCR1B = 0
+
+        | ( 0 << WGM13 ) | ( 1 << WGM12 )                   // Fast 8-bit PWM mode (5)
+        | ( 1 << CS12  ) | ( 0 << CS11  ) | ( 1 << CS10 )   // clk/1024
+        ;
+
+    OCR1A = 20; // default
+
+    TIMSK1 |= ( 1 << OCIE1A ) | ( 1 << TOIE1 );
+
+    g_servo_active = true;
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void disable_hw_for_servo()
+{
+    if ( ! g_servo_active ) return;
+
+    TCCR1A = 0;
+    TCCR1B = 0;
+    TIMSK1 &= ~( ( 1 << OCIE1A ) | ( 1 << TOIE1 ) );
+
+    PORTC &= ~( 1 << PC0 );
+
+    g_servo_active = false;
+}
+
+// ----------------------------------------------------------------------------------------
+
+ISR( TIMER1_COMPA_vect )
+{
+    PORTC &= ~( 1 << PC0 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+ISR( TIMER1_OVF_vect )
+{
+    PORTC |= ( 1 << PC0 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void hw_init()
+{
+    //--    Turn off all interrupts.
+
+    cli();
+    interrupts_clear_all();
+
+    //--    Indicator LEDs on PORTC.1, PORTC.2
+
+    DDRC |= ( 1 << PC1 ) | ( 1 << PC2 );
+    leds_off();
+
+    //--    Outputs for motor control and servo.
+
+    DDRB |= ( 1 << PB1 ) | ( 1 << PB2 );
+    DDRC |= ( 1 << PC0 );
+    DDRD |= ( 1 << PD5 ) | ( 1 << PD6 );
+
+    //--    Set Timer2 as an (approximate) 100 Hz internal clock.
+
+    TCCR2A = 0
+
+        | ( 0 << COM2A1 ) | ( 0 << COM2A0 )             // OC2A disconnected
+        | ( 0 << COM2B1 ) | ( 0 << COM2B0 )             // OC2B disconnected
+        | ( 1 << WGM21  ) | ( 0 << WGM20  )             // CTC (mode 2)
+        ;
+
+    TCCR2B = 0
+
+        | ( 0 << WGM22 )                                    // CTC (mode 2)
+        | ( 1 << CS22  ) | ( 1 << CS21 ) | ( 1 << CS22 )    // clk/1024
+        ;
+
+    OCR2A = 195;    // gives 100.16025641025641 Hz
+
+    TIMSK2 |= ( 1 << OCIE2A );
+
+    //--    Initialize the motor and servo control h/w to "off".
+
+    disable_hw_for_motors();
+    disable_hw_for_servo();
+
+    //--    When we sleep we want the "idle" mode e.g. wake up on any interrupt.
+
+    set_sleep_mode( SLEEP_MODE_IDLE );
+
+    //--    Initialize the serial port redirection.
+
+    redir_initialize();
+
+    //--    Re-enable interrupts.
+
+    sei();
+}
+
+// ----------------------------------------------------------------------------------------
+
+ISR( TIMER2_COMPA_vect )
+{
+    ++g_ticks;
+}
+
+// ----------------------------------------------------------------------------------------
+
+uint32_t tickcount()
+{
+    return g_ticks;
+}
+
+// ----------------------------------------------------------------------------------------
+
+void leds_off()
+{
+    PORTC &= ~( ( 1 << PC1 ) | ( 1 << PC2 ) );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void led_one_off()
+{
+    PORTC &= ~( 1 << PC1 );
+}
+
+void led_one_on()
+{
+    PORTC |= ( 1 << PC1 );
+}
+
+void led_one_toggle()
+{
+    PORTC ^= ( 1 << PC1 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void led_two_off()
+{
+    PORTC &= ~( 1 << PC2 );
+}
+
+void led_two_on()
+{
+    PORTC |= ( 1 << PC2 );
+}
+
+void led_two_toggle()
+{
+    PORTC ^= ( 1 << PC2 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void motor_set_off()
+{
+    disable_hw_for_motors();
+    OCR0A = 0;
+    OCR0B = 0;
+}
+
+// ----------------------------------------------------------------------------------------
+
+void motor_m1_stop()
+{
+    enable_hw_for_motors();
+    OCR0A = 0;
+    PORTD &= ~( 1 << PD5 );
+}
+
+void motor_m1_fwd( uint8_t value )
+{
+    enable_hw_for_motors();
+    OCR0A = value;
+    PORTD &= ~( 1 << PD5 );
+}
+
+void motor_m1_rev( uint8_t value )
+{
+    enable_hw_for_motors();
+    OCR0A = 255 - value;
+    PORTD |= ( 1 << PD5 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void motor_m2_stop()
+{
+    enable_hw_for_motors();
+    OCR0B = 0;
+    PORTD &= ~( 1 << PD6 );
+}
+
+void motor_m2_fwd( uint8_t value )
+{
+    enable_hw_for_motors();
+    OCR0B = value;
+    PORTD &= ~( 1 << PD6 );
+}
+
+void motor_m2_rev( uint8_t value )
+{
+    enable_hw_for_motors();
+    OCR0B = 255 - value;
+    PORTD |= ( 1 << PD6 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void servo_set_off()
+{
+    disable_hw_for_servo();
+}
+
+void servo_set_position( uint8_t value )
+{
+    enable_hw_for_servo();
+
+    if ( value < 20 )
+    {
+        OCR1A = 20;
+    }
+    else if ( value > 39 )
+    {
+        OCR1A = 39;
+    }
+    else
+    {
+        OCR1A = value;
+    }
+}
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cone-bot/jamfile	Sun Nov 04 21:18:29 2012 -0800
@@ -0,0 +1,44 @@
+# -----------------------------------------------------------------------------------------
+#
+#   projects/cone-bot/jamfile
+#   Copyright (C) 2012 Bob Cook
+#
+#   Bob Cook Development, Robotics Library
+#   http://www.bobcookdev.com/rl/
+#    
+#   Permission is hereby granted, free of charge, to any person obtaining a copy
+#   of this software and associated documentation files (the "Software"), to deal
+#   in the Software without restriction, including without limitation the rights
+#   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+#   copies of the Software, and to permit persons to whom the Software is
+#   furnished to do so, subject to the following conditions:
+#
+#   The above copyright notice and this permission notice shall be included in
+#   all copies or substantial portions of the Software.
+#
+#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+#   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+#   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+#   THE SOFTWARE.
+#
+# -----------------------------------------------------------------------------------------
+
+if $(TRACE) { echo "trace /projects/cone-bot/jamfile" ; }
+
+SubDir TOP projects cone-bot ;
+
+# -----------------------------------------------------------------------------------------
+
+avr_executable
+    cone-bot atmega168
+    : main.cpp hw.cpp diag.cpp
+      packages.avr.device.pkg
+      packages.avr.redir.pkg
+      packages.common.util.pkg
+    ;
+
+# -----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cone-bot/main.cpp	Sun Nov 04 21:18:29 2012 -0800
@@ -0,0 +1,334 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+// 
+//  Cone-bot, the wonderfully odd yet intriguing useless robot.
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+// 
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+// 
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#include <avr/interrupt.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/sleep.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "project_defs.h"
+
+#include "functions.h"
+
+#include "packages/avr/device/int_helpers.h"
+#include "packages/avr/device/spinwait.h"
+
+#include "packages/avr/redir/redir.h"
+
+#include "packages/common/util/misc.h"
+
+// always after other includes
+#include "packages/avr/device/workaround34734.h"
+
+// ----------------------------------------------------------------------------------------
+
+const uint8_t ACTION_MOVE_FWD = 0;
+const uint8_t ACTION_MOVE_REV = 1;
+const uint8_t ACTION_TURN_R   = 2;
+const uint8_t ACTION_TURN_L   = 3;
+const uint8_t ACTION_BLINK    = 4;
+const uint8_t ACTION_UP_DOWN  = 5;
+const uint8_t ACTION_COUNT    = 6;
+
+static void print_action( uint8_t action )
+{
+    switch ( action )
+    {
+        case ACTION_MOVE_FWD:
+            printf_P( PSTR("move forward") );
+            break;
+
+        case ACTION_MOVE_REV:
+            printf_P( PSTR("move reverse") );
+            break;
+
+        case ACTION_TURN_R:
+            printf_P( PSTR("turn right") );
+            break;
+
+        case ACTION_TURN_L:
+            printf_P( PSTR("turn left") );
+            break;
+
+        case ACTION_BLINK:
+            printf_P( PSTR("blink") );
+            break;
+
+        case ACTION_UP_DOWN:
+            printf_P( PSTR("cone up/down") );
+            break;
+
+        default:
+            printf_P( PSTR("(unknown)") );
+            break;
+    }
+}
+
+// ----------------------------------------------------------------------------------------
+
+static uint8_t choose_next_action()
+{
+    static uint8_t last_action = 0;
+
+    uint8_t action;
+
+    for ( ;; )
+    {
+        action = static_cast< uint8_t >( random() % ACTION_COUNT );
+
+        if ( action == last_action ) continue;
+
+        if ( action == ACTION_MOVE_FWD && last_action == ACTION_MOVE_REV ) continue;
+        if ( action == ACTION_MOVE_REV && last_action == ACTION_MOVE_FWD ) continue;
+
+        if ( action == ACTION_BLINK && last_action == ACTION_UP_DOWN ) continue;
+        if ( action == ACTION_UP_DOWN && last_action == ACTION_BLINK ) continue;
+
+        break;
+    }
+
+    last_action = action;
+    return action;
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void action_move_fwd()
+{
+    motor_m1_fwd( 120 );
+    motor_m2_fwd( 120 );
+    spinwait_delay_ms( 1200 );
+    motor_set_off();
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void action_move_rev()
+{
+    motor_m1_rev( 80 );
+    motor_m2_rev( 80 );
+    spinwait_delay_ms( 1000 );
+    motor_set_off();
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void action_turn_right()
+{
+    motor_m2_fwd( 120 );
+    spinwait_delay_ms( 1000 );
+    motor_m1_fwd( 120 );
+    spinwait_delay_ms( 1000 );
+    motor_set_off();
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void action_turn_left()
+{
+    motor_m1_fwd( 120 );
+    spinwait_delay_ms( 1000 );
+    motor_m2_fwd( 120 );
+    spinwait_delay_ms( 1000 );
+    motor_set_off();
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void action_blink()
+{
+    led_one_on();
+    led_two_on();
+    spinwait_delay_ms( 75 );
+    led_one_off();
+    led_two_off();
+    
+    spinwait_delay_ms( 75 );
+
+    led_one_on();
+    led_two_on();
+    spinwait_delay_ms( 200 );
+    led_one_off();
+    led_two_off();
+
+    spinwait_delay_ms( 250 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void action_cone_up_down()
+{
+    // 20 = full up, 39 = full down
+
+    servo_set_position( 30 );
+    spinwait_delay_ms( 250 );
+
+    servo_set_position( 39 );
+    spinwait_delay_ms( 125 );
+
+    servo_set_position( 30 );
+    spinwait_delay_ms( 250 );
+
+    servo_set_position( 39 );
+    spinwait_delay_ms( 500 );
+    servo_set_off();
+}
+
+// ----------------------------------------------------------------------------------------
+
+static void do_action( uint8_t action )
+{
+    switch ( action )
+    {
+        case ACTION_MOVE_FWD:
+            action_move_fwd();
+            break;
+
+        case ACTION_MOVE_REV:
+            action_move_rev();
+            break;
+
+        case ACTION_TURN_R:
+            action_turn_right();
+            break;
+
+        case ACTION_TURN_L:
+            action_turn_left();
+            break;
+
+        case ACTION_BLINK:
+            action_blink();
+            break;
+
+        case ACTION_UP_DOWN:
+            action_cone_up_down();
+            break;
+    }
+}
+
+// ----------------------------------------------------------------------------------------
+
+int main()
+{
+    //--    Initialize the hardware and send a "hello we started" message.
+
+    hw_init();
+    printf_P( PSTR("hello world\n") );
+
+    //--    Retract the cone position, give the servo time to react.
+
+    servo_set_position( 39 );
+    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") );
+
+        uint8_t action = choose_next_action();
+        do_action( action );
+
+        spinwait_delay_ms( 5000 );
+    }
+
+    for ( ;; )
+    {
+        action_blink();
+        spinwait_delay_ms( 5000 );
+    }
+#endif
+
+    for ( ;; )
+    {
+        action_blink();
+
+        spinwait_delay_ms( 500 );
+
+//        action_cone_up_down();
+
+//        spinwait_delay_ms( 500 );
+
+        motor_m1_fwd( 120 );
+        motor_m2_fwd( 120 );
+        spinwait_delay_ms( 2000 );
+
+//        motor_set_off();
+//        spinwait_delay_ms( 500 );
+
+//        motor_m1_rev( 85 );
+//        spinwait_delay_ms( 2000 );
+//        motor_m1_stop();
+
+//        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;
+}
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cone-bot/project_defs.h	Sun Nov 04 21:18:29 2012 -0800
@@ -0,0 +1,49 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//  
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//  
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#if !defined( PROJECT_DEFS_H )
+#define PROJECT_DEFS_H
+
+// ----------------------------------------------------------------------------------------
+//  packages/avr/common
+
+#define PRJ_CPU_FREQ    20000000L
+
+// ----------------------------------------------------------------------------------------
+//  packages/avr/device/uart
+
+#define PRJ_UART0_USE_POLLED_MODE
+#define PRJ_UART0_BUFFER_SIZE   32
+#define PRJ_UART0_INITIAL_BAUDRATE  19200
+
+// ----------------------------------------------------------------------------------------
+//  packages/avr/redir
+
+#define PRJ_REDIR_VIA_UART0
+#define PRJ_REDIR_OUTPUT_DO_AUTO_CRLF
+
+
+#endif // #if !defined( PROJECT_DEFS_H )
+// ----------------------------------------------------------------------------------------
+