Updates
This commit is contained in:
94
looper.py
94
looper.py
@@ -6,11 +6,33 @@ import time
|
|||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import pyvisa
|
||||||
|
import vxi11
|
||||||
|
|
||||||
# Initial state
|
# Initial state
|
||||||
toggle_state = 0
|
toggle_state = 0
|
||||||
|
|
||||||
ser = serial.Serial(port='/dev/ttyACM0', baudrate=115200)
|
#ser = serial.Serial(port='/dev/ttyACM0', baudrate=115200)
|
||||||
|
|
||||||
|
VISA_ADDRESS = "TCPIP::192.168.45.3::INSTR"
|
||||||
|
|
||||||
|
rm = pyvisa.ResourceManager()
|
||||||
|
|
||||||
|
SPD = rm.open_resource(
|
||||||
|
VISA_ADDRESS,
|
||||||
|
write_termination='\n',
|
||||||
|
read_termination='\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
class DS1202ZE(vxi11.Instrument):
|
||||||
|
def __init__(self, host, *args, **kwargs):
|
||||||
|
super(DS1202ZE, self).__init__(host, *args, **kwargs)
|
||||||
|
def get_identification(self):
|
||||||
|
return self.ask("*IDN?")
|
||||||
|
def get_vavg_channel1(self):
|
||||||
|
return self.ask(":MEAS:ITEM? VAVG, CHAN1\n")
|
||||||
|
def get_vavg_channel2(self):
|
||||||
|
return self.ask(":MEAS:ITEM? VAVG, CHAN2\n")
|
||||||
|
|
||||||
class Tee(object):
|
class Tee(object):
|
||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
@@ -56,6 +78,24 @@ def pack_integers_to_bytes(*integers: int) -> bytes:
|
|||||||
|
|
||||||
return struct.pack(format_string, *full_packet)
|
return struct.pack(format_string, *full_packet)
|
||||||
|
|
||||||
|
def set_voltage():
|
||||||
|
global SPD
|
||||||
|
|
||||||
|
voltage = random.randint(9, 30)
|
||||||
|
voltage_f = float(voltage)
|
||||||
|
|
||||||
|
SPD.write('CH1:VOLT ' + str(voltage_f)) # Send power supply source voltage
|
||||||
|
|
||||||
|
print(f"[{time.strftime('%H:%M:%S')}] VOLTAGE SET TO: " + str(voltage) + "V")
|
||||||
|
|
||||||
|
# Schedule the NEXT random trigger
|
||||||
|
# random.uniform(60, 360) provides the random delay in seconds
|
||||||
|
delay = random.uniform(60, 360)
|
||||||
|
|
||||||
|
# Create and start a new timer thread
|
||||||
|
timer = threading.Timer(delay, set_voltage)
|
||||||
|
timer.start()
|
||||||
|
|
||||||
def trigger_serial_command():
|
def trigger_serial_command():
|
||||||
global toggle_state
|
global toggle_state
|
||||||
global ser
|
global ser
|
||||||
@@ -65,19 +105,14 @@ def trigger_serial_command():
|
|||||||
toggle_state = 1 - toggle_state
|
toggle_state = 1 - toggle_state
|
||||||
|
|
||||||
if toggle_state == 0:
|
if toggle_state == 0:
|
||||||
voltage = 0
|
|
||||||
voltage_mv = 0
|
|
||||||
state = 0
|
state = 0
|
||||||
|
|
||||||
print("VOLTAGE SET TO: OFF")
|
print(f"[{time.strftime('%H:%M:%S')}] SWITCH SET TO: OFF")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Generate voltage level
|
|
||||||
voltage = random.randint(9, 30)
|
|
||||||
voltage_mv = voltage * 1000
|
|
||||||
state = 1
|
state = 1
|
||||||
|
|
||||||
print("VOLTAGE SET TO: " + str(voltage) + "V")
|
print(f"[{time.strftime('%H:%M:%S')}] SWITCH SET TO: ON")
|
||||||
|
|
||||||
# Serial logic
|
# Serial logic
|
||||||
print(f"[{time.strftime('%H:%M:%S')}] SENDING SERIAL COMMAND...")
|
print(f"[{time.strftime('%H:%M:%S')}] SENDING SERIAL COMMAND...")
|
||||||
@@ -86,12 +121,12 @@ def trigger_serial_command():
|
|||||||
|
|
||||||
data = (command, state)
|
data = (command, state)
|
||||||
byte_data = pack_integers_to_bytes(*data)
|
byte_data = pack_integers_to_bytes(*data)
|
||||||
ser.write(serial.to_bytes(byte_data))
|
#ser.write(serial.to_bytes(byte_data))
|
||||||
|
|
||||||
# Schedule the NEXT random trigger
|
# Schedule the NEXT random trigger
|
||||||
# random.uniform(0.1, 100.0) provides the random delay in seconds
|
# random.uniform(0.1, 100.0) provides the random delay in seconds
|
||||||
delay = random.uniform(min_delay_input_f, max_delay_input_f)
|
delay = random.uniform(min_delay_input_f, max_delay_input_f)
|
||||||
print(f"NEXT COMMAND IN {delay:.2f} SECONDS...")
|
print(f"[{time.strftime('%H:%M:%S')}] NEXT COMMAND IN {delay:.2f} SECONDS...")
|
||||||
|
|
||||||
# Create and start a new timer thread
|
# Create and start a new timer thread
|
||||||
timer = threading.Timer(delay, trigger_serial_command)
|
timer = threading.Timer(delay, trigger_serial_command)
|
||||||
@@ -106,27 +141,33 @@ if __name__ == '__main__':
|
|||||||
build_path = "~/Python/Automotive-Power-Simulator-App/LOGS/" + timestamp_str + ".txt"
|
build_path = "~/Python/Automotive-Power-Simulator-App/LOGS/" + timestamp_str + ".txt"
|
||||||
log_file = Path(build_path).expanduser()
|
log_file = Path(build_path).expanduser()
|
||||||
sys.stdout = Tee(log_file)
|
sys.stdout = Tee(log_file)
|
||||||
print("LOGGING TO: " + str(log_file))
|
print(f"[{time.strftime('%H:%M:%S')}] LOGGING TO: " + str(log_file))
|
||||||
|
|
||||||
|
SPD.write('CH1:VOLT ' + str(24.0)) # Send power supply source voltage
|
||||||
|
SPD.write('CH1:CURR ' + str(1.5)) # Send power supply current limit
|
||||||
|
|
||||||
|
# Enable power supply output source
|
||||||
|
SPD.write('OUTP CH1,ON') # Enable supply output
|
||||||
|
|
||||||
# Get time constants
|
# Get time constants
|
||||||
print ("PLEASE ENTER A MINIMUM DELAY VALUE...")
|
print (f"[{time.strftime('%H:%M:%S')}] PLEASE ENTER A MINIMUM DELAY VALUE...")
|
||||||
|
|
||||||
min_delay_input = input()
|
min_delay_input = input()
|
||||||
min_delay_input_f = float(min_delay_input)
|
min_delay_input_f = float(min_delay_input)
|
||||||
|
|
||||||
print("MINIMUM TIME DELAY: " + str(min_delay_input_f))
|
print(f"[{time.strftime('%H:%M:%S')}] MINIMUM TIME DELAY: " + str(min_delay_input_f))
|
||||||
|
|
||||||
# Get time constants
|
# Get time constants
|
||||||
print ("PLEASE ENTER A MAXIMUM DELAY VALUE...")
|
print (f"[{time.strftime('%H:%M:%S')}] PLEASE ENTER A MAXIMUM DELAY VALUE...")
|
||||||
|
|
||||||
max_delay_input = input()
|
max_delay_input = input()
|
||||||
max_delay_input_f = float(max_delay_input)
|
max_delay_input_f = float(max_delay_input)
|
||||||
|
|
||||||
print("MAXIMUM TIME DELAY: " + str(max_delay_input_f))
|
print("f[{time.strftime('%H:%M:%S')}] MAXIMUM TIME DELAY: " + str(max_delay_input_f))
|
||||||
|
|
||||||
# Start the very first trigger
|
# Start the very first trigger
|
||||||
trigger_serial_command()
|
trigger_serial_command()
|
||||||
# Setup serial port
|
set_voltage()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while (1):
|
while (1):
|
||||||
@@ -134,16 +175,17 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
state = 0
|
state = 0
|
||||||
command = 83 # 0x53 'S' ASCII
|
command = 115 # 0x73 's' ASCII
|
||||||
voltage_mv = 0
|
|
||||||
|
|
||||||
b1 = (voltage_mv >> 24) & 0xFF # Most Significant Byte
|
data = (command, state)
|
||||||
b2 = (voltage_mv >> 16) & 0xFF
|
|
||||||
b3 = (voltage_mv >> 8) & 0xFF
|
|
||||||
b4 = voltage_mv & 0xFF
|
|
||||||
|
|
||||||
data = (command, state, b1, b2, b3, b4)
|
|
||||||
byte_data = pack_integers_to_bytes(*data)
|
byte_data = pack_integers_to_bytes(*data)
|
||||||
ser.write(serial.to_bytes(byte_data))
|
#ser.write(serial.to_bytes(byte_data))
|
||||||
|
|
||||||
ser.close()
|
#ser.close()
|
||||||
|
|
||||||
|
SPD.write('CH1:VOLT ' + str(0.0)) # Send power supply source voltage
|
||||||
|
SPD.write('CH1:CURR ' + str(0.0)) # Send power supply current limit
|
||||||
|
|
||||||
|
# Disable power supply output source
|
||||||
|
SPD.write('OUTP CH1,OFF')
|
||||||
|
SPD.close()
|
||||||
Reference in New Issue
Block a user