|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# This file is part of FSE 2019. |
| 4 | +# |
| 5 | +# FSE 2019 is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# FSE 2019 is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with FSE 2019. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +"""Driver for 2 TB6612FNG DC motors mounted to a single chassis.""" |
| 19 | + |
| 20 | +import os |
| 21 | +import time |
| 22 | + |
| 23 | +import wiringpi |
| 24 | + |
| 25 | +from gpio_manager import GPIO_Manager |
| 26 | + |
| 27 | + |
| 28 | +class MotorDriver(GPIO_Manager): |
| 29 | + """Interface with 2 TB6612FNG DC Motor drivers.""" |
| 30 | + # Configure motor 1 |
| 31 | + _m1_dir1_pin = 6 |
| 32 | + _m1_dir2_pin = 12 |
| 33 | + _m1_pwm_pin_annex = 5 # solve error mapping (Vers.01) |
| 34 | + _m1_pwm_pin = 18 |
| 35 | + |
| 36 | + # Configure motor 2 |
| 37 | + _m2_dir1_pin = 19 |
| 38 | + _m2_dir2_pin = 16 |
| 39 | + _m2_pwm_pin_annex = 26 # solve error mapping (Vers.01) |
| 40 | + _m2_pwm_pin = 13 |
| 41 | + |
| 42 | + _standby_pin = 20 |
| 43 | + |
| 44 | + PWM_OUTPUTS = [_m1_pwm_pin, _m2_pwm_pin] |
| 45 | + |
| 46 | + INPUT_PINS = [_m1_pwm_pin_annex, _m2_pwm_pin_annex] |
| 47 | + |
| 48 | + OUTPUT_PINS = [_m1_dir1_pin, _m1_dir2_pin, |
| 49 | + _m2_dir1_pin, _m2_dir2_pin, |
| 50 | + _standby_pin] |
| 51 | + _pins = PWM_OUTPUTS + INPUT_PINS + OUTPUT_PINS |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def to_dc(dc): |
| 55 | + return (1023 * dc) / 100 |
| 56 | + |
| 57 | + def __init__(self): |
| 58 | + if os.getuid(): |
| 59 | + raise RuntimeError("MotorDriver can only be used by sudoer.") |
| 60 | + super(MotorDriver, self).__init__() |
| 61 | + for pin in self.OUTPUT_PINS: |
| 62 | + wiringpi.pinMode(pin, wiringpi.OUTPUT) |
| 63 | + |
| 64 | + for pin in self.PWM_OUTPUTS: |
| 65 | + wiringpi.pinMode(pin, wiringpi.PWM_OUTPUT) |
| 66 | + |
| 67 | + for pin in self.INPUT_PINS: |
| 68 | + wiringpi.pinMode(pin, wiringpi.INPUT) |
| 69 | + # Make sure all annex_pin are set LOW |
| 70 | + wiringpi.pullUpDnControl(pin, wiringpi.PUD_DOWN) |
| 71 | + |
| 72 | + def right_forward(self): |
| 73 | + """Drive right motor forward.""" |
| 74 | + wiringpi.digitalWrite(self._m1_dir1_pin, wiringpi.LOW) |
| 75 | + wiringpi.digitalWrite(self._m1_dir2_pin, wiringpi.HIGH) |
| 76 | + |
| 77 | + def right_back(self): |
| 78 | + """Drive right motor back.""" |
| 79 | + wiringpi.digitalWrite(self._m1_dir1_pin, wiringpi.HIGH) |
| 80 | + wiringpi.digitalWrite(self._m1_dir2_pin, wiringpi.LOW) |
| 81 | + |
| 82 | + def left_forward(self): |
| 83 | + """Drive left motor forward.""" |
| 84 | + wiringpi.digitalWrite(self._m2_dir1_pin, wiringpi.HIGH) |
| 85 | + wiringpi.digitalWrite(self._m2_dir2_pin, wiringpi.LOW) |
| 86 | + |
| 87 | + def left_back(self): |
| 88 | + """Drive left motor back.""" |
| 89 | + wiringpi.digitalWrite(self._m2_dir1_pin, wiringpi.LOW) |
| 90 | + wiringpi.digitalWrite(self._m2_dir2_pin, wiringpi.HIGH) |
| 91 | + |
| 92 | + def forward(self, duty_cycle=25): |
| 93 | + """Drive chassis forward.""" |
| 94 | + self.right_forward() |
| 95 | + self.left_forward() |
| 96 | + wiringpi.pwmWrite(self._m1_pwm_pin, self.to_dc(duty_cycle)) |
| 97 | + wiringpi.pwmWrite(self._m2_pwm_pin, self.to_dc(duty_cycle)) |
| 98 | + wiringpi.digitalWrite(self._standby_pin, wiringpi.HIGH) |
| 99 | + |
| 100 | + def reverse(self, duty_cycle=25): |
| 101 | + """Drive chassis back.""" |
| 102 | + self.right_back() |
| 103 | + self.left_back() |
| 104 | + wiringpi.pwmWrite(self._m1_pwm_pin, self.to_dc(duty_cycle)) |
| 105 | + wiringpi.pwmWrite(self._m2_pwm_pin, self.to_dc(duty_cycle)) |
| 106 | + wiringpi.digitalWrite(self._standby_pin, wiringpi.HIGH) |
| 107 | + |
| 108 | + def right(self, duty_cycle=25): |
| 109 | + """Turn chassis clockwise.""" |
| 110 | + self.right_forward() |
| 111 | + self.left_back() |
| 112 | + wiringpi.pwmWrite(self._m1_pwm_pin, self.to_dc(duty_cycle)) |
| 113 | + wiringpi.pwmWrite(self._m2_pwm_pin, self.to_dc(duty_cycle)) |
| 114 | + wiringpi.digitalWrite(self._standby_pin, wiringpi.HIGH) |
| 115 | + |
| 116 | + def left(self, duty_cycle=25): |
| 117 | + """Turn chassis counterclockwise.""" |
| 118 | + self.right_back() |
| 119 | + self.left_forward() |
| 120 | + wiringpi.pwmWrite(self._m1_pwm_pin, self.to_dc(duty_cycle)) |
| 121 | + wiringpi.pwmWrite(self._m2_pwm_pin, self.to_dc(duty_cycle)) |
| 122 | + wiringpi.digitalWrite(self._standby_pin, wiringpi.HIGH) |
| 123 | + |
| 124 | + def stop(self): |
| 125 | + """Stop chassis.""" |
| 126 | + for pin in self.PWM_OUTPUTS: |
| 127 | + wiringpi.pwmWrite(pin, 0) |
| 128 | + for pin in self.OUTPUT_PINS: |
| 129 | + wiringpi.digitalWrite(pin, wiringpi.LOW) |
| 130 | + |
| 131 | + def __exit__(self, *args): |
| 132 | + wiringpi.pwmWrite(self._m1_pwm_pin, 0) |
| 133 | + wiringpi.pwmWrite(self._m2_pwm_pin, 0) |
| 134 | + super(MotorDriver, self).__exit__() |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + with MotorDriver() as tb6612fng: |
| 139 | + while True: |
| 140 | + print "forward" |
| 141 | + tb6612fng.forward() |
| 142 | + time.sleep(3) |
| 143 | + |
| 144 | + tb6612fng.stop() |
| 145 | + time.sleep(3) |
| 146 | + |
| 147 | + print "reverse" |
| 148 | + tb6612fng.reverse() |
| 149 | + time.sleep(3) |
| 150 | + |
| 151 | + tb6612fng.stop() |
| 152 | + time.sleep(3) |
| 153 | + |
| 154 | + print "left" |
| 155 | + tb6612fng.left() |
| 156 | + time.sleep(3) |
| 157 | + |
| 158 | + tb6612fng.stop() |
| 159 | + time.sleep(3) |
| 160 | + |
| 161 | + print "right" |
| 162 | + tb6612fng.right() |
| 163 | + time.sleep(3) |
| 164 | + |
| 165 | + tb6612fng.stop() |
| 166 | + time.sleep(3) |
| 167 | + |
| 168 | + print "Done!" |
0 commit comments