aboutsummaryrefslogtreecommitdiffstats
path: root/System/motor.py
diff options
context:
space:
mode:
authorMatt Strapp <matt@mattstrapp.net>2022-01-27 15:29:31 -0600
committerMatt Strapp <matt@mattstrapp.net>2022-01-27 15:29:31 -0600
commit772cff67fd3b491d015ba89601a0a098c69edd69 (patch)
tree7c1f6b3110bb45913bc0b895c0754d5cefda52e0 /System/motor.py
parentAdd Uploads to the gitignore. (diff)
downloadee4511w-772cff67fd3b491d015ba89601a0a098c69edd69.tar
ee4511w-772cff67fd3b491d015ba89601a0a098c69edd69.tar.gz
ee4511w-772cff67fd3b491d015ba89601a0a098c69edd69.tar.bz2
ee4511w-772cff67fd3b491d015ba89601a0a098c69edd69.tar.lz
ee4511w-772cff67fd3b491d015ba89601a0a098c69edd69.tar.xz
ee4511w-772cff67fd3b491d015ba89601a0a098c69edd69.tar.zst
ee4511w-772cff67fd3b491d015ba89601a0a098c69edd69.zip
Add code that was not committed before
Diffstat (limited to 'System/motor.py')
-rw-r--r--[-rwxr-xr-x]System/motor.py9
1 files changed, 9 insertions, 0 deletions
diff --git a/System/motor.py b/System/motor.py
index b22dbe2..427d393 100755..100644
--- a/System/motor.py
+++ b/System/motor.py
@@ -2,6 +2,7 @@
# Import required modules
import RPi.GPIO as GPIO
+from time import sleep
# Constants: parameters that the caller cannot modify
# Frequency: We have determined that the optimal frequency for our motor is 1kHz
@@ -22,11 +23,15 @@ class Motor:
GPIO.setup(self.reverse_pin, GPIO.OUT)
# Set speed pin as a PWM output
self.speed_pwm = GPIO.PWM(speed_pin, pwm_frequency)
+ self.current_speed = 0.
# 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 == self.current_speed:
+ # do not attempt to readjust speed; this can cause erratic behavior
+ return
if speed < -100.0 or speed > 100.0:
return
# Stop any previous movements
@@ -43,6 +48,8 @@ class Motor:
GPIO.output(self.reverse_pin, GPIO.LOW)
# Start the PWM output to start moving the motor
self.speed_pwm.start(abs(speed))
+ self.current_speed = speed
+ sleep((1./pwm_frequency) * 3)
# END Move
# Stop the motor from spinning.
@@ -53,6 +60,7 @@ class Motor:
# Set the direction outputs to brake
GPIO.output(self.forward_pin, GPIO.HIGH)
GPIO.output(self.reverse_pin, GPIO.HIGH)
+ self.current_speed = 0.
# END Brake
# Set the motor to coast (i.e. Do not provide power to the motor, but still allow it to spin)
@@ -63,5 +71,6 @@ class Motor:
# Set the direction outputs to coast
GPIO.output(self.forward_pin, GPIO.LOW)
GPIO.output(self.reverse_pin, GPIO.LOW)
+ self.current_speed = 0.
# END Coast
\ No newline at end of file