Help Synchronizing and logging multiple channels
Posted: Mon Jun 13, 2022 4:36 pm
Hello, I am using a 1046_0 PhidgetBridge to read 3x Load Cells. My goal is to simply log data from each input at 120hz and write the data to CSV format. For my application it is important that there is a minimal time difference between samples in each row of sensor data - in other words I am trying to avoid a race condition or dropped samples among channels resulting in misalignment of samples in my log data.
Example: Suppose I build a weigh scale by using 3x load cells to support a platform upon which I place a test weight of 1kg. When I place the weight I expect to see the signal from all 3 inputs increase at roughly the same time. However, if there is a sample misalignment/dropped sample/race condition, there may be a significant delay in response from one or more channels.
I have been reading the Phidget22 API documentation and do not see this multi-channel synchronized logging topic covered. There is discussion of polling vs callbacks but they only talk about logging at the individual channel level and do not appear to provide tips on how to optimize/synchronize samples of multiple channels over time.
Here is my code in a Jypyter Notebook: The code I have written so far Creates, Addresses, Opens, and Attaches to three input channels. They all share the same callback function which appends the latest sample from each channel to a dict of corresponding {channel_number: [s1, s2, s3, sn...], }.
When I am done logging I run a function that closes each channel. Then I count the number of samples recorded from each channel and they are usually 1-2 samples apart which makes sense due to the time difference between opening and closing each channel. However, sometimes they are off by quite a bit more, like 15 samples - this is concerning.
Next I find the channel with the lowest number of samples and drop the most recent samples from the other channels until all channels have the same number of samples, then write the data to CSV (rows = samples/time, cols = channels)
But I have no confidence that my samples will always be properly synchronized.
What is the technical term for this kind of problem/solution and what is the best approach to ensure that each row of sensor samples are synchronized with each other?
Thank you!
Example: Suppose I build a weigh scale by using 3x load cells to support a platform upon which I place a test weight of 1kg. When I place the weight I expect to see the signal from all 3 inputs increase at roughly the same time. However, if there is a sample misalignment/dropped sample/race condition, there may be a significant delay in response from one or more channels.
I have been reading the Phidget22 API documentation and do not see this multi-channel synchronized logging topic covered. There is discussion of polling vs callbacks but they only talk about logging at the individual channel level and do not appear to provide tips on how to optimize/synchronize samples of multiple channels over time.
Here is my code in a Jypyter Notebook: The code I have written so far Creates, Addresses, Opens, and Attaches to three input channels. They all share the same callback function which appends the latest sample from each channel to a dict of corresponding {channel_number: [s1, s2, s3, sn...], }.
Code: Select all
def getCallback(data_dict):
def onVoltageRatioChange(self, voltageRatio):
data_dict[self.getChannel()].append(voltageRatio)
return onVoltageRatioChange
Next I find the channel with the lowest number of samples and drop the most recent samples from the other channels until all channels have the same number of samples, then write the data to CSV (rows = samples/time, cols = channels)
Code: Select all
# Store the number of samples from the channel with the lowest sample count
data_len = min([len(data[ch]) for ch in lc_channels])
# Drop excess samples from the other channels
res = [data[ch][:data_len] for ch in lc_channels]
# Make a Data Frame from the sensor data
df_data = pd.DataFrame(res).T
csv = df_data.to_csv()
What is the technical term for this kind of problem/solution and what is the best approach to ensure that each row of sensor samples are synchronized with each other?
Thank you!