Picar Cam

Categories:

Drehstrohmzähler - OCR - Analytics

pi Car - X timer - job to take Photo

import time
from datetime import datetime, timedelta

import sys
import os
from robot_hat.utils import reset_mcu
from picarx import Picarx
from vilib import Vilib
from time import sleep, time, strftime, localtime
import readchar

import os
user = os.getlogin()
user_home = os.path.expanduser(f'~{user}')

reset_mcu()
sleep(0.2)

px = Picarx()
g_sleep_iteration =10
g_sleep_hit=60
g_ticks = 0
debug =0

def send_message():
    # This function is where you'll send your message.
    # For demonstration, it just prints to the console.
    print(f"Message sent at {datetime.now()}")
    
    # make photo 
    take_photo()

def take_photo():
    _time = strftime('%Y-%m-%d-%H-%M-%S',localtime(time()))
    name = 'photo_track_%s'%_time
    path = f"{user_home}/Pictures/picar-x/"
    Vilib.take_photo(name, path)
    print('\nphoto save as %s%s.jpg'%(path,name))    
    
def run_scheduler():
    # Times of day you want to send messages, in 24-hour format.
    alert_times = ["07:00", "18:00"]
    alert_times = []
    for i in range(24):
        stime = f"0{i}:00"[-5:]
        alert_times.append(stime) # jede stunde 
        for q in range(1,4): 
            min = 0 + q*15
            stime = f"0{i}:{min}"
            stime = stime[-5:]
            alert_times.append(stime) # jede viertel - stunde 
    # Convert alert times to datetime.time objects for easier comparison.
    # alert_times = [datetime.strptime(t[-5:], "%H:%M").time() for t in alert_times]
    alert_times_2 =[]
    for t in alert_times:
         alert_times_2.append(datetime.strptime(t[-5:], "%H:%M").time())
    alert_times = alert_times_2
    
    while True:
        # now = datetime.now()
        # current_time = now.time()
        current_time = getCurrentTime()
        # Check if the current time is close to any alert time (within one minute).
        for alert_time in alert_times:
            if current_time.hour == alert_time.hour and current_time.minute == alert_time.minute:
                send_message()
                # Wait a bit after sending the message to avoid duplicate sends.
                print (f"time.sleep({g_sleep_hit})")
                sleep(g_sleep_hit)

        # Sleep for a short while before checking the time again to reduce CPU usage.
        sleep(g_sleep_iteration)

def getCurrentTime ():
    now = datetime.now()
    current_time = now.time()
    if debug :
        global g_ticks
        g_ticks +=1
        h = g_ticks%60
        stime = f"{(g_ticks//60)%24}:{g_ticks%60}"[-5:]
        current_time = datetime.strptime(stime, "%H:%M").time()
    print ("current_time:", current_time)
    return current_time


if __name__ == "__main__":
    speed = 0
    status = 'stop'

    Vilib.camera_start(vflip=False,hflip=False)
    Vilib.display(local=True,web=True)
    sleep(2)  # wait for startup
    run_scheduler()
    # blink_point()

Written on March 16, 2024