RTD Value

Moderator: RevPiModIO

Post Reply
troll100
Posts: 31
Joined: 08 Jan 2020, 11:46

RTD Value

Post by troll100 »

Hello,
i am trying to print the current value of the RTDValue_1 every millisecond, and in the end save the values in a csv sheet.

the code i used:

import revpimodio2
import threading

rpi=revpimodio2.RevPiModIO(autorefresh=True)
temp=rpi.io.RTDValue_1.value
def printit():
threading.Timer(0.1,printit).start()
print(temp)
printit()


When i run m programm the rtd value just stays the same and does not increase or decrease if i heat it up.

Best regards,
troll100
User avatar
nicolaiB
KUNBUS
Posts: 931
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: RTD Value

Post by nicolaiB »

Hi,

this behavior is correct, as the process image is never refreshed. RevPiModIO has a some nice helper funtions for cyclic tasks:

Code: Select all

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import revpimodio2

class RTDDemo():
    def __init__(self):
        self.rpi = revpimodio2.RevPiModIO(autorefresh=True)
        self.rpi.handlesignalend()

        # call self.loop() on each cycle (cycletime is configurable)
        self.cycleloop(self.loop)

    def cycle(self):
        print("RTD value: {}".format(self.rpi.io.RTDValue_1.value)

if __name__ == "__main__":
    root = RTDDemo()
    root.start()
Depending on your needs it might be better to use an event based approach. For more details have a look into the examples in the RevPiModIO documentation.
Nicolai
troll100
Posts: 31
Joined: 08 Jan 2020, 11:46

Re: RTD Value

Post by troll100 »

when i run your Code exactly as you wrote it down, i always get an syntaxerror in the "if __name___ == "main":"
I am not familiar with classes and functions in python :oops:
User avatar
nicolaiB
KUNBUS
Posts: 931
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: RTD Value

Post by nicolaiB »

I see that I missed a closing bracket in the line with the print statement. Should be

Code: Select all

def cycle(self):
        print("RTD value: {}".format(self.rpi.io.RTDValue_1.value)) 
troll100
Posts: 31
Joined: 08 Jan 2020, 11:46

Re: RTD Value

Post by troll100 »

okay, so how i now call the method ccycle that prints the value of RTD...?
e.g.
a=RTDDemo.cycle()
print(a) does not work. cause cycle needs a positional argument. but what kind of argument :/
User avatar
nicolaiB
KUNBUS
Posts: 931
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: RTD Value

Post by nicolaiB »

My fault, here is hopefully the correct version:

Code: Select all

def cycle(self, ct):
        print("RTD value: {}".format(self.rpi.io.RTDValue_1.value)) 
troll100
Posts: 31
Joined: 08 Jan 2020, 11:46

Re: RTD Value

Post by troll100 »

Now i get an Attribute error… RTDDemo has no Attribute rpi …
:|

how would you call the method that gives you the rdtvalue ?

I think im too stupid to get it ... :?:
User avatar
nicolaiB
KUNBUS
Posts: 931
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: RTD Value

Post by nicolaiB »

Hi,

there were some additional mistakes in the code, because I have written it on the mobile. So here is a tested snippet, which also has some comments:

Code: Select all

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import revpimodio2

class RTDDemo():
    def __init__(self):
        # create new instance of revpimodio2
        self.rpi = revpimodio2.RevPiModIO(autorefresh=True)

        # handle ctrl+c and ensure a clean shutdown
        self.rpi.handlesignalend()

        # start main loop, which calls self.cycle() on each cycle (cycletime is configurable)
        self.rpi.cycleloop(self.cycle)

    def cycle(self, ct):
        # read value of RTDValue_1
        value = self.rpi.io.RTDValue_1.value

        # print value to stdout
        print("RTD value: {}".format(value))

if __name__ == "__main__":
    # create new instance of the RTDDemo class above
    app = RTDDemo()
If you want to process the data further, I would recommend to extend the cycle() method.

Nicolai
troll100
Posts: 31
Joined: 08 Jan 2020, 11:46

Re: RTD Value

Post by troll100 »

Hey Nicolai,
thanks for your efforts.

Now the pogramm runs; and the temp in the Output Changes.
But only when i define rpi = revpimodio2.RevPiModIO(autorefresh=True) before the class RTDDemo()
Witout this i get the traceback RTDDemo hast no attribute rpi.
Post Reply