我是靠谱客的博主 真实豆芽,这篇文章主要介绍python大疆EP下单专用库大疆EP主要函数代码在线玩大疆(无人机),现在分享给大家,希望可以做个参考。

目录

下单

专用库

安装库

引库

大疆EP主要函数

1、robomaster.chassis(EP 底盘模块)

(1)drive_speed(x=0.0, y=0.0, z=0.0, timeout=None) #设置底盘速度参数:

(2)drive_wheels(w1=0, w2=0, w3=0, w4=0, timeout=None) #设置麦轮转速参数:

(3)move(x=0, y=0, z=0, xy_speed=0.5, z_speed=30) #控制底盘运动当指定位置,坐标轴原点为当前位置

2、robomaster.gimbal(EP 云台模块)

(1)drive_speed(pitch_speed=30.0,yaw_speed=30.0) #控制以一定速度转动

(2)move(pitch=0, yaw=0, pitch_speed=30, yaw_speed=30)#控制云台运动到指定位置,坐标轴原点为当前位置

(3)recenter(pitch_speed=60, yaw_speed=60)

3、robomaster.led(EP 装甲灯模块)

(1)set_gimbal_led(comp='top_all', r=255, g=255, b=255, led_list=[0, 1, 2, 3], effect='on') 设置云台灯效

(2)set_led(comp='all', r=0, g=0, b=0, effect='on', freq=1) 设置整机装甲灯效

4、robomaster.blaster EP 发射器模块

(1)fire(fire_type='water', times=1) #发射

(2)set_led(brightness=2, effect=blaster.LED_ON) #设置发射器灯效

5、robomaster.camera EP 摄像机模块

(1)start_video_stream(display=True, resolution='720p') #开启视频流

(2)read_cv2_image(timeout=3, strategy='pipeline') #读取一帧视频流帧 返回值为图片

(3)read_video_frame(timeout=3, strategy='pipeline') #读取一帧视频流帧 返回值为视频流帧字节流

6、robomaster.vision EP 视觉识别功能模块

1、sub_detect_info(name, color=None, callback=None, *args, **kw) #订阅智能识别消息

代码

EP扫码连接

EP工程车机械臂操控

EP工程车机械爪

EP工程车

EP录音

获取EP的ID

 显示EP摄像头画面

EP标签识别

标记标签

瞄准标签

按下空格键瞄准指定标签

按下空格键发射激光

在线玩大疆(无人机)


下单

官网购买

机甲大师 RoboMaster EP 教育拓展套装 - DJI 大疆创新

专用库

先用python操控大疆ep,第一步当然是引库啦!

库名称:Robomaster

安装库

cmd

复制代码
1
pip install robomaster

安装是吧用管理员身份再试一试

引库

完整引库

复制代码
1
2
import robomaster from robomaster import robot

也可以只要一行

复制代码
1
from robomaster import robot

大疆EP主要函数

1robomaster.chassisEP 底盘模块)

控制地盘速度,位置、订阅底盘的数据,控制麦克纳姆轮等操作

(1drive_speed(x=0.0, y=0.0, z=0.0, timeout=None) #设置底盘速度参数:

复制代码
1
2
3
x – float:[-3.5,3.5],x 轴向运动速度即前进速度,单位 m/s y – float:[-3.5,3.5],y 轴向运动速度即横移速度,单位 m/s z – float:[-600,600],z 轴向运动速度即旋转速度,单位 °/s
timeout – float:(0,inf) ,超过指定时间内未收到麦轮转速指令,主动控制机器人停止,单位 s
范例:
复制代码
1
2
3
4
5
import time ep_chassis.drive_speed(x=0.5, y=0, z=0, timeout=5) time.sleep(3) #以 0.5m/s 的速度前进 3 秒 ep_chassis.drive_speed(x=-0.5, y=0, z=0, timeout=5) time.sleep(3) #以 0.5m/s 的速度后退 3 秒

(2drive_wheels(w1=0, w2=0, w3=0, w4=0, timeout=None) #设置麦轮转速参数:

w1 – int:[-1000,1000] ,右前麦轮速度,以车头方向前进旋转为正方向,单位 rpm
w2 – int:[-1000,1000] ,左前麦轮速度,以车头方向前进旋转为正方向,单位 rpm
w3 – int:[-1000,1000] ,左后麦轮速度,以车头方向前进旋转为正方向,单位 rpm
w4 – int:[-1000,1000] ,右后麦轮速度,以车头方向前进旋转为正方向,单位 rpm
timeout – float:(0,inf) ,超过指定时间内未收到麦轮转速指令,主动控制机器人停止,单位 s
范例:
复制代码
1
2
3
import time ep_chassis.drive_wheels(w1=0, w2=50, w3=0, w4=0) time.sleep(1) #以 50rpm 速度转动左前轮

3move(x=0, y=0, z=0, xy_speed=0.5, z_speed=30) #控制底盘运动当指定位置,坐标轴原点为当前位置

参数 :
x float: [-5,5] x 轴向运动距离,单位 m
y float: [-5,5] y 轴向运动距离,单位 m
z float: [-1800,1800] z 轴向旋转角度,单位 °
xy_speed float: [0.5,2] xy 轴向运动速度,单位 m/s
z_speed float: [10,540] z 轴向旋转速度,单位 ° /s
范例:
复制代码
1
ep_chassis.move(x=0.5, y=0, z=0, xy_speed=0.7) #前进 0.5m,速度0.7m/s

2robomaster.gimbalEP 云台模块)

1drive_speed(pitch_speed=30.0,yaw_speed=30.0) #控制以一定速度转动

参数:

pitch_speed – float: [-360, 360] ,上下角度调整速度,单位 °/s
yaw_speed – float: [-360, 360] ,左右角度轴速度,单位 °/s
范例:
复制代码
1
robomaster.gimbal.drive_speed(pitch_speed=30.0, yaw_speed=30.0)

2)move(pitch=0, yaw=0, pitch_speed=30, yaw_speed=30)#控制云台运动到指定位置,坐标轴原点为当前位置

参数 :
pitch – float: [-55, 55] pitch 轴角度,单位 °
yaw – float: [-55, 55] yaw 轴角度,单位 °
pitch_speed – float: [0, 540] pitch 轴运动速速,单位 °/s
yaw_speed – float: [0, 540] yaw 轴运动速度,单位 °/s
范例:
复制代码
1
2
ep_gimbal.moveto(pitch=15,yaw=90,pitch_speed=50,yaw_speed=100).wait_for_complet ed() # 云台以 pitch 角速度 50 度每秒,yaw 角速度 100 度每秒 旋转到 pitch=15, yaw=90

3recenter(pitch_speed=60, yaw_speed=60)

参数 :
pitch_speed – float: [-360, 360] pitch 轴速度,单位 °/s
yaw_speed – float: [-360, 360] yaw 轴速度,单位 °/s
范例:
复制代码
1
2
ep_gimbal.move(pitch=0, yaw=-100).wait_for_completed() # 控制云台向左旋转 100 度

3robomaster.led(EP 装甲灯模块)

1set_gimbal_led(comp='top_all', r=255, g=255, b=255, led_list=[0, 1, 2, 3], effect='on') 设置云台灯效

参数 :
comp – enum: (“top_all”, “top_left”, “top_right”) ,云台部位
r – int: [0, 255] RGB 红色分量值
g – int: [0, 255] RGB 绿色分量值
b – int: [0, 255] RGB 蓝色分量值
led_list – list [idx0, idx1, …] idx int[0,7] 云台灯序号列表 .
effect – enum: (“on”, “off”) ,灯效类型
范例:
复制代码
1
robomaster.led.set_gimbal_led(comp='top_all', r=255, g=255, b=255, led_list=[0, 1, 2, 3], effect='on')

2set_led(comp='all', r=0, g=0, b=0, effect='on', freq=1) 设置整机装甲灯效

参数 :
comp – (“all”, “top_all”, “top_right”, “top_left”, “bottom_all”, “bottom_front”, “bottom_back”,
“bottom_left”, “bottom_right”) 灯效部位, all: 所有装甲灯; top_all: 云台所有装甲灯;
top_right: 云台右侧装甲灯; top_left: 云台左侧装甲灯 ; bottom_all: 底盘所有装甲灯;
bottom_front: 前装甲灯; bottom_back: 后装甲灯; bottom_left: 左装甲灯; bottom_right:
装甲灯
r – int: [0~255] RGB 红色分量值
g – int: [0~255] RGB 绿色分量值
b – int: [0~255] RGB 蓝色分量值
effect – (“on”, “off”, “flash”, “breath”, “scrolling”) 灯效类型, on: 常亮; off: 常灭; flash: 闪烁;
breath: 呼吸; scrolling: 跑马灯(仅对云台灯有效)
freq – int: [1, 10] ,闪烁频率,仅对闪烁灯效有效

4robomaster.blaster EP 发射器模块

(1fire(fire_type='water', times=1) #发射

参数 :
fire_type – enum: (“water”, “ir”) , 发射器发射类型,
WATER_FIRE ——水弹、 INFRARED_FIRE ——红外弹
times – 发射次数
范例:
复制代码
1
ep_blaster.fire(fire_type=blaster.WATER_FIRE, times=3) #发射三次水弹

(2)set_led(brightness=2, effect=blaster.LED_ON) #设置发射器灯效

参数 :
brightness – int:[0,255] ,亮度
effect – enum:(“on”, “off”) on 表示常亮, off 表示常灭
范例:
复制代码
1
ep_blaster.set_led(brightness=255, effect=blaster.LED_ON)

5robomaster.camera EP 摄像机模块

1start_video_stream(display=True, resolution='720p') #开启视频流

参数 :
display – bool,是否显示视频流(True/False
resolution – enum: (“360p”, “540p”, “720p”) ,设置图传分辨率尺寸
范例:
复制代码
1
2
3
4
# 显示十秒图传 ep_camera.start_video_stream(display=True, resolution=camera.STREAM_360P) time.sleep(10) ep_camera.stop_video_stream()

2read_cv2_image(timeout=3, strategy='pipeline') #读取一帧视频流帧 返回值为图片

参数 :
timeout – float: (0, inf) ,超时参数,在 timeout 时间内未获取到视频流帧,函数返回
strategy – enum: (“pipeline”, “newest”) ,读取帧策略: pipeline 依次读取缓存的帧信息,
newest 获取最新的一帧 数据,会清空旧的数据帧
范例:
复制代码
1
img = ep_camera.read_cv2_image(strategy="newest")

3read_video_frame(timeout=3, strategy='pipeline') #读取一帧视频流帧 返回值为视频流帧字节流

参数 :
timeout – float: (0, inf) ,超时时间,超过指定 timeout 时间后函数返回
strategy – enum: (“pipeline”, “newest”) 读取帧策略: pipeline 流水线依次读取, newest
取最新的一帧数据, 注意会清空老的数据帧队列

6robomaster.vision EP 视觉识别功能模块

1sub_detect_info(name, color=None, callback=None, *args, **kw) #订阅智能识别消息

参数 :
name – enum: (“person”, “gesture”, “line”, “marker”, “robot”) person 行人, gesture 手势,
line 线识别, marker 标签识别,robot 机器人识别 color – enum:(“red”, “green”, “blue”): 指定识别颜色,仅线识别和标签识别时生效 callback – 回调函数,返回数据 (list(rect_info)):
返回信息 : 包含的信息如下: person 行人识别: (x, y, w, h), x 中心点 x 轴坐标, y 中心点 y
轴坐标, w 宽度, h 高度 gesture 手势识别: (x, y, w, h), x 中心点 x 轴坐标, y 中心点 y
坐标, w 宽度, h 高度 line 线识别: (x, y, theta, C) x x 轴坐标, y y 轴坐标, theta
切线角, C 曲率 marker 识别: (x, y, w, h, marker), x 中心点 x 轴坐标, y 中心点 y 轴坐标,
w 宽度, h 高度, marker 识别到的标签 robot 机器人识别: (x, y, w, h) x 中心点 x 轴坐标,
y 中心点 y 轴坐标, w 宽度, h 高度

代码

EP扫码连接

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import time import robomaster from robomaster import conn from MyQR import myqr from PIL import Image QRCODE_NAME = "qrcode.png" if __name__ == '__main__': helper = conn.ConnectionHelper() info = helper.build_qrcode_string(ssid="网络名称", password="网络密码") myqr.run(words=info) time.sleep(1) img = Image.open(QRCODE_NAME) img.show() if helper.wait_for_connection(): print("Connected!") else: print("Connect failed!")

EP工程车机械臂操控

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 机械臂 import robomaster from robomaster import robot # 同一个路由器下面如果有多个机器人,要指定目标机器人的IP地址 # robomaster.config.ROBOT_IP_STR = "192.168.50.250" ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta") ep_arm = ep_robot.robotic_arm # 向前移动20毫米 ep_arm.move(x=20, y=0).wait_for_completed() # 向后移动20毫米 ep_arm.move(x=-20, y=0).wait_for_completed() # 向上移动20毫米 ep_arm.move(x=0, y=20).wait_for_completed() # 向下移动20毫米 ep_arm.move(x=0, y=-20).wait_for_completed() # 回中 ep_arm.recenter().wait_for_completed() ep_robot.close()

EP工程车机械爪

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import robomaster, time from robomaster import robot # 同一个路由器下面如果有多个机器人,要指定目标机器人的IP地址 # robomaster.config.ROBOT_IP_STR = "192.168.50.250" ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta") ep_gripper = ep_robot.gripper # 张开机械爪 ep_gripper.open(power=50) time.sleep(3) ep_gripper.pause() # 闭合机械爪 ep_gripper.close(power=50) time.sleep(1) ep_gripper.pause() ep_robot.close()

EP工程车

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import robomaster, time from robomaster import robot # 同一个路由器下面如果有多个机器人,要指定目标机器人的IP地址 # robomaster.config.ROBOT_IP_STR = "192.168.50.250" ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta") ep_arm = ep_robot.robotic_arm # 回中 ep_arm.recenter().wait_for_completed() # 假设杯子在右前方 # 向前1.5米 ep_robot.chassis.move(x=1.5, y=0, z=0).wait_for_completed() # 向右转 ep_robot.chassis.move(x=0, y=0, z=90).wait_for_completed() # 向前0.3米 ep_robot.chassis.move(x=0.3, y=0, z=0).wait_for_completed() # 机械爪张开 ep_robot.gripper.open() time.sleep(2) # 机械臂下降 ep_arm.move(x=0, y=-10).wait_for_completed() # 机械臂向前 ep_arm.move(x=20, y=0).wait_for_completed() # 机械爪闭合 ep_robot.gripper.close() time.sleep(2) # 机械臂收回 ep_arm.recenter() ep_robot.close()

EP录音

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from robomaster import robot if __name__ == '__main__': ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta") ep_camera = ep_robot.camera print("start to record:") # 采样率设为16000 ep_camera.record_audio(save_file="output.wav", seconds=5, sample_rate=16000) print("record finished") ep_robot.close()

获取EP的ID

复制代码
1
2
3
4
5
6
from robomaster import conn if __name__ == '__main__': # set the time for scanning ep robot conn.scan_robot_ip_list(timeout=10)

后面代码所需图片

图片名称:

复制代码
1
background.png

 显示EP摄像头画面

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 导入需要的模块 import pygame import sys from pygame.locals import * from robomaster import * import cv2 def show_video(): # 获取机器人第一视角图像帧 img = ep_robot.camera.read_cv2_image(strategy="newest") # 转换图像格式,转换为pygame的surface对象 # if img.any(): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.transpose(img) # 行列互换 img = pygame.surfarray.make_surface(img) screen.blit(img, (0, 0)) # 绘制图象 pygame.init() screen_size = width, height = 1280, 720 screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("联合行动") bg = pygame.image.load("background.png").convert() screen.blit(bg, (0, 0)) pygame.display.update() # 创建机器人对象,连接机器人 ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta", sn="3JKDH5D001H89W") ep_robot.led.set_led(comp="all", r=0, g=255, b=0) # 亮绿灯 print("连接机器人成功") # 打开视频流 ep_robot.camera.start_video_stream(display=False) pygame.time.wait(100) ep_robot.led.set_led(comp="all", r=255, g=255, b=255) # 亮绿灯 clock = pygame.time.Clock() while True: clock.tick(25) # 将帧数设置为25帧 for event in pygame.event.get(): if event.type == QUIT: ep_robot.close() pygame.quit() sys.exit() show_video() pygame.display.update()

EP标签识别

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 导入需要的模块 import pygame import sys from pygame.locals import * from robomaster import * import cv2 def show_video(): # 获取机器人第一视角图像帧 img = ep_robot.camera.read_cv2_image(strategy="newest") # 转换图像格式,转换为pygame的surface对象 # if img.any(): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.transpose(img) # 行列互换 img = pygame.surfarray.make_surface(img) screen.blit(img, (0, 0)) # 绘制图象 def on_detect_marker(marker_info): """智能识别标签的回调函数""" global markers markers = marker_info pygame.init() screen_size = width, height = 1280, 720 screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("联合行动") bg = pygame.image.load("background.png").convert() screen.blit(bg, (0, 0)) pygame.display.update() # 创建机器人对象,连接机器人 ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta", sn="3JKDH5D001H89W") ep_robot.led.set_led(comp="all", r=0, g=255, b=0) # 亮绿灯 print("连接机器人成功") # 打开视频流 ep_robot.camera.start_video_stream(display=False) pygame.time.wait(100) ep_robot.led.set_led(comp="all", r=255, g=255, b=255) # 亮绿灯 # 启动摄像头智能识别 markers = [] # 存储识别到的标签数据 ep_robot.vision.sub_detect_info(name="marker", callback=on_detect_marker) clock = pygame.time.Clock() while True: clock.tick(25) # 将帧数设置为25帧 for event in pygame.event.get(): if event.type == QUIT: ep_robot.close() pygame.quit() sys.exit() show_video() pygame.display.update()

标记标签

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 导入需要的模块 import pygame import sys from pygame.locals import * from robomaster import * import cv2 def show_video(): # 获取机器人第一视角图像帧 img = ep_robot.camera.read_cv2_image(strategy="newest") # 转换图像格式,转换为pygame的surface对象 # if img.any(): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.transpose(img) # 行列互换 img = pygame.surfarray.make_surface(img) screen.blit(img, (0, 0)) # 绘制图象 def on_detect_marker(marker_info): """智能识别标签的回调函数""" global markers markers = marker_info def draw_rect(aim_marker): """给目标标签画正方形""" for marker in markers: if marker[4] == aim_marker: # 计算左上角点坐标(a, b)、宽(w)、高(h)的像素值 a = int((marker[0] - marker[2] / 2) * width) b = int((marker[1] - marker[3] / 2) * height) w = int(marker[2] * width) h = int(marker[3] * height) pygame.draw.rect(screen, (0, 255, 0), (a, b, w, h), 3) pygame.init() screen_size = width, height = 1280, 720 screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("联合行动") bg = pygame.image.load("background.png").convert() screen.blit(bg, (0, 0)) pygame.display.update() # 创建机器人对象,连接机器人 ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta", sn="3JKDH5D001H89W") ep_robot.led.set_led(comp="all", r=0, g=255, b=0) # 亮绿灯 print("连接机器人成功") # 打开视频流 ep_robot.camera.start_video_stream(display=False) pygame.time.wait(100) ep_robot.led.set_led(comp="all", r=255, g=255, b=255) # 亮绿灯 # 启动摄像头智能识别 markers = [] # 存储识别到的标签数据 ep_robot.vision.sub_detect_info(name="marker", callback=on_detect_marker) # 瞄准的标签 aim_marker = '2' clock = pygame.time.Clock() while True: clock.tick(25) # 将帧数设置为25帧 for event in pygame.event.get(): if event.type == QUIT: ep_robot.close() pygame.quit() sys.exit() show_video() draw_rect(aim_marker) pygame.display.update()

瞄准标签

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 导入需要的模块 import pygame import sys from pygame.locals import * from robomaster import * import cv2 def show_video(): # 获取机器人第一视角图像帧 img = ep_robot.camera.read_cv2_image(strategy="newest") # 转换图像格式,转换为pygame的surface对象 # if img.any(): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.transpose(img) # 行列互换 img = pygame.surfarray.make_surface(img) screen.blit(img, (0, 0)) # 绘制图象 def on_detect_marker(marker_info): """智能识别标签的回调函数""" global markers markers = marker_info def draw_rect(aim_marker): """给目标标签画正方形""" for marker in markers: if marker[4] == aim_marker: # 计算左上角点坐标(a, b)、宽(w)、高(h)的像素值 a = int((marker[0] - marker[2] / 2) * width) b = int((marker[1] - marker[3] / 2) * height) w = int(marker[2] * width) h = int(marker[3] * height) pygame.draw.rect(screen, (0, 255, 0), (a, b, w, h), 3) def marker_tracking(aim_marker): find = False # 标记是否找到目标标签 for marker in markers: if marker[4] == aim_marker: find = True x = marker[0] # 标签中心点x的比例 y = marker[1] # 标签中心点y的比例 if x < 0.45: yaw_speed = -20 elif x > 0.55: yaw_speed = 20 else: yaw_speed = 0 if y < 0.50: pitch_speed = 20 elif y > 0.6: pitch_speed = -20 else: pitch_speed = 0 # print(yaw_speed,pitch_speed) ep_robot.gimbal.drive_speed(pitch_speed=pitch_speed, yaw_speed=yaw_speed) # 移动云台到中心点 if not find: # 如果没有找到目标标签,就让云台停止移动 ep_robot.gimbal.drive_speed(pitch_speed=0, yaw_speed=0) pygame.init() screen_size = width, height = 1280, 720 screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("联合行动") bg = pygame.image.load("background.png").convert() screen.blit(bg, (0, 0)) pygame.display.update() # 创建机器人对象,连接机器人 ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta", sn="3JKDH5D001H89W") ep_robot.led.set_led(comp="all", r=0, g=255, b=0) # 亮绿灯 print("连接机器人成功") # 打开视频流 ep_robot.camera.start_video_stream(display=False) pygame.time.wait(100) ep_robot.led.set_led(comp="all", r=255, g=255, b=255) # 亮绿灯 # 启动摄像头智能识别 markers = [] # 存储识别到的标签数据 ep_robot.vision.sub_detect_info(name="marker", callback=on_detect_marker) # 瞄准的标签 aim_marker = '2' clock = pygame.time.Clock() while True: clock.tick(25) # 将帧数设置为25帧 for event in pygame.event.get(): if event.type == QUIT: ep_robot.close() pygame.quit() sys.exit() show_video() draw_rect(aim_marker) marker_tracking(aim_marker) pygame.display.update()

按下空格键瞄准指定标签

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 导入需要的模块 import pygame import sys from pygame.locals import * from robomaster import * import cv2 def show_video(): # 获取机器人第一视角图像帧 img = ep_robot.camera.read_cv2_image(strategy="newest") # 转换图像格式,转换为pygame的surface对象 # if img.any(): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.transpose(img) # 行列互换 img = pygame.surfarray.make_surface(img) screen.blit(img, (0, 0)) # 绘制图象 def on_detect_marker(marker_info): """智能识别标签的回调函数""" global markers markers = marker_info def draw_rect(aim_marker): """给目标标签画正方形""" for marker in markers: if marker[4] == aim_marker: # 计算左上角点坐标(a, b)、宽(w)、高(h)的像素值 a = int((marker[0] - marker[2] / 2) * width) b = int((marker[1] - marker[3] / 2) * height) w = int(marker[2] * width) h = int(marker[3] * height) pygame.draw.rect(screen, (0, 255, 0), (a, b, w, h), 3) def marker_tracking(aim_marker): find = False # 标记是否找到目标标签 for marker in markers: if marker[4] == aim_marker: find = True x = marker[0] # 标签中心点x的比例 y = marker[1] # 标签中心点y的比例 if x < 0.45: yaw_speed = -20 elif x > 0.55: yaw_speed = 20 else: yaw_speed = 0 if y < 0.50: pitch_speed = 20 elif y > 0.6: pitch_speed = -20 else: pitch_speed = 0 # print(yaw_speed,pitch_speed) ep_robot.gimbal.drive_speed(pitch_speed=pitch_speed, yaw_speed=yaw_speed) # 移动云台到中心点 if not find: # 如果没有找到目标标签,就让云台停止移动 ep_robot.gimbal.drive_speed(pitch_speed=0, yaw_speed=0) pygame.init() screen_size = width, height = 1280, 720 screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("联合行动") bg = pygame.image.load("background.png").convert() screen.blit(bg, (0, 0)) pygame.display.update() # 创建机器人对象,连接机器人 ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta", sn="3JKDH5D001H89W") ep_robot.led.set_led(comp="all", r=0, g=255, b=0) # 亮绿灯 print("连接机器人成功") # 打开视频流 ep_robot.camera.start_video_stream(display=False) pygame.time.wait(100) ep_robot.led.set_led(comp="all", r=255, g=255, b=255) # 亮绿灯 # 启动摄像头智能识别 markers = [] # 存储识别到的标签数据 ep_robot.vision.sub_detect_info(name="marker", callback=on_detect_marker) # 瞄准的标签 aim_marker = '2' clock = pygame.time.Clock() while True: clock.tick(25) # 将帧数设置为25帧 for event in pygame.event.get(): if event.type == QUIT: ep_robot.close() pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_1: aim_marker = '1' if event.key == K_2: aim_marker = '2' if event.key == K_3: aim_marker = '3' show_video() draw_rect(aim_marker) marker_tracking(aim_marker) pygame.display.update()

按下空格键发射激光

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 导入需要的模块 import pygame import sys from pygame.locals import * from robomaster import * import cv2 from drone import Drone def show_video(): # 获取机器人第一视角图像帧 img = ep_robot.camera.read_cv2_image(strategy="newest") # 转换图像格式,转换为pygame的surface对象 # if img.any(): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.transpose(img) # 行列互换 img = pygame.surfarray.make_surface(img) screen.blit(img, (0, 0)) # 绘制图象 def on_detect_marker(marker_info): """智能识别标签的回调函数""" global markers markers = marker_info def draw_rect(aim_marker): """给目标标签画正方形""" for marker in markers: if marker[4] == aim_marker: # 计算左上角点坐标(a, b)、宽(w)、高(h)的像素值 a = int((marker[0] - marker[2] / 2) * width) b = int((marker[1] - marker[3] / 2) * height) w = int(marker[2] * width) h = int(marker[3] * height) pygame.draw.rect(screen, (0, 255, 0), (a, b, w, h), 3) def marker_tracking(aim_marker): find = False # 标记是否找到目标标签 for marker in markers: if marker[4] == aim_marker: find = True x = marker[0] # 标签中心点x的比例 y = marker[1] # 标签中心点y的比例 if x < 0.45: yaw_speed = -20 elif x > 0.55: yaw_speed = 20 else: yaw_speed = 0 if y < 0.50: pitch_speed = 20 elif y > 0.6: pitch_speed = -20 else: pitch_speed = 0 # print(yaw_speed,pitch_speed) ep_robot.gimbal.drive_speed(pitch_speed=pitch_speed, yaw_speed=yaw_speed) # 移动云台到中心点 if not find: # 如果没有找到目标标签,就让云台停止移动 ep_robot.gimbal.drive_speed(pitch_speed=0, yaw_speed=0) def fire(): # 射击标签 ep_robot.blaster.fire(fire_type=blaster.INFRARED_FIRE) # 发射激光 pygame.init() screen_size = width, height = 1280, 720 screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("联合行动") bg = pygame.image.load("background.png").convert() screen.blit(bg, (0, 0)) pygame.display.update() # 创建机器人对象,连接机器人 ep_robot = robot.Robot() ep_robot.initialize(conn_type="sta", sn="3JKDH5D001H89W") ep_robot.led.set_led(comp="all", r=0, g=255, b=0) # 亮绿灯 print("连接机器人成功") # 打开视频流 ep_robot.camera.start_video_stream(display=False) pygame.time.wait(100) ep_robot.led.set_led(comp="all", r=255, g=255, b=255) # 亮绿灯 # 启动摄像头智能识别 markers = [] # 存储识别到的标签数据 ep_robot.vision.sub_detect_info(name="marker", callback=on_detect_marker) # 瞄准的标签 aim_marker = '2' clock = pygame.time.Clock() while True: clock.tick(25) # 将帧数设置为25帧 for event in pygame.event.get(): if event.type == QUIT: ep_robot.close() pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_1: aim_marker = '1' if event.key == K_2: aim_marker = '2' if event.key == K_3: aim_marker = '3' if event.key == K_SPACE: fire() show_video() draw_rect(aim_marker) marker_tracking(aim_marker) pygame.display.update()

在线玩大疆(无人机)

来大疆官网体验无人机飞行 (dji.com)

最后

以上就是真实豆芽最近收集整理的关于python大疆EP下单专用库大疆EP主要函数代码在线玩大疆(无人机)的全部内容,更多相关python大疆EP下单专用库大疆EP主要函数代码在线玩大疆(无人机)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部