Python Forum
Python script runs on startup but does not register keystrokes.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python script runs on startup but does not register keystrokes.
#1
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.

 

# 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()
Reply
#2
Just for a quick check try setting up wasd for up/down/left/right to determine if the keyboard is communicating at all.
Reply
#3
(Sep-06-2018, 07:34 PM)Vysero Wrote: Just for a quick check try setting up wasd for up/down/left/right to determine if the keyboard is communicating at all.


Ok - I did try this, and the output displayed on the screen as well, instead of being registered as program inputs.
Reply
#4
Ok, I have it fixed now. I kept looking around and added the command line to the ~/.bashrc instead of rc.local. It seems it was stuck in limbo between background and foreground.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help to boot application on computer startup jacksfrustration 3 587 Jan-22-2025, 07:43 AM
Last Post: chaebol2th
  How to re-register .py file extension to new moved Python dir (on Windows)? pstein 5 1,249 Nov-06-2024, 03:06 PM
Last Post: DeaD_EyE
  problem program runs in a loop jasserin 0 780 May-18-2024, 03:07 PM
Last Post: jasserin
  Using Autostart to run a GUI program at startup. Rpi Edward_ 1 1,242 Oct-28-2023, 03:19 PM
Last Post: SpongeB0B
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 7,926 Jun-29-2023, 11:57 AM
Last Post: gologica
  Another program runs bho68 7 2,366 Nov-08-2022, 08:16 PM
Last Post: bho68
  Pymodbus Write value to register stsxbel 10 13,629 Aug-18-2022, 01:42 PM
Last Post: DeaD_EyE
  Simple syntax to asynchronously get access to MODBUS register orion67 1 3,826 Jan-22-2022, 12:40 PM
Last Post: orion67
  how to startup canbus interface on pi korenron 2 3,462 Oct-24-2021, 09:51 PM
Last Post: DeaD_EyE
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 2,068 Oct-08-2021, 03:44 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020