Balloon Collaboration
December 10, 2019, 10:49:01 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Hosting for barntek.net has been extended for another 3 years
 
   Home   Help Search gallery Calendar Login Register  
Pages: [1]
  Print  
Author Topic: PiCAM Software  (Read 1084 times)
Larry
Administrator
Hero Member
*****
Posts: 1167



View Profile
« on: May 26, 2015, 02:12:22 AM »

Looks like I am rewriting the PiCam control software.  I think I am simplifying it and making it easier to modify in the future.  I stripped all the code dealing with cyphering the GPS Sentences.  My gpsParse.py program does that for.  All I have to do is read the Current.dat file to get the current stats from the GPS.  

Right now, this program only records Video or takes a picture, but I do have it stamping the pictures with EXIF tags developed from the GPS info.  

Next, I have to decide how call for pics or video, and also work in servo control.

I have it sequenced out now for the whole flight.  I have safe guards in place in case the GPS fails, it should still get pics and videos based on time.
I still need to add code to use a USB memory stick instead of the SD Card for storage.

Next will be writing an altitude simulator and testing the program out.


Ok, so I have tested the program with the Altitude Simulator, and it ran thru, finally, with no problems.  I still need to test the GPIO for the launch switch, then this program will be ready.

Quote
#!/usr/bin/python

### General Info ######################################################
###  piCAM.py
###  Control for PiCam to take pics and vids during balloon flight
###  ...by Larry Barnes

import time

### Setup GPIO/PWM ####################################################
import RPIO
from RPIO import PWM
servo=PWM.Servo()
RPIO.setup(24, RPIO.IN, pull_up_down=RPIO.PUD_DOWN)

### PiCamera Setup ####################################################
import picamera
camera=picamera.PiCamera()

### Variable Setup ####################################################
PATH='/mnt/usb/Data'
DOWN=2300
OUT=1400
UP=600
prevALT=0
flagLaunch=0
flagALT1=0
flagALT2=0
flagALT3=0
flagPOP=0
flagLand=0
crlf="%s%s" % (chr(13),chr(10))

### Functions #########################################################
def gpio_callback(gpio_id, value):
  global flagLaunch
  if gpio_id==24 and flagLaunch==0:
    getCurrent()
    print "Launch"
    flagLaunch=1
    servo.set_servo(23,DOWN)
    print "Looking DOWN"
    FNAM=PATH+"/Vids/Launch1.h264"
    camera.start_recording(FNAM, inline_headers=False)
    with open('/mnt/usb/Data/Launch.dat','w') as d:
      d.write(GPS_TIME+"\n")
      d.write(GPS_DATE+"\n")
      d.write(GPS_LAT+"\n")
      d.write(GPS_LON+"\n")
      d.write(str(GPS_ALT)+"\n")
      d.write(GPS_SPEED+"\n")
      d.write(GPS_COURSE+"\n")


def getCurrent():
  ### Collect Current GPS Stats #######################################
  global GPS_DATE, GPS_SPEED, GPS_COURSE, GPS_TIME, GPS_LAT, GPS_LON, GPS_ALT

  with open('/mnt/usb/Data/Current.dat','r') as d:
    data=d.readline()
  datum=data.split(',')
  GPS_DATE=datum[0]
  GPS_SPEED=datum[1]
  GPS_COURSE=datum[2]
  GPS_TIME=datum[3]
  GPS_LAT=datum[4]
  GPS_LON=datum[5]
  GPS_ALT=float(datum[6])
  print "Current file Read and Parsed..."


def EXIF(): 
  ### Setup EXIF Tags #################################################
  ### ...Latitude
  LAT=GPS_LAT.split(".")[0]+GPS_LAT.split(".")[1]
  DEG=LAT[:2]
  MIN=LAT[2:]
  camera.exif_tags['GPS.GPSLatitude']=DEG+'/1,'+MIN+'/10000,0/1'
  camera.exif_tags['GPS.GPSLatitudeRef']="N"
  ### ...Longitude
  LON=GPS_LON.split(".")[0]+GPS_LON.split(".")[1]
  DEG=LON[:3]
  MIN=LON[3:]
  camera.exif_tags['GPS.GPSLongitude']=DEG+'/1,'+MIN+'/10000,0/1'
  camera.exif_tags['GPS.GPSLongitudeRef']="W"
  ### ...Altitude
  camera.exif_tags['GPS.GPSAltitudeRef']="0"
  AL=str(GPS_ALT).split(".")
  ALT=AL[0]+AL[1]
  camera.exif_tags['GPS.GPSAltitude']=ALT+'/10'
  ### ...Ground Speed
  camera.exif_tags['GPS.GPSSpeedRef']="N"
  KNT=GPS_SPEED.split(".")[0]
  camera.exif_tags['GPS.GPSSpeed']=KNT+'/1'
  ### ...Track/Course
  camera.exif_tags['GPS.GPSTrackRef']='T'
  CRS=GPS_COURSE.split(".")[0]
  camera.exif_tags['GPS.GPSTrack']=CRS+'/1'
  ### ...Copyright
  camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2015 Barntek Solutions'
  ### ...GPSTimeStamp
  tHr=GPS_TIME[:2]
  tMn=GPS_TIME[2:4]
  tSc=GPS_TIME[-2:]
  camera.exif_tags['GPS.GPSTimeStamp']=tHr+"/1,"+tMn+"/1,"+tSc+"/1"
  ### ...GPSDateStamp
  dYR="20"+GPS_DATE[-2:]
  dMN=GPS_DATE[2:4]
  dDY=GPS_DATE[:2]
  dDate="%s:%s:%s" % (dYR,dMN,dDY)
  camera.exif_tags['GPS.GPSDateStamp']=dDate
  print "EXIF Tags prepped..."

### Main Program ######################################################

### Camera Setup
camera.resolution=(1920,1080)
camera.start_preview()
servo.set_servo(23,DOWN)
time.sleep(2)
servo.set_servo(23,UP)
time.sleep(2)
servo.set_servo(23,OUT)
time.sleep(2)

### SEQ: 1 - Prelaunch
### Take snaps while waiting for launch
print "Seq: 1 - Prelaunch"
while flagLaunch==0:
  getCurrent()
  EXIF()
  FNAM=PATH+"/Pics/img"+GPS_TIME+".jpg"
  print FNAM
  camera.capture(FNAM)
  time.sleep(10)
  print GPS_ALT
  if int(GPS_ALT)>200 and flagLaunch==0: # if launch w/out switch
    flagLaunch=1
    servo.set_servo(23,DOWN)
    print "Looking DOWN"
    FNAM=PATH+"/Vids/Launch2.h264"
    camera.start_recording(FNAM, inline_headers=False)

### SEQ: 2 - Launch Video
### Video running
print "Seq: 2 - Launch Video"
t=int(time.time())
while flagALT1==0:
  time.sleep(10)
  ET=int(time.time())-t
  getCurrent()
  print GPS_ALT
  if int(GPS_ALT)>1500 or ET>360:
    camera.stop_recording()
    flagALT1=1
    with open('/mnt/usb/Data/End_Launch_Vid.dat','w') as d:
      d.write(GPS_TIME+"\n")
      d.write(GPS_DATE+"\n")
      d.write(GPS_LAT+"\n")
      d.write(GPS_LON+"\n")
      d.write(str(GPS_ALT)+"\n")
      d.write(GPS_SPEED+"\n")
      d.write(GPS_COURSE+"\n")

### SEQ: 3 - Horizon Snaps
print "Seq: 3 - Horizon Snaps"
servo.set_servo(23,OUT)
print "Looking OUT..."
t=int(time.time())
while flagALT2==0:
  time.sleep(15)
  ET=int(time.time())-t
  getCurrent()
  RATE=int(GPS_ALT)-prevALT
  prevALT=int(GPS_ALT)
  if (int(GPS_ALT)>26000) or (RATE<0) or (ET>5400):
    flagALT2=1
  EXIF()
  FNAM=PATH+"/Pics/OUT_"+GPS_TIME+".jpg"
  print FNAM
  camera.capture(FNAM)

### SEQ: 4 - Balloon Watch
print "Seq: 4 - Balloon Watch"
t=int(time.time())
servo.set_servo(23,UP)
print "Looking UP"
FNAM=PATH+"/Vids/Vid_POP.h264"
camera.start_recording(FNAM, inline_headers=False)
while flagPOP==0:
  time.sleep(60)
  ET=int(time.time())-t                                                             
  getCurrent()
  RATE=int(GPS_ALT)-prevALT
  prevALT=int(GPS_ALT)
  if (flagPOP==1) or (RATE<0) or (ET>360):
    camera.stop_recording()
    flagPOP=1
    with open('/mnt/usb/Data/End_POP_Vid.dat','w') as d:
      d.write(GPS_TIME+"\n")
      d.write(GPS_DATE+"\n")
      d.write(GPS_LAT+"\n")
      d.write(GPS_LON+"\n")
      d.write(str(GPS_ALT)+"\n")
      d.write(GPS_SPEED+"\n")
      d.write(GPS_COURSE+"\n")


### SEQ: 5 - Descent Snaps
print "Seq: 5 - Descent Snaps"
servo.set_servo(23,DOWN)
print "Looking DOWN"
t=int(time.time())
while flagALT3==0:
  time.sleep(15)
  ET=int(time.time())-t
  getCurrent()
  if (int(GPS_ALT)<2000) or (ET>5700):
    flagALT3=1
  EXIF()
  FNAM=PATH+"/Pics/DOWN_"+GPS_TIME+".jpg"                                             
  print FNAM
  camera.capture(FNAM)

### SEQ: 6 - Landing Video
print "Seq: 6 - Landing Video"
servo.set_servo(23,DOWN)
print "Looking DOWN"
FNAM=PATH+"/Vids/Vid_POP.h264"
camera.start_recording(FNAM, inline_headers=False)
while flagPOP==0:
  time.sleep(10)
  getCurrent()
  if int(gps_ALT)<200 or ET>360:
    flagPOP=1
    with open('/mnt/usb/Data/Est_Land.dat','w') as d:
      d.write(GPS_TIME+"\n")
      d.write(GPS_DATE+"\n")
      d.write(GPS_LAT+"\n")
      d.write(GPS_LON+"\n")
      d.write(str(GPS_ALT)+"\n")
      d.write(GPS_SPEED+"\n")
      d.write(GPS_COURSE+"\n")

### SEQ: 7 - Video Run-Out
print "Seq: 7 - Video Run-Out"
time.sleep(120)
camera.stop_recording()
« Last Edit: June 05, 2015, 02:25:09 AM by Larry » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!