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
}
}
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];
}
}
// 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 ){
// 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
}
+// 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;
+}
+
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) {
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
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;
// 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);
+ }
}
}