Stepper's pins now have independant config/port
authorArthur Wolf <wolf.arthur@gmail.com>
Wed, 4 Jan 2012 22:15:51 +0000 (23:15 +0100)
committerArthur Wolf <wolf.arthur@gmail.com>
Wed, 4 Jan 2012 22:15:51 +0000 (23:15 +0100)
gcc4mbed/mri/mri.ar
gcc4mbed/mri/mri.o
gcc4mbed/samples/CTest/main.o
gcc4mbed/samples/SDFileSystem/SDFileSystem/SDFileSystem.o
src/libs/Config.h
src/modules/robot/Planner.cpp
src/modules/robot/Stepper.cpp
src/modules/robot/Stepper.h

dissimilarity index 73%
index 40f9108..e2f6ce1 100644 (file)
Binary files a/gcc4mbed/mri/mri.ar and b/gcc4mbed/mri/mri.ar differ
dissimilarity index 75%
index 48bf22f..4d229f5 100644 (file)
Binary files a/gcc4mbed/mri/mri.o and b/gcc4mbed/mri/mri.o differ
dissimilarity index 99%
index 71318e6..78ffe14 100644 (file)
Binary files a/gcc4mbed/samples/CTest/main.o and b/gcc4mbed/samples/CTest/main.o differ
dissimilarity index 95%
index f919cf6..7466339 100644 (file)
Binary files a/gcc4mbed/samples/SDFileSystem/SDFileSystem/SDFileSystem.o and b/gcc4mbed/samples/SDFileSystem/SDFileSystem/SDFileSystem.o differ
index e00d73f..564b95f 100644 (file)
@@ -10,6 +10,7 @@
 #include "mbed.h"
 #include "libs/Kernel.h"
 #include "libs/utils.h"
+#include "libs/Pin.h"
 #include <string>
 using std::string;
 
@@ -36,12 +37,21 @@ class ConfigValue{
             if( this->found == false && this->default_set == true ){
                 return this->default_double;
             }else{
-                double result = atof(remove_non_number(value).c_str());
+                double result = atof(remove_non_number(this->value).c_str());
                 if( result == 0.0 && this->value.find_first_not_of("0.") != string::npos ){
                     error("config setting '%s' with value '%s' is not a valid number, please see http://smoothieware.org/configuring-smoothie\r\n", this->key.c_str(), this->value.c_str() );
                 }
                 return result; 
             }
+           
+        }
+
+        std::string as_string(){
+            if( this->found == false && this->default_set == true ){
+                return this->default_string;
+            }else{
+                return this->value; 
+            }
         }
 
         bool as_bool(){
@@ -54,7 +64,12 @@ class ConfigValue{
                     return false;
                 } 
             }
+        }
 
+        Pin* as_pin(){
+            Pin* pin = new Pin();
+            pin->from_string(this->value);
+            return pin;
         }
 
         ConfigValue* by_default(double value){
@@ -63,6 +78,12 @@ class ConfigValue{
             return this; 
         }
 
+        ConfigValue* by_default(std::string value){
+            this->default_set = true;
+            this->default_string = value;
+            return this;
+        }
+
         bool has_characters( string mask ){
             if( this->value.find_first_of(mask) != string::npos ){ return true; }else{ return false; } 
         }
@@ -77,6 +98,7 @@ class ConfigValue{
         bool found;
         bool default_set;
         double default_double; 
+        string default_string;
 };
 
 
index 6b0f00d..6e06464 100644 (file)
@@ -39,26 +39,18 @@ void Planner::on_config_reload(void* argument){
 // Append a block to the queue, compute it's speed factors
 void Planner::append_block( int target[], double feed_rate, double distance, double deltas[] ){
    
-    // Do not append block with no movement
-    //if( target[ALPHA_STEPPER] == this->position[ALPHA_STEPPER] && target[BETA_STEPPER] == this->position[BETA_STEPPER] && target[GAMMA_STEPPER] == this->position[GAMMA_STEPPER] ){ this->computing = false; return; }
-
-
-
     // Stall here if the queue is ful
-    //this->kernel->serial->printf("aaa\r\n");
     while( this->kernel->player->queue.size() >= this->kernel->player->queue.capacity()-2 ){ 
         wait_us(500); 
     }
-    //this->kernel->serial->printf("bbb\r\n");
 
     Block* block = this->kernel->player->new_block();
     block->planner = this;   
 
     // Direction bits
     block->direction_bits = 0; 
-    char direction_bits[3] = {this->kernel->stepper->alpha_dir_pin, this->kernel->stepper->beta_dir_pin, this->kernel->stepper->gamma_dir_pin}; 
     for( int stepper=ALPHA_STEPPER; stepper<=GAMMA_STEPPER; stepper++){ 
-        if( target[stepper] < position[stepper] ){ block->direction_bits |= (1<<direction_bits[stepper]); } 
+        if( target[stepper] < position[stepper] ){ block->direction_bits |= (1<<stepper); } 
     }
     
     // Number of steps for each stepper
index 90b79ac..75ae783 100644 (file)
@@ -48,39 +48,21 @@ void Stepper::on_module_loaded(){
     NVIC_SetPriority(TIMER1_IRQn, 3); 
     LPC_TIM0->TCR = 1; 
 
-
-    // Step and Dir pins as outputs
-    this->step_gpio_port->FIODIR |= this->step_mask;
-    this->dir_gpio_port->FIODIR  |= this->dir_mask;
-
 }
 
 // Get configuration from the config file
 void Stepper::on_config_reload(void* argument){
-    LPC_GPIO_TypeDef *gpios[5] ={LPC_GPIO0,LPC_GPIO1,LPC_GPIO2,LPC_GPIO3,LPC_GPIO4};
+    
     this->microseconds_per_step_pulse   =  this->kernel->config->value(microseconds_per_step_pulse_ckeckusm  )->by_default(5     )->as_number();
     this->acceleration_ticks_per_second =  this->kernel->config->value(acceleration_ticks_per_second_checksum)->by_default(100   )->as_number();
     this->minimum_steps_per_minute      =  this->kernel->config->value(minimum_steps_per_minute_checksum     )->by_default(1200  )->as_number();
     this->base_stepping_frequency       =  this->kernel->config->value(base_stepping_frequency_checksum      )->by_default(100000)->as_number();
-    this->step_gpio_port      = gpios[(int)this->kernel->config->value(step_gpio_port_checksum               )->by_default(0     )->as_number()];
-    this->dir_gpio_port       = gpios[(int)this->kernel->config->value(dir_gpio_port_checksum                )->by_default(0     )->as_number()];
-    this->alpha_step_pin                =  this->kernel->config->value(alpha_step_pin_checksum               )->by_default(1     )->as_number();
-    this->beta_step_pin                 =  this->kernel->config->value(beta_step_pin_checksum                )->by_default(2     )->as_number();
-    this->gamma_step_pin                =  this->kernel->config->value(gamma_step_pin_checksum               )->by_default(3     )->as_number();
-    this->alpha_dir_pin                 =  this->kernel->config->value(alpha_dir_pin_checksum                )->by_default(4     )->as_number();
-    this->beta_dir_pin                  =  this->kernel->config->value(beta_dir_pin_checksum                 )->by_default(5     )->as_number();
-    this->gamma_dir_pin                 =  this->kernel->config->value(gamma_dir_pin_checksum                )->by_default(6     )->as_number();
-    this->step_mask                     = ( 1 << this->alpha_step_pin ) + ( 1 << this->beta_step_pin ) + ( 1 << this->gamma_step_pin );
-    this->dir_mask                      = ( 1 << this->alpha_dir_pin  ) + ( 1 << this->beta_dir_pin  ) + ( 1 << this->gamma_dir_pin  );
-    this->step_bits[ALPHA_STEPPER ]     = this->alpha_step_pin;
-    this->step_bits[BETA_STEPPER  ]     = this->beta_step_pin;
-    this->step_bits[GAMMA_STEPPER ]     = this->gamma_step_pin;
-    this->step_invert_mask = ( this->kernel->config->value( alpha_step_pin_checksum )->is_inverted() << this->alpha_step_pin ) +
-                             ( this->kernel->config->value( beta_step_pin_checksum  )->is_inverted() << this->beta_step_pin  ) + 
-                             ( this->kernel->config->value( gamma_step_pin_checksum )->is_inverted() << this->gamma_step_pin );
-    this->dir_invert_mask =  ( this->kernel->config->value( alpha_dir_pin_checksum  )->is_inverted() << this->alpha_dir_pin  ) +
-                             ( this->kernel->config->value( beta_dir_pin_checksum   )->is_inverted() << this->beta_dir_pin   ) + 
-                             ( this->kernel->config->value( gamma_dir_pin_checksum  )->is_inverted() << this->gamma_dir_pin  );
+    this->alpha_step_pin                =  this->kernel->config->value(alpha_step_pin_checksum               )->by_default("1.21"     )->as_pin()->as_output();
+    this->beta_step_pin                 =  this->kernel->config->value(beta_step_pin_checksum                )->by_default("1.23"     )->as_pin()->as_output();
+    this->gamma_step_pin                =  this->kernel->config->value(gamma_step_pin_checksum               )->by_default("1.22!"    )->as_pin()->as_output();
+    this->alpha_dir_pin                 =  this->kernel->config->value(alpha_dir_pin_checksum                )->by_default("1.18"     )->as_pin()->as_output();
+    this->beta_dir_pin                  =  this->kernel->config->value(beta_dir_pin_checksum                 )->by_default("1.20"     )->as_pin()->as_output();
+    this->gamma_dir_pin                 =  this->kernel->config->value(gamma_dir_pin_checksum                )->by_default("1.19"     )->as_pin()->as_output();
 
     // Set the Timer interval for Match Register 1, 
     LPC_TIM0->MR1 = (( SystemCoreClock/4 ) / 1000000 ) * this->microseconds_per_step_pulse; 
@@ -135,8 +117,9 @@ extern "C" void TIMER0_IRQHandler (void){
     if((LPC_TIM0->IR >> 1) & 1){
         LPC_TIM0->IR |= 1 << 1;
         // Clear step pins 
-        stepper->step_gpio_port->FIOSET =  stepper->step_invert_mask & stepper->step_mask;
-        stepper->step_gpio_port->FIOCLR = ~stepper->step_invert_mask & stepper->step_mask; 
+        stepper->alpha_step_pin->set(0);
+        stepper->beta_step_pin->set(0); 
+        stepper->gamma_step_pin->set(0);
     }
     if((LPC_TIM0->IR >> 0) & 1){
         LPC_TIM0->IR |= 1 << 0;
@@ -148,17 +131,13 @@ extern "C" void TIMER0_IRQHandler (void){
 // config_step_timer. It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
 inline void Stepper::main_interrupt(){
    
-    // TODO: Explain this magic 
     // Step dir pins first, then step pinse, stepper drivers like to know the direction before the step signal comes in
-    // Clear dir   pins 
-    this->dir_gpio_port->FIOSET  =                          this->dir_invert_mask          & this->dir_mask; 
-    this->dir_gpio_port->FIOCLR  =     ~                    this->dir_invert_mask          & this->dir_mask;
-    // Set   dir   pins
-    this->dir_gpio_port->FIOSET  =   (     this->out_bits ^ this->dir_invert_mask    )     & this->dir_mask;
-    this->dir_gpio_port->FIOCLR  =   ( ~ ( this->out_bits ^ this->dir_invert_mask  ) )     & this->dir_mask; 
-    // Set   step  pins 
-    this->step_gpio_port->FIOSET =   (     this->out_bits ^ this->step_invert_mask   )     & this->step_mask;  
-    this->step_gpio_port->FIOCLR =   ( ~ ( this->out_bits ^ this->step_invert_mask ) )     & this->step_mask;
+    this->alpha_dir_pin->set(  ( this->out_bits >> 0  ) & 1 );
+    this->beta_dir_pin->set(   ( this->out_bits >> 1  ) & 1 );
+    this->gamma_dir_pin->set(  ( this->out_bits >> 2  ) & 1 );
+    this->alpha_step_pin->set( ( this->out_bits >> 3  ) & 1 );
+    this->beta_step_pin->set(  ( this->out_bits >> 4  ) & 1 );
+    this->gamma_step_pin->set( ( this->out_bits >> 5  ) & 1 );
 
     if( this->current_block != NULL ){
         // Set bits for direction and steps 
@@ -168,7 +147,7 @@ inline void Stepper::main_interrupt(){
             if( this->counters[stpr] > this->offsets[stpr] && this->stepped[stpr] < this->current_block->steps[stpr] ){
                 this->counters[stpr] -= this->offsets[stpr] ;
                 this->stepped[stpr]++;
-                this->out_bits |= (1 << this->step_bits[stpr]);
+                this->out_bits |= (1 << (stpr+3) );
             } 
         } 
         // If current block is finished, reset pointer
index 3830e29..e2d6aed 100644 (file)
@@ -62,16 +62,12 @@ class Stepper : public Module {
         int base_stepping_frequency;
         LPC_GPIO_TypeDef* step_gpio_port;
         LPC_GPIO_TypeDef* dir_gpio_port; 
-        unsigned short alpha_step_pin;
-        unsigned short beta_step_pin;
-        unsigned short gamma_step_pin;
-        unsigned short alpha_dir_pin;
-        unsigned short beta_dir_pin;
-        unsigned short gamma_dir_pin;
-        unsigned int step_mask;
-        unsigned int dir_mask;
-        unsigned int step_invert_mask;
-        unsigned int dir_invert_mask;
+        Pin* alpha_step_pin;
+        Pin* beta_step_pin;
+        Pin* gamma_step_pin;
+        Pin* alpha_dir_pin;
+        Pin* beta_dir_pin;
+        Pin* gamma_dir_pin;
         unsigned short step_bits[3];
         int counter_increment;
 };