예제 #1
0
class StreamDrive(object):
    def __init__(self):
        # Set up GPIO for RPi
        GPIO.setmode(GPIO.BOARD)
        forwardPin = 7
        backwardPin = 11
        leftPin = 13
        rightPin = 15
        controlStraightPin = 29
        self.motor = Motor(forwardPin, backwardPin, controlStraightPin,
                           leftPin, rightPin)
        self.sensor = UltrasonicSensor()
        # Initialize server for receiving driving instructions
        self.server_socket = socket.socket()
        self.server_socket.bind(('0.0.0.0', 8000))
        self.server_socket.listen(0)
        self.server_connection, self.client_address = self.server_socket.accept(
        )
        print("Connection from: ", self.client_address)
        # Initialize client for streaming camera pictures
        self.client_socket = socket.socket()
        self.client_socket.connect(('192.168.1.11', 8000))
        # Make a file-like object out of the client connection
        self.client_connection = self.client_socket.makefile('wb')
        # Start the stream
        self.stream()

    def stream(self):
        try:
            # Initialize camera and allow some warmup time, create stream
            self.camera = picamera.PiCamera()
            self.camera.resolution = (320, 240)
            self.camera.framerate = 10
            time.sleep(2)
            start = time.time()
            stream = io.BytesIO()
            for frame in self.camera.capture_continuous(stream,
                                                        'jpeg',
                                                        use_video_port=True):
                # Write the length of the capture to the stream
                # and flush to make sure it gets sent
                self.client_connection.write(struct.pack('<L', stream.tell()))
                self.client_connection.flush()
                # Rewind the stream and send image data through
                stream.seek(0)
                self.client_connection.write(stream.read())
                # Reset stream for next capture
                stream.seek(0)
                stream.truncate()
                # Write sensor data to server
                distance = self.sensor.measure_average()
                self.server_connection.send(struct.pack("f", distance))
                # Get the pressed key
                key = self.server_connection.recv(1024).decode()
                if (key == "w"):
                    self.motor.forward()
                elif (key == "s"):
                    self.motor.backward()
                elif (key == "a"):
                    self.motor.forward_left()
                elif (key == "d"):
                    self.motor.forward_right()
                elif (key == "space"):
                    self.motor.stop()
                elif (key == "x"):
                    print("Program terminated")
                    break
                else:
                    self.motor.stop()
                key = ""
            # Write a length of 0 to the stream to signal we're done
            self.client_connection.write(struct.pack('<L', 0))

        finally:
            self.client_connection.close()
            self.client_socket.close()
            self.server_connection.close()
            self.server_socket.close()
            GPIO.cleanup()
예제 #2
0
forwardPin = 7
backwardPin = 11
leftPin = 13
rightPin = 15
controlStraightPin = 29

motor = Motor(forwardPin, backwardPin, controlStraightPin, leftPin, rightPin)

while True:
    key = get_keys()
    if (key == "w"):
        motor.forward()
    elif (key == "s"):
        motor.backward()
    elif (key == "a"):
        motor.forward_left()
    elif (key == "d"): 
        motor.forward_right()
    elif (key == "space"):
        motor.stop()
    elif (key == "x"):
        print("Program terminated")
        break
    else:
        motor.stop()
    key = ""

GPIO.cleanup()