aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--System_Python/encoder.py6
-rw-r--r--System_Python/motor.py6
-rw-r--r--System_Python/system.py68
-rw-r--r--System_Python/test_Encoder.py4
-rw-r--r--System_Python/test_System.py17
5 files changed, 93 insertions, 8 deletions
diff --git a/System_Python/encoder.py b/System_Python/encoder.py
index b70fac6..4069a86 100644
--- a/System_Python/encoder.py
+++ b/System_Python/encoder.py
@@ -30,14 +30,14 @@ class Encoder:
print("ERROR. Unable to setup the configuration required")
# Wait some time to before reading
time.sleep(0.5)
- def setZero(self):
+ def set_zero(self):
# Take current position as zero
- self.offset=self.readPosition('Raw')
+ self.offset=self.read_position('Raw')
def clockup(self):
GPIO.output(self.clk_pin,1)
def clockdown(self):
GPIO.output(self.clk_pin,0)
- def readPosition(self, format):
+ 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)
diff --git a/System_Python/motor.py b/System_Python/motor.py
index 0f7d891..b22dbe2 100644
--- a/System_Python/motor.py
+++ b/System_Python/motor.py
@@ -26,7 +26,7 @@ class Motor:
# Move the motor at a given speed, given as a floating point percentage (-100 <= x <= 100)
# If speed is less than 0, motor will run in reverse, otherwise it will run forward
- def Move(self, speed):
+ def move(self, speed):
if speed < -100.0 or speed > 100.0:
return
# Stop any previous movements
@@ -47,7 +47,7 @@ class Motor:
# Stop the motor from spinning.
# To brake the motor, both direction outputs are set to HIGH
- def Brake(self):
+ def brake(self):
# Stop any current PWM signals
self.speed_pwm.stop()
# Set the direction outputs to brake
@@ -57,7 +57,7 @@ class Motor:
# Set the motor to coast (i.e. Do not provide power to the motor, but still allow it to spin)
# To coast the motor, both direction outputs are set to LOW
- def Coast(self):
+ def coast(self):
# Stop any current PWM signals
self.speed_pwm.stop()
# Set the direction outputs to coast
diff --git a/System_Python/system.py b/System_Python/system.py
new file mode 100644
index 0000000..064700c
--- /dev/null
+++ b/System_Python/system.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+from motor import Motor
+from encoder import Encoder
+
+# IO pin definitions
+### Motor pins
+motor_speed_pin = 17
+motor_forward_pin = 27
+motor_reverse_pin = 22
+### Encoder pins (shared by both encoders)
+encoder_clock_pin = 2
+encoder_data_pin = 3
+### Angular encoder pins
+encoder_angular_cs_pin = 4
+### Linear encoder pins
+encoder_linear_cs_pin = 5
+
+
+# System Class
+# This is the primary interface a student will use to control the pendulum.
+class System:
+ def __init__(self):
+ # Initialize the motor.
+ self.motor = Motor(motor_speed_pin, motor_forward_pin, motor_reverse_pin)
+ # Initialize the angular encoder.
+ 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.set_zero()
+ # END __init__()
+
+ # Get the values of the encoders to determine the angular and linear position of the pendulum.
+ # Values are returned as a tuple: (angle, linear).
+ ### angle: 0 indicates the pendulum is exactly straight up.
+ ##### 180 or -180 indicate the pendulum is exactly straight down.
+ ##### Positive values indicate the pendulum is leaning to the right.
+ ##### Negative values indicate the pendulum is leaning to the left.
+ ### linear: 0 indicates the pendulum is exactly in the middle of the track.
+ ##### Positive values indicate the pendulum is right-of-center.
+ ##### Negative values indicate the pendulum is left-of-center.
+ def measure(self):
+ 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 = 0
+ return (angular_position, linear_position)
+ # END measure()
+
+ # Adjust the pendulum's linear position using the motor.
+ ### speed: Acceptable values range from -100 to 100 (as a percentage), with 100/-100 being the maximum adjustment speed.
+ ##### Negative values will move the pendulum to the left.
+ ##### Positive values will move the pendulum to the right.
+ def adjust(self, speed):
+ # cap the speed inputs
+ if speed > 100.0:
+ speed = 100.0
+ if speed < -100.0:
+ speed = -100.0
+ # change the motor speed
+ # TODO: Make sure the motor is oriented so that positive speed the correct direction (same for negative). Change the values otherwise.
+ self.motor.move(speed)
+ # END adjust()
+# END System
+ \ No newline at end of file
diff --git a/System_Python/test_Encoder.py b/System_Python/test_Encoder.py
index 08804fa..b667cc1 100644
--- a/System_Python/test_Encoder.py
+++ b/System_Python/test_Encoder.py
@@ -7,8 +7,8 @@ cs_pin = 4
data_pin = 3
e = Encoder(clk_pin, cs_pin, data_pin)
-e.setZero()
+e.set_zero()
while(1):
- print(e.readPosition('Degrees'))
+ print(e.read_position('Degrees'))
time.sleep(0.001) \ No newline at end of file
diff --git a/System_Python/test_System.py b/System_Python/test_System.py
new file mode 100644
index 0000000..af218e0
--- /dev/null
+++ b/System_Python/test_System.py
@@ -0,0 +1,17 @@
+# This test file implements a super simple control-type function for testing the System library.
+# DO NOT TEST ON ASSEMBLED PHYSICAL SYSTEM! It will probably break it.
+
+from system import System
+
+# Return a speed based on current encoder angle.
+# Convert an angle to speed (180 degrees = max speed)
+def control_function(angle):
+ return (angle / 180.0) * 100.0
+
+# Main program
+sys = System()
+while 1:
+ angle, linear = sys.measure()
+ speed = control_function(angle)
+ sys.adjust(speed)
+ \ No newline at end of file