EV3相关功能的python实现
mnsd

EV3相关功能的python实现,通过python程序来控制EV3主机进行相关的操作。

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
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
from pybricks.messaging import BluetoothMailboxClient, TextMailbox


# Create your objects here.
ev3 = EV3Brick()
ev3.buttons.pressed() # 返回按下的按键的列表,返回值为Button类型
ev3.light.on(Color.RED) # 红绿橙
ev3.light.off() # 关闭按键灯

# Write your program here.
ev3.speaker.beep() # 两个参数,频率,持续
ev3.speaker.play_notes(['C4/4', 'C4/4', 'G4/4', 'G4/4'], tempo=120)
ev3.speaker.play_file("123.mp3")
ev3.speaker.say("hello,world")
ev3.speaker.set_volume(60)

ev3.screen.clear() # 清屏
ev3.screen.draw_text(x, y, text, text_color=Color.BLACK, background_color=None) # 写文字
ev3.screen.print("hello,world")
#ev3.set_font()
ev3_img = Image(ImageFile.EV3_ICON)
ev3.screen.load_image(ev3_img)
ev3.screen.draw_image(10, 10, ev3_img, transparent=Color.RED)
ev3.screen.draw_pixel(10, 10, color=Color.BLACK)
ev3.screen.draw_line(x1, y1, x2, y2, width=1, color=Color.BLACK)
ev3.screen.draw_box(x1, y1, x2, y2, r=0, fill=False, color=Color.BLACK)
ev3.screen.draw_circle(x, y, r, fill=False, color=Color.BLACK)

ev3.battery.voltage() # 电压
ev3.battery.current() # 电流

# 马达
motor1 = Motor("A", positive_direction=Direction.CLOCKWISE, gears=None)
motor1.speed() # 获取当前速度
motor1.angle() # 获取当前角度
motor1.reset_angle(angle)
motor1.stop()
motor1.brake()
motor1.hold()
motor1.run(5)
motor1.run_time(5, 1000, then=Stop.HOLD, wait=True) # Stop.COAST Stop.BRAKE
motor1.run_angle(5, 60, then=Stop.HOLD, wait=True)
motor1.run_target(5, 60, then=Stop.HOLD, wait=True)
motor1.run_until_stalled(5, then=Stop.COAST, duty_limit=None)

# 按压传感器
touch_sensor = TouchSensor("S1")
touch_sensor.pressed() # 获取当前状态

# 颜色传感器
color_sensor = ColorSensor("S2")
color_sensor.color() # 获取检测到的颜色
color_sensor.ambient() # 返回光的强度,百分比
color_sensor.reflection() # 反射百分比
color_sensor.rgb() # 返回红绿蓝的值,百分比

# 超声波传感器
ultrasonic_sensor = UltrasonicSensor("S3")
ultrasonic_sensor.distance(silent=False) # 测距
ultrasonic_sensor.presence() # 检测是否有其他超声波

#陀螺仪
gyro_sensor = GyroSensor("S4", positive_direction=Direction.CLOCKWISE) # 顺时针方向,COUNTERCLOCKWISE逆时针方向
gyro_sensor.speed() # 返回陀螺仪速度
gyro_sensor.angle() # 返回陀螺仪角速度
gyro_sensor.reset_angle(60)

wait(1000) # 等待1000ms

stop_watch = StopWatch()
stop_watch.time() # 获取秒表当前时间
stop_watch.pause() # 停止秒表
stop_watch.resume() # 继续秒表
stop_watch.reset() # 重置秒表

# 蓝牙服务端
server = BluetoothMailboxServer()
mbox = TextMailbox('greeting', server)
print('waiting for connection...')
server.wait_for_connection()
print('connected!')
mbox.wait()
print(mbox.read())
mbox.send('hello to you!')

# 蓝牙客户端
SERVER = 'ev3dev'
client = BluetoothMailboxClient()
mbox = TextMailbox('greeting', client)
print('establishing connection...')
client.connect(SERVER)
print('connected!')
mbox.send('hello!')
mbox.wait()
print(mbox.read())
 评论
评论插件加载失败
正在加载评论插件