Python GUI not accepting Values from RTD sensor

Topics about the Software of Revolution Pi
Post Reply
pumpkin1131
Posts: 1
Joined: 15 Mar 2024, 19:24
Answers: 0

Python GUI not accepting Values from RTD sensor

Post by pumpkin1131 »

Hello,

I am trying to write a simple script that will create a python GUI to monitor some system voltage and current levels, along with the temperature. I figured I would start with the RTD sensor and get that working to make sure everything works.

I am using the Connect 4, with 1 AIO module. I have a simple program that is working, that reads 'RTDValue_1' and prints it to a python terminal. This mini program is working fine.

Below is the program I am having trouble with. for some reason the RTDValues do not want to be utilized with the GUI.
I am getting an error on line 43, saying AttributeError: module 'revpimodio2.io' has no attribute 'RTDValue_1'

import PySimpleGUI as sg
import time
import revpimodio2 as rpi

sg.theme('SystemDefault')

layout = [[sg.Txt('Select Desired Frequency: (1kHz-10kHz)'), sg.Slider(range=(1000,10000),default_value=1000, orientation='h', key=('frequency'), enable_events=True)],
[sg.Txt('Enter Desired Setpoint(1-100)'), sg.Slider(range=(1,100),default_value=1000, orientation='h', key=('-SETPOINT-'), enable_events=True)],
[sg.Button('Submit', bind_return_key=True)],
[sg.Txt('The first positive wave form lasts....'), sg.Txt(size=(8, 1), key='-OUTPUT1-')],
[sg.Txt('The first negative wave form lasts....'), sg.Txt(size=(8, 1), key='-OUTPUT2-')],
[sg.Txt('The rest period after the positive half is..'), sg.Txt(size=(8, 1), key='-OUTPUT3-')],
[sg.Txt('The rest period after the negitive half is..'), sg.Txt(size=(8, 1), key='-OUTPUT4-')],
[sg.Txt('Ambient Temperature: '), sg.Txt(size=(8, 1), key='RTDValue_1')],
[sg.Txt('Voltage Measurement: '), sg.Txt(size=(8, 1), key='Voltage_Input_1')],
[sg.Txt('Current Measurement: '), sg.Txt(size=(8, 1), key='Voltage_Input_2')]]

window = sg.Window('MFPC', layout, size=(1980,1080),element_justification='center',finalize=True)
window.Maximize()


while (True):
event, values = window.read(timeout=0)

if event != sg.WIN_CLOSED:

try:
# Inputs
frequency = int(values['frequency'])
if frequency < 1000 or frequency > 10000:
raise ValueError("Frequency must be between 1kHz and 10kHz")
setpoint = int(values['-SETPOINT-'])
if setpoint < 1 or setpoint > 100:
raise ValueError("Setpoint must be between 1 and 100")


# wave calculations
intermediate_setpoint = (setpoint / 100)
period = (1 / frequency)
voltage_on_time = (period * intermediate_setpoint) / 2
set_point_off = ((1 - intermediate_setpoint) * period) / 2

temper = rpi.io.RTDValue_1.value
print(temper)
x = input('Hello')
window['RTDValue_1'].update(temper)

except ValueError as e:
print(f"error:e")
else:
break
window.close()


If any python wizzards are out there any help would be great. I feel as if the program is very close.
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Python GUI not accepting Values from RTD sensor

Post by RevPiModIO »

Try the following code. Sorry about the new format, black reformatted it.

The only thing that was wrong was that you didn't use an instance of RevPiModIO. I now create an instance "rpi = revpimodio2.RevPiModIO()" in one line which runs without autorefresh, that's important because you have your own cycle in the While loop. In the While loop, I use "with rpi.io:", which reads the process image (i.e. all inputs) when entering. In the with-block I read the RTD value and set the variable. You could read even more inputs in the With block or even write outputs that are set when leaving the block. Within the block, neither the values of the inputs nor the outputs change.

You need at least version 2.7.1 of RevPiModIO2 for this! Which can be updated via "apt-get update && apt-get upgrade" if not already installed.

Code: Select all

import PySimpleGUI as sg
import revpimodio2

sg.theme("SystemDefault")

layout = [
    [
        sg.Txt("Select Desired Frequency: (1kHz-10kHz)"),
        sg.Slider(
            range=(1000, 10000),
            default_value=1000,
            orientation="h",
            key=("frequency"),
            enable_events=True,
        ),
    ],
    [
        sg.Txt("Enter Desired Setpoint(1-100)"),
        sg.Slider(
            range=(1, 100),
            default_value=1000,
            orientation="h",
            key=("-SETPOINT-"),
            enable_events=True,
        ),
    ],
    [sg.Button("Submit", bind_return_key=True)],
    [
        sg.Txt("The first positive wave form lasts...."),
        sg.Txt(size=(8, 1), key="-OUTPUT1-"),
    ],
    [
        sg.Txt("The first negative wave form lasts...."),
        sg.Txt(size=(8, 1), key="-OUTPUT2-"),
    ],
    [
        sg.Txt("The rest period after the positive half is.."),
        sg.Txt(size=(8, 1), key="-OUTPUT3-"),
    ],
    [
        sg.Txt("The rest period after the negitive half is.."),
        sg.Txt(size=(8, 1), key="-OUTPUT4-"),
    ],
    [sg.Txt("Ambient Temperature: "), sg.Txt(size=(8, 1), key="RTDValue_1")],
    [sg.Txt("Voltage Measurement: "), sg.Txt(size=(8, 1), key="Voltage_Input_1")],
    [sg.Txt("Current Measurement: "), sg.Txt(size=(8, 1), key="Voltage_Input_2")],
]

window = sg.Window(
    "MFPC", layout, size=(1980, 1080), element_justification="center", finalize=True
)
window.Maximize()

rpi = revpimodio2.RevPiModIO()

while True:
    event, values = window.read(timeout=0)

    if event != sg.WIN_CLOSED:

        try:
            # Inputs
            frequency = int(values["frequency"])
            if frequency < 1000 or frequency > 10000:
                raise ValueError("Frequency must be between 1kHz and 10kHz")
            setpoint = int(values["-SETPOINT-"])
            if setpoint < 1 or setpoint > 100:
                raise ValueError("Setpoint must be between 1 and 100")

            # wave calculations
            intermediate_setpoint = setpoint / 100
            period = 1 / frequency
            voltage_on_time = (period * intermediate_setpoint) / 2
            set_point_off = ((1 - intermediate_setpoint) * period) / 2

            # This will read all inputs, process your code and writes all outputs after leaving with block
            # Reminder: Inside the with block the IO values will NOT be refreshed
            with rpi.io:
                temper = rpi.io.RTDValue_1.value

            print(temper)
            window["RTDValue_1"].update(temper)

        except ValueError as e:
            print(f"error:e")
    else:
        break

window.close()
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
Post Reply