1 # This script runs an automated voltage sweep while collecting ADC DBGLN() output
2 # from an ExpressLRS RX using devAnalogVbat. The receiver should be compiled with:
3 # -DDEBUG_VBAT_ADC -DDEBUG_LOG -DDEBUG_CRSF_NO_OUTPUT -DRCVR_UART_BAUD=460800
4 # (lots of UART errors at the default 420000)
5 # If firmware is workign correctly `cat /dev/ttyUSB0` will show values such as
11 # Uses the KoradSerial library for power supply control, just dump this in the library directory
12 # https://github.com/starforgelabs/py-korad-serial
13 from koradserial
import KoradSerial
29 if len(self
.vals
) < 3:
33 self
.min = self
.vals
[0]
34 self
.max = self
.vals
[-1]
36 for i
in range(1, len(self
.vals
)-1):
37 self
.mean
+= self
.vals
[i
]
38 self
.mean
= self
.mean
/ (len(self
.vals
)-2)
42 return f
'n={len(self.vals)} mean={self.mean} min={self.min} max={self.max}'
46 bVal
= ser
.read_until()
50 def doVoltageSample(ser
, voltage
):
52 adcMedian
= MedianAvg()
54 sLine
= ser
.read_until().decode('ascii')
55 if sLine
.startswith('$ADC,') and sLine
.endswith('\r\n'):
57 iADC
= int(sLine
[5:-2])
60 if len(adcMedian
) >= 10:
62 print(f
'{voltage:0.1f},{adcMedian.mean:0.4f},{adcMedian.min},{adcMedian.max}')
69 with serial
.Serial(args
.rport
, baudrate
=args
.rbaud
, timeout
=0.010) as rcvr
:
70 with
KoradSerial(args
.kport
) as pwr
:
72 #print("Model: ", pwr.model)
73 pwrch
= pwr
.channels
[0]
77 print('voltage,mean,min,max')
78 for v
in range(args
.start
, args
.end
+args
.step
, args
.step
):
80 pwrch
.voltage
= voltage
82 doVoltageSample(rcvr
, voltage
)
86 if __name__
== '__main__':
87 parser
= argparse
.ArgumentParser(
88 description
="Run a voltage sweep using a Korad power supply and recevie ADC data from an ELRS receiver")
89 parser
.add_argument("-b", "--rbaud", type=int, default
=460800,
90 help="Receiver UART baud")
91 parser
.add_argument("-e", "--end", type=int, default
=100,
92 help="Ending voltage (centidegrees 500 = 5.00V)")
93 parser
.add_argument("-k", "--kport", type=str, default
='/dev/ttyACM0',
94 help="Port used for Korad power supply UART")
95 parser
.add_argument("-r", "--rport", type=str, default
='/dev/ttyUSB0',
96 help="Port used for receiver UART")
97 parser
.add_argument("-s", "--start", type=int, default
=3100,
98 help="Starting voltage (centidegrees 500 = 5.00V)")
99 parser
.add_argument("-t", "--step", type=int, default
=-10,
100 help="Voltage step (centidegrees -10 = -0.10V)")
102 args
= parser
.parse_args()