123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- unsigned int motor_counter = 0;
- unsigned char motor_moved = 0;
- int sgThreshold = 4;
- int sgFilter = 0;
- int direction = 1;
- unsigned int lower_SG_threshold = 0;
- unsigned int upper_SG_threshold = 0;
- unsigned char number_of_SG_readings=0;
- unsigned char current_increment_step_size=0;
- unsigned char lower_current_limit=0;
- char chopperMode = 0;
- char t_off = 2;
- char t_blank = 24;
- char h_start = 8;
- char h_end = 6;
- char h_decrement = 0;
- void startMotor() {
- Serial.println(F("Configuring stepper driver"));
-
- tmc26XStepper.setSpreadCycleChopper(t_off,t_blank,h_start,h_end,h_decrement);
- tmc26XStepper.setRandomOffTime(0);
- tmc26XStepper.setMicrosteps(32);
- tmc26XStepper.setStallGuardThreshold(sgThreshold,sgFilter);
-
- digitalWrite(ENABLE_PIN,LOW);
- tmc26XStepper.start();
- tmc26XStepper.setSpeed(10);
- TCNT2=setupTimer2(10000);
- sei();
- }
- void runMotor() {
- if (running && !tmc26XStepper.isMoving()) {
- tmc26XStepper.step(direction*10000);
- Serial.println("run");
- }
- if (!running & tmc26XStepper.isMoving()) {
- tmc26XStepper.stop();
- Serial.println("stop");
- }
- }
- void setSpeed(unsigned int targetSpeed) {
- if (targetSpeed>0 && targetSpeed<MAX_SPEED) {
- Serial.print(F("Setting speed: "));
- Serial.println(targetSpeed);
- tmc26XStepper.setSpeed(targetSpeed);
- }
- else {
- Serial.print(F("improper speed "));
- Serial.println(targetSpeed);
- }
- }
- void setMicrostepping(int microstepping) {
- if (microstepping<1 || microstepping>256) {
- Serial.print(F("Improperd microstepping setting [1...256]: "));
- Serial.print(microstepping);
- }
- else {
- tmc26XStepper.setMicrosteps(microstepping);
- }
- }
- void setStallGuardThreshold(int threshold) {
- if (threshold<-64 || threshold > 63) {
- Serial.print(F("Improper Stall Guard Threshold [-64...63]: "));
- Serial.println(threshold);
- }
- else {
- sgThreshold = threshold;
- tmc26XStepper.setStallGuardThreshold(threshold,sgFilter);
- }
- }
- void setStallGuardFilter(int filter) {
- if (filter) {
- sgFilter=1;
- }
- else {
- sgFilter=0;
- }
- tmc26XStepper.setStallGuardThreshold(sgThreshold,sgFilter);
- }
- void setCurrent(int current) {
- if (current>0 && current <1700) {
- tmc26XStepper.setCurrent(current);
- }
- else {
- Serial.print(F("Improper current {0 ... 1200}: "));
- Serial.print(current);
- }
- }
- void updateChopper() {
-
- if (chopperMode==0) {
- tmc26XStepper.setSpreadCycleChopper(t_off,t_blank,h_start,h_end,h_decrement);
- }
- }
- void updateCoolStep() {
- tmc26XStepper.setCoolStepConfiguration(
- lower_SG_threshold, upper_SG_threshold, number_of_SG_readings,
- current_increment_step_size, lower_current_limit);
- }
- unsigned char setupTimer2(float timeoutFrequency){
- unsigned char result;
-
- result=(int)((257.0-(TIMER_CLOCK_FREQ/timeoutFrequency))+0.5);
-
-
-
-
-
- TCCR2A = 0;
- TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20;
-
- TIMSK2 = 1<<TOIE2;
-
- TCNT2=result;
- return(result);
- }
- ISR(TIMER2_OVF_vect) {
- motor_moved = tmc26XStepper.move();
- motor_counter++;
- }
|