def test_gps_parser(disp):
    disp.gps.has_fix = True
    disp.gps.parse_nmea("$GPZDA,172809.456,12,07,2021,00,00")
    print("%s" % (comutils.fmt_time(utime.localtime(disp.get_date()))))
    # 2021/07/12-17:28:09
    disp.gps.parse_nmea("$GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031")
    print("%f %f %s" % (disp.get_longitude(), disp.get_latitude(), comutils.fmt_time(utime.localtime(disp.get_date()))))
    # -122.037828 37.391098 2021/07/12-17:28:14
    disp.gps.parse_nmea("$GPGLL,4916.45,N,12311.12,W,225444,A")
    print("%f %f %s" % (disp.get_longitude(), disp.get_latitude(), comutils.fmt_time(utime.localtime(disp.get_date()))))
    # -123.185325 49.274168 2021/07/12-22:54:44
    disp.gps.parse_nmea("$GPRMC,041425.00,A,4916.45,N,12311.12,W,0.068,,070121,,,D")
    print("%f %f %s" % (disp.get_longitude(), disp.get_latitude(), comutils.fmt_time(utime.localtime(disp.get_date()))))
    # -123.185325 49.274168 2021/01/07-04:14:25
    disp.gps.has_fix  = False
    disp.gps.has_date = False
def test():
    mgr = TimeLocationManager()
    print("Start Time From File: " + str(mgr.start_time))
    print("File Time Stamp %u" % mgr.start_sec)
    i = 0
    ms = pyb.millis()
    while i < 5:
        mgr.tick()
        print("tick " + comutils.fmt_time(mgr.get_time()))
        pyb.delay(1001)
        i += 1
    print("=============")
    print("Time Test")
    i = 0
    while i < 3:
        utc_yr = 2020 + (pyb.rng() % 2)
        utc_month = 1 + (pyb.rng() % 12)
        utc_day = 1 + (pyb.rng() % 27)
        utc_hr = (pyb.rng() % 24)
        utc_min = (pyb.rng() % 60)
        utc_sec = (pyb.rng() % 60)
        mgr.set_utc_time(utc_yr, utc_month, utc_day, utc_hr, utc_min, utc_sec)
        print("%s = %s" % (str(
            (utc_yr, utc_month, utc_day, utc_hr, utc_min, utc_sec)),
                           str(mgr.get_time())))
        i += 1
    print("=============")
    print("Rotation Test")
    i = 0
    while i < 10:
        longitude = pyb.rng() % 360
        if longitude > 180:
            longitude -= 360
        mgr.set_location(longitude, None)
        mgr.set_utc_time(2021 + (pyb.rng() % 20), 1 + (pyb.rng() % 12),
                         1 + (pyb.rng() % 27),
                         pyb.rng() % 24,
                         pyb.rng() % 60,
                         pyb.rng() % 60)
        ang = mgr.get_angle()
        if ang < 1 or ang > 359:
            print("%d, %s, %0.4f" %
                  (longitude, comutils.fmt_time(mgr.get_time()), ang))
            i += 1
    print("=============")
def test_gps(disp):
    now = pyb.millis()
    disp.task()
    if disp.has_gps_fix():
        t = disp.get_date()
        if t == 0:
            timestr = "no time data"
        else:
            timestr = comutils.fmt_time(utime.localtime(t))
        print("GPS[%u]: long= %f , lat= %f , time= %s" % (now, disp.get_longitude(), disp.get_latitude(), timestr))
    elif disp.gps_ok:
        if disp.gps.has_nmea:
            print("GPS[%u] no fix" % (now))
        else:
            print("GPS[%u] no data" % (now))
    else:
        print("GPS[%u] error" % (now))
Example #4
0
    def task(self):
        gc.collect()
        self.diag_cnt += 1
        self.t = pyb.millis()
        self.time_mgr.tick(latest_millis = self.t)
        self.tick_all, self.dur_all = self.diag_tick(self.t, self.tick_all, self.dur_all) # debug loop speed

        if self.debug:
            if (self.diag_cnt % 20) == 0:
                print("tick %u %u" % (self.diag_cnt, self.frm_cnt))

        self.task_network()

        if self.cam.check_init() == False:
            if self.websock is not None and pyb.elapsed_millis(self.websock_millis) > 500:
                self.update_websocket()
            return

        if self.cam.snapshot_check():
            # camera has finished an exposure
            self.img = self.cam.snapshot_finish()
            if self.img is not None:
                self.frm_cnt += 1
            else:
                self.cam_err = True

            if self.sleeping:
                #red_led.off()
                green_led.off()
                blue_led.off()
                if self.extdisp is not None:
                    if self.cam_err:
                        self.extdisp.set_error("ERR + SLP")
                    else:
                        self.extdisp.set_error("SLEEP")
                    self.extdisp.prep(None, None, None)
                    self.extdisp.show()
                return

            # day mode is just auto exposure for testing
            if self.daymode:
                self.cam.init(gain_db = -1, shutter_us = -1)
                self.snap_millis = pyb.millis()
                if self.img is not None:
                    self.histogram = self.img.get_histogram()
                    self.img_stats = self.histogram.get_statistics()
                    if self.packjpeg:
                        self.compress_img()
                self.cam.snapshot_start()
                if self.use_leds:
                    green_led.toggle()
                return # this will skip solving
            # take the next frame with settings according to mode
            self.cam.init(gain_db = self.settings["gain"], shutter_us = self.settings["shutter"], force_reset = self.cam_err)

            already_done = False
            if self.cam.check_init() == False:
                already_done = True
                if self.imgstream_sock is None:
                    self.solve()
                if self.packjpeg or self.imgstream_sock is not None:
                    self.compress_img()
                    if self.imgstream_sock is not None:
                        self.update_imgstream()
                while self.cam.check_init() == False:
                    self.task_network()

            if self.packjpeg == False and self.imgstream_sock is None:
                self.cam.snapshot_start()
                self.snap_millis = pyb.millis()
            if self.use_leds:
                green_led.toggle()
            if self.imgstream_sock is None and already_done == False:
                self.solve()
            if self.packjpeg or self.imgstream_sock is not None:
                if already_done == False:
                    self.compress_img()
                    if self.imgstream_sock is not None:
                        self.update_imgstream()
                self.cam.snapshot_start()
                self.snap_millis = pyb.millis()
            self.cam_err = self.cam.has_error

            self.update_websocket()

            if self.extdisp is not None:
                if self.cam_err:
                    self.extdisp.set_error("CAM ERR")
                elif self.img is None:
                    self.extdisp.set_error("IMG ERR")
                elif self.expo_code != star_finder.EXPO_JUST_RIGHT:
                    self.extdisp.set_error("EXPO ERR")
                else:
                    self.extdisp.set_error(None)
                stable_solution = self.stable_solution()
                if stable_solution is None:
                    self.extdisp.prep(None, None, None)
                    self.extdisp.show()
                else:
                    stable_solution.get_pole_coords()
                    rot = stable_solution.get_rotation() + self.time_mgr.get_angle()
                    polecoords = (stable_solution.x, stable_solution.y)
                    if self.settings["use_refraction"]:
                        refrac = comutils.get_refraction(self.time_mgr.latitude)
                        polecoords = comutils.move_point_vector(polecoods, (refrac * stable_solution.pix_per_deg, rot + 90))
                    self.extdisp.prep(polecoords, (self.settings["center_x"], self.settings["center_y"]), rot)
                    self.extdisp.show()

        else:
            if self.extdisp is not None:
                self.extdisp.task()
                t = self.extdisp.get_date()
                if t != 0:
                    self.time_mgr.set_utc_time_epoch(t)
                    self.time_mgr.set_location(self.extdisp.get_longitude(), self.extdisp.get_latitude())
                    self.settings["longitude"] = self.time_mgr.longitude
                    self.settings["latitude"]  = self.time_mgr.latitude
                    if self.has_time == False:
                        exclogger.log_exception("Time Obtained (%u)" % pyb.millis(), time_str=comutils.fmt_time(self.time_mgr.get_time()))
                    self.has_time = True

            if pyb.elapsed_millis(self.snap_millis) > 5000:
                self.cam_err = True
                if self.debug:
                    print("warning: camera timeout")
                if self.daymode:
                    self.cam.init(gain_db = -1, shutter_us = -1, force_reset = True)
                else:
                    self.cam.init(gain_db = self.settings["gain"], shutter_us = self.settings["shutter"], force_reset = True)
                while self.cam.check_init() == False:
                    self.task_network()
                self.cam.snapshot_start()
                self.snap_millis = pyb.millis()
                if self.extdisp is not None:
                    self.extdisp.set_error("CAM ERR")
                    self.extdisp.show()

            if self.websock is not None and pyb.elapsed_millis(self.websock_millis) > 500:
                self.update_websocket()
Example #5
0
 def execute_query(self, req, reply = False, save = False):
     need_save = False
     request_page, request_urlparams = captive_portal.split_get_request(req)
     if self.debug and reply:
         print(" keys %u" % len(request_urlparams))
     for i in request_urlparams.keys():
         v = request_urlparams[i]
         v = self.try_parse_setting(v)
         if i in self.settings:
             need_save = True
             self.settings[i] = v
             if self.debug and (reply or save):
                 print("setting \"%s\" value \"%s\"" % (i, str(v)))
             if i == "time":
                 self.time_mgr.set_utc_time_epoch(v)
                 if self.has_time == False:
                     exclogger.log_exception("Time Obtained (%u)" % pyb.millis(), time_str=comutils.fmt_time(self.time_mgr.get_time()))
                 self.has_time = True
             elif i == "longitude":
                 self.time_mgr.set_location(v, None)
                 self.settings[i] = self.time_mgr.longitude # normalized
             elif i == "latitude":
                 self.time_mgr.set_location(None, v)
                 self.settings[i] = self.time_mgr.latitude # normalized
             elif i == "max_stars":
                 self.max_stars = v
         elif i == "rand_id":
             self.websock_randid = v
         elif i == "packjpeg":
             self.packjpeg = (v == True)
             if self.zoom != self.prevzoom and self.packjpeg:
                 self.compress_img()
         elif i == "zoom":
             self.zoom = v
             if self.zoom != self.prevzoom and self.packjpeg:
                 self.compress_img()
         elif i == "hotpixels":
             self.hot_pixels = star_finder.decode_hotpixels(v)
         elif i == "accel_sec":
             self.accel_sec = v
         elif i == "use_debug":
             self.debug = v
         elif i == "use_leds":
             self.use_leds = v
             if v == False:
                 red_led.off()
                 green_led.off()
                 blue_led.off()
         elif i == "sleep":
             self.sleeping = v
             if v == False:
                 self.use_leds = False
                 red_led.off()
                 green_led.off()
                 blue_led.off()
         elif i == "save":
             save = (v == True)
             need_save = True
         else:
             print("unknown setting \"%s\": \"%s\"" % (i, str(v)))
     if need_save and save:
         self.save_settings()
Example #6
0
    def __init__(self, debug = False, simulate_file = None, use_leds = True):
        exclogger.init()
        self.highspeed = False
        self.daymode = False
        self.simulate = False
        self.cam = astro_sensor.AstroCam(simulate = simulate_file)
        self.cam.init(gain_db = 32, shutter_us = 1000000)
        self.time_mgr = time_location.TimeLocationManager()
        self.has_time = False

        self.debug = debug
        self.use_leds = use_leds
        self.sleeping = False
        self.cam_err = False

        t = pyb.millis()
        self.diag_cnt    = 0
        self.frm_cnt     = 0
        self.tick_all    = t
        self.dur_all     = -1
        self.solu_dur    = -1
        self.snap_millis = 0

        self.settings = {}
        self.settings.update({"longitude":   self.time_mgr.longitude})
        self.settings.update({"latitude":    self.time_mgr.latitude})
        self.settings.update({"time":        self.time_mgr.get_sec()})
        self.settings.update({"center_x":    self.cam.width / 2})
        self.settings.update({"center_y":    self.cam.height / 2})
        self.settings.update({"gain":        self.cam.gain})
        self.settings.update({"shutter":     self.cam.shutter})
        self.settings.update({"thresh":      (0)})
        self.settings.update({"use_refraction": False})
        self.settings.update({"force_solve": False})
        self.settings.update({"max_stars":   0})
        self.load_settings()
        self.time_mgr.readiness = False
        exclogger.log_exception("Time Guessed (%u)" % pyb.millis(), time_str=comutils.fmt_time(self.time_mgr.get_time()))

        self.portal = captive_portal.CaptivePortal(debug = self.debug)

        self.img = None
        self.img_compressed = None
        self.extra_fb = None
        self.expo_code = 0
        self.histogram = None
        self.img_stats = None
        self.stars = []
        self.hot_pixels = []
        self.max_stars = 0
        self.packjpeg = False
        self.zoom = 1
        self.prevzoom = 1
        self.mem_errs = 0
        self.accel_sec = 0
        self.solution = None
        if self.portal is not None:
            self.register_http_handlers()
        while self.cam.check_init() == False:
            self.cam.check_init()
        self.cam.snapshot_start()
        self.snap_millis = pyb.millis()

        self.imgstream_sock = None
        self.websock = None
        self.websock_millis = 0
        self.websock_randid = 0
        self.stream_sock_err = 0

        self.extdisp = None
        try:
            import extdisp
            self.extdisp = extdisp.ExtDisp()
            if self.extdisp.oled.test_connect():
                self.extdisp.oled.init_display()
                self.extdisp.oled_ok = True
                print("OLED initialized on boot")
            if self.extdisp.gps.test_connect():
                print("GPS module found on boot")
        except Exception as exc:
            exclogger.log_exception(exc)