1

I'm attempting to use the SG90 servo as a steering mechanism for a RC car I have successfully gotten it to respond with A (left) and D (right) but after a few seconds of not inputting any instructions, it stops responding and locks up at a certain angle.

(video of the issue) https://youtu.be/K5UR1pHKGgA

import RPi.GPIO as GPIO
import pigpio
GPIO.setmode(GPIO.BOARD)
from time import sleep
import sys, tty, termios, time
import time
from pynput import keyboard

GPIO.setwarnings(False)
GPIO.setup(07, GPIO.OUT)
pwm=GPIO.PWM(07, 50)
pwm.start(7.5)

def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch





def steering():
    i = 1
    while i == 1:

        char = getch()
        if(char == "a"):
            pwm.ChangeDutyCycle(12.5)
            print (" Left ")
        if(char == "d"):
            pwm.ChangeDutyCycle(2.5)
            print (" Right ")
        if(char == "x"):
            print(" Program Ended ")
            break

steering()
GPIO.cleanup()
Atzin
  • 127
  • 11
  • On an unrelated note, I would encase the `steering()` function call and the `GPIO.cleanup()` call in a try-except block to be super safe (see https://raspi.tv/2013/rpi-gpio-basics-3-how-to-exit-gpio-programs-cleanly-avoid-warnings-and-protect-your-pi; scroll down to the "Let’s try: a better way" section) – CrepeGoat Dec 14 '18 at 00:59
  • I see you have `pigpio` imported; did you have any luck with that library? Nothing stands out as causing problems in your code but [this question](https://raspberrypi.stackexchange.com/questions/68386/pwm-stop-respond-after-hundreds-of-start-stop) suggests a bug exists in RPi.GPIO, though with different code. – FThompson Dec 14 '18 at 01:11

0 Answers0