Raspberry pi Pyephem日期比较未触发代码

Raspberry pi Pyephem日期比较未触发代码,raspberry-pi,pyephem,Raspberry Pi,Pyephem,我在凌晨3点从crontab用树莓皮触发了这个脚本。其想法是在一天中拍摄一段timelapse视频,但只在日出和日落之间拍摄。触发时的代码计算日落和日出时间,然后等待日出。然后它应该开始拍照,但由于某种原因,它似乎不会在日出时触发。我想这可能是因为“if home.date==sunrise:”太准确了吧?我怎样才能让它忽略几分之一秒,甚至几秒钟呢 这是完整的代码- import os import time import ephem # find time of sun rise and s

我在凌晨3点从crontab用树莓皮触发了这个脚本。其想法是在一天中拍摄一段timelapse视频,但只在日出和日落之间拍摄。触发时的代码计算日落和日出时间,然后等待日出。然后它应该开始拍照,但由于某种原因,它似乎不会在日出时触发。我想这可能是因为“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()
导入操作系统
导入时间
进口以弗所
#找出日出日落的时间
sun=ephem.sun()
home=ephem.Observer()
home.lat,home.lon='52.8709','-1.797'#你的lat长
sun.compute(主页)
日出,日落=家。下一个日出(太阳)),
家。下一次(日落)
daylightminutes=(日落-日出)*1440#找出白天有多少分钟
daylightminutes=(整轮(daylightminutes))
def dostuff():
如果home.date==日出:
导入日期时间
导入系统
帧数=60#日光分钟-需要在timelapse视频中显示600#个图像
FPS_IN=4#您希望在视频中显示的每秒图像数
FPS_OUT=4#完成视频24中的FPS数是一个很好的值
TIMEBETWEEN=6#图片之间的秒数,60=1分钟
#拍摄延时视频所需的照片
帧数=0
当帧数<帧时:
imageNumber=str(frameCount).zfill(7)
操作系统(“raspistill-rot 270-o/home/pi/image%s.jpg”%(imageNumber))
帧数+=1
时间。睡眠(时间介于-6之间)#拍照大约需要6秒
#在变量datetime中记录当前时间和日期
datetime=time.strftime(“%Y%m%d-%H%m”)
#使用拍摄的图像制作timelapse视频
操作系统(“avconv-r%s-i/home/pi/image%s.jpg-r%s-vcodec libx264-crf 20-g15-vf crop=2592:1458,scale=1280:720/home/pi/timelapse%s.mp4”%(FPS_IN,'%7d',FPS_OUT,datet$
#将timelapse视频发送到dropbox
从子流程导入调用
photofile=“/home/pi/Dropbox Uploader/Dropbox\u Uploader.sh upload/home/pi/timelapse%s.mp4/home/pi/timelapse%s.mp4”%(日期时间,日期时间)
调用([photofile],shell=True)
#删除timelapse视频副本以及它所包含的所有在Rpi上本地保存的图像
操作系统(“rm/home/pi/timelapse%s.mp4”%(日期时间))
操作系统(“rm/home/pi/image*”)
sys.exit()
虽然home.date(a)一旦你创建了一个
观察者,它的
日期将保持不变,直到你手动进入并更改它。因此,你不断检查
sunrise
home.date
将永远不会更改,因此永远不会真正到达sunrise。你可能想现在调用PyEphem
()
routine,这样您对当前时间的想法就可以继续了

(b) 您是正确的,您不太可能恰好在日出的毫秒进行
=
比较,因此您可能希望使用不等式进行比较


(c) 在日出到来之前,您的循环似乎会以100%的速度驱动CPU,每秒检查数千次日出是否已经到来。您可能需要
时间。在等待覆盆子Pi变凉时睡眠(1)

谢谢布兰登,我知道它有类似的音调。我是一个noob,但现在看看我如何称呼(),与不平等性相同,我接受你关于不合理工作量的观点,将进行一场比赛,看看我能想出什么。干杯Steves请记住,大量的
print
语句可以帮助你看到发生了什么,可以帮助你观察你的价值观是在日期和时间上进步还是静止不动king经常添加
print
语句,直到我最终能看到发生了什么!嗨,Brandon,我已经用一些print语句更改了ephem.now()的home.date,现在正在更改。还为while循环添加了第二个延迟。当我打印日出时,它不会显示任何毫秒,ephem.now()也不会显示,所以现在似乎有触发的机会,但是代码每隔一秒跳转一次,所以也许我需要将睡眠时间减少到半秒,然后我应该及时触发。虽然我不需要那种敏锐度,但我不知道如何让它只用一分钟而不是一秒钟触发。我想我可能使用了它-Sunrisp=ephem.date(日出+1*ephem.minute)打印日出def dostuff():如果日出