0

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?

Conor McG
  • 13
  • 5
  • 3
    Quick note: variables can't start with a number like `1HZ_PIN` or `50HZ_PIN` –  Jan 26 '17 at 14:57
  • Another quick note: `x.isoformat()` will not convert x to a string. You would need to store the result of x.isoformat() in another variable. However, you can use datetime objects as keys in Python, which is why you’re not getting an error out of that mistake. You should be able to remove that line with no effect on your code. – Jerry Stratton Jan 26 '17 at 15:01
  • @Jerry Stratton I removed that line and as you said it had no effect but why is the keys not matching the rising edge times that are printed. Would you like me to include the result from the console. There is also no order in the dictionary with the keys as I presumed they would be in chronological order. – Conor McG Jan 26 '17 at 15:27
  • No, keys are not in chronological order. To work with keys in a sorted manner, you’ll need to pull the keys and sort them yourself. You can use `variable = d.keys()` to get just the keys, and then sort them. (http://stackoverflow.com/questions/14472795/how-do-i-sort-a-list-of-datetime-or-date-objects) – Jerry Stratton Jan 26 '17 at 15:48

2 Answers2

1

I think you are printing the datetime value:

print 'Voltage Sinusoid rising edge'
print datetime.now()

then you calculate it again and save it in a variable:

x = datetime.now()

These two times can be different. Changing it to:

x = datetime.now()
print x

should work and you should get matching printed and saved times.

Kroj
  • 76
  • 4
0

I believe @user3811623 has the right answer. I also wanted to point out -

  • As @JerryStratton mentioned, you do not change the value of x just by applying x.isoformat(), for that you want to assign the value back to x as well -

x = x.isoformat()

  • Dictionary keys are not sorted in chronological order, for keeping key entering order you want ordereddict -

https://docs.python.org/3/library/collections.html#collections.OrderedDict

  • I find it curious that you're getting 50 valuess in your dictionary, as you initiate it inside the loop, thus resetting it.

For example, this-

while true:
    d={}
    x=raw_input('> ')
    d[k]=k
    if k=='s':
        break

with

> t
> y
> u
> s

yields

>>>d
{'s': 's'}

Not sure how you're getting to 50 keys here.

Jay
  • 2,535
  • 3
  • 32
  • 44