Go to Post Titanium is almost twice as heavy as aluminum. I can have the bragging aspect of making a bullet resistant robot, but if someone is coming into a competition and shooting my robot, I have more important things to worry about! - Andrew Blair [more]
Home
Go Back   Chief Delphi > Technical > Programming > Python
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 10-07-2014, 21:37
MCeGEAR MCeGEAR is offline
Registered User
FRC #1243
 
Join Date: Jan 2010
Location: US
Posts: 12
MCeGEAR is an unknown quantity at this point
Python Multi Processing

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.

Code:
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

Last edited by MCeGEAR : 11-07-2014 at 19:42. Reason: Forgot the code brackets
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 03:46.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi