Hello All,
I need some help with multi-processing in python on the bot. I would use multi threading but because of the global lock in CPython that isn’t doable, or at least that is what i assume is what is blocking its use i could be just completely wrong and miss using the threading.
None-the-less, here is a class i wrote up to get rate from a infinite turn pot hooked up to a roller, it works as long as it is ran fast enough, other wise it will miss a turn or two and give out some funky data, it isn’t very clean and doesn’t work on the bot because of no multi-threading support.
If i could get some help it would be great, I would prefer to get multi-processing working, but if i could have it run under a timed event properly instead that would be fine as well.
Thank you all very much the code is below, feel free to take and use or re-write it any way you feel.
class potRate():
def __init__(self, potObj, timerObj):
self.rate = 0.0
self.stopThread = False
self.lastRate = 0.0
self.lastPotVal = potObj.GetVoltage()
self.potObj = potObj
self.timerObj = timerObj
self.timerObj.Start()
self.timerObj.Reset()
def calcValue(self):
while not self.stopThread:
try:
#This tells us how much it has traveled since it was last looked at by comparing last to current
Rate = 0.0
lastRate = self.rate
potVal = self.potObj.GetVoltage()
potDiff = (self.potObj.GetVoltage() - self.lastPotVal)
#The pots give some jumpy data so this is a very basic filter that seprates out any garbage in readings
if (abs(potDiff) >= 0.02):
#here we actually calulate out the rate, rate is just distance travled over time, we time how fast the pot moved
#Becuase of the way the shooter works, we don't want negitive values
if (potDiff > 0):
Rate = potDiff/self.timerObj.Get()
Rate = (Rate + lastRate)/2
else:
pass
except ZeroDivisionError:
Rate = 0.0
print("RPM error zero divison")
except:
print("RPM error unhaneled")
self.stopThread = True
self.rate = Rate
self.lastPotVal = potVal
self.timerObj.Reset()
def Start(self):
self.stopThread = False
self.rateThread = threading.Thread(target=self.calcValue, args = ())
self.rateThread.daemon = True
self.rateThread.start()
def Stop(self):
self.stopThread = True
self.rateThread.join()
def getRate(self):
return self.rate