Skip to main content
replaced http://abyz.co.uk/ with http://abyz.me.uk/
Source Link

Personally I'd use the Python curses module (import curses) to handle keyboard entry. There are example of using curses within pigpio at http://abyz.co.uk/rpi/pigpio/code/gpio_status_py.ziphttp://abyz.me.uk/rpi/pigpio/code/gpio_status_py.zip and http://abyz.co.uk/rpi/pigpio/code/PCF8591.ziphttp://abyz.me.uk/rpi/pigpio/code/PCF8591.zip.

Personally I'd use the Python curses module (import curses) to handle keyboard entry. There are example of using curses within pigpio at http://abyz.co.uk/rpi/pigpio/code/gpio_status_py.zip and http://abyz.co.uk/rpi/pigpio/code/PCF8591.zip.

Personally I'd use the Python curses module (import curses) to handle keyboard entry. There are example of using curses within pigpio at http://abyz.me.uk/rpi/pigpio/code/gpio_status_py.zip and http://abyz.me.uk/rpi/pigpio/code/PCF8591.zip.

example code added
Source Link
joan
  • 71.7k
  • 5
  • 74
  • 107

Edited to add example code

#!/usr/bin/env python

# servo_key.py
# 2015-04-10
# Public Domain

import time
import curses
import atexit

import pigpio 

SERVO = 4

MIN_PW = 1000
MID_PW = 1500
MAX_PW = 2000

NONE        = 0
LEFT_ARROW  = 1
RIGHT_ARROW = 2
UP_ARROW    = 3
DOWN_ARROW  = 4
HOME        = 5
QUIT        = 6

def getch():
   global in_escape, in_cursor
   c = stdscr.getch()

   key = NONE

   if c == 27:
      in_escape = True
      in_cursor = False
   elif c == 91 and in_escape:
      in_cursor = True
   elif c == 68 and in_cursor:
      key = LEFT_ARROW
      in_escape = False
   elif c == 67 and in_cursor:
      key = RIGHT_ARROW
      in_escape = False
   elif c == 65 and in_cursor:
      key = UP_ARROW
      in_escape = False
   elif c == 66 and in_cursor:
      key = DOWN_ARROW
      in_escape = False
   elif c == 72 and in_cursor:
      key = HOME
      in_escape = False
   elif c == 113 or c == 81:
      key = QUIT
   else:
      in_escape = False
      in_cursor = False

   return key

def cleanup():
   curses.nocbreak()
   curses.echo()
   curses.endwin()
   pi.stop()

pi = pigpio.pi()

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()

atexit.register(cleanup) # Ensure original screen state is restored.

in_escape = False
in_cursor = False

pulsewidth = MID_PW

pi.set_servo_pulsewidth(SERVO, pulsewidth)

while True:

   time.sleep(0.01)

   c = getch()

   if c == QUIT:
      break

   pw = pulsewidth

   if c == HOME:
      pw = MID_PW # Stop.
   elif c == UP_ARROW:
      pw = MAX_PW # Fastest clockwise.
   elif c == DOWN_ARROW:
      pw = MIN_PW # Fastest anti-clockwise
   elif c == LEFT_ARROW:
      pw = pw - 5 # Shorten pulse.
      if pw < MIN_PW:
         pw = MIN_PW
   elif c == RIGHT_ARROW:
      pw = pw + 5 # Lengthen pulse.
      if pw > MAX_PW:
         pw = MAX_PW

   if pw != pulsewidth:
      pulsewidth = pw
      pi.set_servo_pulsewidth(SERVO, pulsewidth)

Edited to add example code

#!/usr/bin/env python

# servo_key.py
# 2015-04-10
# Public Domain

import time
import curses
import atexit

import pigpio 

SERVO = 4

MIN_PW = 1000
MID_PW = 1500
MAX_PW = 2000

NONE        = 0
LEFT_ARROW  = 1
RIGHT_ARROW = 2
UP_ARROW    = 3
DOWN_ARROW  = 4
HOME        = 5
QUIT        = 6

def getch():
   global in_escape, in_cursor
   c = stdscr.getch()

   key = NONE

   if c == 27:
      in_escape = True
      in_cursor = False
   elif c == 91 and in_escape:
      in_cursor = True
   elif c == 68 and in_cursor:
      key = LEFT_ARROW
      in_escape = False
   elif c == 67 and in_cursor:
      key = RIGHT_ARROW
      in_escape = False
   elif c == 65 and in_cursor:
      key = UP_ARROW
      in_escape = False
   elif c == 66 and in_cursor:
      key = DOWN_ARROW
      in_escape = False
   elif c == 72 and in_cursor:
      key = HOME
      in_escape = False
   elif c == 113 or c == 81:
      key = QUIT
   else:
      in_escape = False
      in_cursor = False

   return key

def cleanup():
   curses.nocbreak()
   curses.echo()
   curses.endwin()
   pi.stop()

pi = pigpio.pi()

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()

atexit.register(cleanup) # Ensure original screen state is restored.

in_escape = False
in_cursor = False

pulsewidth = MID_PW

pi.set_servo_pulsewidth(SERVO, pulsewidth)

while True:

   time.sleep(0.01)

   c = getch()

   if c == QUIT:
      break

   pw = pulsewidth

   if c == HOME:
      pw = MID_PW # Stop.
   elif c == UP_ARROW:
      pw = MAX_PW # Fastest clockwise.
   elif c == DOWN_ARROW:
      pw = MIN_PW # Fastest anti-clockwise
   elif c == LEFT_ARROW:
      pw = pw - 5 # Shorten pulse.
      if pw < MIN_PW:
         pw = MIN_PW
   elif c == RIGHT_ARROW:
      pw = pw + 5 # Lengthen pulse.
      if pw > MAX_PW:
         pw = MAX_PW

   if pw != pulsewidth:
      pulsewidth = pw
      pi.set_servo_pulsewidth(SERVO, pulsewidth)
Source Link
joan
  • 71.7k
  • 5
  • 74
  • 107

I'm not sure if anyone will write the code for you. It's too broad a question.

You need to acquire a basic understanding of Python programming first, otherwise you'll be forever asking questions.

Personally I'd use the Python curses module (import curses) to handle keyboard entry. There are example of using curses within pigpio at http://abyz.co.uk/rpi/pigpio/code/gpio_status_py.zip and http://abyz.co.uk/rpi/pigpio/code/PCF8591.zip.

On the servo speed. Generally the speed of continuous rotation servos is controlled by the pulse width. 1500µs is the dead band and means stop (perhaps controlled by a trimming screw in the servo housing). As you increase the pulse width to 2000µs the servo goes faster and faster in one direction. As you decrease the pulse width to 1000µs the servo goes faster and faster in the other direction.

1000µs     1250µs     1500µs     1750µs     2000µs
fastest    fast       stop       fast       fastest
anti-      anti-                 clockwise  clockwise
clockwise  clockwise