Pyephem日期比较不触发代码

时间:2014-03-09 08:12:52

标签: raspberry-pi pyephem

我在凌晨3点从覆盆子pi上的crontab触发了这个脚本。这个想法是在一天中拍摄一个间隔拍摄视频,但只能在日出和日落之间拍摄。触发时的代码计算太阳落山和太阳升起时间,然后等待日出。然后它应该开始拍摄照片,但由于某种原因它似乎没有在日出时触发。我想这可能是因为'if home.date == sunrise:'太精确了?我怎样才能让它忽略一秒甚至几秒的分数?

这是完整的代码 -

import os
import time
import ephem

# find time of sun rise and sunset
sun = ephem.Sun()
home = ephem.Observer()
home.lat, home.lon = '52.8709', '-1.797' #your lat long
sun.compute(home)
sunrise, sunset = home.next_rising(sun)),
                   home.next_setting(sun))
daylightminutes = (sunset - sunrise) * 1440 # find howmany minutes of daylight there are
daylightminutes = (round(daylightminutes))
def dostuff() :
        if home.date == sunrise:
                import datetime
                import sys
                FRAMES = 60#daylightminutes -600 # number of images you want in timelapse video
                FPS_IN = 4 # number of images per second you want in video
                FPS_OUT = 4 # number of fps in finished video 24 is a good value
                TIMEBETWEEN = 6 # number of seconds between pictures, 60 = 1 minute
                #take the pictures needed for the time lapse video
                frameCount = 0
                while frameCount < FRAMES:
                    imageNumber = str(frameCount).zfill(7)
                    os.system("raspistill -rot 270 -o /home/pi/image%s.jpg"%(imageNumber))
                    frameCount += 1
                    time.sleep(TIMEBETWEEN - 6) #Takes roughly 6 seconds to take a picture
                #record current time and date in variable datetime
                datetime = time.strftime("%Y%m%d-%H%M")
                # make the timelapse video out of the images taken
                os.system("avconv -r %s -i /home/pi/image%s.jpg -r %s -vcodec libx264 -crf 20 -g 15 -vf crop=2592:1458,scale=1280:720 /home/pi/timelapse%s.mp4" %(FPS_IN,'%7d',FPS_OUT,datet$
                #send the timelapse video to dropbox
                from subprocess import call
                photofile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload /home/pi/timelapse%s.mp4 /home/pi/timelapse%s.mp4" %(datetime,datetime)
                call ([photofile], shell=True)
                #remove the timelapse video copy and all images it is made up of that are held localy on the Rpi
                os.system("rm /home/pi/timelapse%s.mp4"%(datetime))
                os.system("rm /home/pi/image*")
                sys.exit()
while home.date <= sunrise:
        dostuff()

如果您发现可能导致问题的任何其他问题,请与我们联系。

干杯

史蒂夫

1 个答案:

答案 0 :(得分:0)

(a)创建Observer后,其date会一直保持不变,直到您手动到达并更改它为止。因此,home.datesunrise进行检查的now()永远不会改变,因此永远不会真正达到日出。你可能想要调用PyEphem ==例程,这样你对当前时间的想法就可以进展。

(b)你是正确的,你几乎不可能在日出的毫秒时间进行time.sleep(1)比较,所以你可能想要用不等式进行比较。

(c)在日出到来之前,你的循环看起来会驱动CPU 100%每秒检查数千次是否日出已经到来。在等待保持Raspberry Pi冷却时,您可能需要{{1}}。