aboutsummaryrefslogtreecommitdiffstats
path: root/System_Python/encoder.py
diff options
context:
space:
mode:
authorRaspberry Pi <raspberrypi@umn.edu>2019-11-16 23:03:46 -0600
committerRaspberry Pi <raspberrypi@umn.edu>2019-11-16 23:03:46 -0600
commitd851617a9df12ee6971de8d2c99e43a4d4fd6d27 (patch)
tree994ec9e71b98f66452253cb39650e496fb6c32d6 /System_Python/encoder.py
parentFixed some indentation issues and leftover from porting. Compiles and potenti... (diff)
downloadee4511w-d851617a9df12ee6971de8d2c99e43a4d4fd6d27.tar
ee4511w-d851617a9df12ee6971de8d2c99e43a4d4fd6d27.tar.gz
ee4511w-d851617a9df12ee6971de8d2c99e43a4d4fd6d27.tar.bz2
ee4511w-d851617a9df12ee6971de8d2c99e43a4d4fd6d27.tar.lz
ee4511w-d851617a9df12ee6971de8d2c99e43a4d4fd6d27.tar.xz
ee4511w-d851617a9df12ee6971de8d2c99e43a4d4fd6d27.tar.zst
ee4511w-d851617a9df12ee6971de8d2c99e43a4d4fd6d27.zip
Updated encoder to have an offset when zeroing (allows that initial zero can be set to upright instead of hanging). Updated system to allow for different angular units to be used (passed by argument on constructor, then passed to encoder.read_position when used). Bug fixes to system_swingup_test. Swingup test now runs properly (and almost actually did a swing up at one point), couldn't keep testing because the system accidentally destroyed itself, so called it time to stop to let glue dry....
Diffstat (limited to 'System_Python/encoder.py')
-rw-r--r--System_Python/encoder.py44
1 files changed, 23 insertions, 21 deletions
diff --git a/System_Python/encoder.py b/System_Python/encoder.py
index 8b7053c..63bc73e 100644
--- a/System_Python/encoder.py
+++ b/System_Python/encoder.py
@@ -14,59 +14,61 @@ class Encoder:
# Set the board IO (just in case it hasn't been done yet)
GPIO.setmode(GPIO.BCM)
# Setup class varaiable
- self.offset=0
+ self.offset = 0
self.clk_pin = clk_pin
self.cs_pin = cs_pin
self.data_pin = data_pin
# Setup the IO
try:
- GPIO.setup(self.clk_pin,GPIO.OUT)
- GPIO.setup(self.cs_pin,GPIO.OUT)
- GPIO.setup(self.data_pin,GPIO.IN)
+ GPIO.setup(self.clk_pin, GPIO.OUT)
+ GPIO.setup(self.cs_pin, GPIO.OUT)
+ GPIO.setup(self.data_pin, GPIO.IN)
# Setup the CS and CLK to be high
- GPIO.output(PIN_CLK,1)
- GPIO.output(PIN_CS,1)
+ GPIO.output(PIN_CLK, 1)
+ GPIO.output(PIN_CS, 1)
except:
- print("ERROR. Unable to setup the configuration required")
+ # If this fails, it's likely because the IO has already been configured. The encoders share some pins. Ignore the failure
+ time.sleep(0.01) # Used just to have something in the exception catch
+ #print("ERROR. Unable to setup the configuration required")
# Wait some time to before reading
time.sleep(0.5)
- def set_zero(self):
+ def set_zero(self, offset = 0):
# Take current position as zero
- self.offset=self.read_position('Raw')
+ self.offset = self.read_position('Raw') - offset
def clockup(self):
- GPIO.output(self.clk_pin,1)
+ GPIO.output(self.clk_pin, 1)
def clockdown(self):
- GPIO.output(self.clk_pin,0)
+ GPIO.output(self.clk_pin, 0)
def read_position(self, format):
# Most of this is based of timing diagram of encoder
# Pull CS low to start reading
- GPIO.output(self.cs_pin,0)
+ GPIO.output(self.cs_pin, 0)
# Delay necessary before reading is ready
time.sleep(delay*2)
data = 0
# Clockdown necessary before reading
self.clockdown()
# Go through 10 bits needed to read
- for i in range(0,10):
+ for i in range(0, 10):
# Clock up to start reading one bit
self.clockup()
# Shift data left and insert input
- data<<=1
- data|=GPIO.input(self.data_pin)
+ data <<= 1
+ data |= GPIO.input(self.data_pin)
# Clock down after finish reading
self.clockdown()
# Pull CS high after finish reading
- GPIO.output(self.cs_pin,1)
+ GPIO.output(self.cs_pin, 1)
# Format with offset, Max is 1024
- data=(data-self.offset)%1024
+ data = (data - self.offset) % 1024
# Data is linearly mapped
if format=="Raw":
return data
- elif format=="Degrees":
- degrees=(data/1024.0)*360.0
+ elif format == "Degrees":
+ degrees = (data/1024.0) * 360.0
return degrees
- elif format=="Radian":
- radians=(data/1024.0)*(2.0*math.pi)
+ elif format == "Radians":
+ radians = (data/1024.0) * (2.0*math.pi)
return radians
else:
print("ERROR. Invalid format (Raw, Degrees, Radians)")