mirror of
				https://github.com/KevinMidboe/python-gpiozero.git
				synced 2025-10-29 17:50:37 +00:00 
			
		
		
		
	Make Motors PWM devices with optional speed argument on methods
This commit is contained in:
		| @@ -253,10 +253,10 @@ robot = Robot(left=(4, 14), right=(17, 18)) | |||||||
|  |  | ||||||
| | Method | Description | Arguments | | | Method | Description | Arguments | | ||||||
| | ------ | ----------- | --------- | | | ------ | ----------- | --------- | | ||||||
| | `forward()` | Drive the robot forwards. | `seconds` - The number of seconds to drive for. If `None`, stay on. Default: `None` | | | `forward()`  | Drive the robot forwards.  | `speed` - Speed at which to drive the motors, `0` to `1`. Default: `1` | | ||||||
| | `backward()` | Drive the robot backwards. | `seconds` - The number of seconds to drive for. If `None`, stay on. Default: `None` | | | `backward()` | Drive the robot backwards. | `speed` - Speed at which to drive the motors, `0` to `1`. Default: `1` | | ||||||
| | `left()` | Make the robot turn left. | `seconds` - The number of seconds to turn for. If `None`, stay on. Default: `None` | | | `left()`     | Make the robot turn left.  | `speed` - Speed at which to drive the motors, `0` to `1`. Default: `1` | | ||||||
| | `right()` | Make the robot turn right. | `seconds` - The number of seconds to turn for. If `None`, stay on. Default: `None` | | | `right()`    | Make the robot turn right. | `speed` - Speed at which to drive the motors, `0` to `1`. Default: `1` | | ||||||
| | `stop()`     | Stop the robot.            | None | | | `stop()`     | Stop the robot.            | None | | ||||||
|  |  | ||||||
| ## Ryanteck MCB Robot | ## Ryanteck MCB Robot | ||||||
|   | |||||||
| @@ -161,6 +161,6 @@ motor = Motor(forward=17, back=18) | |||||||
|  |  | ||||||
| | Method | Description | Arguments | | | Method | Description | Arguments | | ||||||
| | ------ | ----------- | --------- | | | ------ | ----------- | --------- | | ||||||
| | `forward()` | Drive the motor forwards. | `seconds` - The number of seconds to stay on for. If `None`, stay on. Default: `None` | | | `forward()` | Drive the motor forwards. | `speed` - Speed at which to drive the motor, `0` to `1`. Default: `1` | | ||||||
| | `backward()` | Drive the motor backwards. | `seconds` - The number of seconds to stay on for. If `None`, stay on. Default: `None` | | | `backward()` | Drive the motor backwards. | `speed` - Speed at which to drive the motor, `0` to `1`. Default: `1` | | ||||||
| | `stop()` | Stop the motor. | None | | | `stop()` | Stop the motor. | None | | ||||||
|   | |||||||
| @@ -229,65 +229,76 @@ class Robot(object): | |||||||
|         self._left = Motor(forward=left_forward, back=left_back) |         self._left = Motor(forward=left_forward, back=left_back) | ||||||
|         self._right = Motor(forward=right_forward, back=right_back) |         self._right = Motor(forward=right_forward, back=right_back) | ||||||
|  |  | ||||||
|     def left(self, seconds=None): |         self._min_pwm = self._left._min_pwm | ||||||
|         """ |         self._max_pwm = self._left._max_pwm | ||||||
|         Make the robot turn left. If seconds given, stop after given number of |  | ||||||
|         seconds. |  | ||||||
|  |  | ||||||
|         seconds: `None` |     def forward(self, speed=1): | ||||||
|             Number of seconds to turn left for |  | ||||||
|         """ |         """ | ||||||
|         self._left.forward() |         Drive the robot forward. | ||||||
|         self._right.backward() |  | ||||||
|         if seconds is not None: |  | ||||||
|             sleep(seconds) |  | ||||||
|             self._left.stop() |  | ||||||
|             self._right.stop() |  | ||||||
|  |  | ||||||
|     def right(self, seconds=None): |         speed: `1` | ||||||
|  |             Speed at which to drive the motors, 0 to 1. | ||||||
|         """ |         """ | ||||||
|         Make the robot turn right. If seconds given, stop after given number of |         self._left._backward.off() | ||||||
|         seconds. |         self._right._backward.off() | ||||||
|  |  | ||||||
|         seconds: `None` |         self._left._forward.on() | ||||||
|             Number of seconds to turn right for |         self._right._forward.on() | ||||||
|         """ |         if speed < 1: | ||||||
|         self._right.forward() |             sleep(0.1)  # warm up the motors | ||||||
|         self._left.backward() |             self._left._forward.value = speed | ||||||
|         if seconds is not None: |             self._right._forward.value = speed | ||||||
|             sleep(seconds) |  | ||||||
|             self._left.stop() |  | ||||||
|             self._right.stop() |  | ||||||
|  |  | ||||||
|     def forward(self, seconds=None): |     def backward(self, speed=1): | ||||||
|         """ |         """ | ||||||
|         Drive the robot forward. If seconds given, stop after given number of |         Drive the robot backward. | ||||||
|         seconds. |  | ||||||
|  |  | ||||||
|         seconds: `None` |         speed: `1` | ||||||
|             Number of seconds to drive forward for |             Speed at which to drive the motors, 0 to 1. | ||||||
|         """ |         """ | ||||||
|         self._left.forward() |         self._left._forward.off() | ||||||
|         self._right.forward() |         self._right._forward.off() | ||||||
|         if seconds is not None: |  | ||||||
|             sleep(seconds) |  | ||||||
|             self._left.stop() |  | ||||||
|             self._right.stop() |  | ||||||
|  |  | ||||||
|     def backward(self, seconds=None): |         self._left._backward.on() | ||||||
|         """ |         self._right._backward.on() | ||||||
|         Drive the robot backward. If seconds given, stop after given number of |         if speed < 1: | ||||||
|         seconds. |             sleep(0.1)  # warm up the motors | ||||||
|  |             self._left._backward.value = speed | ||||||
|  |             self._right._backward.value = speed | ||||||
|  |  | ||||||
|         seconds: `None` |     def left(self, speed=1): | ||||||
|             Number of seconds to drive backward for |  | ||||||
|         """ |         """ | ||||||
|         self._left.backward() |         Make the robot turn left. | ||||||
|         self._right.backward() |  | ||||||
|         if seconds is not None: |         speed: `1` | ||||||
|             sleep(seconds) |             Speed at which to drive the motors, 0 to 1. | ||||||
|             self._left.stop() |         """ | ||||||
|             self._right.stop() |         self._right._backward.off() | ||||||
|  |         self._left._forward.off() | ||||||
|  |  | ||||||
|  |         self._right._forward.on() | ||||||
|  |         self._left._backward.on() | ||||||
|  |         if speed < 1: | ||||||
|  |             sleep(0.1)  # warm up the motors | ||||||
|  |             self._right._forward.value = speed | ||||||
|  |             self._left._backward.value = speed | ||||||
|  |  | ||||||
|  |     def right(self, speed=1): | ||||||
|  |         """ | ||||||
|  |         Make the robot turn right. | ||||||
|  |  | ||||||
|  |         speed: `1` | ||||||
|  |             Speed at which to drive the motors, 0 to 1. | ||||||
|  |         """ | ||||||
|  |         self._left._backward.off() | ||||||
|  |         self._right._forward.off() | ||||||
|  |  | ||||||
|  |         self._left._forward.on() | ||||||
|  |         self._right._backward.on() | ||||||
|  |         if speed < 1: | ||||||
|  |             sleep(0.1)  # warm up the motors | ||||||
|  |             self._left._forward.value = speed | ||||||
|  |             self._right._backward.value = speed | ||||||
|  |  | ||||||
|     def stop(self): |     def stop(self): | ||||||
|         """ |         """ | ||||||
|   | |||||||
| @@ -195,7 +195,7 @@ class PWMOutputDevice(DigitalOutputDevice): | |||||||
|     def value(self, n): |     def value(self, n): | ||||||
|         _min = self._min_pwm |         _min = self._min_pwm | ||||||
|         _max = self._max_pwm |         _max = self._max_pwm | ||||||
|         if _min >= n >= _max: |         if _min <= n <= _max: | ||||||
|             n *= 100 |             n *= 100 | ||||||
|         else: |         else: | ||||||
|             raise GPIODeviceError( |             raise GPIODeviceError( | ||||||
| @@ -289,22 +289,31 @@ class Motor(object): | |||||||
|         if not all([forward, back]): |         if not all([forward, back]): | ||||||
|             raise GPIODeviceError('forward and back pins must be provided') |             raise GPIODeviceError('forward and back pins must be provided') | ||||||
|  |  | ||||||
|         self._forward = OutputDevice(forward) |         self._forward = PWMOutputDevice(forward) | ||||||
|         self._backward = OutputDevice(back) |         self._backward = PWMOutputDevice(back) | ||||||
|  |  | ||||||
|     def forward(self): |         self._min_pwm = self._forward._min_pwm | ||||||
|  |         self._max_pwm = self._forward._max_pwm | ||||||
|  |  | ||||||
|  |     def forward(self, speed=1): | ||||||
|         """ |         """ | ||||||
|         Drive the motor forwards |         Drive the motor forwards | ||||||
|         """ |         """ | ||||||
|         self._forward.on() |         self._backward.value = self._min_pwm | ||||||
|         self._backward.off() |         self._forward.value = self._max_pwm | ||||||
|  |         if speed < 1: | ||||||
|  |             sleep(0.1)  # warm up the motor | ||||||
|  |             self._forward.value = speed | ||||||
|  |  | ||||||
|     def backward(self): |     def backward(self, speed=1): | ||||||
|         """ |         """ | ||||||
|         Drive the motor backwards |         Drive the motor backwards | ||||||
|         """ |         """ | ||||||
|         self._backward.on() |         self._forward.value = self._min_pwm | ||||||
|         self._forward.off() |         self._backward.value = self._max_pwm | ||||||
|  |         if speed < 1: | ||||||
|  |             sleep(0.1)  # warm up the motor | ||||||
|  |             self._backward.value = speed | ||||||
|  |  | ||||||
|     def stop(self): |     def stop(self): | ||||||
|         """ |         """ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user