stepper code now only iterates on stepper motors that are actually active, thus savin...
authorArthur Wolf <wolf.arthur@gmail.com>
Tue, 14 Aug 2012 21:34:15 +0000 (23:34 +0200)
committerArthur Wolf <wolf.arthur@gmail.com>
Tue, 14 Aug 2012 21:34:15 +0000 (23:34 +0200)
src/libs/StepTicker.cpp
src/libs/StepTicker.h
src/libs/StepperMotor.cpp
src/libs/StepperMotor.h

index 47f7167..342cabb 100644 (file)
@@ -27,14 +27,15 @@ StepTicker::StepTicker(){
     LPC_TIM0->MR1 = 10000000;
     LPC_TIM0->MCR = 11;              // Match on MR0, reset on MR0, match on MR1
     LPC_TIM0->TCR = 1;               // Enable interrupt
-    
+
     // Default start values 
     this->debug = 0;
     this->has_axes = 0;
     this->set_frequency(0.001);
     this->set_reset_delay(100);
     this->last_duration = 0;
-    
+    this->active_motors[0] = NULL;   
+
     NVIC_EnableIRQ(TIMER0_IRQn);     // Enable interrupt handler
 }
 
@@ -63,14 +64,47 @@ StepperMotor* StepTicker::add_stepper_motor(StepperMotor* stepper_motor){
 }
 
 inline void StepTicker::tick(){ 
-    for(unsigned int i=0; i < this->stepper_motors.size(); i++){ 
-        this->stepper_motors[i]->tick();
+    //for(unsigned int i=0; i < this->stepper_motors.size(); i++){ 
+    //    this->stepper_motors[i]->tick();
+    //}
+
+    uint8_t current_id = 0; 
+    StepperMotor* current = this->active_motors[0];
+    while(current != NULL ){
+        current->tick(); 
+        current_id++;
+        current = this->active_motors[current_id];
     }
 }
 
 inline void StepTicker::reset_tick(){
-    for(unsigned int i=0; i < this->stepper_motors.size(); i++){
-        this->stepper_motors[i]->step_pin->set(0);
+    
+    /*for(unsigned int i=0; i < this->stepper_motors.size(); i++){
+        StepperMotor* motor = this->stepper_motors[i];
+        motor->step_pin->set(0);
+        if( motor->exit_tick ){
+            if( motor->dont_remove_from_active_list_yet ){
+                motor->dont_remove_from_active_list_yet = false;
+            }else{
+                this->remove_motor_from_active_list(motor); 
+            }
+        }
+    }*/
+    uint8_t current_id = 0; 
+    StepperMotor* current = this->active_motors[0];
+    while(current != NULL ){
+        current->step_pin->set(0);
+        if( current->exit_tick ){
+            if( current->dont_remove_from_active_list_yet ){
+                current->dont_remove_from_active_list_yet = false;
+            }else{
+                this->remove_motor_from_active_list(current); 
+                current_id--;
+            }
+        }
+        current_id++;
+        current = this->active_motors[current_id];
     }
 }
 
@@ -92,13 +126,10 @@ extern "C" void TIMER0_IRQHandler (void){
         
         // Do not get out of here before everything is nice and tidy
         LPC_TIM0->MR0 = 2000000;
-   
         LPC_TIM0->IR |= 1 << 0;   // Reset it 
         
         // Step pins 
-        uint32_t aa = LPC_TIM0->TC; 
         global_step_ticker->tick(); 
-        uint32_t bb = LPC_TIM0->TC; 
     
         // Maybe we have spent enough time in this interrupt so that we have to reset the pins ourself
         if( LPC_TIM0->TC > global_step_ticker->delay ){
@@ -107,7 +138,6 @@ extern "C" void TIMER0_IRQHandler (void){
             // Else we have to trigger this a tad later, using MR1
             LPC_TIM0->MR1 = global_step_ticker->delay; 
         } 
-        uint32_t cc = LPC_TIM0->TC; 
 
         // If we went over the duration an interrupt is supposed to last, we have a problem 
         // That can happen tipically when we change blocks, where more than usual computation is done
@@ -160,4 +190,34 @@ extern "C" void TIMER0_IRQHandler (void){
 
 }
 
+// We make a list of steppers that want to be called so that we don't call them for nothing
+void StepTicker::add_motor_to_active_list(StepperMotor* motor){
+    //printf("adding %p with exit: %u \r\n", motor, motor->exit_tick );
+    uint8_t current_id = 0; 
+    StepperMotor* current = this->active_motors[0];
+    while(current != NULL ){
+        if( current == motor ){ 
+            return;  // Motor already in list
+        }
+        current_id++;
+        current = this->active_motors[current_id];
+    }
+    this->active_motors[current_id  ] = motor;
+    this->active_motors[current_id+1] = NULL;
+}
+
+void StepTicker::remove_motor_from_active_list(StepperMotor* motor){
+    //printf("removing %p with exit: %u \r\n", motor, motor->exit_tick );
+    uint8_t current_id = 0;
+    uint8_t offset = 0; 
+    StepperMotor* current = this->active_motors[0];
+    while( current != NULL ){
+        if( current == motor ){ offset++; }
+        this->active_motors[current_id] = this->active_motors[current_id+offset];
+        current_id++;
+        current = this->active_motors[current_id+offset];
+    }
+    this->active_motors[current_id] = NULL;
+}
+
 
index 6127bcd..fdd6e4d 100644 (file)
@@ -25,6 +25,8 @@ class StepTicker{
         StepperMotor* add_stepper_motor(StepperMotor* stepper_motor);
         void set_reset_delay( double seconds );
         void reset_tick();
+        void add_motor_to_active_list(StepperMotor* motor);
+        void remove_motor_from_active_list(StepperMotor* motor);
 
         double frequency;
         vector<StepperMotor*> stepper_motors; 
@@ -33,6 +35,8 @@ class StepTicker{
         uint32_t debug;
         uint32_t last_duration;
         bool has_axes;
+
+        StepperMotor* active_motors[12];
 };
 
 
index 01edb31..529c98a 100644 (file)
@@ -17,6 +17,7 @@ StepperMotor::StepperMotor(){
     this->direction_bit = 0;
     this->step_bit = 0;
     this->update_exit_tick();
+    this->dont_remove_from_active_list_yet = false;
 }
 
 StepperMotor::StepperMotor(Pin* step, Pin* dir, Pin* en) : step_pin(step), dir_pin(dir), en_pin(en) {
@@ -28,6 +29,7 @@ StepperMotor::StepperMotor(Pin* step, Pin* dir, Pin* en) : step_pin(step), dir_p
     this->direction_bit = 0;
     this->step_bit = 0;
     this->update_exit_tick();
+    this->dont_remove_from_active_list_yet = false;
 }
 
 // Called a great many times per second, to step if we have to now
@@ -44,16 +46,12 @@ bool StepperMotor::tick(){
         return false; 
     }
    
-    //printf("tock\r\n");
-
     // increase the ( fixed point ) counter by one tick 11t
     this->fx_counter += (uint64_t)((uint64_t)1<<32);  
 
     // if we are to step now 10t
     if( this->fx_counter >= this->fx_ticks_per_step ){
     
-        //printf("tick!\r\n");
-        
         // move counter back 11t
         this->fx_counter -= this->fx_ticks_per_step;
 
@@ -87,10 +85,20 @@ bool StepperMotor::tick(){
 
 // This is just a way not to check for ( !this->moving || this->paused || this->fx_ticks_per_step == 0 ) at every tick()
 inline void StepperMotor::update_exit_tick(){
-    if( !this->moving || this->paused || this->fx_ticks_per_step == 0 ){ 
-        this->exit_tick = true; 
+    if( !this->moving || this->paused || this->fx_ticks_per_step == 0 ){
+        // We must exit tick() after setting the pins, no bresenham is done 
+        if( this->exit_tick == false ){
+            //printf("set for removal %p \r\n", this);
+            this->exit_tick = true;
+            this->dont_remove_from_active_list_yet = true; 
+        }
     }else{
-        this->exit_tick = false;
+        // We must do the bresenham in tick()
+        if( this->exit_tick == true ){ 
+            this->exit_tick = false;
+            //printf("adding motor %p \r\n", this);
+            this->step_ticker->add_motor_to_active_list(this);
+        }
     }
 }
 
index 6cd311c..17bf4d0 100644 (file)
@@ -49,6 +49,7 @@ class StepperMotor {
         uint64_t fx_ticks_per_step;
         
         bool exit_tick;
+        bool dont_remove_from_active_list_yet;
 };