aboutsummaryrefslogtreecommitdiffstats
path: root/System_Python/system.py
diff options
context:
space:
mode:
authordamic014 <damic014@umn.edu>2019-10-15 13:19:19 -0500
committerdamic014 <damic014@umn.edu>2019-10-15 13:19:19 -0500
commit8bc676964bad1d98c360193303101951fc5e3812 (patch)
tree48a0b63be4fc6d4505b7ec25da8f5cef80723c03 /System_Python/system.py
parentAdd System library and system test file. Still need to finalize some implemen... (diff)
downloadee4511w-8bc676964bad1d98c360193303101951fc5e3812.tar
ee4511w-8bc676964bad1d98c360193303101951fc5e3812.tar.gz
ee4511w-8bc676964bad1d98c360193303101951fc5e3812.tar.bz2
ee4511w-8bc676964bad1d98c360193303101951fc5e3812.tar.lz
ee4511w-8bc676964bad1d98c360193303101951fc5e3812.tar.xz
ee4511w-8bc676964bad1d98c360193303101951fc5e3812.tar.zst
ee4511w-8bc676964bad1d98c360193303101951fc5e3812.zip
Add a linear encoder class for abstracting the angular-to-linear encoding, which will be based on the specs of the system. This is the basic implementation we've talked about, but there might be some issues still because the position is only read when we tell it, so keeping track of number of rotations might not be very accurate - will need to test and possibly brainstorm alternate ideas.
Diffstat (limited to 'System_Python/system.py')
-rw-r--r--System_Python/system.py41
1 files changed, 37 insertions, 4 deletions
diff --git a/System_Python/system.py b/System_Python/system.py
index 064700c..d3a7ba4 100644
--- a/System_Python/system.py
+++ b/System_Python/system.py
@@ -26,7 +26,7 @@ class System:
self.encoder_angular = Encoder(encoder_clock_pin, encoder_angular_cs_pin, encoder_data_pin)
self.encoder_angular.set_zero()
# Initialize the linear encoder.
- self.encoder_linear = Encoder(encoder_clock_pin, encoder_linear_cs_pin, encoder_data_pin)
+ self.encoder_linear = Linear_Encoder(encoder_clock_pin, encoder_linear_cs_pin, encoder_data_pin)
self.encoder_linear.set_zero()
# END __init__()
@@ -43,9 +43,7 @@ class System:
angular_position = self.encoder_angular.read_position('Degrees')
if angular_position > 180:
angular_position = angular_position - 360
- # TODO: Implement linear position
- # Need to determine how to keep track of position based on gearing and rotations.
- #linear_position = self.encoder_linear.read_position('Raw')
+ #linear_position = self.encoder_linear.read_position()
linear_position = 0
return (angular_position, linear_position)
# END measure()
@@ -65,4 +63,39 @@ class System:
self.motor.move(speed)
# END adjust()
# END System
+
+# Linear Encoder class
+# This class is to help with using an absolute encoder for linear position sensing as assembled in the physical system.
+# The function definitions here are the same as with the regular encoder (pseudo-interface).
+class Linear_Encoder:
+ DIAMETER = 4 # MEASURE THIS
+
+ def __init__(self, clk_pin, cs_pin, data_pin):
+ self.encoder = Encoder(clk_pin, cs_pin, data_pin)
+ set_zero()
+ def set_zero(self):
+ # Set the zero position for the encoder
+ self.encoder.set_zero()
+ # Reset the internal position counter
+ self.rotations = 0
+ self.last_position = 0
+ def read_position(self):
+ # Read the position of the encoder
+ position = self.encoder.read_position('Raw')
+ # Compare to last known position
+ # NOTE: For now, assume that we are moving the smallest possible distance (i.e. 5 -> 1 is -4, not 1020)
+ if position - self.last_position > 0:
+ if position < 512 and self.last_position > 512:
+ # We are moving to the right (positive) and have completed a new rotation
+ self.rotations++
+ else:
+ if position > 512 and self.last_position < 512:
+ # We are moving to the left (negative) and have completed a new rotation
+ self.rotations--
+ # Save the last position for the next calculation
+ self.last_position = position
+
+ # compute the position based on the system parameters
+ # linear position = (2pi*r)(n) + (2pi*r)(position/1024) = (2pi*r)(n + position/1024) = (pi*d)(n + position/1024)
+ return (pi*DIAMETER)*(self.rotations + position/1024)
\ No newline at end of file