Forráskód Böngészése

reset theta after stopping or execution

Tuan Nguyen 1 éve
szülő
commit
cfb628cf90
2 módosított fájl, 29 hozzáadás és 1 törlés
  1. 14 1
      arduino_code/arduino_code.ino
  2. 15 0
      theta_rho_app.py

+ 14 - 1
arduino_code/arduino_code.ino

@@ -49,17 +49,30 @@ void setup() {
     homing();
 }
 
+void resetThetaToNearestMultiple() {
+    currentTheta = fmod(currentTheta, 2.0 * M_PI);
+    Serial.print("currentTheta reset to: ");
+    Serial.println(currentTheta);
+}
+
 void loop() {
     // Check for incoming serial commands or theta-rho pairs
     if (Serial.available() > 0) {
         String input = Serial.readStringUntil('\n');
 
         // Ignore invalid messages
-        if (input != "HOME" && !input.endsWith(";")) {
+        if (input != "HOME" && input != "RESET_THETA" && !input.endsWith(";")) {
             Serial.println("IGNORED");
             return;
         }
 
+        if (input == "RESET_THETA") {
+            resetThetaToNearestMultiple();  // Reset currentTheta
+            Serial.println("THETA_RESET"); // Notify Python
+            Serial.println("READY");
+            return;
+        }
+
         if (input == "HOME") {
             homing();
             return;

+ 15 - 0
theta_rho_app.py

@@ -107,6 +107,20 @@ def run_theta_rho_file(file_path):
                 if response == "READY":
                     send_coordinate_batch(ser, batch)
                     break
+    reset_theta()
+                
+def reset_theta():
+    ser.write(b"RESET_THETA\n")
+    while True:
+        if ser.in_waiting > 0:
+            response = ser.readline().decode().strip()
+            print(f"Arduino response: {response}")
+            if response == "THETA_RESET":
+                print("Theta successfully reset.")
+                break
+            else:
+                print("No response or unexpected response:", response)
+        time.sleep(0.5)  # Small delay to avoid busy waiting
 
 @app.route('/')
 def index():
@@ -178,6 +192,7 @@ def run_theta_rho():
 def stop_execution():
     global stop_requested
     stop_requested = True
+    reset_theta()
     return jsonify({'success': True})
 
 @app.route('/send_home', methods=['POST'])