def message_received(client, server, message):

    # First position indicates message type
    message_type = ast.literal_eval(message)[0]

    if (message_type == 0):
        """ adding new lines that user draw on map """
        tasks = ast.literal_eval(message)[1]
        lines = tasks['line']
        polygons = []
        lines = [[
            dronekit.LocationGlobal(point["lat"], point["lng"], 20)
            for point in line
        ] for line in lines]

        line_task.extend(lines)
        undistributed_tasks[0] = line_task

        #Polytask
        poly_tasks = []
        for polygon in polygons:
            polygon = [[
                dronekit.LocationGlobal(point["lat"], point["lng"], 20)
                for point in line
            ] for line in polygon]
            closing_line = [polygon[len(polygon) - 1][1], polygon[0][0]]
            polygon.append(closing_line)
            poly_tasks.append(polygon)

        polygon_task.extend(poly_tasks)
        undistributed_tasks[1] = poly_tasks

        #Search task
        search_tasks = tasks['search']
        search_tasks_temp = []

        #Convert to DroneKit's Global Position Object
        for task in search_tasks:
            task_temp = []
            for point in task:
                point = dronekit.LocationGlobal(point[0], point[1], 20)
                task_temp.append(point)
            search_tasks_temp.append(task_temp)
        search_tasks = search_tasks_temp

        if len(search_tasks) > 0:
            undistributed_tasks[2] = generate_search_coordinates(search_tasks)

    # New Parameter recieved
    if (message_type == 1):
        """ Change Parameters """
        vehicle_id = ast.literal_eval(message)[1]
        vehicle = Drone_Services.find_vehicle_by_id(vehicles, vehicle_id)

        new_max_speed = ast.literal_eval(message)[2]
        vehicle.max_speed = float(new_max_speed)
        vehicle.groundspeed = float(new_max_speed)

        new_max_battery_time = ast.literal_eval(message)[3]
        vehicle.max_battery_time = float(new_max_battery_time)
        vehicle.current_battery = float(new_max_battery_time)
        vehicle.start_up_time = time.time()

        new_max_carry_weight = ast.literal_eval(message)[4]
        vehicle.max_carry_weight = float(new_max_carry_weight)

    # Reacharge battery
    if (message_type == 2):
        vehicle_id = ast.literal_eval(message)[1]
        vehicle = Drone_Services.find_vehicle_by_id(vehicles, vehicle_id)

        vehicle.current_battery = vehicle.max_battery_time
        vehicle.start_up_time = time.time()
示例#2
0
def message_received(client, server, message):
    # First position indicates message type
    message_type = ast.literal_eval(message)[0]

    # Adding new tasks that user draw on map
    if (message_type == 0):

        # Line task
        tasks = ast.literal_eval(message)[1]
        lines = tasks['line']
        lines = [[
            dronekit.LocationGlobal(point["lat"], point["lng"], altitude)
            for point in line
        ] for line in lines]

        global u_tasks
        u_tasks.extend(lines)

        # Pickup task
        pickup_tasks_temp = []
        for pickup_task in tasks['pickup']:
            line = pickup_task["line"]
            weight = pickup_task["weight"]
            line = [
                dronekit.LocationGlobal(line[0]["lat"], line[0]["lng"],
                                        altitude),
                dronekit.LocationGlobal(line[1]["lat"], line[1]["lng"],
                                        altitude), weight, "weight"
            ]
            pickup_tasks_temp.append(line)

        u_tasks.extend(pickup_tasks_temp)

        # Search task
        search_tasks = tasks['search']
        search_tasks_temp = []

        # Convert to DroneKit's Global Position Object
        for task in search_tasks:
            task_temp = []
            for point in task:
                point = dronekit.LocationGlobal(point[0], point[1], altitude)
                task_temp.append(point)
            search_tasks_temp.append(task_temp)
        search_tasks = search_tasks_temp

        if len(search_tasks) > 0:
            search_tasks = Drone_Services.generate_search_coordinates(
                search_tasks)
            u_tasks.append(search_tasks)

        # Obstacles
        obstacles_from_client = tasks['obstacles']
        obstacles_temp = []

        for obstacle in obstacles_from_client:
            point_temp = []
            for point in obstacle:
                point = dronekit.LocationGlobal(point[0], point[1], altitude)
                point_temp.append(point)
            obstacles_temp.append(point_temp)

        global obstacles
        obstacles.extend(obstacles_temp)

    # New Parameter recieved
    if (message_type == 1):
        """ Change Parameters """
        vehicle_id = ast.literal_eval(message)[1]
        vehicle = Drone_Services.find_vehicle_by_id(vehicles, vehicle_id)

        new_max_speed = ast.literal_eval(message)[2]
        vehicle.max_speed = float(new_max_speed)
        vehicle.groundspeed = float(new_max_speed)

        new_max_battery_time = ast.literal_eval(message)[3]
        vehicle.max_battery_time = float(new_max_battery_time)
        vehicle.current_battery = float(new_max_battery_time)
        vehicle.start_up_time = time.time()

        new_max_carry_weight = ast.literal_eval(message)[4]
        vehicle.max_carry_weight = float(new_max_carry_weight)

    # Reacharge battery
    if (message_type == 2):
        vehicle_id = ast.literal_eval(message)[1]
        vehicle = Drone_Services.find_vehicle_by_id(vehicles, vehicle_id)

        vehicle.current_battery = vehicle.max_battery_time
        vehicle.start_up_time = time.time()

    # Open new cygwin terminal
    if (message_type == 3):
        subprocess.call('start cd C:\cygwin ^& cygwin', shell=True)

    # Start new simulated drone
    if (message_type == 4):
        Drone_Services.start_new_simulated_drone(vehicles, UAV_BASE_PORT,
                                                 server)

    # Delete mission and stop vehicles
    if (message_type == 5):
        for vehicle in vehicles:
            vehicle.nextlocations = []
            free_vehicles = []
            obstacles = []
            u_tasks = []
            vehicle.simple_goto(
                dronekit.LocationGlobal(vehicle.location.global_frame.lat,
                                        vehicle.location.global_frame.lon,
                                        altitude))