|
| 1 | +import sys |
| 2 | +import logging |
| 3 | +from smbus2 import SMBus |
| 4 | + |
| 5 | +from robocon.brain.cytron import CytronBoard |
| 6 | +from robocon.brain.greengiant import ( |
| 7 | + GreenGiantInternal, |
| 8 | + GreenGiantGPIOPinList, |
| 9 | + GreenGiantMotors, |
| 10 | + _GG_SERVO_PWM_BASE, |
| 11 | + _GG_GPIO_PWM_BASE, |
| 12 | + _GG_GPIO_GPIO_BASE, |
| 13 | + _GG_SERVO_GPIO_BASE) |
| 14 | + |
| 15 | +_logger = logging.getLogger("robot_io") |
| 16 | + |
| 17 | +def setup_logging(level): |
| 18 | + """Display the just the message when logging events |
| 19 | + Sets the logging level to `level`""" |
| 20 | + _logger.setLevel(level) |
| 21 | + |
| 22 | + handler = logging.StreamHandler(sys.stdout) |
| 23 | + handler.setLevel(level) |
| 24 | + |
| 25 | + fmt = logging.Formatter("%(message)s") |
| 26 | + handler.setFormatter(fmt) |
| 27 | + |
| 28 | + _logger.addHandler(handler) |
| 29 | + |
| 30 | +class IO(): |
| 31 | + _initialised = False |
| 32 | + |
| 33 | + def __init__(self, max_motor_voltage=6, enable_12v=True, enable_5v=True, log_level=logging.INFO): |
| 34 | + self._max_motor_voltage = max_motor_voltage |
| 35 | + |
| 36 | + self._initialised = False |
| 37 | + self._warnings = [] |
| 38 | + |
| 39 | + setup_logging(log_level) |
| 40 | + |
| 41 | + if type(self)._initialised: |
| 42 | + raise RuntimeError("IO object acquires hardware locks for its" |
| 43 | + " sole use and so can only be used once.") |
| 44 | + |
| 45 | + self.bus = SMBus(1) |
| 46 | + self._green_giant = GreenGiantInternal(self.bus) |
| 47 | + self._gg_version = self._green_giant.get_version() |
| 48 | + if self._gg_version >= 10: |
| 49 | + # enable power rails |
| 50 | + self._green_giant.set_motor_power(True) |
| 51 | + self.enable_12v = enable_12v |
| 52 | + self.enable_5v = enable_5v |
| 53 | + self._adc_max = 5 |
| 54 | + # configure User IO Ports |
| 55 | + self.servos = GreenGiantGPIOPinList(self.bus, self._gg_version, self._adc_max, _GG_SERVO_GPIO_BASE, _GG_SERVO_PWM_BASE) |
| 56 | + self.gpio = GreenGiantGPIOPinList(self.bus, self._gg_version, self._adc_max, _GG_GPIO_GPIO_BASE, _GG_GPIO_PWM_BASE) |
| 57 | + # configure motor drivers |
| 58 | + self.motors = GreenGiantMotors(self.bus, self._max_motor_voltage) |
| 59 | + ## thinks, perhaps this should be inherrent to using the motors and |
| 60 | + ## open load detection can be in there? |
| 61 | + self.motors.enable_motors(True) |
| 62 | + else: |
| 63 | + # power rails |
| 64 | + self._green_giant.set_motor_power(True) |
| 65 | + self._adc_max = self._green_giant.get_fvr_reading() |
| 66 | + # user IO |
| 67 | + self.servos = GreenGiantGPIOPinList(self.bus, self._gg_version, None, None, _GG_SERVO_PWM_BASE) |
| 68 | + self.gpio = GreenGiantGPIOPinList(self.bus, self._gg_version, self._adc_max, _GG_GPIO_GPIO_BASE , None) |
| 69 | + # configure motor drivers |
| 70 | + self.motors = CytronBoard(self._max_motor_voltage) |
| 71 | + |
| 72 | + type(self)._initialised = True |
| 73 | + |
| 74 | + @property |
| 75 | + def enable_motors(self): |
| 76 | + """Return if motors are currently enabled |
| 77 | +
|
| 78 | + For the GG board this will be the state of the 12v line, which we cannot query, |
| 79 | + so return what it was set to. |
| 80 | +
|
| 81 | + For the PiLow series the Motors have both a power control and a enable. Generally |
| 82 | + the Power should not be switched on and off, just the enable bits. The power may |
| 83 | + be tripped in extreme circumstances. I guess that here we want to report any |
| 84 | + reason for the motors not working, which includes power and enable |
| 85 | +
|
| 86 | + """ |
| 87 | + if self._gg_version < 10: |
| 88 | + return self._green_giant.enable_12v |
| 89 | + else: |
| 90 | + return self._green_giant.get_motorpwr() and self._green_giant.get_enable() |
| 91 | + |
| 92 | + @enable_motors.setter |
| 93 | + def enable_motors(self, on): |
| 94 | + """An nice alias for set_12v""" |
| 95 | + if self._version < 10: |
| 96 | + return self._green_giant.enable_motors(on) |
| 97 | + |
| 98 | + @property |
| 99 | + def enable_12v(self): |
| 100 | + return self._green_giant.get_12v_acc_power() |
| 101 | + |
| 102 | + @enable_12v.setter |
| 103 | + def enable_12v(self, on): |
| 104 | + self._green_giant.set_12v_acc_power(on) |
| 105 | + |
| 106 | + @property |
| 107 | + def enable_5v(self): |
| 108 | + return self._green_giant.get_5v_acc_power() |
| 109 | + |
| 110 | + @enable_5v.setter |
| 111 | + def enable_5v(self, on): |
| 112 | + self._green_giant.set_5v_acc_power(on) |
| 113 | + |
| 114 | + def stop(self): |
| 115 | + """Stops the robot and cuts power to the motors. |
| 116 | +
|
| 117 | + does not touch the servos position. |
| 118 | + """ |
| 119 | + self.enable_12v = False |
| 120 | + self.motors.stop() |
| 121 | + |
| 122 | + def set_user_led(self, val=True): |
| 123 | + self._green_giant.set_user_led(val) |
| 124 | + |
| 125 | + def __del__(self): |
| 126 | + """Frees hardware resources held by the vision object""" |
| 127 | + logging.warning("Destroying robot object") |
| 128 | + type(self)._initialised = False |
| 129 | + |
0 commit comments