forked from M-Labs/nix-servo
add both channels in loopback
This commit is contained in:
parent
125611f51e
commit
9e34cd9dff
|
@ -1,47 +1,59 @@
|
||||||
import time
|
import time
|
||||||
from pyfastservo import adc, dac
|
from pyfastservo import adc, dac
|
||||||
from pyfastservo.common import (
|
from pyfastservo.common import (
|
||||||
ADC_CH1_HIGH_ADDR,
|
ADC_CH0_HIGH_ADDR, ADC_CH0_LOW_ADDR,
|
||||||
ADC_CH1_LOW_ADDR,
|
ADC_CH1_HIGH_ADDR, ADC_CH1_LOW_ADDR,
|
||||||
CH1_HIGH_WORD_ADDR,
|
CH0_HIGH_WORD_ADDR, CH0_LOW_WORD_ADDR,
|
||||||
CH1_LOW_WORD_ADDR,
|
CH1_HIGH_WORD_ADDR, CH1_LOW_WORD_ADDR,
|
||||||
read_from_memory,
|
read_from_memory, write_to_memory
|
||||||
write_to_memory
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def read_adc():
|
def read_adc(channel):
|
||||||
adc_value = adc.read_adc_channel(ADC_CH1_HIGH_ADDR, ADC_CH1_LOW_ADDR)
|
if channel == 0:
|
||||||
return adc_value
|
return adc.read_adc_channel(ADC_CH0_HIGH_ADDR, ADC_CH0_LOW_ADDR)
|
||||||
|
elif channel == 1:
|
||||||
|
return adc.read_adc_channel(ADC_CH1_HIGH_ADDR, ADC_CH1_LOW_ADDR)
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid ADC channel")
|
||||||
|
|
||||||
def write_dac(value):
|
def write_dac(value):
|
||||||
dac.set_dac_output(value)
|
dac.set_dac_output(value)
|
||||||
|
|
||||||
def read_dac():
|
def read_dac(channel):
|
||||||
|
if channel == 0:
|
||||||
|
high_word = read_from_memory(CH0_HIGH_WORD_ADDR, 1)[0]
|
||||||
|
low_word = read_from_memory(CH0_LOW_WORD_ADDR, 1)[0]
|
||||||
|
elif channel == 1:
|
||||||
high_word = read_from_memory(CH1_HIGH_WORD_ADDR, 1)[0]
|
high_word = read_from_memory(CH1_HIGH_WORD_ADDR, 1)[0]
|
||||||
low_word = read_from_memory(CH1_LOW_WORD_ADDR, 1)[0]
|
low_word = read_from_memory(CH1_LOW_WORD_ADDR, 1)[0]
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid DAC channel")
|
||||||
return (high_word << 8) | low_word
|
return (high_word << 8) | low_word
|
||||||
|
|
||||||
def perform_loopback_test(test_value):
|
def perform_loopback_test(test_value):
|
||||||
print(f"Setting DAC output to {test_value}...")
|
print(f"\nSetting DAC output to {test_value}...")
|
||||||
write_dac(test_value)
|
write_dac(test_value)
|
||||||
time.sleep(0.1) # Allow time for the signal to stabilize
|
time.sleep(0.1) # Allow time for the signal to stabilize
|
||||||
|
|
||||||
print("Reading ADC value...")
|
for channel in [0, 1]:
|
||||||
adc_value = read_adc()
|
print(f"\nTesting Channel {channel}")
|
||||||
print(f"ADC readback: {adc_value}")
|
|
||||||
|
|
||||||
print("Reading DAC value...")
|
print(f"Reading ADC CH{channel} value...")
|
||||||
dac_value = read_dac()
|
adc_value = read_adc(channel)
|
||||||
print(f"DAC readback: {dac_value}")
|
print(f"ADC CH{channel} readback: {adc_value}")
|
||||||
|
|
||||||
|
print(f"Reading DAC CH{channel} value...")
|
||||||
|
dac_value = read_dac(channel)
|
||||||
|
print(f"DAC CH{channel} readback: {dac_value}")
|
||||||
|
|
||||||
if abs(test_value - adc_value) <= 2 and abs(test_value - dac_value) <= 2:
|
if abs(test_value - adc_value) <= 2 and abs(test_value - dac_value) <= 2:
|
||||||
print("Loopback test PASSED!")
|
print(f"Loopback test for Channel {channel} PASSED!")
|
||||||
else:
|
else:
|
||||||
print("Loopback test FAILED!")
|
print(f"Loopback test for Channel {channel} FAILED!")
|
||||||
print(f"Expected: {test_value}, ADC: {adc_value}, DAC: {dac_value}")
|
print(f"Expected: {test_value}, ADC: {adc_value}, DAC: {dac_value}")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test_value = 0x2000 # Mid-range value (8192 in decimal)
|
test_value = 0x0FFF # Mid-range value (4095 in decimal)
|
||||||
perform_loopback_test(test_value)
|
perform_loopback_test(test_value)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue