Waiting for several hours before resuming execution

26 views Asked by At

I'm writing some code to control the altitude and azimuth of a solar panel, moving every hour between 6am and 6pm, then returning to the start position in the evening and resuming again 6am the following day. Go easy - never written anything in any language before.

# AltAztoSteppers.py - date 30:03:24
import astropy.coordinates as coord
from astropy.time import Time
import astropy.units as u
import time
from time import sleep
from gpiozero import OutputDevice, Button

# define pins
Altdir = OutputDevice(18)
Altpulse = OutputDevice(23)
Azdir = OutputDevice(17)
Azpulse = OutputDevice(25)
Altswitch = Button(22)
Azswitch = Button(27)

# set the variables:
Altstart = 0.0
Alttemp = Altstart
Azstart = 90.0
Aztemp = Azstart
loc = coord.EarthLocation(lon=-0.048 * u.deg, lat=51.138 * u.deg)

def Altpos():
    """function to return the Altitude angle of the sun at the current time"""
    altaz = coord.AltAz(location=loc, obstime=now)
    sun = coord.get_sun(now)
    Altnow = sun.transform_to(altaz).alt
    Altnow = Altnow.degree
    return Altnow


def Azpos():
    """function to return the Azimuth angle of the sun at the current time"""
    altaz = coord.AltAz(location=loc, obstime=now)
    sun = coord.get_sun(now)
    Aznow = sun.transform_to(altaz).az
    Aznow = Aznow.degree    # Aznow is a local variable
    return Aznow


while True:
    if Altswitch.value==1 or Azswitch.value==1: # safety feature
        print("Program halted - limit switch triggered")
        break
    now = Time.now()
    hours = int(now.ymdhms[3])
    minutes = int(now.ymdhms[4])
    newtime = hours             # newtime is a counter so program only runs once every hour
    while hours >= 6:           # change back to 6am
        now = Time.now()
        hours = int(now.ymdhms[3])
        minutes = int(now.ymdhms[4])
        
        while hours==newtime:
            
            *#code to calculate the difference in Altitude and Azimuth from one hour to the next* 

            newtime = hours+1   # counter to make sure the program only runs once every hour
            
            if hours>18:        # test for after 6pm, go back to the start position
                
            *#code to send panel back to the start-of-day position*
            
            else:
                if hours > 12 and hours < 19:  # afternoon setting, Altitude decreases
                    Altdir.on()                #set stepper DIR to reverse

            *#code to check am or pm, pulse Altitude stepper in reverse*

        hours = int(now.ymdhms[3])
        minutes = int(now.ymdhms[4])
        print("137 waiting for the next hour. Newtime is ",newtime,"hours = ",hours)
        time.sleep(60) 
    print("waiting for 6am")
    time.sleep(60)
    newtime = hours + 1
else:    
    print("end of file")    

At the moment, during the night hours 6pm to 6am the following day, I'm checking the hour value every minute - time.sleep(60) - and moving the panel back to the limit switches every hour. Is there a more elegant or recommended method of achieving a long delay? The code will be run on a Raspberry Pi doing nothing else other than controlling the panel.

0

There are 0 answers