def set_pin(self, name, state): """Set the state of pin 'name' to state 'state' """ if not name in self.PINS: log.error("No pin exists for %s." % name) return PIN = self.PINS[name] if running_on_rpi(): if self.PIN_TYPES[PIN] == 'OUTPUT': # for an output, the state is on or off (true or false) GPIO.output(PIN, state) elif self.PIN_TYPES[PIN] == 'SERVO': # for a servo, the state is the desired angle self.SERVOS[PIN].setAngle(state) elif self.PIN_TYPES[PIN] == 'STEPPER': # for a stepper, the state is the desired angle # positive is clockwise, negative is counterclockwise if state >= 0: self.STEPPERS[PIN].turnClockwise(abs(state)) else: self.STEPPERS[PIN].turnCounterClockwise(abs(state)) log.info("Set %s, pin %d, to %s" % (name, PIN, state)) else: log.error("Not running on RPi. Can't set pin.")
def add_pin(self, name, pin_number, pin_type): """Add a pin to the pin map and initialize to pin_type. """ if None != pin_number: self.PINS[name] = pin_number else: log.warning("No pin number set for %s" % name) return if running_on_rpi(): if pin_type == 'OUTPUT': GPIO.setup(pin_number, GPIO.OUT) self.PIN_TYPES[pin_number] = pin_type elif pin_type == 'INPUT': GPIO.setup(pin_number, GPIO.IN) self.PIN_TYPES[pin_number] = pin_type elif pin_type == 'SERVO': # TODO: Add interface for setting frequency of servo self.PIN_TYPES[pin_number] = pin_type self.SERVOS[pin_number] = ServoMotor(pin_number) else: log.warning("Unknown pin type: %s" % pin_type) self.PIN_TYPES[pin_number] = None else: log.error("Cannot initialize pin, not running on raspberry pi.") log.info("%s set to pin %d as type %s" % (name, pin_number, pin_type))
def set_sample_led(light_on, LED_PIN): """Turn on the LEDs surrounding the sample so a picture can be taken """ if running_on_rpi(): if None != LED_PIN: # light_on = True will turn on LED, False will turn off log.info("Setting sample LED to " + str(light_on)) GPIO.output(LED_PIN, light_on) else: log.error("LED pin set to None.") else: log.error("Not running on RPi")
def __init__(self): """Initialize the hardware class by configuring hardware setup. """ self.PINS = {} self.PIN_TYPES = {} self.SERVOS = {} self.STEPPERS = {} # configure raspberry pi to BOARD mode # this means all pin numbers should be the true pin number on the pi if running_on_rpi(): GPIO.setmode(GPIO.BOARD) log.info("Raspberry pi set to GPIO.BOARD.") log.info("Hardware class created.")
def setAngle(self, desired_angle_deg): """Set the angle of this servo to a desired angle in degrees. """ ratio = desired_angle_deg / 180.0 min_angle_freq_hz = 2.5 max_angle_freq_hz = 12.5 # TODO: Test this if running_on_rpi(): self.pwm.ChangeDutyCycle(min_angle_freq_hz + max_angle_freq_hz * ratio) else: log.warning( "Cannot set angle of servo to %d, not running on RPi." % desired_angle_deg)
def turnCounterClockwise(self, degrees): """Drive the stepper motor counterclockwise 'degrees' """ # convert degrees to steps ratio = degrees / 360.0 steps = int(self.complete_revolution_steps * ratio) if running_on_rpi(): for i in range(steps): for halfstep in range(8): for pin in self.control_pins: GPIO.output(self.control_pins[pin], halfstep[halfstep][pin]) time.sleep(self.step_time_s) else: log.warning( "Cannot turn stepper motor counter-clockwise %d degrees, not running on RPi." % degrees)
"""@module prepare_sample Prepare sample for sensing Author: Josh Rands Date: 2/9/2020 Email: [email protected] """ import logging as log import time import init from helpers import running_on_rpi from hardware_iface import open_main_valve, close_main_valve_halfway if running_on_rpi(): import RPi.GPIO as GPIO # Global params for timing stuff test_chamber_fill_time_s = 4 reagent_drip_time_s = 1 def mix_reagent(): """Drive the stepper motor to mix the reagent in the sample tube """ log.warning("Reagent not being mixed.") # TODO: Turn on mixing prop motor with stepper pins... # TODO: Wait until mixed
def get_img(source, file_name=None): """Return a 2-dimensional array of rgb pixels. """ width = init.sensing_config.data['img_width'] height = init.sensing_config.data['img_height'] if source == 'file': log.info("Getting image from file.") if None == file_name: file_name = input("Enter image name: ") img = image.imread("imgs/" + file_name) # save image to sample.png for debugging image.imsave('raw-sample.png', img) return img elif source == 'usb': log.info("Getting image from camera.") cam_id = init.sensing_config.data['camera_id'] pygame.camera.init() pygame.camera.list_cameras() cam = pygame.camera.Camera("/dev/video" + str(cam_id)) cam.start() # Take the picture init.hardware.set_pin('sensing_led', True) img = cam.get_image() init.hardware.set_pin('sensing_led', False) img = pygame.transform.scale(img, (width, height)) pixels = pygame.surfarray.array3d(img) pygame.image.save(img,"raw-sample.png") cam.stop() return pixels elif source == 'rpi': log.info("Getting image from RPi camera.") if running_on_rpi(): with picamera.PiCamera() as camera: with picamera.array.PiRGBArray(camera) as stream: # Turn on LED init.hardware.set_pin('sensing_led', True) # sleep to guarantee led is on... time.sleep(0.1) # adjust resolution for easier data processing camera.resolution = (width, height) camera.capture(stream, format='rgb') # was 'bgr' # At this point the image is available as stream.array img = stream.array # save to file camera.capture('raw-sample.png') # Turn off LED init.hardware.set_pin('sensing_led', False) return img else: log.error("Not running on RPi.") else: log.error("Invalid image source.") return None