Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino script upload #160

Open
wants to merge 17 commits into
base: DEV
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions Gui/QtGUIutils/QtApplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

from Gui.python.logging_config import logger

import subprocess


class QtApplication(QWidget):
Expand Down Expand Up @@ -594,7 +595,20 @@ def createMain(self):
self.relay_model_combo.addItems(InstrumentCluster.package_map.keys())
self.relay_model_status = QLabel()
self.relay_remote_control = QCheckBox("Use relay")
self.relay_remote_control.setChecked(False)
self.relay_ScriptUpload = QPushButton("&Upload Script")
self.relay_remote_control.setChecked(False)


try:
#self.relay_ScriptUpload.clicked.connect(self.uploadScript(self.relay_port_combobox.currentText()))
if self.relay_port_combobox.currentText() != None:
self.relay_ScriptUpload.clicked.connect(self.uploadScript)
except Exception as e:
print(self.relay_port_combobox.currentText())
CollinKa marked this conversation as resolved.
Show resolved Hide resolved
print(f"Unexpected error: {e}")
print("relay box ScriptUpload error")


self.relay_remote_control.toggled.connect(lambda: self.enableDevice("relay"))
self.relay_port_combobox.activated.connect(
lambda: self.update_instrument_info(
Expand All @@ -611,12 +625,13 @@ def createMain(self):
if self.relay_remote_control.isChecked()
else self.relay_group.setDisabled(True)
)

relay_layout.addWidget(relay_board_port_label)
relay_layout.addWidget(self.relay_port_combobox)
relay_layout.addWidget(self.relay_model_label)
relay_layout.addWidget(self.relay_model_combo)
relay_layout.addWidget(self.relay_model_status)
relay_layout.addWidget(self.relay_ScriptUpload)
relay_layout.addStretch(1)
self.relay_group.setLayout(relay_layout)

Expand Down Expand Up @@ -838,6 +853,43 @@ def createMain(self):
"relay": self.relay_group,
"multimeter": self.multimeter_group,
}

def test(self):
CollinKa marked this conversation as resolved.
Show resolved Hide resolved
print("do not things")

def uploadScript(self):
try:

#temporary solution for dealing with arduino:avr can't be found inside docker
CollinKa marked this conversation as resolved.
Show resolved Hide resolved
compileResult=subprocess.run(
"$GUI_dir/bin/arduino-cli compile --fqbn arduino:avr:uno $GUI_dir/FirmwareImages/relay_box_firmware/",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
portName = self.relay_port_combobox.currentText().lstrip("ASRL").rstrip("::INSTR")
#portName = "/dev/ttyACM0"
uploadResult=subprocess.run(
f"$GUI_dir/bin/arduino-cli upload -p {portName} --fqbn arduino:avr:uno $GUI_dir/FirmwareImages/relay_box_firmware/",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
print(compileResult.stdout)
CollinKa marked this conversation as resolved.
Show resolved Hide resolved
print(uploadResult.stdout)
if "New upload port" in str(uploadResult.stdout):
print("upload completed")
print(compileResult.stderr)
print(uploadResult.stderr)

except subprocess.CalledProcessError as err:
print("current port:" + str(portName))
print("please check the port addrss")
print("Unable to upload script to Arduino")
logger.error("Unable to upload script to Arduino")
logger.error(compileResult.stderr)
logger.error(uploadResult.stderr)


def setDefault(self):
if self.expertMode is False:
Expand Down
120 changes: 44 additions & 76 deletions Gui/python/ArduinoWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import pyvisa as visa
import subprocess
import numpy as np
import Gui.siteSettings as site_settings
from Gui.python.logging_config import logger



class ArduinoWidget(QWidget):
stop = pyqtSignal()

Expand Down Expand Up @@ -71,114 +73,80 @@ def createArduino(self):
self.UseArduino.clicked.connect(self.frozeArduinoPanel)
self.ReleaseArduino = QPushButton("&Release")
self.ReleaseArduino.clicked.connect(self.releaseArduinoPanel)
self.TempScriptUpload = QPushButton("&Upload script")
self.TempScriptUpload.clicked.connect(self.TemScriptUploder)
self.ReleaseArduino.setDisabled(True)

self.ArduinoBox.addWidget(self.ArduinoStatusLabel)
self.ArduinoBox.addWidget(self.ArduinoCombo)
self.ArduinoBox.addWidget(self.ArduinoBaudRate)
self.ArduinoBox.addWidget(self.ArduinoBRCombo)
# self.ArduinoBox.addWidget(self.ArduinoValues)
self.ArduinoBox.addStretch(1)
self.ArduinoBox.addWidget(self.UseArduino)
self.ArduinoBox.addWidget(self.ReleaseArduino)
self.ArduinoBox.addWidget(self.TempScriptUpload)
self.ArduinoGroup.setLayout(self.ArduinoBox)
self.mainLayout.addWidget(self.ArduinoGroup, 0, 0)

self.ArduinoMeasureValue = QLabel()
self.mainLayout.addWidget(self.ArduinoMeasureValue, 1, 0)


def listResources(self):
self.ResourcesManager = visa.ResourceManager("@py")
self.ResourcesList = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was removed? Do we not actually need it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listResources() is needed. It is used for getting Arduino communication port from site_settings.

self.ResourcesList.append(site_settings.TemArduinoPort)
return self.ResourcesList


def TemScriptUploder(self):

try:
self.ResourcesList = self.ResourcesManager.list_resources()
print(self.ResourcesList)
self.getDeviceName()
return list(self.deviceMap.keys())
except Exception as err:
logger.error("Failed to list all resources: {}".format(err))
self.ResourcesList = ()
return self.ResourcesList

def getDeviceName(self):
self.deviceMap = {}
for device in self.ResourcesList:
try:
pipe = subprocess.Popen(
[
"udevadm",
"info",
" --query",
"all",
"--name",
device.lstrip("ASRL").rstrip("::INSTR"),
"--attribute-walk",
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
raw_output = pipe.communicate()[0]
vendor_list = [
info
for info in raw_output.splitlines()
if b"ATTRS{idVendor}" in info or b"ATTRS{vendor}" in info
]
product_list = [
info
for info in raw_output.splitlines()
if b"ATTRS{idProduct}" in info or b"ATTRS{product}" in info
]
idvendor = (
vendor_list[0]
.decode("UTF-8")
.split("==")[1]
.lstrip('"')
.rstrip('"')
.replace("0x", "")
)
idproduct = (
product_list[0]
.decode("UTF-8")
.split("==")[1]
.lstrip('"')
.rstrip('"')
.replace("0x", "")
)
deviceId = "{}:{}".format(idvendor, idproduct)
pipeUSB = subprocess.Popen(
["lsusb", "-d", deviceId],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
usbInfo = pipeUSB.communicate()[0]
deviceName = usbInfo.decode("UTF-8").split(deviceId)[-1].lstrip(" ")

if deviceName == None:
logger.warning("No device name found for {}:".format(device))
# self.deviceMap[device] = device
elif "Arduino" in deviceName:
self.deviceMap[deviceName] = device
else:
pass
except Exception as err:
logger.error("Error found:{}".format(err))
# self.deviceMap[device] = device
compileResult=subprocess.run(
"$GUI_dir/bin/arduino-cli compile --fqbn arduino:avr:uno $GUI_dir/FirmwareImages/relay_box_firmware/",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there the exact same code in 2 places?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "upload script" at Arduinowidget is for uploading the temperature monitoring script. And the one at QtApplication is for uploading the relay box script. The reason why I can't use "uploade script" in QtAplication for uploading temperature monitor script is that the button "Upload script" inside Arduinowidget can only connect the self function inside class ArduinoWidget.

shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
deviceName = self.ArduinoCombo.currentText()
portName = deviceName.lstrip("ASRL").rstrip("::INSTR")
uploadResult=subprocess.run(
f"$GUI_dir/bin/arduino-cli upload -p {portName} --fqbn arduino:avr:uno $GUI_dir/FirmwareImages/relay_box_firmware/",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)


if "New upload port" in str(uploadResult.stdout):
print("relay box script upload completed")


except subprocess.CalledProcessError as err:
logger.error("Unable to TM upload script to Arduino")
logger.error(compileResult.stderr)
logger.error(uploadResult.stderr)


def frozeArduinoPanel(self):
# Block for ArduinoSupply operation



try:

self.setSerial(
self.deviceMap[self.ArduinoCombo.currentText()],
self.ArduinoCombo.currentText(),
self.ArduinoBRCombo.currentText(),
)
print(self.deviceMap[self.ArduinoCombo.currentText()])
self.ArduinoCombo.setDisabled(True)
self.ArduinoBRCombo.setDisabled(True)
self.UseArduino.setDisabled(True)
self.ReleaseArduino.setDisabled(False)
except Exception as err:
logger.error("Unable to use Arduino")
self.ArduinoGoodStatus = False

def releaseArduinoPanel(self):
self.serial.close()
self.ArduinoCombo.setDisabled(False)
Expand Down
6 changes: 6 additions & 0 deletions Gui/siteConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
'fc7.board.1' : '192.168.1.80',
'fc7.board.2' : '192.168.1.81',
}
##############################################
#arduinot board for monitor testing temperature
TemArduinoPort = "ASRL/dev/ttyACM0::INSTR"




##############################################

Expand Down
8 changes: 7 additions & 1 deletion compileSubModules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ fi;
cd $GUI_dir

echo "-----GUI Environment Setup Complete-----"
echo " "
echo " "

#download and save arduino command line interface into the folder $PWD/bin
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
#download the compilation tool-kit for UNO board
$PWD/bin/arduino-cli core install arduino:avr