/* find the reference point in the locked-position. we need to to this at least every time the unit is powered on */ void find_reference(uint8_t state, uint16_t max_steps_CW, uint16_t max_steps_CCW) { enable_stepper(1); //enable motors or you won't be able to do anything /* this means, that the gear is already near the reed contact and triggers the endstop switch * we need to turn the gear CW to get a clear reference point later */ if(state==1) { while(max_steps_CW) { max_steps_CW = max_steps_CW - move_stepper(1); } } /* we are now far enough from the endstop switch and can now begin the process * There are 2 conditions to break the loop: Either all steps have been used up (error!) * or we found the reference point! */ while((max_steps_CCW > 0) && (ENDSTOP_PINREG & (1<<ENDSTOP_PIN))) { max_steps_CCW = max_steps_CCW - move_stepper(0); } /* check if there are any steps left. If so, set endstop_reached status. If not, set error status * this is necessary because we did not allow any other functions while referencing. That, in turn, * is necessary to make sure the reference operations is not interrupted or otherwise messed with */ if(max_steps_CCW){ status.locked = 1; status.unlocked = 0; status.error = 0; status.open = 0; position = 0; ms_100_tick = 0; while(ms_100_tick<=5){} /* move to neutral position */ while(position < NEUTRAL) { position = position + move_stepper(1); } }else{ status.error = 1; status.locked = 0; status.unlocked = 0; status.open = 0; } /* disable the motor once we finished */ enable_stepper(0); status.reached_endstop = 0; }
/* move key to a specific position (in steps counted from reference point) */ uint8_t move_to_position(int16_t target) { if(target>position) { position = position + move_stepper(1); } if(target<position) { position = position - move_stepper(0); } if(target==position) { return 1; }else{ return 0; } }
int main (void) { init_octopus(); ioport_set_pin_dir(STEP,IOPORT_DIR_OUTPUT); ioport_set_pin_dir(DIR,IOPORT_DIR_OUTPUT); ioport_set_pin_dir(SLEEP,IOPORT_DIR_OUTPUT); ioport_set_pin_dir(MS1,IOPORT_DIR_OUTPUT); ioport_set_pin_dir(MS2,IOPORT_DIR_OUTPUT); ioport_set_pin_dir(MS3,IOPORT_DIR_OUTPUT); ioport_set_pin_level(STEP,0); ioport_set_pin_level(DIR,0); ioport_set_pin_level(SLEEP,0); ioport_set_pin_level(MS1,0); ioport_set_pin_level(MS2,0); ioport_set_pin_level(MS3,0); initial_animation(); for (;;) { int direction; int step1; int step2; int step3; int step4; int step5; scanf("%1i%4x%4x%4x%4x%4x",&direction,&step1,&step2,&step3,&step4,&step5); move_stepper(direction,step1,step2,step3,step4,step5); if(switch_state(SW1) == 0) { ioport_set_pin_level(SLEEP,1); ioport_toggle_pin_level(DIR); full_step(); for(int i=1;i<1000;i++) { ioport_toggle_pin_level(STEP); delay_ms(1); } ioport_set_pin_level(SLEEP,0); move_stepper(1,300,0,0,0,0); // ioport_set_pin_level(LED1,1); // delay_ms(200); // ioport_set_pin_level(LED2,1); // delay_ms(200); // ioport_set_pin_level(LED3,1); // delay_ms(200); // ioport_set_pin_level(LED4,1); // delay_ms(200); // ioport_set_pin_level(LED5,1); // delay_ms(200); // ioport_set_pin_level(LED6,1); // delay_ms(200); } if(switch_state(SW2) == 0) { move_stepper(1,120,60,10,5,5); } } }