diff options
author | Raspberry Pi <pi@umn.edu> | 2019-09-24 16:57:14 -0500 |
---|---|---|
committer | Raspberry Pi <pi@umn.edu> | 2019-09-24 16:57:14 -0500 |
commit | 1e298b701fbc1c1048122efc5181fb37eeba2829 (patch) | |
tree | c523f9a6a4cf044846fa2d4a721f9346c0106950 | |
parent | Add motor test file. Add __init__.py to allow library imports. (diff) | |
download | ee4511w-1e298b701fbc1c1048122efc5181fb37eeba2829.tar ee4511w-1e298b701fbc1c1048122efc5181fb37eeba2829.tar.gz ee4511w-1e298b701fbc1c1048122efc5181fb37eeba2829.tar.bz2 ee4511w-1e298b701fbc1c1048122efc5181fb37eeba2829.tar.lz ee4511w-1e298b701fbc1c1048122efc5181fb37eeba2829.tar.xz ee4511w-1e298b701fbc1c1048122efc5181fb37eeba2829.tar.zst ee4511w-1e298b701fbc1c1048122efc5181fb37eeba2829.zip |
Tested on RPi and minor changes to make it work.
Diffstat (limited to '')
-rw-r--r-- | System_Python/motor.py | 67 | ||||
-rw-r--r-- | System_Python/test_Motor.py | 26 |
2 files changed, 80 insertions, 13 deletions
diff --git a/System_Python/motor.py b/System_Python/motor.py new file mode 100644 index 0000000..0f7d891 --- /dev/null +++ b/System_Python/motor.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python
+
+# Import required modules
+import RPi.GPIO as GPIO
+
+# Constants: parameters that the caller cannot modify
+# Frequency: We have determined that the optimal frequency for our motor is 1kHz
+pwm_frequency = 1000
+
+# Motor Class
+# This controls the motor at the given IO
+class Motor:
+ def __init__(self, speed_pin, forward_pin, reverse_pin):
+ # Set the board IO (just in case it hasn't been done yet)
+ GPIO.setmode(GPIO.BCM)
+ # Set our variables for the directional pins
+ self.forward_pin = forward_pin
+ self.reverse_pin = reverse_pin
+ # setup the IO
+ GPIO.setup(speed_pin, GPIO.OUT)
+ GPIO.setup(self.forward_pin, GPIO.OUT)
+ GPIO.setup(self.reverse_pin, GPIO.OUT)
+ # Set speed pin as a PWM output
+ self.speed_pwm = GPIO.PWM(speed_pin, pwm_frequency)
+ # END __init__
+
+ # 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):
+ if speed < -100.0 or speed > 100.0:
+ return
+ # Stop any previous movements
+ self.speed_pwm.stop()
+ # Set the duty cycle for the speed of the motor
+ self.speed_pwm.ChangeDutyCycle(abs(speed))
+ if speed < 0:
+ # Set direction to reverse
+ GPIO.output(self.forward_pin, GPIO.LOW)
+ GPIO.output(self.reverse_pin, GPIO.HIGH)
+ else:
+ # Set the direction to forward
+ GPIO.output(self.forward_pin, GPIO.HIGH)
+ GPIO.output(self.reverse_pin, GPIO.LOW)
+ # Start the PWM output to start moving the motor
+ self.speed_pwm.start(abs(speed))
+ # END Move
+
+ # Stop the motor from spinning.
+ # To brake the motor, both direction outputs are set to HIGH
+ def Brake(self):
+ # Stop any current PWM signals
+ self.speed_pwm.stop()
+ # Set the direction outputs to brake
+ GPIO.output(self.forward_pin, GPIO.HIGH)
+ GPIO.output(self.reverse_pin, GPIO.HIGH)
+ # END Brake
+
+ # 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):
+ # Stop any current PWM signals
+ self.speed_pwm.stop()
+ # Set the direction outputs to coast
+ GPIO.output(self.forward_pin, GPIO.LOW)
+ GPIO.output(self.reverse_pin, GPIO.LOW)
+ # END Coast
+
\ No newline at end of file diff --git a/System_Python/test_Motor.py b/System_Python/test_Motor.py index f14af59..600f209 100644 --- a/System_Python/test_Motor.py +++ b/System_Python/test_Motor.py @@ -2,22 +2,22 @@ from motor import Motor import time
# Decide which pins to hook up to on the Pi before running
-speed_pin = 15
-forward_pin = 16
-reverse_pin = 17
+speed_pin = 17
+forward_pin = 27
+reverse_pin = 22
m = Motor(speed_pin, forward_pin, reverse_pin)
dir = 'ascending'
speed = 0.0
while 1:
- m.Move(speed)
- if speed >= 100.0:
- dir = 'descending'
- elif speed <= -100.0:
- dir = 'ascending'
- if dir == 'ascending':
- speed = speed + 0.5
- else:
- speed = speed - 0.5
- time.sleep(0.1)
\ No newline at end of file + m.Move(speed)
+ if speed >= 100.0:
+ dir = 'descending'
+ elif speed <= -100.0:
+ dir = 'ascending'
+ if dir == 'ascending':
+ speed = speed + 0.5
+ else:
+ speed = speed - 0.5
+ time.sleep(0.1)
\ No newline at end of file |