TheCoderChild
Posts: 5
Joined: Fri Dec 01, 2017 5:25 pm
Contact: Website

Slow Astro Pi Code

Fri Dec 01, 2017 5:32 pm

My code is running very slowly when doing certain calculations. It is tracking the ISS then finding out the nearest timezone then the local time there. It is running so slow it is just about functioning. I have tried testing on several different Raspberry PIs (all pi 3s). The code running on the PI3 was slow so one the 1b it will be much slower. I need to find a way without breaking the rules to speed up the code a lot. Please Help thanks in advance. Here is the Code:

Code: Select all

from sense_hat import SenseHat
import ephem
import time
import datetime
from timezonefinder import TimezoneFinder
import os
from random import randint

#Astro Pi Concept

tf = TimezoneFinder()
sense = SenseHat()

name = ("ISS (ZARYA)")
line1 = ("1 25544U 98067A   17289.51526942  .00003700  00000-0  62982-4 0  9992")
line2 = ("2 25544  51.6418 153.4906 0004809  30.2192  49.5232 15.54187126 80624")

def Logo():
    p=[255,51,152]
    g=[0,255,0]
    b = [0, 0, 0]
    d=[64, 64, 64]

    logo_1= [
            b, b, b, g, g, b, b, b,
            b, b, p, p, p, p, b, b,
            b, p, p, p, p, p, p, b,
            b, p, p, p, p, p, p, b,
            b, p, p, p, p, p, p, b,
            b, b, p, p, p, p, b, b,
            b, b, b, p, p, b, b, b,
            b, b, b, b, b, b, b, b,
            ]
    sense.set_pixels(logo_1)

    time.sleep(0.7)

    logo_2= [
            b, b, b, g, g, b, b, d,
            b, b, p, p, p, p, b, d,
            b, p, p, p, p, p, p, d,
            b, p, p, p, p, p, p, d,
            b, p, p, p, p, p, p, d,
            b, b, p, p, p, p, b, d,
            b, b, b, p, p, b, b, d,
            b, b, b, b, b, b, b, d,
            ]
    sense.set_pixels(logo_2)

    time.sleep(0.7)

    logo_3= [
            b, b, b, g, g, b, d, d,
            b, b, p, p, p, p, d, d,
            b, p, p, p, p, p, d, d,
            b, p, p, p, p, p, d, d,
            b, p, p, p, p, p, d, d,
            b, b, p, p, p, p, d, d,
            b, b, b, p, p, b, d, d,
            b, b, b, b, b, b, d, d,
            ]
    sense.set_pixels(logo_3)

    time.sleep(0.7)
    
    logo_4= [
            b, b, b, g, g, d, d, d,
            b, b, p, p, p, d, d, d,
            b, p, p, p, p, d, d, d,
            b, p, p, p, p, d, d, d,
            b, p, p, p, p, d, d, d,
            b, b, p, p, p, d, d, d,
            b, b, b, p, p, d, d, d,
            b, b, b, b, b, d, d, d,
            ]
    sense.set_pixels(logo_4)

    time.sleep(0.7)
    
    logo_5= [
            b, b, b, g, d, d, d, d,
            b, b, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, b, p, p, d, d, d, d,
            b, b, b, p, d, d, d, d,
            b, b, b, b, d, d, d, d,
            ]
    sense.set_pixels(logo_5)

    logo_6= [
            b, b, b, g, d, d, d, d,
            b, b, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, b, p, p, d, d, d, d,
            b, b, b, p, d, d, d, d,
            b, b, b, b, d, d, d, d,
            ]
    sense.set_pixels(logo_6)

    time.sleep(0.7)

    logo_7= [
            b, b, b, g, d, d, d, d,
            b, b, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, p, p, p, d, d, d, d,
            b, b, p, p, d, d, d, d,
            b, b, b, p, d, d, d, d,
            b, b, b, b, d, d, d, d,
            ]
    sense.set_pixels(logo_7)

    time.sleep(0.7)

    logo_8= [
            b, b, b, d, d, d, d, d,
            b, b, p, d, d, d, d, d,
            b, p, p, d, d, d, d, d,
            b, p, p, d, d, d, d, d,
            b, p, p, d, d, d, d, d,
            b, b, p, d, d, d, d, d,
            b, b, b, d, d, d, d, d,
            b, b, b, d, d, d, d, d,
            ]
    sense.set_pixels(logo_8)

    time.sleep(0.7)

    logo_9= [
            b, b, d, d, d, d, d, d,
            b, b, d, d, d, d, d, d,
            b, p, d, d, d, d, d, d,
            b, p, d, d, d, d, d, d,
            b, p, d, d, d, d, d, d,
            b, b, d, d, d, d, d, d,
            b, b, d, d, d, d, d, d,
            b, b, d, d, d, d, d, d,
            ]
    sense.set_pixels(logo_9)

    time.sleep(0.7)

    logo_10= [
            b, d, d, d, d, d, d, d,
            b, d, d, d, d, d, d, d,
            b, d, d, d, d, d, d, d,
            b, d, d, d, d, d, d, d,
            b, d, d, d, d, d, d, d,
            b, d, d, d, d, d, d, d,
            b, d, d, d, d, d, d, d,
            b, d, d, d, d, d, d, d,
            ]
    sense.set_pixels(logo_10)

    time.sleep(0.7)
    
    logo_11= [
            d, d, d, d, d, d, d, d,
            d, d, d, d, d, d, d, d,
            d, d, d, d, d, d, d, d,
            d, d, d, d, d, d, d, d,
            d, d, d, d, d, d, d, d,
            d, d, d, d, d, d, d, d,
            d, d, d, d, d, d, d, d,
            d, d, d, d, d, d, d, d,
            ]
    sense.set_pixels(logo_11)
    
Logo()
    
def Clock():
    T = [255, 0, 0]
    I = [204, 255, 255]

    if local_time == ("00") or local_time == ("12") :

        clock1 = [

            I, I, I, I, I, I, I, I,
            I, T, T, T, T, T, T, I,
            I, T, I, I, I, I, T, I,
            I, T, I, T, T, I, T, I,
            I, T, I, T, T, I, T, I,
            I, T, I, I, I, I, T, I,
            I, T, T, T, T, T, T, I,
            I, I, I, I, I, I, I, I

            ]

        sense.set_pixels(clock1)
        
            
    if local_time == ("01") or local_time == ("13"):

        clock2 = [

            T, T, T, I, I, I, I, T,
            T, T, T, T, T, T, T, T,
            T, T, T, I, T, T, T, T,
            T, T, I, I, T, T, T, T,
            T, T, T, I, T, T, T, T,
            T, T, T, I, T, T, T, T,
            T, T, I, I, I, T, T, T,
            T, T, T, T, T, T, T, T

            ]

        sense.set_pixels(clock2)

    if local_time == ("02") or local_time == ("14"):

        clock3 = [

            T, T, T, I, I, I, I, I,
            T, T, T, T, T, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, T, T, I, T, T, T,
            T, T, I, I, I, T, T, T,
            T, T, I, T, T, T, T, T,
            T, T, I, I, I, T, T, T,
            T, T, T, T, T, T, T, T

            ]

        sense.set_pixels(clock3)

    if local_time == ("03") or local_time == ("15"):

        clock4 = [

            T, T, T, I, I, I, I, I,
            T, T, T, T, T, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, T, T, I, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, T, T, I, T, T, T,
            T, T, I, I, I, T, T, T,
            T, T, T, T, T, T, T, T

            ]

        sense.set_pixels (clock4)
 
    if local_time == ("04") or local_time == ("16"):

        clock5 = [

            T, T, T, I, I, I, I, I,
            T, T, T, T, T, T, T, I,
            T, T, I, T, T, T, T, I,
            T, T, I, T, I, T, T, I,
            T, T, I, I, I, I, T, I,
            T, T, T, T, I, T, T, I,
            T, T, T, T, I, T, T, T,
            T, T, T, T, T, T, T, T

            ]

        sense.set_pixels (clock5)

    if local_time == ("05") or local_time == ("17"):
        clock6 = [

            T, T, T, I, I, I, I, I,
            T, T, T, T, T, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, I, T, T, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, T, T, I, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, T, T, T, T, I, I

            ]

        sense.set_pixels (clock6)

    if local_time == ("06") or local_time == ("18"):
        clock7 = [

            T, T, T, I, I, I, I, I,
            T, T, I, T, T, T, T, I,
            T, T, I, T, T, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, I, T, I, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, T, T, T, T, T, I,
            T, T, T, I, I, I, I, I
            ]

        sense.set_pixels(clock7)

    if local_time == ("07") or local_time == ("19"):
        clock8 = [

            T, T, T, I, I, I, I, I,
            T, T, T, T, T, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, T, T, I, T, T, I,
            T, T, T, T, I, T, T, I,
            T, T, T, T, I, T, T, I,
            T, T, T, T, T, T, T, I,
            T, I, I, I, I, I, I, I

            ]

        sense.set_pixels(clock8)

    if local_time == ("08") or local_time == ("20"):
        clock9 = [

            T, T, T, I, I, I, I, I,
            T, T, T, T, T, T, T, I,
            T, T, I, I, I, I, T, I,
            T, T, I, T, T, I, T, I,
            T, T, I, I, I, I, T, I,
            T, T, I, T, T, I, T, I,
            I, T, I, I, I, I, T, I,
            I, I, I, I, I, I, I, I
            ]

        sense.set_pixels(clock9)

    if local_time == ("09") or local_time == ("21"):
        clock10 = [

            T, T, T, I, I, I, I, I,
            T, T, T, T, T, T, T, I,
            T, T, I, I, I, T, T, I,
            T, T, I, T, I, T, T, I,
            I, T, I, I, I, T, T, I,
            I, T, T, T, I, T, T, I,
            I, T, T, T, T, T, T, I,
            I, I, I, I, I, I, I, I
            ]

        sense.set_pixels(clock10)

    if local_time == ("10") or local_time == ("22"):
        clock11 = [

            T, T, T, I, I, I, I, I,
            T, T, T, T, T, T, T, I,
            I, T, I, T, I, I, T, I,
            I, T, I, T, I, I, T, I,
            I, T, I, T, I, I, T, I,
            I, T, I, T, I, I, T, I,
            I, T, T, T, T, T, T, I,
            I, I, I, I, I, I, I, I
            ]

        sense.set_pixels(clock11)

    if local_time == ("11") or local_time == ("23"):
        clock12 = [

            I, T, T, I, I, I, I, I,
            I, T, T, T, T, T, T, I,
            I, T, I, T, I, T, T, I,
            I, T, I, T, I, T, T, I,
            I, T, I, T, I, T, T, I,
            I, T, I, T, I, T, T, I,
            I, T, T, T, T, T, T, I,
            I, I, I, I, I, I, I, I,
            ]
            

        sense.set_pixels(clock12)
    time.sleep(3)

def Environment():
    temp = sense.get_temperature()

    temp = round(temp,0)

    temp = str(temp)

    if temp > ("0") and temp < ("10") or temp == ("10"):
        colour = [40, 108, 255]
    if temp > ("10") and temp < ("20") or temp == ("20"):
        colour = [255, 255, 0]
    if temp > ("20") and temp < ("30") or temp == ("30"):
        colour = [255, 153, 0]
    if temp > ("30") and temp < ("40") or temp == ("40"):
        colour = [255, 92, 51]
    if temp > ("40") and temp < ("50") or temp == ("50"):
        colour = [225, 0, 0]

    sense.clear(colour)

    sense.show_message(temp, back_colour = (colour), scroll_speed = 0.08)

def Location():
    c1 = randint(0, 255)
    c2 = randint(0, 255)
    c3 = randint(0, 255)

    colour = [c1, c2, c3]
        
    sense.show_message(str(latitude), scroll_speed = 0.08, back_colour = (colour))
    sense.show_message(str(longitude), scroll_speed = 0.08, back_colour = (colour))              


while True :

    tle_rec = ephem.readtle(name, line1, line2)
    tle_rec.compute()

    lat = (str(tle_rec.sublat))
    long = (str(tle_rec.sublong))

    lat = lat.split(":")
    long = long.split(":")

    lat = (float(lat[0]))
    long = (float(long[0]))
    
    print (lat, long)

    latitude = (lat)
    longitude = (long)
    print (tf.closest_timezone_at(lng=longitude, lat=latitude, delta_degree=20))
    timezone = (tf.closest_timezone_at(lng=longitude, lat=latitude, delta_degree=2))
    
    os.environ['TZ'] = (timezone)
    time.tzset()
    print (time.strftime("%X"))
    local_time = (str(time.strftime("%X")[:2]))
    daytime = ("06")
    nighttime = ("20")
    morning = ("11")
    afternoon = ("14")

    if local_time > daytime  and local_time < nighttime or local_time == daytime:
        print("Day Time")
        print("")
        
        if local_time < morning and local_time > daytime or local_time == daytime:

           X = [225,225,0]
           Z = [32,32,32]

           sun1 = [

           Z, Z, Z, Z, Z, Z, Z, Z,
           Z, Z, Z, Z, Z, Z, Z, Z,
           Z, Z, X, X, X, X, Z, Z,
           Z, Z, X, X, X, X, Z, Z,
           Z, Z, X, X, X, X, Z, Z,
           Z, Z, X, X, X, X, Z, Z,
           Z, Z, Z, Z, Z, Z, Z, Z,
           Z, Z, Z, Z, Z, Z, Z, Z

           ] 

           sense.set_pixels(sun1)
    
        if local_time > morning and local_time < afternoon or local_time == afternoon or local_time == morning:

            X = [225,225,0]

            sun2 = [

            X, X, X, X, X, X, X ,X,
            X, X, X, X, X, X, X ,X,
            X, X, X, X, X, X, X ,X,
            X, X, X, X, X, X, X ,X,
            X, X, X, X, X, X, X ,X,
            X, X, X, X, X, X, X ,X,
            X, X, X, X, X, X, X ,X,
            X, X, X, X, X, X, X ,X,

            ]

            sense.set_pixels(sun2)

        if local_time > afternoon and local_time < nighttime:

            W = [255, 128, 0]

            sun3 = [

            W, W, W, W, W, W, W, W,
            W, W, W, W, W, W, W, W,
            W, W, W, W, W, W, W, W,
            W, W, W, W, W, W, W, W,
            W, W, W, W, W, W, W, W,
            W, W, W, W, W, W, W, W,
            W, W, W, W, W, W, W, W,
            W, W, W, W, W, W, W, W

            ]

            sense.set_pixels(sun3)

    else:
        print("Night Time")
        print ("")

        Y = [192 ,192, 192]
        Q = [64, 64, 64]

        moon1 = [

        Y, Y, Y, Y, Q, Y, Y, Y,
        Y, Y, Y, Y, Y, Y, Y, Y,
        Y, Q, Y, Y, Y, Y, Q, Y,
        Y, Y, Y, Y, Y, Y, Y, Y,
        Y, Y, Y, Q, Y, Y, Y, Y,
        Y, Y, Y, Y, Y, Y, Y, Y,
        Q, Y, Y, Y, Y, Y, Y, Y,
        Y, Y, Y, Y, Y, Y, Q, Y

        ]

        sense.set_pixels(moon1)
            
    sense.stick.direction_right = Clock

    sense.stick.direction_left = Environment

    sense.stick.direction_up = Location

    temp = sense.get_temperature()

    with open("/home/pi/Astro_Pi/data", 'a') as f:
        f.write("\n")
        f.write(str(latitude))
        f.write("\n")
        f.write(str(longitude))
        f.write("\n")
        f.write(str(timezone))
        f.write("\n")
        f.write(str(temp))
        f.write("\n")
        f.write(str(time.strftime("%X")))
        f.write("\n")

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1661
Joined: Fri Oct 14, 2011 8:06 pm
Location: London, United Kingdom
Contact: Website Twitter

Re: Slow Astro Pi Code

Mon Dec 04, 2017 8:46 am

Hi there;

Just quickly looking at your code I've identified a line which is quite computationally costly inside your main loop;
Try moving this line outside of your while loop, it doesn't need to be run every time - you just need to call the compute method every time.

Code: Select all

tle_rec = ephem.readtle(name, line1, line2)
I hope this helps.

Dave

TheCoderChild
Posts: 5
Joined: Fri Dec 01, 2017 5:25 pm
Contact: Website

Re: Slow Astro Pi Code

Mon Dec 04, 2017 9:01 am

Thanks Dave Spice will try

User avatar
bensimmo
Posts: 3014
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Slow Astro Pi Code

Mon Dec 04, 2017 10:54 am

I have just tested this code
(I can see places where things could be better, your IFs for example and tidying up the logo code, etc)

but after installing modules (through pip3 both ephem and timezonefinder)

I get this error (in Thonny)

Code: Select all

34.0 -179.0
Pacific/Midway
Traceback (most recent call last):
  File "/home/pi/Desktop/DataLogging/AstroPi CoderChild.py", line 431, in <module>
    os.environ['TZ'] = (timezone)
  File "/usr/lib/python3.5/os.py", line 730, in __setitem__
    value = self.encodevalue(value)
  File "/usr/lib/python3.5/os.py", line 798, in encode
    raise TypeError("str expected, not %s" % type(value).__name__)
TypeError: str expected, not NoneType
Does this not happen for you ?

TheCoderChild
Posts: 5
Joined: Fri Dec 01, 2017 5:25 pm
Contact: Website

Re: Slow Astro Pi Code

Mon Dec 04, 2017 11:13 am

change the delta degrees to 25 sorry for posting wrong version this should fix it

TheCoderChild
Posts: 5
Joined: Fri Dec 01, 2017 5:25 pm
Contact: Website

Re: Slow Astro Pi Code

Mon Dec 04, 2017 9:14 pm

Thanks Dave for the advice yet it makes no difference the slow bit seems to be the timezones and finding out the time in them.

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1661
Joined: Fri Oct 14, 2011 8:06 pm
Location: London, United Kingdom
Contact: Website Twitter

Re: Slow Astro Pi Code

Tue Dec 05, 2017 9:24 am

Ah so, well done, so this might be your first encounter with code profiling! If you go onto be a games developer this kind of thing becomes your life. There are special tools out there which let you identify the slow parts of your code so that you can concentrate on improving them.

For now though I have a possible suggestion which is to look into multi-threading. This lets you have two (or more) branches of code running simultaneously and what one usually does is this situation is push all the slow stuff you need to do onto another thread and just let it run slowly. Thus not impacting the main "root" thread of your code where you're, for example, updating the screen with an animation.

Here's a code example which demonstrates the concept:

Code: Select all

#!/usr/bin/python3

import queue
import time
import threading as th

running = True
q = queue.Queue()  # FIFO (First in First Out)

def slow_work():
    global running
    global q
    while running:
        if not q.empty():
            task = q.get()  # take from the queue
            print("Removed %s from queue" % task)
            time.sleep(5)  # simulated work delay
        else:
            time.sleep(0.1)

slow_worker = th.Thread(target=slow_work)
slow_worker.start()

task = 0

while running:
    task += 1
    print("Adding %s to queue" % task)
    q.put(task)  # put onto the queue
    time.sleep(1)
    if task == 20:
        while not q.empty():  # wait for queue processing to finish
            time.sleep(1)
        running = False

print("Done")

User avatar
bensimmo
Posts: 3014
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Slow Astro Pi Code

Tue Dec 05, 2017 2:43 pm

TheCoderChild wrote:
Mon Dec 04, 2017 11:13 am
change the delta degrees to 25 sorry for posting wrong version this should fix it
I posted but it seems to be missing so I'll try again, I probably pressed preview.

You need to create your file before you can amend it.

Have a nosy at SenseHat data logging article for a nice way to do this and thread writes as well.

TheCoderChild
Posts: 5
Joined: Fri Dec 01, 2017 5:25 pm
Contact: Website

Re: Slow Astro Pi Code

Wed Dec 06, 2017 8:11 am

Thanks for the help I have isolated and speed up the segment of code now it is super fast.

Return to “Astro Pi”

Who is online

Users browsing this forum: No registered users and 1 guest