Ich habe ein inzwischen gut laufendes Stepper-Motor Projekt (danke nochmals für die Unterstützung @MicroBahner !) mit Moba Tools im Einsatz.
Als kleine Draufgabe zur Motorsteuerung habe ich jetzt auch einen HC-SR04 Ultraschallsensor angeschlossen, der mit sich mit meinem Rig mitbewegt und im Grunde ein Abstandssensor zum Endpunkt/ ein Park-Piepserl sein soll.
Im manuellen Fahrmodus (Einmal klicken startet stepper.rotate, ein zweites Mal klicken stepper.stop) funktioniert ein Notstop durch ein vom Sensor ausgelöstes stepper.stop einwandfrei.
Im "Normalmodus", also einer Fahrt mittels stepper.move, schafft er aber die Unterbrechung nicht. Ich habe das Sensorsystem über millis() realisiert um keine Verzögerungen durch Delay zu erhalten, aber stepper.move lässt sich, einmal gestartet, nicht von seinem Weg abbringen. Gibt es hier eine andere Möglichkeit für einen Notstop?
Ich kopiere zur besseren Übersicht jetzt nur mal die Normal Run, Manual Run und Stepper Funktionen hier rein. Sollte der ganze Code (inkl. ner Menge Touchscreen Code und anderer Funktionen, die nichts mit dem Sensor zu tun haben) notwendig werden, stelle ich ihn gerne noch hoch.
Normal:
/***************************************************************************
* normal run function
****************************************************************************/
void doNormalRun(int speed_set, int ramp_set, long distance_set){
stepper.setMaxSpeed(speed_set);
stepper.setRampLen(ramp_set);
updateStr("Starting Normal Run");
tft.fillRect(0, tft.height()-35, tft.width(), 5, RED); // Create Red Progress Bar Basis
currentPos = stepper.currentPosition();
stepper.move(distance_set*10*motorDirection);
while(stepper.stepsToDo() > 0){
Serial.println(stepper.moving());
tft.fillRect(0, tft.height()-35, tft.width()-(tft.width()/100*stepper.moving()), 5, GREEN); // Progress Bar
}
if(stepper.stepsToDo() == 0)
{
tft.fillRect(0, tft.height()-35, tft.width(), 5, BLACK); // "Remove" Progress Bar
}
if(return_to_home == true){
updateStr("Returning Home");
stepper.moveTo(currentPos);
while(stepper.stepsToDo() > 0){ // Progress Bar for Home Run
Serial.println(stepper.moving());
tft.fillRect(tft.width(), tft.height()-35, -(tft.width()-(tft.width()/100*stepper.moving())), 5, BLUE);
}
if(stepper.currentPosition() == currentPos){
tft.fillRect(0, tft.height()-35, tft.width(), 5, BLACK); // "Remove" Progress Bar
}
}
}
Manual:
//--------------------MANUAL button - RETURN TO HOME BUGGY?
if ((p.y >= 10) && (p.x >= 140) && (p.y <= 100) && (p.x <= 210) ) {
currentPos = stepper.currentPosition();
//Set Basic Speed and Acceleration/Ramp of each Steppers at startup
stepper.setMaxSpeed(speed_set);
stepper.setRampLen(ramp_set);
if(manual_start_or_stop == true){ //start run on first click
//stepper.setZero(0); //set current Position as 0
updateStr("Starting Manual Run");
stepper.rotate(motorDirection);
manual_start_or_stop = !manual_start_or_stop;
}
else{ // stop run on second click
//Serial.println(stepper.currentPosition()); // DEBUG
String traveledMsg = "Manual Run stopped after ";
traveledMsg.concat(stepper.currentPosition());
traveledMsg.concat(" Steps.");
updateStr(traveledMsg);
manual_start_or_stop = !manual_start_or_stop;
if(return_to_home == true){
Serial.print("Current Position: "); Serial.println(stepper.currentPosition());
Serial.print("Start Position: ");Serial.println(currentPos);
updateStr("Returning Home");
stepper.moveTo(currentPos);
if(stepper.currentPosition() == currentPos){ // turn motor of after reaching Home
stepper.stop();
}
} else{stepper.stop();} //motor off
}
}
Sensor:
void setup() {
pingTimer = millis(); // Start Timer
}
void loop() {
// start optional sonar module
if (millis() >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer += pingSpeed; // Set the next ping time.
sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
}
}
/*****************************************************************************
* Optional Sonar Module
****************************************************************************/
void echoCheck() {
if (sonar.check_timer()) { // This is how you check to see if the ping was received.
int myPing = sonar.ping_result / US_ROUNDTRIP_CM;
Serial.print("Ping: ");
Serial.print(myPing); // Ping returned, uS result in ping_result, convert to cm with US_ROUNDTRIP_CM.
Serial.println("cm");
if(myPing<=7){stepper.stop();} // Emergency Brake if closer than 7cm to Target
}
}