I have a program running on the raspberry pi, where there are 2 inputs, a 1Hz signal and a 50Hz signal. I am recording the time elapsed from the rising edge of the 50Hz signal to that of the 1Hz signal and then calculating the phase difference. I want the key to represent the current time of the rising edge of 50Hz waveforms and the value to be the phase difference relative to the 1Hz signal.
Here is my code:
import pigpio
import time
import math
from datetime import datetime
counter = 0
1HZ_PIN = 23
50HZ_PIN = 24
pi = pigpio.pi()
d = {}
pi.set_mode(1HZ_PIN, pigpio.INPUT)
pi.set_pull_up_down(1HZ_PIN, pigpio.PUD_UP)
pi.set_mode(50HZ_PIN, pigpio.INPUT)
pi.set_pull_up_down(50HZ, pigpio.PUD_UP)
while True:
if pi.wait_for_edge(1HZ_PIN, pigpio.RISING_EDGE):
print 'PPS Rising edge'
t1 = datetime.now()
print t1
counter = 0
while pi.wait_for_edge(50HZ_PIN, pigpio.RISING_EDGE) and counter <50):
d = {}
counter = counter + 1
print 'Voltage Sinusoid rising edge'
print datetime.now()
x = datetime.now()
x.isoformat() # I added this line as you can't place datetime objects in a dictionary
elapsed_time = x - t1
elapsed_time_float = elapsed_time.total_seconds() # can't prerform arithmetic operations on datetime object
phase = elapsed_time_float / 0.02/360
d[x] = phase
The last line I presumed would place each recorded rising edge time as a key and the corresponding phase difference as the value. But when I print the list there 50 key/value pairs in dictionary as expected but they do not match the printed rising edge times. Is there a proper way to place variables from this while loop into a dictionary?