-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest22.py
138 lines (97 loc) · 4.48 KB
/
test22.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!usr/bin/python3
# -*- coding: utf-8 -*-
#导入库
import RobotApi
import time
#初始化 RobotApi.ubtRobotinitialize() RobotApi.ubtrobotinitialize()
#RobotApi.ubtRobotinitialize() RobotApi.ubtrobotinitialize()
RobotApi.ubtRobotInitialize()
#连接机器人
ret = RobotApi.ubtRobotConnect("SDK", "1", "127.0.0.1")
#ret = RobotApi.ubtRobotConnect("SDK", "1", "127.0.0.1")
#ret = RobotApi.ubtRobotConnect("SDK", "1", "127.0.0.1")
if 0 != ret:
# 测返回值
print('连接失败,错误代码:%s' % robotinfo.acName)
exit(1)
#主程序
#机器人检测到红外(超声波)传感器距离障碍物小于10cm时举左手、闪红灯、
#建立循环
infrared_sensor = RobotApi.UBTEDU_ROBOTINFRARED_SENSOR_T()
while True:
#延时执行
time.sleep(3)
ret = RobotApi.ubtReadSensorValue('infrared', infrared_sensor, 4)
#ret = RobotApi.ubtReadSensorValue("infrared", infrared_sensor, 4)
if ret!= 0:
print('读取不到传感器,错误代码:%d' % ret)
else:
print('传感器读取得数值为:%d mm' % infrared_sensor.iValue)
#print("Read Sensor Value: %d mm" % infrared_sensor.iValue)
#转换距离单位
infrared_int = infrared_sensor.iValue/10
if infrared_int < 10:
#导入api执行动作
RobotApi.ubtSetRobotMotion('raise', 'left', 3, 1)
#api.ubtSetRobotMotion("raise", "left", 3, 1)
#控制灯光
RobotApi.ubtSetRobotLED('button', 'red', 'breath')
#将文字实例化
infrared_str = str('距离太近,请远离一点') + str('距离障碍物') + str(infrared_int) + str('厘米')
#infrared_str = str("距离太近,请远离一点") + str("距离障碍物") + str(infrared_int) + str("cm")
## 定义一个用于阅读的字符对象
#ret = api.ubtVoiceTTS(isInterrputed, infrared_str)
#调用TTs语音输出
ret = RobotApi.ubtVioceTTS(1, infrared_str)
# api.ubtSetRobotLED("button", "red", "breath")
#并通过语音模块说:“距离太近,请远离一点”,
#检测到红外(超声波)传感器距离障碍物大于20cm时做鞠躬动作、闪黄灯,并说:“谢谢!”。
elif infrsred_int >20:
RobotApi.ubtSetRobotMotion('bow', 'front', 2, 1)
RobotApi.ubtSetRobotLED('button', 'yellow', 'breath')
infrared_str = str('谢谢') + str('距离障碍物') + str(infrared_int) + str('厘米')
ret= RobotApi.ubtVioceTTS(1, infrared_str)
if ret != 0:
#TTS返回值
print('调用TTS失败,返回错误代码:%d' % ret)
# 断开连接关闭实例化
RobotApi.ubtRobotDisconnect("SDK","1","127.0.0.1")
RobotApi.ubtRobotDeinitialize()
# api.ubtRobotDisconnect("SDK","1","127.0.0.1")
# #断开连接
# api.ubtRobotDeinitialize()
# RobotApi.UBTEDU_ROBOTINFRARED_SENAOR_T() 这句话好难打多打几次练熟
# !/usr/bin/python
# _*_ coding: utf-8 -*-
import time
import RobotApi
RobotApi.ubtRobotInitialize()
# ------------------------------Connect---------------------------------
RobotApi.ubtRobotInitialize()
# 连接机器人
ret = RobotApi.ubtRobotConnect("SDK", "1", "127.0.0.1")
# ret = RobotApi.ubtRobotConnect("SDK", "1", "127.0.0.1")
# ret = RobotApi.ubtRobotConnect("SDK", "1", "127.0.0.1")
if 0 != ret:
# 测返回值
print('连接失败,错误代码:%s' % robotinfo.acName)
exit(1)
# ---------------------------Read Sensor Value--------------------------
touch_sensor = RobotApi.UBTEDU_ROBOTTOUCH_SENSOR_T()
# RobotApi.ubtSetRobotMotion("raise", "left", 3, 1)
while True:
time.sleep(1)
ret = RobotApi.ubtReadSensorValue("touch", touch_sensor, 4)
if ret != 0:
print("Can not read Sensor value. Error code: %d" % (ret))
else:
print("Read Touch Sensor Value: %d " % (touch_sensor.iValue))
if touch_sensor.iValue > 0:
ret = RobotApi.ubtVoiceTTS(1, "很高兴认识你,人类朋友,我是人工智能教育机器人 偃师! ")
RobotApi.ubtSetRobotLED("button", "red", "blink")
time.sleep(2)
RobotApi.ubtSetRobotMotion("raise", "left", 3, 1)
RobotApi.ubtSetRobotLED("button", "blue", "breath")
# ---------------------------Disconnect---------------------------------
RobotApi.ubtRobotDisconnect("SDK", "1", '127.0.0.1')
RobotApi.ubtRobotDeinitialize()