diff options
Diffstat (limited to 'System/motor.py')
-rw-r--r--[-rwxr-xr-x] | System/motor.py | 9 |
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 |