stepper.c
Go to the documentation of this file.
00001 /***************************************************************************
00002 ***  Fraunhofer IPA 
00003 ***  Robotersysteme 
00004 ***  Projekt: 3D cartography demonstrator
00005 ****************************************************************************
00006 ****************************************************************************
00007 ***  Autor: Julio Sagardoy (goa-js)
00008 ***************************************************************************/
00009 
00017 
00018 #include <avr/io.h>
00019 #include <avr/interrupt.h>
00020 #include <util/delay.h>
00021 
00023 #include "encoder.h"
00024 #include "stepper.h"
00025 #include "utils.h"
00026 #include "usart.h"
00027 
00028 static int16_t curr_step_num = 0;               
00029 volatile char indexSet = 0;                             
00030 static int16_t rem_steps;
00031 static uint8_t latency;
00032 
00033 
00036 void stepper_set_direction( int8_t dir )
00037 {
00038         if(dir >= 1)
00039                 BITSET(PORTL,PL3);      //Rotation direcion CW
00040         else
00041                 BITCLR(PORTL,PL3);      //Rotation direcion to CCW
00042 }
00043 
00046 int8_t stepper_get_direction()
00047 {
00048         //Seen from top:
00049         // 1: CW
00050         // 0: CCW
00051         return BITREAD(PINL,PL3);
00052 }
00053 
00056 int16_t stepper_read()
00057 {
00058         return curr_step_num;
00059 }
00060 
00063 void stepper_set( const int16_t target_pos )
00064 {
00065         for(int16_t i=curr_step_num; i<target_pos; i++)
00066                 stepper_advance();
00067 }
00068 
00069 void stepper_set_rem_steps( const int16_t steps)
00070 {
00071         rem_steps = steps;
00072 }
00073 int16_t stepper_get_rem_steps()
00074 {
00075         return rem_steps;
00076 }
00077 void stepper_set_latency( const uint8_t lat)
00078 {
00079         latency = lat;
00080 }
00081 uint8_t stepper_get_latency()
00082 {
00083         return latency;
00084 }
00085         
00088 void stepper_advance()
00089 {
00090         BITCLR(PORTL,PL0);      
00091         _delay_us(900); // Hold line down to advance one step
00092         BITSET(PORTL,PL0);
00093         
00094         // check direction to update step counter
00095         if( stepper_get_direction() )
00096                 curr_step_num++ ;
00097         else
00098                 curr_step_num-- ;
00099 }
00100 
00103 void stepper_cal()
00104 {
00105         //Rotate pan CCW till index is found
00106         indexSet = 0;
00107         stepper_set_direction(0);
00108 
00109         while( !indexSet )
00110         {
00111                 _delay_ms(15);          // 20 ms interstep is really slow, but securs no extra step after index is passed
00112                 stepper_advance();      // comand single step advance
00113         }
00114         
00115         stepper_set_direction(1);       // change direciton of rotation and some steps (this is to avoid having to manually reposition again when recalibrating to avoid)
00116         for(int8_t i=0; i<4; i++)
00117         {       
00118                 _delay_ms(15);          // 20 ms interstep is really slow, but securs no extra step after index is passed
00119                 stepper_advance();
00120         }
00121         
00122         _delay_ms(100);         // delay to let motor settle
00123         indexSet = 0;           // reset index flag
00124         curr_step_num = 0;      // reset step count
00125         encoder_reset();        // reset encoder counter
00126 
00127         uart_puts("L");         // from now on, the platform is calibrated. Notify host:
00128 }
00129 
00132 ISR(PCINT0_vect)
00133 {
00134         indexSet = 1;
00135 }
00136 
00139 void stepper_init()
00140 {
00141         DDRL = 0x6F;            // set pin direction
00142 
00143         // Set stepper "configuration"
00144         BITSET(PORTL,PL0);              // keep clk high. 
00145         BITSET(PORTL,PL5);              // reset counter, drive down for a clk cycle
00146         BITSET(PORTL,PL1);              // step type.
00147                                                         // | Full step | Low  |
00148                                                         // | Half step | High |
00149                                                         
00150         BITSET(PORTL,PL2);              // chopper controls __ lines:
00151                                                         // | ABCD | High |
00152                                                         // | INH  | Low  |
00153 
00154         BITSET(PORTL,PL4);              // enable pull-up for HOME pin of L297
00155         BITSET(PORTL,PL5);              // reset counter, drive down for a clk cycle
00156         BITSET(PORTL,PL6);              // enable ic
00157 }


cob_3d_mapping_demonstrator
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:46