Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/masskro0/Website
Browse files Browse the repository at this point in the history
Merge commit
  • Loading branch information
masskro0 committed Jul 23, 2019
2 parents dc965e4 + 35beede commit 11a7506
Show file tree
Hide file tree
Showing 3 changed files with 521 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Arduino/servoFunctions.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//move servo from idle position to destination and back
void move_servo(struct servoMot *serv){
//check if you have to move forward or backward
if (serv->target_pos > serv->idle_pos){
//move to destination
int i = serv->current_pos;

#ifdef DEBUGSERVO
Serial.print("i should be current pos: ");
Serial.println(i);
#endif

while(!(serv->state)){
//check if servo is at its destination
if (serv->current_pos == (serv->target_pos)-1){
serv->state = 1;
}
(serv->connected_servo).write(i);

#ifdef DEBUGSERVO
Serial.print("position i: ");
Serial.println(i);
Serial.print("state: ");
Serial.println(serv->state);
#endif

serv->current_pos = i;
i++;
delay(20);

}
i = serv->current_pos;
//return to idle position
while (serv->state){
if (serv->current_pos == (serv->idle_pos)+1){
serv->state = 0;
}
(serv->connected_servo).write(i);

#ifdef DEBUGSERVO
Serial.print("position i: ");
Serial.println(i);
Serial.print("target_pos_fetcher: ");
Serial.println(serv->target_pos);
#endif

serv->current_pos = i;
i--;
delay(10);
}
} else {
int i = serv->current_pos;
while(!(serv->state)){
if (serv->current_pos == (serv->target_pos)+1){
serv->state = 1;
}
(serv->connected_servo).write(i);
serv->current_pos = i;
i--;
delay(20);

}
i = serv->current_pos;
//return to idle position
while (serv->state){
if (serv->current_pos == (serv->idle_pos)-1){
serv->state = 0;
}
(serv->connected_servo).write(i);
serv->current_pos = i;
i++;
delay(10);
}
}
}

void move_servos_to_idle(struct servoMot *serv1, struct servoMot *serv2){
//move to starting position
(serv1->connected_servo).write(serv1->idle_pos);
serv1->current_pos = serv1->idle_pos;
(serv2->connected_servo).write(serv2->idle_pos);
serv2->current_pos = serv2->idle_pos;
}
65 changes: 65 additions & 0 deletions Arduino/stepperFunctions.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//turns the stepper motor to position pos with delays del after each step
int stepperSteps(struct stepperMot *mot, int pos, int del){
if (mot->current_pos != pos){
#ifdef DEBUGSTEP
Serial.print("test");
#endif
//if delaytime is reached move one step
if (millis() > mot->old_steppertime){
//switch between four steps
switch(mot->stepperstate){
case 0: PORTC |= (1<<mot->pinA0);
PORTC &= ~(1<<mot->pinA1);
PORTC |= (1<<mot->pinA2);
PORTC &= ~(1<<mot->pinA3);
mot->stepperstate++;
break;
case 1: PORTC &= ~(1<<mot->pinA0);
PORTC |= (1<<mot->pinA1);
PORTC |= (1<<mot->pinA2);
PORTC &= ~(1<<mot->pinA3);
mot->stepperstate++;
break;
case 2: PORTC &= ~(1<<mot->pinA0);
PORTC |= (1<<mot->pinA1);
PORTC &= ~(1<<mot->pinA2);
PORTC |= (1<<mot->pinA3);
mot->stepperstate++;
break;
case 3: PORTC |= (1<<mot->pinA0);
PORTC &= ~(1<<mot->pinA1);
PORTC &= ~(1<<mot->pinA2);
PORTC |= (1<<mot->pinA3);
mot->stepperstate = 0;
break;
}
mot->current_pos++;
mot->old_steppertime = millis() + del;
}
//return is working
return 1;
} else {
//return is finished
return 0;
}
}

void calibrate(struct stepperMot *mot){
#ifdef DEBUGSTEP
Serial.println("tries to calibrate");
#endif

mot->is_working = 1;
//move stepper until the interrupt is triggered
while((mot->current_pos) > 0){
//set destination of stepper to infinity to keep turning
mot->is_working = stepperSteps(mot, 65535, STEPPERSPEED);
#ifdef DEBUGSTEP
Serial.print("calibrating, current pos: ");
Serial.println(mot->current_pos);
#endif
}
mot->is_working = 0;//maybe delete, for servo test
write_to_all_clients('r'); //stupid
stepper1.is_calibrated = 1;
}
Loading

0 comments on commit 11a7506

Please sign in to comment.