I am attempting to implement a servo motor to steer the car.
This allows for more precise and variable turns, since the servo is able to turn to a specific angle instead of just switching directions (left or right in this case).
So far I have got the car to turn right and left when the corresponding arrow keys are pushed. However, when I press the up and down arrows the car doesn't move and the servo makes a slight hum, but doesn't move much.
These are the additions I made to the drive_api.py program, mostly found online.
Declaring the servo at the top of the program:
import argparse
import tornado.ioloop
import tornado.web
from datetime import datetime
import os
from operator import itemgetter
import RPi.GPIO as GPIO
import requests
from time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm=GPIO.PWM(12, 50)
pwm.start(0)
SetAngle function near the bottom of the program that handles servo movement and logic
def SetAngle(angle):
duty = angle / 18 + 2
GPIO.output(12 , True)
pwm.ChangeDutyCycle(duty)
sleep(1)
GPIO.output(12, False)
pwm.ChangeDutyCycle(0)
if __name__ == "__main__":
# Parse CLI args
ap = argparse.ArgumentParser()
ap.add_argument("-s", "--speed_percent", required=True, help="Between 0 and 100")
args = vars(ap.parse_args())
GPIO.setmode(GPIO.BOARD)
motor = Motor(16, 18, 22, 19, 21, 23)
log_entries = []
settings = {'speed':float(args['speed_percent'])}
app = make_app(settings)
app.listen(81)
tornado.ioloop.IOLoop.current().start()
Setting the angles for left, right and stop:
def forward(self, speed):
""" pinForward is the forward Pin, so we change its duty
cycle according to speed. """
self.pwm_backward.ChangeDutyCycle(0)
self.pwm_forward.ChangeDutyCycle(speed)
def forward_left(self, speed):
""" pinForward is the forward Pin, so we change its duty
cycle according to speed. """
self.pwm_backward.ChangeDutyCycle(0)
self.pwm_forward.ChangeDutyCycle(speed)
# self.pwm_right.ChangeDutyCycle(0)
# self.pwm_left.ChangeDutyCycle(100)
SetAngle(120)
def forward_right(self, speed):
""" pinForward is the forward Pin, so we change its duty
cycle according to speed. """
self.pwm_backward.ChangeDutyCycle(0)
self.pwm_forward.ChangeDutyCycle(speed)
# self.pwm_left.ChangeDutyCycle(0)
# self.pwm_right.ChangeDutyCycle(100)
SetAngle(0)
def backward(self, speed):
""" pinBackward is the forward Pin, so we change its duty
cycle according to speed. """
self.pwm_forward.ChangeDutyCycle(0)
self.pwm_backward.ChangeDutyCycle(speed)
def left(self, speed):
""" pinForward is the forward Pin, so we change its duty
cycle according to speed. """
# self.pwm_right.ChangeDutyCycle(0)
# self.pwm_left.ChangeDutyCycle(speed)
def right(self, speed):
""" pinForward is the forward Pin, so we change its duty
cycle according to speed. """
# self.pwm_left.ChangeDutyCycle(0)
# self.pwm_right.ChangeDutyCycle(speed)
def stop(self):
""" Set the duty cycle of both control pins to zero to stop the motor. """
self.pwm_forward.ChangeDutyCycle(0)
self.pwm_backward.ChangeDutyCycle(0)
# self.pwm_left.ChangeDutyCycle(0)
# self.pwm_right.ChangeDutyCycle(0)
SetAngle(50)
If anyone might know why I am getting the issues above, please comment.
Thanks.
I am attempting to implement a servo motor to steer the car.
This allows for more precise and variable turns, since the servo is able to turn to a specific angle instead of just switching directions (left or right in this case).
So far I have got the car to turn right and left when the corresponding arrow keys are pushed. However, when I press the up and down arrows the car doesn't move and the servo makes a slight hum, but doesn't move much.
These are the additions I made to the drive_api.py program, mostly found online.
Declaring the servo at the top of the program:
SetAngle function near the bottom of the program that handles servo movement and logic
Setting the angles for left, right and stop:
If anyone might know why I am getting the issues above, please comment.
Thanks.