Page 1 of 1

Software PWM and read analog input at the same time

Posted: 29 Sep 2020, 09:17
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

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

Posted: 29 Sep 2020, 10:05
by dirk
Hi Ludwig I have moved your post to the RevPiModIO forum.

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

Posted: 01 Oct 2020, 03:39
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