app.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. from flask import Flask, request, jsonify, render_template
  2. import os
  3. import serial
  4. import time
  5. import threading
  6. import serial.tools.list_ports
  7. import math
  8. app = Flask(__name__)
  9. # Theta-rho directory
  10. THETA_RHO_DIR = './patterns'
  11. IGNORE_PORTS = ['/dev/cu.debug-console', '/dev/cu.Bluetooth-Incoming-Port']
  12. os.makedirs(THETA_RHO_DIR, exist_ok=True)
  13. # Serial connection (default None, will be set by user)
  14. ser = None
  15. ser_port = None # Global variable to store the serial port name
  16. stop_requested = False
  17. def list_serial_ports():
  18. """Return a list of available serial ports."""
  19. ports = serial.tools.list_ports.comports()
  20. return [port.device for port in ports if port.device not in IGNORE_PORTS]
  21. def connect_to_serial(port, baudrate=115200):
  22. """Connect to the specified serial port."""
  23. global ser, ser_port
  24. if ser and ser.is_open:
  25. ser.close()
  26. ser = serial.Serial(port, baudrate)
  27. ser_port = port # Store the connected port globally
  28. time.sleep(2) # Allow time for the connection to establish
  29. def disconnect_serial():
  30. """Disconnect the current serial connection."""
  31. global ser, ser_port
  32. if ser and ser.is_open:
  33. ser.close()
  34. ser = None
  35. ser_port = None # Reset the port name
  36. def restart_serial(port, baudrate=115200):
  37. """Restart the serial connection."""
  38. disconnect_serial()
  39. connect_to_serial(port, baudrate)
  40. def parse_theta_rho_file(file_path):
  41. """Parse a theta-rho file and return a list of (theta, rho) pairs."""
  42. coordinates = []
  43. with open(file_path, 'r') as file:
  44. for line in file:
  45. line = line.strip()
  46. # Skip header or comment lines (starting with '#' or empty lines)
  47. if not line or line.startswith("#"):
  48. print(f"Skipping invalid line: {line}")
  49. continue
  50. # Parse lines with theta and rho separated by spaces
  51. try:
  52. theta, rho = map(float, line.split())
  53. coordinates.append((theta, rho))
  54. except ValueError:
  55. print(f"Skipping invalid line: {line}")
  56. return coordinates
  57. def send_coordinate_batch(ser, coordinates):
  58. """Send a batch of theta-rho pairs to the Arduino."""
  59. # print("Sending batch:", coordinates)
  60. batch_str = ";".join(f"{theta:.3f},{rho:.3f}" for theta, rho in coordinates) + ";\n"
  61. ser.write(batch_str.encode())
  62. def send_command(command):
  63. """Send a single command to the Arduino."""
  64. ser.write(f"{command}\n".encode())
  65. print(f"Sent: {command}")
  66. # Wait for "DONE" acknowledgment from Arduino
  67. while True:
  68. if ser.in_waiting > 0:
  69. response = ser.readline().decode().strip()
  70. print(f"Arduino response: {response}")
  71. if response == "DONE":
  72. print("Command execution completed.")
  73. break
  74. # time.sleep(0.5) # Small delay to avoid busy waiting
  75. def run_theta_rho_file(file_path):
  76. """Run a theta-rho file by sending data in optimized batches."""
  77. global stop_requested
  78. stop_requested = False
  79. coordinates = parse_theta_rho_file(file_path)
  80. if len(coordinates) < 2:
  81. print("Not enough coordinates for interpolation.")
  82. return
  83. # Optimize batch size for smoother execution
  84. batch_size = 1 # Smaller batches may smooth movement further
  85. for i in range(0, len(coordinates), batch_size):
  86. # Check stop_requested flag after sending the batch
  87. if stop_requested:
  88. print("Execution stopped by user after completing the current batch.")
  89. break
  90. batch = coordinates[i:i + batch_size]
  91. if i == 0:
  92. send_coordinate_batch(ser, batch)
  93. continue
  94. # Wait until Arduino is READY before sending the batch
  95. while True:
  96. if ser.in_waiting > 0:
  97. response = ser.readline().decode().strip()
  98. if response == "R":
  99. send_coordinate_batch(ser, batch)
  100. break
  101. else:
  102. print(f"Arduino response: {response}")
  103. # Reset theta after execution or stopping
  104. reset_theta()
  105. ser.write("FINISHED\n".encode())
  106. def reset_theta():
  107. ser.write("RESET_THETA\n".encode())
  108. while True:
  109. if ser.in_waiting > 0:
  110. response = ser.readline().decode().strip()
  111. print(f"Arduino response: {response}")
  112. if response == "THETA_RESET":
  113. print("Theta successfully reset.")
  114. break
  115. time.sleep(0.5) # Small delay to avoid busy waiting
  116. def read_serial_responses():
  117. """Continuously read and print all responses from the Arduino."""
  118. global ser
  119. if ser is None or not ser.is_open:
  120. print("Serial connection not established.")
  121. return
  122. while True:
  123. try:
  124. if ser.in_waiting > 0:
  125. response = ser.readline().decode().strip()
  126. print(f"Arduino response: {response}")
  127. except Exception as e:
  128. print(f"Error reading from serial: {e}")
  129. break
  130. @app.route('/')
  131. def index():
  132. return render_template('index.html')
  133. @app.route('/list_serial_ports', methods=['GET'])
  134. def list_ports():
  135. return jsonify(list_serial_ports())
  136. @app.route('/connect_serial', methods=['POST'])
  137. def connect_serial():
  138. port = request.json.get('port')
  139. if not port:
  140. return jsonify({'error': 'No port provided'}), 400
  141. try:
  142. connect_to_serial(port)
  143. return jsonify({'success': True})
  144. except Exception as e:
  145. return jsonify({'error': str(e)}), 500
  146. @app.route('/disconnect_serial', methods=['POST'])
  147. def disconnect():
  148. try:
  149. disconnect_serial()
  150. return jsonify({'success': True})
  151. except Exception as e:
  152. return jsonify({'error': str(e)}), 500
  153. @app.route('/restart_serial', methods=['POST'])
  154. def restart():
  155. port = request.json.get('port')
  156. if not port:
  157. return jsonify({'error': 'No port provided'}), 400
  158. try:
  159. restart_serial(port)
  160. return jsonify({'success': True})
  161. except Exception as e:
  162. return jsonify({'error': str(e)}), 500
  163. @app.route('/list_theta_rho_files', methods=['GET'])
  164. def list_theta_rho_files():
  165. files = os.listdir(THETA_RHO_DIR)
  166. files = sorted([file for file in files if files not in ['clear_from_in.thr', 'clear_from_out.thr']])
  167. return jsonify(sorted(files))
  168. @app.route('/upload_theta_rho', methods=['POST'])
  169. def upload_theta_rho():
  170. file = request.files['file']
  171. if file:
  172. file.save(os.path.join(THETA_RHO_DIR, file.filename))
  173. return jsonify({'success': True})
  174. return jsonify({'success': False})
  175. @app.route('/run_theta_rho', methods=['POST'])
  176. def run_theta_rho():
  177. file_name = request.json.get('file_name')
  178. pre_execution = request.json.get('pre_execution') # New parameter for pre-execution action
  179. if not file_name:
  180. return jsonify({'error': 'No file name provided'}), 400
  181. file_path = os.path.join(THETA_RHO_DIR, file_name)
  182. if not os.path.exists(file_path):
  183. return jsonify({'error': 'File not found'}), 404
  184. try:
  185. # Handle pre-execution actions
  186. if pre_execution == 'clear_in':
  187. clear_in_thread = threading.Thread(target=run_theta_rho_file, args=('./patterns/clear_from_in.thr',))
  188. clear_in_thread.start()
  189. clear_in_thread.join() # Wait for completion before proceeding
  190. elif pre_execution == 'clear_out':
  191. clear_out_thread = threading.Thread(target=run_theta_rho_file, args=('./patterns/clear_from_out.thr',))
  192. clear_out_thread.start()
  193. clear_out_thread.join() # Wait for completion before proceeding
  194. elif pre_execution == 'none':
  195. pass # No pre-execution action required
  196. # Start the main pattern execution
  197. threading.Thread(target=run_theta_rho_file, args=(file_path,)).start()
  198. return jsonify({'success': True})
  199. except Exception as e:
  200. return jsonify({'error': str(e)}), 500
  201. @app.route('/stop_execution', methods=['POST'])
  202. def stop_execution():
  203. global stop_requested
  204. stop_requested = True
  205. return jsonify({'success': True})
  206. @app.route('/send_home', methods=['POST'])
  207. def send_home():
  208. """Send the HOME command to the Arduino."""
  209. try:
  210. send_command("HOME")
  211. return jsonify({'success': True})
  212. except Exception as e:
  213. return jsonify({'error': str(e)}), 500
  214. @app.route('/run_theta_rho_file/<file_name>', methods=['POST'])
  215. def run_specific_theta_rho_file(file_name):
  216. """Run a specific theta-rho file."""
  217. file_path = os.path.join(THETA_RHO_DIR, file_name)
  218. if not os.path.exists(file_path):
  219. return jsonify({'error': 'File not found'}), 404
  220. threading.Thread(target=run_theta_rho_file, args=(file_path,)).start()
  221. return jsonify({'success': True})
  222. @app.route('/delete_theta_rho_file', methods=['POST'])
  223. def delete_theta_rho_file():
  224. data = request.json
  225. file_name = data.get('file_name')
  226. if not file_name:
  227. return jsonify({"success": False, "error": "No file name provided"}), 400
  228. file_path = os.path.join(THETA_RHO_DIR, file_name)
  229. if not os.path.exists(file_path):
  230. return jsonify({"success": False, "error": "File not found"}), 404
  231. try:
  232. os.remove(file_path)
  233. return jsonify({"success": True})
  234. except Exception as e:
  235. return jsonify({"success": False, "error": str(e)}), 500
  236. @app.route('/move_to_center', methods=['POST'])
  237. def move_to_center():
  238. """Move the sand table to the center position."""
  239. try:
  240. if ser is None or not ser.is_open:
  241. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  242. coordinates = [(0, 0)] # Center position
  243. send_coordinate_batch(ser, coordinates)
  244. return jsonify({"success": True})
  245. except Exception as e:
  246. return jsonify({"success": False, "error": str(e)}), 500
  247. @app.route('/move_to_perimeter', methods=['POST'])
  248. def move_to_perimeter():
  249. """Move the sand table to the perimeter position."""
  250. try:
  251. if ser is None or not ser.is_open:
  252. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  253. MAX_RHO = 1
  254. coordinates = [(0, MAX_RHO)] # Perimeter position
  255. send_coordinate_batch(ser, coordinates)
  256. return jsonify({"success": True})
  257. except Exception as e:
  258. return jsonify({"success": False, "error": str(e)}), 500
  259. @app.route('/preview_thr', methods=['POST'])
  260. def preview_thr():
  261. file_name = request.json.get('file_name')
  262. if not file_name:
  263. return jsonify({'error': 'No file name provided'}), 400
  264. file_path = os.path.join(THETA_RHO_DIR, file_name)
  265. if not os.path.exists(file_path):
  266. return jsonify({'error': 'File not found'}), 404
  267. try:
  268. # Read the .thr file and parse the coordinates
  269. with open(file_path, 'r') as file:
  270. lines = file.readlines()
  271. coordinates = []
  272. for line in lines:
  273. # Ignore comments or blank lines
  274. if line.strip().startswith('#') or not line.strip():
  275. continue
  276. theta, rho = map(float, line.split())
  277. coordinates.append((theta, rho))
  278. return jsonify({'success': True, 'coordinates': coordinates})
  279. except Exception as e:
  280. return jsonify({'error': str(e)}), 500
  281. @app.route('/send_coordinate', methods=['POST'])
  282. def send_coordinate():
  283. """Send a single (theta, rho) coordinate to the Arduino."""
  284. global ser
  285. if ser is None or not ser.is_open:
  286. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  287. try:
  288. data = request.json
  289. theta = data.get('theta')
  290. rho = data.get('rho')
  291. if theta is None or rho is None:
  292. return jsonify({"success": False, "error": "Theta and Rho are required"}), 400
  293. # Send the coordinate to the Arduino
  294. send_coordinate_batch(ser, [(theta, rho)])
  295. reset_theta()
  296. return jsonify({"success": True})
  297. except Exception as e:
  298. return jsonify({"success": False, "error": str(e)}), 500
  299. # Expose files for download if needed
  300. @app.route('/download/<filename>', methods=['GET'])
  301. def download_file(filename):
  302. """Download a file from the theta-rho directory."""
  303. return send_from_directory(THETA_RHO_DIR, filename)
  304. @app.route('/serial_status', methods=['GET'])
  305. def serial_status():
  306. global ser, ser_port
  307. return jsonify({
  308. 'connected': ser.is_open if ser else False,
  309. 'port': ser_port # Include the port name
  310. })
  311. @app.route('/set_speed', methods=['POST'])
  312. def set_speed():
  313. """Set the speed for the Arduino."""
  314. global ser
  315. if ser is None or not ser.is_open:
  316. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  317. try:
  318. # Parse the speed value from the request
  319. data = request.json
  320. speed = data.get('speed')
  321. if speed is None:
  322. return jsonify({"success": False, "error": "Speed is required"}), 400
  323. if not isinstance(speed, (int, float)) or speed <= 0:
  324. return jsonify({"success": False, "error": "Invalid speed value"}), 400
  325. # Send the SET_SPEED command to the Arduino
  326. command = f"SET_SPEED {speed}"
  327. send_command(command)
  328. return jsonify({"success": True, "speed": speed})
  329. except Exception as e:
  330. return jsonify({"success": False, "error": str(e)}), 500
  331. if __name__ == '__main__':
  332. # Start the thread for reading Arduino responses
  333. threading.Thread(target=read_serial_responses, daemon=True).start()
  334. app.run(debug=True, host='0.0.0.0', port=8080)