I'm new to python and RPis. I'm building my own robot to learn.

I manage to control my little robot and move it around, as well as moving the pan camera with my keyboard.

While I was searching for a way to slow down the servo sg90 ( moves too fast and jerky) I came to use for loops. Which is not pretty at all but technically it slowed the time it took the servo to go to the angle defined in the code.

While experimenting with that, I made the servo to swipe from left to right. It does so relatively slowly.

But if I wanted to stop the movement, or while it was moving from left to right, interrupt that sweeping motion and instead make it "look up" the command seemed to be queued, and would only activate after the sweeping motion was completed.

sweeping slowly :

elif char == ord('n'):
            for i in numpy.arange(2, 14, 0.05):

"look up":

elif char == ord('n'):
            for i in numpy.arange(2, 14, 0.05):

I imported RPi.GPIO as GPIO

I've also been looking into gpiozero but I'm not sure that would give me any solutions to the situation.

I also created a definition to accelerate the robot:

def accel():
    for i in numpy.arange(20, 60, 5):

But when I press the button to call accel(), the robot starts accelerating and no other command can be given until it reaches its maximum speed.

Is there a way to make each new command a priority?

  • your question is not about the Raspberry Pi ... your question is about the Python programming language ... I suggest that you explore ways to exit a for loop in python – jsotola Mar 13 '20 at 0:23
  • 1
    You might find this easier with the gpiozero library. You just set the value from -1 to 1 (or set the angle) rather than set duty cycle in a loop. It's more easily interruptable too: gpiozero.readthedocs.io/en/stable/api_output.html#servo – ben_nuttall Mar 13 '20 at 10:39
  • sorry for the delay, it wouldn't allow me to reply before. As it turns out, the issue is that a for loop just claims all the process until is done, so its just not the way to go. I will try with multiprocessing at some point. I have seen some viable options out there for arduino, but i need to figure out a way to make it work on python. Thank you for replying – Bernardo Belmar Apr 8 '20 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.