树莓派机器人控制脚本停止后无法正常重启的问题求助
Hey Daniel, let's dig into why your robot's acting wonky after a restart! It sounds like the GPIO cleanup process isn't playing nicely with your restart logic, and we can fix that step by step.
What's Likely Going Wrong
- Half-Baked GPIO Initialization: When you force-kill
vorne.pywithpkill, even thoughstopp.pyrunsGPIO.cleanup(), your restarted script might not be fully reinitializing all GPIO pins from scratch. This leaves lingering state that messes with the motor control. - Unexpected Loop Interruption: The "run 1 second, stop, then run again when pushed" behavior hints your script's loop is relying on an input pin (like pins 5 or 6) that's in a weird state after restart. A physical jostle fixes it temporarily, which points to a signal issue.
- Rushed Cleanup: If
stopp.pyruns beforevorne.pyfully terminates, it might clash with the script still holding GPIO resources.
Fixes to Try
1. Force Full GPIO Reset in vorne.py on Startup
Make sure your main script starts fresh every time, no matter what happened before. Add explicit cleanup and reinitialization at the top:
import RPi.GPIO as GPIO # Wipe any leftover GPIO state first GPIO.cleanup() # Reinitialize EVERYTHING from scratch GPIO.setmode(GPIO.BCM) # Set up output pins for motors GPIO.setup(23, GPIO.OUT) GPIO.setup(24, GPIO.OUT) GPIO.setup(18, GPIO.OUT) GPIO.setup(13, GPIO.OUT) GPIO.setup(17, GPIO.OUT) GPIO.setup(27, GPIO.OUT) # Set up input pins with pull-up resistors GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Your loop logic here while True: # Motor control code pass
This ensures the script doesn't inherit any wonky GPIO states from previous runs.
2. Let vorne.py Exit Gracefully Instead of Forcing It
Instead of pkill-ing the script cold, add signal handling to let it clean up on its own. Modify vorne.py to catch termination signals:
import signal import sys import RPi.GPIO as GPIO def clean_exit(sig, frame): print("Shutting down robot gracefully...") GPIO.cleanup() sys.exit(0) # Listen for termination signals (like from pkill) signal.signal(signal.SIGTERM, clean_exit) signal.signal(signal.SIGINT, clean_exit) # Also handle Ctrl+C for testing # Rest of your initialization and loop code
Then update your kill.sh to send a graceful termination signal instead of forcing a kill:
sudo pkill -SIGTERM -f vorne.py
This way, you might not even need stopp.py anymore—no more race conditions between killing the script and cleaning up pins.
3. Add a Delay Before Running stopp.py (If You Keep It)
If you still want to use stopp.py as a safety net, make sure vorne.py is fully dead before running it. Update your PHP exec calls or kill.sh to wait a moment:
sudo pkill -SIGTERM -f vorne.py sleep 0.5 # Give the script time to exit sudo python /home/pi/Maxwheel/ScriptePython/geradeaus/stopp.py
This prevents stopp.py from trying to clean up pins while vorne.py is still holding them.
4. Debug the Input Pin State
That "push to restart" behavior makes me think your loop is waiting on pins 5 or 6. Add logging to vorne.py to check their states on restart:
while True: pin5 = GPIO.input(5) pin6 = GPIO.input(6) print(f"Pin 5 state: {pin5}, Pin 6 state: {pin6}") # Your motor code here
Run the script manually and check the output—if the pins are showing unexpected values (like low when they should be high), you might have a loose wire or a pull-up resistor issue.
5. Double-Check Hardware Connections
Sometimes the simplest fix is the best. Give the wires connected to pins 5 and 6 a gentle tug to make sure they're seated properly. A loose connection could cause the input signal to drop out randomly, which matches your "push to fix" symptom.
内容的提问来源于stack exchange,提问作者Daniel M

