Files
spaceballoon/instruments.py
2015-10-22 00:00:00 -06:00

159 lines
4.1 KiB
Python

__author__ = 'asc'
debug = True
from random import random
import Adafruit_BMP.BMP085 as BMP085
import picamera
import configparser
import time
import logging
logger = logging.getLogger(__name__)
class Barometer_Debug ():
def __init__(self):
pass
@property
def status (self):
return (0, "Barometer functioning properly")
@property
def temperature (self):
if self.status[0]:
return 'error'
if debug:
temp = random()*100
else:
raise Exception ('Not Debug')
return temp
@property
def pressure (self):
if (self.status[0]):
return 'error'
if debug:
press = random()
else:
raise Exception ('Not Debug')
return press
@property
def altitude (self):
if self.status[0]:
return 'error'
if debug:
alt = random()*100000
else:
raise Exception ('Not Debug')
return alt
class Camera_Debug():
def __init__(self):
pass
@property
def status (self):
return (0, "Camera functioning properly")
def capture (self):
#todo capture image
return ({"file":open("image2.jpg", 'rb')})
#camera debug 2 uses actual camera
class Camera_Debug2():
def __init__(self, low_quality_resolution,
low_quality_compression_pct,
high_quality_resolution=None,
high_qualitycompression_pct=85):
logger.debug("Initializing camera")
time.sleep(1)
self.low_quality_resolution = low_quality_resolution
self.low_quality_compression_pct = low_quality_compression_pct
self._cam = picamera.PiCamera()
logger.debug("Camera intialized")
pass
@property
def status (self):
return (0, "Camera functioning properly")
def capture (self, no_low_quality=False, no_high_quality=False):
#todo image adjustments
img_hi = None
img_lo = None
if not no_high_quality:
logger.debug('Taking high quality photo')
self._cam.capture("temp_img_hi.jpg")
img_hi = open("temp_img_hi.jpg", 'rb')
logger.debug('High quality photo taken, file: {}'.format(img_hi))
if not no_low_quality:
logger.debug('Taking low quality photo (Resolution: {}, JPEG Quality: {}%'.format(self.low_quality_resolution, self.low_quality_compression_pct))
self._cam.capture("temp_img_lo.jpg",
resize=self.low_quality_resolution,
quality=self.low_quality_compression_pct)
img_lo = open("temp_img_lo.jpg", 'rb')
logger.debug('Low quality photo taken, file: {}'.format(img_lo))
return ({"hi":img_hi,
"lo":img_lo})
#barometerdebug2 uses actual barometer
class Barometer_Debug2 ():
def __init__(self):
logger.debug("Intializing barometer")
self.bmp = BMP085.BMP085()
logger.debug("Barometer intilized")
pass
@property
def status (self):
return (0, "Barometer functioning properly")
@property
def temperature (self):
if self.status[0]:
return 'error'
temp = self.bmp.read_temperature()
return temp
@property
def pressure (self):
if (self.status[0]):
return 'error'
# press = 100000*random()
press = self.bmp.read_pressure()
return press
@property
def altitude (self):
if self.status[0]:
return 'error'
#TODO Set the altitude of your current location in meter
alt = self.bmp.read_altitude()
return alt
def read(self):
logger.debug("Reading from barometer")
#refresh each instrument
alt = self.altitude
press = self.pressure
temp = self.temperature
result = {"altitude":alt,
"temperature":temp,
"pressure":press,
}
logger.debug("Barometer reads {}".format(result))
return result