Software PWM and read analog input at the same time
Posted: 29 Sep 2020, 09:17
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
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