Sep-06-2018, 01:30 PM
I am using a raspberry pi zero to control a servo, using a wireless keyboard for a remote. I have an Adafruit 16 channel servo control, using Raspbian Jessie.
I modified the (open source) test script to accept keystrokes and change direction on the servo. I was hoping for something that would stop when I released the button but I have settled for a stop on up or down arrow when correct position is reached.
The script works well, and I've added it to /etc/rc.local with a 10-second delay. The zero boots to the terminal, the script starts with the familiar blank screen and flashing cursor. But it does not register keystrokes.
When I start the script manually it works fine, even if I ctrl=c out and start again. When I press up, the last text I typed in terminal (from my previous session) appears on the screen.
It seems that the keyboard is tied to the terminal, but I need it tied to the application.
I have the script below if it helps. As mentioned before, I did modify it from the Adafruit script.
I modified the (open source) test script to accept keystrokes and change direction on the servo. I was hoping for something that would stop when I released the button but I have settled for a stop on up or down arrow when correct position is reached.
The script works well, and I've added it to /etc/rc.local with a 10-second delay. The zero boots to the terminal, the script starts with the familiar blank screen and flashing cursor. But it does not register keystrokes.
When I start the script manually it works fine, even if I ctrl=c out and start again. When I press up, the last text I typed in terminal (from my previous session) appears on the screen.
It seems that the keyboard is tied to the terminal, but I need it tied to the application.
I have the script below if it helps. As mentioned before, I did modify it from the Adafruit script.
# Simple demo of the PCA9685 PWM servo/LED controller library.
# This will move channel 0 from min to max position repeatedly.
# Original Author: Tony DiCola
# License: Public Domain
import time
import Adafruit_PCA9685
import curses
# Uncomment to enable debug output.
#import logging
#logging.basicConfig(level=logging.DEBUG)
# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()
# Configure min and max servo pulse lengths
servo_min = 385 # Min pulse length out of 4096 150
servo_max = 400 # Max pulse length out of 4096 600
servo_off = 394
# Helper function to make setting a servo pulse width simpler.
def set_servo_pulse(channel, pulse):
pulse_length = 20 # 1,000,000 us per second
pulse_length //= 60 # 60 Hz
print('{0}us per period'.format(pulse_length))
pulse_length //= 4096 # 12 bits of resolution
print('{0}us per bit'.format(pulse_length))
pulse *= 1000 #1000
pulse //= pulse_length
pwm.set_pwm(channel, 0, pulse)
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
screen = curses.initscr()
curses.cbreak()
screen.keypad(True)
try:
while True:
char = screen.getch()
if char == ord('q'):
break
elif char == curses.KEY_LEFT:
pwm.set_pwm(0, 0, servo_min)
time.sleep(.01)
elif char == curses.KEY_RIGHT:
pwm.set_pwm(0, 0, servo_max)
time.sleep(0.1)
elif char == curses.KEY_DOWN:
pwm.set_pwm(0, 0, servo_off)
elif char == curses.KEY_UP:
pwm.set_pwm(0, 0, servo_off)
finally:
curses.nocbreak(); screen.keypad(0); curses.echo()
curses.endwin()
