1

I'm trying to control a robot via a Raspberry Pi, using Python language. If I run the commands from the shell they work fine. If I call them from a *.py script the robot doesn't move. If in the script I put the command print('hello') the word 'hello' is displayed but the motors still do not move. How can I solve the problem? Thank you

enter image description here

from gpiozero import Motor

motor1 = Motor(4, 14)
motor2 = Motor(17, 27)

motor1.forward()
motor2.forward()

UPDATE: I solved the problem with sleep(5) after the command for move motors. Thanks!!!

Leonardo
  • 51
  • 6
  • 1
    What do you mean by "run the commands from the shell"? Show is the shell commands that worked. The most common cause of something like this is having the wrong pin numbers. Have you triple checked that? – Tim Roberts Jul 25 '23 at 17:57
  • If I run the "python" command and then manually run (example: >>> motor1.forward()) every single command works fine. – Leonardo Jul 25 '23 at 18:05
  • You changed the code in the question and removed the `while True:` loop. Why? – Michael Butscher Jul 25 '23 at 18:08
  • 1
    I would guess that your script is simply exiting before the motors have had a chance to move by any noticeable amount. – jasonharper Jul 25 '23 at 18:12
  • 2
    MAYBE the `Motor` class automatically cleans itself up. What happens if you put an `input()` as the last statement, forcing it to wait until you press "Enter"? – Tim Roberts Jul 25 '23 at 18:12
  • I changed the code in the question to make it easier. input() non è necessario perché se eseguo direttamente da python i comandi funzionano. Non funzionano correttamente se li chiamo dallo script *.py – Leonardo Jul 25 '23 at 18:18
  • I don't see any "Python commands executed from the shell" in your posting. Therefore I have no idea what you are actually asking. Also, what's the point in posting a photograph of some electronic gadget here? Are you trying to advertise something? – user1934428 Jul 26 '23 at 08:56

1 Answers1

0

did you check this one, maybe the problem is related to the library https://stackoverflow.com/a/72207592/11594679

alternatively, did you check, if the path of the library is a problem. Do you call the script with the same python version, that you execute as shell? Maybe the gpio library is not installed for the one you call the script with.

spaceKelan
  • 67
  • 7
  • Yes, I always call the library with the same python path. In the link you shared to me you can see an output with errors. In my case the code works perfectly only if executed directly by calling python. Alternatively if I run it from script no errors are released but simply no movement is produced to the motors. – Leonardo Jul 25 '23 at 20:42
  • but does that mean, when you check for the return value (which you could also introduce to the forward function to check its performance) is executed ? – spaceKelan Jul 26 '23 at 08:09
  • 1
    because maybe you need to add a sleep in the script as the forward is only executed as long as the script runs while in the terminal it is executed until you give the next command? – spaceKelan Jul 26 '23 at 09:29