Software PWM and read analog input at the same time

Moderator: RevPiModIO

Post Reply
lujakob
Posts: 4
Joined: 23 Jul 2020, 13:54
Answers: 0

Software PWM and read analog input at the same time

Post by lujakob »

Hey all,

I have problems with my PWM running and retrieving analog values at the same time.

I have connected a motor that I run through the PWM of the Revolution Pi. I let this motor run at different speeds and toggle it with a button.
I realized this with the following code (These are only excerpts):

import revpimodio2
class MyRevPiApp():

def event_flipflop_o3(self, ioname, iovalue):
self.rpi.io.O_3.value = not self.rpi.io.O_3.value
if self.rpi.io.O_3.value:
self.rpi.io.PWM_5.value = 20
else:
self.rpi.io.PWM_5.value = 70


Then I use a current sensor to measure the current going through the wire that drives the motor.
I obtain the values of the current sensor via an analog input of the Revolution Pis and retrieve them cyclically.
I realized this with the following code:

import sys
import revpimodio2
import threading
import paho.mqtt.client as mqtt
import time

rpi = revpimodio2.RevPiModIO(autorefresh=True)

import paho.mqtt.client as mqtt #import the client1
broker_address="192.168.0.198"
#broker_address="iot.eclipse.org" #use external broker
client = mqtt.Client("P1") #create new instance
client.connect(broker_address) #connect to broker
client.publish("topic/voltage","OFF")#publish

def printCurrentValue():
threading.Timer(1.0, printCurrentValue).start()
if rpi.io.InputValue_2.value < 2490:
print("Value = ", rpi.io.InputValue_2.value)
client.publish("topic/voltage", rpi.io.InputValue_2.value)
else:
print(2499)
client.publish("topic/voltage", rpi.io.InputValue_2.value)

printCurrentValue()


When I run my python scripts one at a time I have no problems and the PWM runs stable.
But as soon as I let the PWM run and then execute the script to query the analog readings, the PWM is unstable and the motor stutters.

Can someone explain to me what the reason for this is and how I can solve this problem?
Many thanks for the help:)
Regards
Ludwig
Online
User avatar
dirk
KUNBUS
Posts: 1925
Joined: 15 Dec 2016, 13:19
Answers: 4

Re: Software PWM and read analog input at the same time

Post by dirk »

Hi Ludwig I have moved your post to the RevPiModIO forum.
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Software PWM and read analog input at the same time

Post by RevPiModIO »

Hi Ludwig!

If you are using RevPiModIO, it will force the IO values, which are set in your python program to the process image. This could be very important, if you are controlling a plant with the system. So RevPiModIO is the master of your process image.

If you are starting two programs with RevPiModIO, each instance will force there values. So, if you are setting the value of PWM_1 to 30 in program A, the program B will reset PWM_1 to 0, because there was no change of the value. After that, program A will reset the value back to 30 again, and so on.

What can you do:
1) Rewrite your control program and integrate the complete logic to the one program (safest way)
or
2) Set the shared_procimg=True switch in your .RevPiModIO(autorefresh=True, shared_procimg=True) call.

Regards
Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
Post Reply