changeset 140:c2a99aad2e9b main

Project updates, should be finished.
author Bob Cook <bob@bobcookdev.com>
date Sun, 20 May 2012 16:25:54 -0700
parents 1a3366477e09
children 880379afa4b3
files main/projects/robocam/jamfile main/projects/robocam/main.cpp main/projects/robocam/slew.cpp main/projects/robocam/slew.h main/projects/robocam/steppers.cpp
diffstat 5 files changed, 318 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/main/projects/robocam/jamfile	Fri Apr 06 12:19:43 2012 -0700
+++ b/main/projects/robocam/jamfile	Sun May 20 16:25:54 2012 -0700
@@ -39,6 +39,7 @@
       data.cpp
       diag.cpp
       errorcondition.cpp
+      slew.cpp
       steppers.cpp
       trigger.cpp
       packages.avr.device.pkg
--- a/main/projects/robocam/main.cpp	Fri Apr 06 12:19:43 2012 -0700
+++ b/main/projects/robocam/main.cpp	Sun May 20 16:25:54 2012 -0700
@@ -39,6 +39,7 @@
 #include "data.h"
 #include "diag.h"
 #include "errorcondition.h"
+#include "slew.h"
 #include "steppers.h"
 #include "project_defs.h"
 
@@ -106,16 +107,17 @@
 
     //--    Indicator LEDs on PORTC.0, PORTC.1, and PORTC.2
 
-    DDRC |= ( 1 << PC0 ) | ( 1 << PC1 ) | ( 1 << PC2 );
+    DDRC  |= ( 1 << PC0 ) | ( 1 << PC1 ) | ( 1 << PC2 );
+    PORTC |= ( 1 << PC0 ) | ( 1 << PC1 ) | ( 1 << PC2 );
 
     //--    Stepper controls on PORTD:
     //
-    //          PA0: x step
-    //          PA1: x direction
-    //          PA2: y step
-    //          PA3: y direction
-    //          PA4: z step
-    //          PA5: z direction
+    //          PA0: z direction
+    //          PA1: z step
+    //          PA2: y direction
+    //          PA3: y step
+    //          PA4: x direction
+    //          PA5: x step
 
     DDRA |= ( 1 << PA0 ) | ( 1 << PA1 ) 
          |  ( 1 << PA2 ) | ( 1 << PA3 ) 
@@ -177,7 +179,7 @@
 
     TIMSK |= ( 1 << OCIE1A );
 
-    //--    Set up Timer2 as a 62.5 kHz interrupt timer.
+    //--    Set up Timer2 as a 125 kHz interrupt timer.
 
     TCCR2 = 0
         | ( 0 << WGM20 )                                    // CTC mode
@@ -186,7 +188,7 @@
         | ( 0 << CS22  ) | ( 1 << CS21 ) | ( 1 << CS20 )    // clk/8
         ;
 
-    OCR2 = 32;
+    OCR2 = 16;
 
     TIMSK |= ( 1 << OCIE2 );
 
@@ -214,13 +216,13 @@
 
         case state_recording_read_x:
             g_temp_x = adc_get_result();
-            adc_start_read( JOYSTICK_ADC_CHANNEL_Y /* y axis */ );
+            adc_start_read( JOYSTICK_ADC_CHANNEL_Y );
             g_state = state_recording_read_y;
             break;
 
         case state_recording_read_y:
             g_temp_y = adc_get_result();
-            adc_start_read( JOYSTICK_ADC_CHANNEL_Z /* z axis */ );
+            adc_start_read( JOYSTICK_ADC_CHANNEL_Z );
             g_state = state_recording_read_z;
             break;
 
@@ -252,9 +254,15 @@
 
         case state_recording_update:
             {
-                uint8_t  stepper_x = joystick_x_convert_raw_value( g_temp_x );
-                uint8_t  stepper_y = joystick_y_convert_raw_value( g_temp_y );
-                uint8_t  stepper_z = joystick_z_convert_raw_value( g_temp_z );
+#if 0
+                uint8_t stepper_x = slew_x( joystick_x_convert_raw_value( g_temp_x ) );
+                uint8_t stepper_y = slew_y( joystick_y_convert_raw_value( g_temp_y ) );
+                uint8_t stepper_z = slew_z( joystick_z_convert_raw_value( g_temp_z ) );
+#else
+                uint8_t stepper_x = joystick_x_convert_raw_value( g_temp_x );
+                uint8_t stepper_y = joystick_y_convert_raw_value( g_temp_y );
+                uint8_t stepper_z = joystick_z_convert_raw_value( g_temp_z );
+#endif
                 // save the new values
                 data_save_values( stepper_x, stepper_y, stepper_z );
                 // update the steppers
@@ -262,7 +270,7 @@
                 stepper_y_set( stepper_y );
                 stepper_z_set( stepper_z );
                 // kick off reading new joystick values
-                adc_start_read( JOYSTICK_ADC_CHANNEL_X /* x axis */ );
+                adc_start_read( JOYSTICK_ADC_CHANNEL_X );
                 g_state = state_recording_read_x;
             }
             break;
@@ -415,21 +423,21 @@
         {
             if ( current_mode == mode_playback )
             {
+                current_mode = mode_record;
                 led_green_off();
-                current_mode = mode_record;
                 led_red_on();
             }
             else if ( current_mode == mode_record )
             {
-                led_red_off();
                 current_mode = mode_test_trigger;
                 led_green_on();
+                led_red_on();
             }
             else
             {
                 current_mode = mode_playback;
-                led_red_on();
                 led_green_on();
+                led_red_off();
             }
 
             display_show_mode( current_mode );
@@ -439,9 +447,11 @@
             if ( current_mode == mode_playback )
             {
                 led_green_pulse();
+                led_red_off();
             }
             else if ( current_mode == mode_record )
             {
+                led_green_off();
                 led_red_pulse();
             }
             else
@@ -488,7 +498,7 @@
 
 static void do_start_recording_machine()
 {
-    adc_start_read( JOYSTICK_ADC_CHANNEL_X /* x axis */ );
+    adc_start_read( JOYSTICK_ADC_CHANNEL_X );
     g_state = state_recording_read_x;
     ADCSRA |= ( 1 << ADIE );
 }
@@ -647,12 +657,20 @@
     led_green_blink();
     printf_P( PSTR("reverse playback mode\n") );
 
+    hd44780_clear_line( 1 );
+    hd44780_write_pgm( PSTR("preparing") );
+
     if ( ! data_prepare_to_reverse() )
     {
+        hd44780_clear_line( 1 );
+        hd44780_write_pgm( PSTR("failed prep") );
         error_fatal( error_reverse_prep_failed );
     }
     printf_P( PSTR("reverse prep complete\n") );
 
+    hd44780_clear_line( 1 );
+    hd44780_write_pgm( PSTR("started") );
+
     led_green_on();
     do_start_reverse_machine();
 
@@ -678,6 +696,9 @@
     stepper_y_off();
     stepper_z_off();
 
+    hd44780_clear_line( 1 );
+    hd44780_write_pgm( PSTR("done...") );
+
     printf_P( PSTR("reverse playback finished\n") );
     led_red_off();
     led_green_off();
@@ -693,7 +714,7 @@
 
     hw_init();
 
-    printf_P( PSTR("\n\nRobotic Camera Platform project (version 1.1.0)\n") );
+    printf_P( PSTR("\n\nRobotic Camera Platform project (version 1.2.0)\n") );
     printf_P( PSTR("firmware written by Bob Cook (bob@bobcookdev.com)\n\n") );
 
     stepper_x_off();
@@ -732,11 +753,22 @@
         }
 
         led_green_blink();
+        spinwait_delay_ms( 2000 );
+
         hd44780_clear_screen();
         hd44780_write_pgm( PSTR("Reverse") );
         
-        spinwait_delay_ms( 5000 );
-        led_red_off();
+        hd44780_set_position( 7, 1 );
+        hd44780_write_pgm( PSTR("(reverse)") );
+
+        while ( ! button_2_down() ) ;
+        while ( button_2_down() ) ;
+
+        hd44780_clear_line( 1 );
+        hd44780_write_pgm( PSTR("running...") );
+
+        spinwait_delay_ms( 250 );
+        led_green_off();
 
         if ( ! do_reverse() )
         {
@@ -754,12 +786,21 @@
         }
 
         led_red_blink();
+        spinwait_delay_ms( 2000 );
 
-        spinwait_delay_ms( 5000 );
+        hd44780_clear_screen();
+        hd44780_write_pgm( PSTR("Reverse") );
+
+        hd44780_set_position( 7, 1 );
+        hd44780_write_pgm( PSTR("(reverse)") );
+
+        while ( ! button_2_down() ) ;
+        while ( button_2_down() ) ;
 
         hd44780_clear_line( 1 );
-        hd44780_write_pgm( PSTR("reverse...") );
-        
+        hd44780_write_pgm( PSTR("running...") );
+
+        spinwait_delay_ms( 250 );
         led_red_off();
 
         if ( ! do_reverse() )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/projects/robocam/slew.cpp	Sun May 20 16:25:54 2012 -0700
@@ -0,0 +1,155 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+// 
+//  Slew rate controller for the robotic camera platform steppers.
+//
+//  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 "slew.h"
+
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+
+#include "packages/avr/device/spinwait.h"
+
+#include "packages/common/util/misc.h"
+
+// always after other includes
+#include "packages/avr/device/workaround34734.h"
+
+// ----------------------------------------------------------------------------------------
+
+inline uint16_t scale_up( uint8_t value )
+{
+    return ( value << 4 );
+}
+
+inline uint8_t scale_down( uint16_t value )
+{
+    return ( value >> 4 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+static volatile uint16_t g_stepper_x_slewed = scale_up( 40 );
+static volatile uint16_t g_stepper_y_slewed = scale_up( 40 );
+static volatile uint16_t g_stepper_z_slewed = scale_up( 40 );
+
+// ----------------------------------------------------------------------------------------
+
+static uint16_t max_slew( uint16_t current, uint16_t target )
+{
+    uint16_t max;
+
+    if ( target > current )
+    {
+        max = target - current;
+    }
+    else
+    {
+        max = current - target;
+    }
+
+    max /= 10;
+    max += 2;
+
+    if ( max > scale_up( 2 ) )
+    {
+        return scale_up( 2 );
+    }
+
+    return max;
+}
+
+// ----------------------------------------------------------------------------------------
+
+uint8_t slew_x( uint8_t input )
+{
+    uint16_t target = scale_up( input );
+    uint16_t max    = max_slew( g_stepper_x_slewed, target );
+
+    if ( ( target + max ) < g_stepper_x_slewed )
+    {
+        g_stepper_x_slewed -= max;
+    }
+    else if ( ( g_stepper_x_slewed + max ) < target )
+    {
+        g_stepper_x_slewed += max;
+    }
+    else
+    {
+        g_stepper_x_slewed = target;
+    }
+
+    return scale_down( g_stepper_x_slewed );
+}
+
+// ----------------------------------------------------------------------------------------
+
+uint8_t slew_y( uint8_t input )
+{
+    uint16_t target = scale_up( input );
+    uint16_t max    = max_slew( g_stepper_y_slewed, target );
+
+    if ( ( target + max ) < g_stepper_y_slewed )
+    {
+        g_stepper_y_slewed -= max;
+    }
+    else if ( ( g_stepper_y_slewed + max ) < target )
+    {
+        g_stepper_y_slewed += max;
+    }
+    else
+    {
+        g_stepper_y_slewed = target;
+    }
+
+    return scale_down( g_stepper_y_slewed );
+}
+
+// ----------------------------------------------------------------------------------------
+
+uint8_t slew_z( uint8_t input )
+{
+    uint16_t target = scale_up( input );
+    uint16_t max    = max_slew( g_stepper_z_slewed, target );
+
+    if ( ( target + max ) < g_stepper_z_slewed )
+    {
+        g_stepper_z_slewed -= max;
+    }
+    else if ( ( g_stepper_z_slewed + max ) < target )
+    {
+        g_stepper_z_slewed += max;
+    }
+    else
+    {
+        g_stepper_z_slewed = target;
+    }
+
+    return scale_down( g_stepper_z_slewed );
+}
+
+// ----------------------------------------------------------------------------------------
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/projects/robocam/slew.h	Sun May 20 16:25:54 2012 -0700
@@ -0,0 +1,39 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+// 
+//  Slew rate controller for the robotic camera platform steppers.
+//
+//  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 <stdint.h>
+
+// ----------------------------------------------------------------------------------------
+
+uint8_t slew_x( uint8_t );
+uint8_t slew_y( uint8_t );
+uint8_t slew_z( uint8_t );
+
+// ----------------------------------------------------------------------------------------
+
--- a/main/projects/robocam/steppers.cpp	Fri Apr 06 12:19:43 2012 -0700
+++ b/main/projects/robocam/steppers.cpp	Sun May 20 16:25:54 2012 -0700
@@ -41,6 +41,15 @@
 
 // ----------------------------------------------------------------------------------------
 
+#define STEPPER_PIN_X_STEP      PA1
+#define STEPPER_PIN_X_DIR       PA0
+#define STEPPER_PIN_Y_STEP      PA3
+#define STEPPER_PIN_Y_DIR       PA2
+#define STEPPER_PIN_Z_STEP      PA5
+#define STEPPER_PIN_Z_DIR       PA4
+
+// ----------------------------------------------------------------------------------------
+
 static volatile uint8_t g_stepper_x_setpoint;
 static volatile uint8_t g_stepper_x_current;
 static volatile uint8_t g_stepper_x_counter;
@@ -55,24 +64,21 @@
 
 #if 0
 static const uint8_t PROGMEM g_stepper_counts[] = {
-      8,        9,       10,       11,       12,     
-     13,       14,       15,       16,       17,     
-     18,       19,       20,       21,       22,     
-     23,       24,       25,       26,       27,     
-     28,       29,       30,       32,       34,     
-     36,       38,       42,       45,       50,     
-     56,  59,  63,  68,  73,  80,  87,  97, 109, 125,
+     10,  13,  16,  19,  22,  25,  28,  31,  34,  37,
+     40,  43,  46,  50,  53,  56,  59,  62,  65,  68,
+     71,  74,  77,  80,  83,  86,  90,  93,  96,  99,
+    102, 105, 108, 111, 114, 117, 120, 123, 126,
+      0, // zero rotation
       0, // zero rotation
-    125, 109,  97,  87,  80,  73,  68,  63,  59,  56,
-     50,       45,       42,       38,       36,
-     34,       32,       30,       29,       28,
-     27,       26,       25,       24,       23,
-     22,       21,       20,       19,       18,
-     17,       16,       15,       14,       13,
-     12,       11,       10,        9,        8,
+      0, // zero rotation
+         126, 123, 120, 117, 114, 111, 108, 105, 102,
+     99,  96,  93,  90,  86,  83,  80,  77,  74,  71,
+     68,  65,  62,  59,  56,  53,  50,  46,  43,  40,
+     37,  34,  31,  28,  25,  22,  19,  16,  13,  10,
 };
 #endif
 
+#if 0
 static const uint8_t PROGMEM g_stepper_counts[] = {
      46,  49,  52,  55,  58,  61,  64,  67,  70,  73,
      76,  79,  82,  85,  88,  91,  94,  97, 100, 103,
@@ -86,6 +92,28 @@
     103, 100,  97,  94,  91,  88,  85,  82,  79,  76,
      73,  70,  67,  64,  61,  58,  55,  52,  49,  46,
 };
+#endif
+
+#if 1
+static const uint8_t PROGMEM g_stepper_counts[] = {
+           8,        9,       10,       11,       12,     
+          13,       14,       15,       16,       17,     
+          18,       19,       20,       21,       22,     
+          23,       24,       25,       26,       27,     
+          28,       29,       30,       32,       34,     
+          36,       38,       42,       45,       50,     
+          56,  59,  63,  68,  73,  80,  87,  97, 109, 125,
+            0, // zero rotation
+         125, 109,  97,  87,  80,  73,  68,  63,  59,  56,
+          50,       45,       42,       38,       36,
+          34,       32,       30,       29,       28,
+          27,       26,       25,       24,       23,
+          22,       21,       20,       19,       18,
+          17,       16,       15,       14,       13,
+          12,       11,       10,        9,        8,
+};
+#endif
+
 
 static const uint8_t g_stepper_counts_ccw_limit = array_sizeof( g_stepper_counts ) / 2;
 static const uint8_t g_stepper_counts_zero = 40;
@@ -108,26 +136,26 @@
 {
     if ( g_stepper_x_current != 0 && --g_stepper_x_counter == 0 )
     {
-        PORTA |= ( 1 << PA0 ); // stepper x pin
+        PORTA |= ( 1 << STEPPER_PIN_X_STEP );
         g_stepper_x_counter = g_stepper_x_current;
     }
 
     if ( g_stepper_y_current != 0 && --g_stepper_y_counter == 0 )
     {
-        PORTA |= ( 1 << PA2 ); // stepper y pin
+        PORTA |= ( 1 << STEPPER_PIN_Y_STEP );
         g_stepper_y_counter = g_stepper_y_current;
     }
 
     if ( g_stepper_z_current != 0 && --g_stepper_z_counter == 0 )
     {
-        PORTA |= ( 1 << PA4 ); // stepper z pin
+        PORTA |= ( 1 << STEPPER_PIN_Z_STEP );
         g_stepper_z_counter = g_stepper_z_current;
     }
 
     spinwait_delay_us( 3 );
-    PORTA &= ~( 1 << PA0 ); // stepper x pin
-    PORTA &= ~( 1 << PA2 ); // stepper y pin
-    PORTA &= ~( 1 << PA4 ); // stepper z pin
+    PORTA &= ~( 1 << STEPPER_PIN_X_STEP );
+    PORTA &= ~( 1 << STEPPER_PIN_Y_STEP );
+    PORTA &= ~( 1 << STEPPER_PIN_Z_STEP );
 }
 
 // ----------------------------------------------------------------------------------------
@@ -144,11 +172,11 @@
 
     if ( g_stepper_x_setpoint >= g_stepper_counts_ccw_limit )
     {
-        PORTA &= ~( 1 << PA1 ); // stepper x dir
+        PORTA &= ~( 1 << STEPPER_PIN_X_DIR );
     }
     else
     {
-        PORTA |= ( 1 << PA1 ); // stepper x dir
+        PORTA |= ( 1 << STEPPER_PIN_X_DIR );
     }
 }
 
@@ -176,7 +204,7 @@
 void stepper_x_off()
 {
     stepper_x_set( g_stepper_counts_zero );
-    PORTA &= ~( 1 << PA1 ); // stepper x dir
+    PORTA &= ~( 1 << STEPPER_PIN_X_DIR );
 }
 
 // ----------------------------------------------------------------------------------------
@@ -193,11 +221,11 @@
 
     if ( g_stepper_y_setpoint >= g_stepper_counts_ccw_limit )
     {
-        PORTA &= ~( 1 << PA3 ); // stepper y dir
+        PORTA &= ~( 1 << STEPPER_PIN_Y_DIR );
     }
     else
     {
-        PORTA |= ( 1 << PA3 ); // stepper y dir
+        PORTA |= ( 1 << STEPPER_PIN_Y_DIR );
     }
 }
 
@@ -225,7 +253,7 @@
 void stepper_y_off()
 {
     stepper_y_set( g_stepper_counts_zero );
-    PORTA &= ~( 1 << PA3 ); // stepper y dir
+    PORTA &= ~( 1 << STEPPER_PIN_Y_DIR );
 }
 
 // ----------------------------------------------------------------------------------------
@@ -242,11 +270,11 @@
 
     if ( g_stepper_z_setpoint >= g_stepper_counts_ccw_limit )
     {
-        PORTA &= ~( 1 << PA5 ); // stepper z dir
+        PORTA &= ~( 1 << STEPPER_PIN_Z_DIR );
     }
     else
     {
-        PORTA |= ( 1 << PA5 ); // stepper z dir
+        PORTA |= ( 1 << STEPPER_PIN_Z_DIR );
     }
 }
 
@@ -274,7 +302,7 @@
 void stepper_z_off()
 {
     stepper_z_set( g_stepper_counts_zero );
-    PORTA &= ~( 1 << PA5 ); // stepper z dir
+    PORTA &= ~( 1 << STEPPER_PIN_Z_DIR );
 }
 
 // ----------------------------------------------------------------------------------------