Main Code

import sys
import time
import math 
import serial
import pickle
from struct import unpack
import threading
import numpy as np
import socket as socket
import tensorflow as tf
from graph import Graph
import RPi.GPIO as GPIO
from picamera import PiCamera
from picamera.array import PiRGBArray
from keras.models import load_model
from keras.preprocessing import image
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# define pi GPIO
GPIO_TRIGGER = 23

GPIO_ECHO0 = 22
GPIO_ECHO1 = 24

# output pin: Trigger
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)
# input pin: Echo
GPIO.setup(GPIO_ECHO0,GPIO.IN)
GPIO.setup(GPIO_ECHO1,GPIO.IN)
# initialize trigger pin to low
GPIO.output(GPIO_TRIGGER, False)

def distance_sensor():
    

    GPIO.output(GPIO_TRIGGER, True)
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
    start = time.time()

    while GPIO.input(GPIO_ECHO0)==0:
        start = time.time()
    while GPIO.input(GPIO_ECHO1)==0:
        start = time.time()
        
    while GPIO.input(GPIO_ECHO0)==1:
        stop = time.time()
    while GPIO.input(GPIO_ECHO1)==1:
        stop = time.time()


    elapsed = stop-start
    distance = (elapsed * 34300)/2

    return distance

class packet_processing(object):

    def __init__(self, packet):
       self.startx1,self.starty1, self.loc1, self.x1, self.y1,self.startx2,self.starty2, self.loc2, self.x2, self.y2= unpack('=0.9*distance)):
                            steering = angle     #process this to arduino executable
                        if int(steering) in self.feautre_map:
                            steering = int(steering)
                        else:
                            steering = int(steering)+1

                        if (distance_sensor()  <= 40):
                            self.ser.write('s'+str(self.feautre_map[steering]))
                            rawCapture.truncate(0)
                            continue

                       
                        if(obj_type == 6):       #indicating red                       
                            self.ser.write('s'+str(self.feautre_map[steering]))
                        elif(obj_type == 5):   #indicating Green
                            self.ser.write('f'+str(self.feautre_map[steering]))
                        elif(obj_type == 4 ):   # D-sign
                            self.ser.write('s'+str(self.feautre_map[steering]))
                            time.sleep(5)
                            total_stop_time += 5
                        elif(obj_type == 3 and self.direction_list[index+1][1] > 45):   #NoRightTurn
                            self.direction_list = self.directions.remove_edge(vertex, self.direction_list[index+1][0])

                        elif(obj_type == 2 and self.direction_list[index+1][1] < -45):   #NoLeftTurn
                            self.direction_list = self.directions.remove_edge(vertex, self.direction_list[index+1][0])
                        elif(obj_type == 1):  #No parking
                            pass
                        elif(obj_type == 0):#No entry
                            self.direction_list = self.directions.remove_edge(vertex, self.direction_list[index+1][0])
                        else:
                            self.ser.write('f'+str(self.feautre_map[steering]))



                        if (hasattr(self, taxi_task) and len(self.taxi_task) == 4 and  self.taxi_destination_index == 0 ):
                                self.direction_list = direction_list.get_direction(current_vertex, taxi_task[taxi_destination_index], angle)
                                self.taxi_destination_index += 1
                                index = 0
                                break

                        if(hasattr(self, taxi_task) and  index == len(self.direction_list)-1):
                            self.direction_list = direction_list.get_direction(current_vertex, taxi_task[taxi_destination_index], angle)
                            self.taxi_destination_index += 1
                            index = 0
                            break

                            if (current_dist == distance):
                                self.ser.write('s'+str(self.feautre_map[steering]))
                                break
                            rawCapture.truncate(0)

                except IndexError:
                        pass
        finally:
                GPIO.cleanup()
                "Round ended over !"

    def coordinate_server(self, ip_adress, port):
        packetList = []
        while True:
            client =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            client.connect((ip_adress, port))
            data = client.recv(1024)
        
            if data != "":
                packetList.append(data)

            if len(packetList) == 2:
                break

            pkt1 = packet_processing(packetList[0])
            pkt2 =  packet_processing(packetList[1])
        if(pkt1.location_type() == 0 and pkt2.location_type() == 1):
            self.directions.eval_line_equations(pkt1.x_coordinate(), pkt1.y_coordinate())
            v1, v2 = self.directions.pickup_dropoff_vertices()
            self.directions.eval_line_equations(pkt2.x_coordinate(), pkt2.y_coordinate())
            v3, v4 = self.directions.pickup_dropoff_vertices()
            self.taxi_task = [v1, v2, v3, v4]
        elif(pkt1.location_type() == 1 and pkt2.location_type() == 0):
            self.directions.eval_line_equations(pkt2.x_coordinate(), pkt2.y_coordinate())
            v1, v2 = self.directions.pickup_dropoff_vertices()
            self.directions.eval_line_equations(pkt1.x_coordinate(), pkt1.y_coordinate())
            v3, v4 = self.directions.pickup_dropoff_vertices()
            self.taxi_task = [v1, v2, v3, v4]
        else:
            print"packet error !"
            

if __name__ == '__main__':
    Main()