tuckershannon / PanTilt

Raspberry Pi Pan & Tilt
3 stars 2 forks source link

Printing value of servo after each keystroke #1

Open calleblyh opened 6 years ago

calleblyh commented 6 years ago

Hi, I have successfully adopted pantilt.py in my BirdWatching remote controlled camera. I would like to see on screen the value of servo1 and servo2 after each keystroke. My servos, both pan and tilt, are limited between 60 and 240. What is the syntax ? Nonprogrammer calleblyh Helsinki

calleblyh commented 6 years ago

Hi again, I have slightly modified the code , see attached code. I hope you can see what I want to have. I have been reading on richardghirst forum it's not possible to retreive servo-position data, I wonder why ? I my code my starting position is 150 ( lets call it home position ) so any arrow keystroke will add or subtract new servo value. Unfortunately my programming skills are not good enough...Furthermore I would like when starting the code both servos take the home position. Also hitting HOME key would reset both servo position. How do I implement that code ? "

Raspberry-Pi pan and tilt using arrow keys script

must be run from Pi's terminal!

use code "python pantilt.py" after you cd into the correct folder!

By Tucker Shannon @ tucksprojects.com 11-08-16

importing required libraries

import curses import os import time import picamera

setting up camera

camera = picamera.PiCamera() camera.resolution = (1024, 768) camera.start_preview(fullscreen=False, window = (680,50,640,480))

activating servo blaster (servod must be in the same folder as this script!)

os.system('sudo ./servod')

flipping the camera for so its not upside down

camera.vflip = True camera.hflip = True

get the curses screen window

screen = curses.initscr()

turn off input echoing

curses.noecho()

respond to keys immediately (don't wait for enter)

curses.cbreak()

map arrow keys to special values

screen.keypad(True)

setting start up servo positions

positions range from (50-250)

servo1 = 150 servo2 = 150

print doesn't work with curses, use addstr instead

pic = 1 try: while True: char = screen.getch() if char == ord('q'):

if q is pressed quit

        break
    if char == ord('p'):
        #if p is pressed take a photo!
        camera.capture('image%s.jpg' % pic)
        pic = pic +1
        screen.addstr(0, 0, 'picture taken! ')
    elif char == curses.KEY_RIGHT:
        screen.addstr(2, 0, 'right ')
        if servo1 > 50:
            servo1 = servo1 -.5
        os.system("echo 0=%s > /dev/servoblaster" %servo1)
    screen.addstr(2, 10,("HOR Position is: XXX" )) 
        time.sleep(0.005)
    elif char == curses.KEY_LEFT:
        screen.addstr(3, 0, 'left ')
        if servo1 < 250:
            servo1 = servo1 +.5
        os.system("echo 0=%s > /dev/servoblaster" %servo1) 
        screen.addstr(3, 10,("HOR Position is: XXX" ))
    time.sleep(0.005)
    elif char == curses.KEY_UP:
        screen.addstr(4, 0, 'up ')
        if servo2 > 50:
            servo2 = servo2 -2
        os.system("echo 1=%s > /dev/servoblaster" %servo2) 
        screen.addstr(4, 10,("VER Position is: YYY" ))
    time.sleep(0.005)
    elif char == curses.KEY_DOWN:
        screen.addstr(5, 0, 'down ')
        if servo2 < 250:    
            servo2 = servo2 +2
        os.system("echo 1=%s > /dev/servoblaster" %servo2) 
        screen.addstr(5, 10,("VER Position is: YYY" ))
    time.sleep(0.005)

finally:

shut down cleanly

curses.nocbreak(); screen.keypad(0); curses.echo()
curses.endwin()

"

calleblyh commented 6 years ago

In your code " elif char == curses.KEY_RIGHT: screen.addstr(2, 0, 'right ') if servo1 > 50: servo1 = servo1 -2 os.system("echo 0=%s > /dev/servoblaster" %servo1) " I donot understand %s althou thats the value that drives the servo to next position (I assume). I need to understand this line of code so I would be able to get the servo1 = servo1 +-N retreived on screen. As a non-programmer I cannot do it. Maybe somebody can help ? Appreciate any input !