From 772cff67fd3b491d015ba89601a0a098c69edd69 Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Thu, 27 Jan 2022 15:29:31 -0600 Subject: Add code that was not committed before --- System/motor.py | 9 +++++++++ 1 file changed, 9 insertions(+) mode change 100755 => 100644 System/motor.py (limited to 'System/motor.py') diff --git a/System/motor.py b/System/motor.py old mode 100755 new mode 100644 index b22dbe2..427d393 --- 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 -- cgit v1.2.3