k80-linux-cooling/Python/tx_speed.py

95 lines
2.8 KiB
Python
Raw Normal View History

2022-10-25 06:29:52 -05:00
import os
2022-10-22 18:19:16 -05:00
import GPUtil
import time
from pySerialTransfer import pySerialTransfer as txfer
2022-10-25 06:59:39 -05:00
from subprocess import getoutput
2022-10-22 18:19:16 -05:00
try:
# ---
# You will need to customize port to your own computer; to find your port:
# With your Arduino plugged in, open Arduino IDE, and click "Select Board". Then, you can see the port.
2022-10-25 06:59:39 -05:00
# The port of the Uno will be "/dev/ttyXXX#", like "/dev/ttyACM0" or "/dev/ttyUSB0". I think macOS is the same.
# Then, just remove "/dev/", and the number, from the port, and type it into the variable `port`
2022-10-22 18:19:16 -05:00
# ---
2022-10-25 06:29:52 -05:00
2022-10-25 06:59:39 -05:00
port = 'ttyACM'
2022-10-25 21:17:40 -05:00
while True:
2022-10-25 06:59:39 -05:00
ports = getoutput(f'ls /dev | grep {port}').split('\n')
2022-10-25 17:17:12 -05:00
if ports != ['']:
2022-10-25 06:59:39 -05:00
ports.sort()
2022-10-25 17:17:12 -05:00
port = ports[len(ports) - 1]
2022-10-25 21:17:40 -05:00
break
2022-11-01 17:30:01 -05:00
print('Waiting for Arduino...')
time.sleep(3)
2022-10-25 06:29:52 -05:00
2022-10-22 18:19:16 -05:00
link = txfer.SerialTransfer(port, 115200, timeout=.1)
link.open()
time.sleep(2)
while True:
time.sleep(1)
2022-10-25 18:56:46 -05:00
gpus = GPUtil.getGPUs()
2022-10-22 18:19:16 -05:00
temp = max(gpu.temperature for gpu in gpus)
speed = int(((temp - 40) / 40) * 100)
2022-10-23 10:22:12 -05:00
min_speed = 0
max_speed = 100
if speed < 0:
speed = min_speed
2022-10-22 18:19:16 -05:00
elif speed > 100:
2022-10-23 10:22:12 -05:00
speed = max_speed
2022-10-22 18:19:16 -05:00
print(f'Temp: {temp}')
print(f'Speed: {speed}')
################################################################################################################
# Send data to the Arduino
################################################################################################################
# send_size will be increased when data is added to payload
send_size = 0
# Adds data to payload
int_size = link.tx_obj(speed, send_size) - send_size
send_size += int_size
# Sends data to Arduino
link.send(send_size)
# Waits for response from Arduino, and reports errors while receiving packets
while not link.available():
if link.status < 0:
if link.status == txfer.CRC_ERROR:
print('Error: CRC_ERROR')
elif link.status == txfer.PAYLOAD_ERROR:
print('Error: PAYLOAD_ERROR')
elif link.status == txfer.STOP_BYTE_ERROR:
print('Error: STOP_BYTE_ERROR')
else:
print('Error: {}'.format(link.status))
# Parse response from Arduino
rec_int = link.rx_obj(obj_type=int, obj_byte_size=int_size, start_pos=(send_size - int_size))
# Evaluate for comm. errors
if speed != rec_int:
print(f'Error: sent {speed}, received {rec_int}')
except KeyboardInterrupt:
try:
link.close()
except:
pass
except:
import traceback
traceback.print_exc()
try:
link.close()
except:
pass