Skip to content

Commit

Permalink
photo snapping
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-iqt committed Dec 24, 2020
1 parent bf2ae91 commit bc619ab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo 'SUBSYSTEM=="vchiq",MODE="0666"' > /etc/udev/rules.d/99-camera.rules
30 changes: 28 additions & 2 deletions pan-tilt-pi/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
import paho.mqtt.client as mqtt
from json.decoder import JSONDecodeError
import pantilthat
from picamera import PiCamera

camera = PiCamera()

args = None
pan = 0
tilt = 0
actualPan = 0
actualTilt = 0
currentPlane=0

# https://stackoverflow.com/questions/45659723/calculate-the-difference-between-two-compass-headings-python

Expand Down Expand Up @@ -73,29 +77,48 @@ def setTilt(azimuth):
def moveCamera():
global actualPan
global actualTilt
global camera

lockedOn = False
while True:
if actualTilt != tilt:
logging.info("Moving Tilt to: %d Goal: %d"%(actualTilt, tilt))
if actualTilt < tilt:
actualTilt += 1
else:
actualTilt -= 1
if actualTilt == tilt:
lockedOn = True
if actualPan != pan:
logging.info("Moving Pan to: %d Goal: %d"%(actualPan, pan))
if actualPan < pan:
actualPan += 1
else:
actualPan -= 1
pantilthat.tilt(actualTilt * -1 + 20)
if actualPan == pan:
lockedOn = True

if lockedOn:
filename = "capture/{}_{}".format(datetime.now().strftime('%Y-%m-%d-%H-%M-%S'), currentPlane)
camera.capture("{}.jpeg".format(filename))



# Turns out that negative numbers mean to move the right and positive numbers mean move to the left...
# I think this is backwards, I am going to switch it, so here I am going to multiply by -1
pantilthat.pan(actualPan * -1)

# Same thing with the tilt. A negative angle moves the camera head up, a positive value down. Backwards!
# Multiplying by -1 again to make it normal. The camera is also off by a little and pointed up a bit, moving it down 20 degrees seems about right
pantilthat.tilt(actualTilt * -1 + 20)
# Sleep for a bit so we're not hammering the HAT with updates
time.sleep(0.005)

#############################################
## MQTT Callback Function ##
#############################################
def on_message(client, userdata, message):
global currentPlane
command = str(message.payload.decode("utf-8"))
#rint(command)
try:
Expand All @@ -115,13 +138,15 @@ def on_message(client, userdata, message):
#logging.info("Bearing: {} Azimuth: {}".format(update["bearing"],update["azimuth"]))
bearingGood = setPan(update["bearing"])
setTilt(update["azimuth"])

currentPlane = update["icao24"]

def main():
global args
global logging
global pan
global tilt
global camera

parser = argparse.ArgumentParser(description='An MQTT based camera controller')

parser.add_argument('-b', '--bearing', help="What bearing is the font of the PI pointed at (0-360)", default=0)
Expand Down Expand Up @@ -151,6 +176,7 @@ def main():
logging.info("---[ Starting %s ]---------------------------------------------" % sys.argv[0])
pantilthat.pan(pan)
pantilthat.tilt(tilt)
camera.resolution = (1024, 768)
threading.Thread(target = moveCamera, daemon = True).start()
# Sleep for a bit so we're not hammering the HAT with updates
time.sleep(0.005)
Expand Down
3 changes: 2 additions & 1 deletion pan-tilt-pi/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ coloredlogs==14.0
paho-mqtt==1.5.0
python-dateutil==2.8.1
pantilthat
smbus
smbus
picamera

0 comments on commit bc619ab

Please sign in to comment.