I have coded a robot in EV3, that it can move forward and turn right when the ultrasonic sensor value is lower than the desired value. The code also checks for any messages that are sent from the computer when a person is detected. My problem is that this code runs once, but I want it to run multiple times. I've seen the issue with my receive() code and fixed it. Now, the code just doesn't run the receive part the second time.
import threading
import socket
import time
from ev3_init import Zeus
from ai_movement import movement_when_person_detected
# ... (other constants and setup)
# ... (socket setup)
received_message = None
stop_initial_movement_event = threading.Event()
# ... (other functions)
def main():
while True:
zeus = Zeus()
try:
print("Starting a new iteration")
# Reset the event for a new iteration
stop_initial_movement_event.clear()
# Create and start the initial movement thread
initial_movement_thread = threading.Thread(target=initial_movement)
initial_movement_thread.start()
# Create and start the receive thread
receive_thread = threading.Thread(target=receive)
receive_thread.start()
# Wait for the initial_movement_thread to complete
initial_movement_thread.join()
# Check if a person was detected
if received_message == "Person detected":
print("Person detected!")
movement_when_person_detected()
# Wait for the receive_thread to complete
receive_thread.join()
finally:
zeus.brake()
if __name__ == "__main__":
main
()
This is a part of my code. Kindly ask for more parts of it if required. Sorry my English is quite bad as I'm not a native speaker.
I tried to edit parts of it but it only made it worse. ChatGPT offered no help on this as it was constantly repeating the same solutions. Thus, I tried making my own changes to where the while loop is and changing up the receive function.