Python无人机配送模拟代码解析
Python无人机配送模拟代码解析
这篇博客文章将解析一段Python无人机配送模拟代码, 该代码展示了如何使用Python与飞行模拟器进行交互, 实现无人机的路径规划和订单配送。
代码功能
这段代码模拟了一个简单的无人机配送系统, 主要功能包括:
- 连接飞行模拟器: 代码使用
rflysim模块与飞行模拟器建立连接, 并获取无人机的位置信息。 - 获取障碍区信息: 代码从模拟器中获取障碍区信息, 并打印出来供参考。
- 接收和处理订单: 代码模拟了接收订单的过程, 并根据订单信息规划无人机的飞行路径。
- 控制无人机飞行: 代码根据规划的路径控制无人机飞行,并在到达指定位置后执行取货和送货操作。
- 完成订单: 代码模拟了完成订单的过程, 并将订单状态更新为已完成。
代码解析
import time # 用于时间控制
import math # 用于数学计算
from geopy.distance import distance # 用于计算地理距离
#from rflysim.client import VehicleClient # 用于与飞行模拟器通信
#from rflysim import Position # 用于表示位置信息
def print_orders(orders): # 打印订单信息
for order_id, order_info in orders.items():
print(f'
{order_id:}')
print(f' time_limit: {order_info.time_limit}')
print(f' fee: {order_info.fee}')
print(f' fine: {order_info.fine}')
r_l = order_info.receipt_location
print(f' receipt : {r_l.points}, {r_l.radius}, {r_l.zone_type}')
s_l = order_info.shipping_location
print(f' shipping: {s_l.points}, {s_l.radius}, {s_l.zone_type}')
print(f'= ' * 32)
print('')
def print_area(areas): # 打印区域信息
print(f'Areas:')
for area_id, area in areas.items():
print(f'
{area_id}:
')
print(f' id: {area.zone_id}')
print(f' radius: {area.radius}')
print(f' points: {area.points}')
print(f' start time: {area.start_time}')
print(f' end time: {area.end_time}')
print('')
def main(): # 主函数
# client = VehicleClient(ip='39.105.208.89', id = 663)# 创建飞行模拟器客户端对象
# client = VehicleClient(660)
# client.enable_rflysim()# 启用与飞行模拟器的通信
while True: # 获取无人机位置信息
# pos = client.get_vehicle_pos()
if len(list(pos.keys())) < 1:
time.sleep(3)
continue
break
# 获取障碍区
client.logger.info(f'Get areas')
areas = client.get_area()
print_area(areas)
# 只控制一架飞机
vehicle_id = list(pos.keys())[0]
client.logger.info(f'Waiting for orders..')
while True:
order_info = client.get_order_info()
if len(list(order_info.keys())) < 1:
continue
# print_orders(order_info)
for order_id, order in order_info.items():
if order.status == 'enable':
break
break
if not client.take_order(vehicle_id, order_id):
client.logger.warn(f'Take order[{order_id}] by [{vehicle_id}] failed..')
sl = Position(
order.shipping_location.points[0],
order.shipping_location.points[1],
30
)
rl = Position(
order.receipt_location.points[0],
order.receipt_location.points[1],
30
)
path = [sl, rl]
client.set_vehicle_path(vehicle_id, path)
# tmp_id = list(pos.keys())[1]
# for id, p in pos.items():
# if id == vehicle_id:
# continue
# if p.z < 1:
# continue
# client.set_vehicle_pos(id, Position(x=areas[74].points[0], y=areas[74].points[1], z=30))
while True:
pos = client.get_vehicle_pos()
if len(list(pos.keys())) < 1:
time.sleep(1)
continue
cur_pos = pos[vehicle_id]
dist = _get_distance(cur_pos, sl)
if dist < float(order.shipping_location.radius):
client.logger.info(f'Get into shipping location, order done..')
if not client.pick_up(vehicle_id, order_id):
client.logger.error(f'Faield to pick order {order_id} from vehicle {vehicle_id}')
if not client.set_vehicle_pos(vehicle_id, rl):
client.logger.error(f'Failed to set vehivle {vehicle_id} to [{sl.x}, {sl.y}]')
break
client.logger.info(f'On the way to shipping location, current location: [{cur_pos.x}, {cur_pos.y}, {cur_pos.z}], distance: {dist}')
time.sleep(1)
while True:
pos = client.get_vehicle_pos()
if len(list(pos.keys())) < 1:
time.sleep(1)
continue
cur_pos = pos[vehicle_id]
dist = _get_distance(cur_pos, rl)
if dist < float(order.receipt_location.radius):
client.logger.info(f'Get into receipt location, going to shipping location..')
if not client.finish_order(vehicle_id, order_id):
client.logger.error(f'Faield to finsh order {order_id} from vehicle {vehicle_id}')
break
client.logger.info(f'On the way to receipt location, current location: [{cur_pos.x}, {cur_pos.y}, {cur_pos.z}], distance: {dist}')
time.sleep(1)
def _get_distance(start, end): # 计算两点之间的距离
# return math.sqrt((start.x - end.x) ** 2 + (start.y - end.y) ** 2)
return distance((start.y, start.x), (end.y, end.x)).meters
if __name__ == '__main__':
main()
代码说明
- 代码中使用
#对每一行进行了注释, 方便理解代码的功能. - 代码中使用
rflysim模块与飞行模拟器进行交互, 您需要先安装该模块才能运行代码. - 代码中部分功能需要根据您的实际需求进行修改, 例如订单信息、路径规划算法等.
希望这篇博客文章能够帮助您理解这段Python无人机配送模拟代码!
原文地址: https://www.cveoy.top/t/topic/NZn 著作权归作者所有。请勿转载和采集!