This is to do with driving an R/C Servo from a Raspberry Pi. I have this
working with motors driving my car, but this takes two pins at opposing
polarities for change of direction. An R/C servo works differently: Instead of
two wires of opposite polarity for the motor direction there is only one
control wire using pulse width modulation between 1 and 2 milliseconds for full
control.
An HTML program calls the python program to drive the car as here:
- -
# 2018-04-05 Modified from Les Pounder program to handle two motors
# one for steering and one for motion
# steering is Left = 10, right = 11
# changing it trying to add Turn for motor control
# 2018-10-31 changing toggle LED to stop motors
from flask import Flask, render_template
from gpiozero import LED, Motor
from time import sleep
# led = LED(25)
motor = Motor(forward=18, backward=23)
turn = Motor(forward=10, backward=11)
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/motor-stop/')
def on():
motor.stop()
return render_template('index.html')
@app.route('/motor-forwards/')
# 31/1/19 modified to test double push on button to stop. see notes.
def motorforwards():
if motor.value == 0:
motor.forward()
elif motor.value > 0:
motor.stop()
return render_template('index.html')
@app.route('/motor-backwards/')
def motorbackwards():
motor.backward()
return render_template('index.html')
@app.route('/motor-left/')
def motorleft():
turn.forward()
sleep(1)
turn.stop()
return render_template('index.html')
@app.route('/motor-right/')
def motorright():
turn.backward()
sleep(1)
turn.stop()
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
--- --
A python test program that drives the Servos, and works. This quite long
because the guy that wrote it documented every step.
-- -- -
# This gives us control of the Raspberry Pi's pins.
import RPi.GPIO as GPIO
# This is only used for time delays... standard Python stuff.
import time
# Tell i which pin number we'll be using to refer to the GPIO pains.
# We will use the physical pin ordering.
GPIO.setmode(GPIO.BOARD)
# We will tell the Broadcom CPU which pins do what.
# There are many pins and most have up to 5 different functions,
# each with a default. Check the pinout to find non-specialized
# "GPIO" pins. We'll use P!-Pin_11 (using BOARD reference),
# which is the same as GPIO17 (Broadcom / BCM reference).
# We need our pin to use the GPIO digital output function, so we just
# tell it to designate this pin for OUTPUT.
pin_number = 18
GPIO.setup(pin_number, GPIO.OUT)
# Now we can use PWM on pin 18. It's software PWM, so don't expect perfect
# results. Linux is a multitasking OS so other processes could interrupt
# the process which generate the PWM signal at any time.
# Raspberry Pi has a hardware PWm channel, but this Pythong library
# does not yet support it.
frequency_hertz = 50
pwm = GPIO.PWM(pin_number, frequency_hertz)
# How to position a servo? All servos are pretty much the same.
# Send repeated purses of an absolute duration (not a relative duty cycle)
# between 0.40 ms and 2.5 ms in duration. A single pulse will only move it
# a short distance in the desired direction. Repeated pulses will continue
# its movement and then once it arrives at the specified position it will
# insruct the motor to forcefully hold its position.
left_position = 0.40
right_position = 2.5
middle_position = (right_position - left_position) / 2 + left_position
# I'll store a sequence of positions for use in a loop later on.
positionList = [left_position, middle_position, right_position, middle_position]
# total number of milliseconds in a a cycle. Given this, we will then
# know both how long we want to pulse in this cycle and how long tghe
# cycle itself is. That is all we need to calculate a duty cycle as
# a percentage.
ms_per_cycle = 1000 / frequency_hertz
frequency_hertz = 50
pwm = GPIO.PWM(pin_number, frequency_hertz)
# How to position a servo? All servos are pretty much the same.
# Send repeated purses of an absolute duration (not a relative duty cycle)
# between 0.40 ms and 2.5 ms in duration. A single pulse will only move it
# a short distance in the desired direction. Repeated pulses will continue
# its movement and then once it arrives at the specified position it will
# insruct the motor to forcefully hold its position.
left_position = 0.40
right_position = 2.5
middle_position = (right_position - left_position) / 2 + left_position
# I'll store a sequence of positions for use in a loop later on.
positionList = [left_position, middle_position, right_position, middle_position]
# total number of milliseconds in a a cycle. Given this, we will then
# know both how long we want to pulse in this cycle and how long tghe
# cycle itself