Best Python code snippet using Airtest
voice_cmd.py
Source:voice_cmd.py
1#!/usr/bin/env python2# -*- coding:utf-8 -*-3import roslib; roslib.load_manifest('voice')4import os, sys5import playsound6import random7import rospy8import math9import copy10import re11from geometry_msgs.msg import Twist #è¿å¨é度ç»æä½ç±»å12from std_msgs.msg import String13# Python2.5 åå§ååä¼å é¤ sys.setdefaultencoding,éæ°è½½å
¥ 14reload(sys)15sys.setdefaultencoding('utf-8')16class cmd_vel:17 def __init__(self):18 rospy.on_shutdown(self.cleanup)19 # åå§åé»è®¤é度20 self.speed_Linear = 0.221 self.speed_Angular = 0.522 self.msg = Twist()23 # self.tts="好ç"24 # éè¡åæ°åmsgç¼åç±»å æ ï¼æ§å¶åå¸æ¶é´25 self.oscillation = {'oscil_Flag':0,'loop_Flag':0,'msg_Num':0,'keyWord':1,1:{'oscil_Num':0,'msg_Buff':Twist()}}26 self.oscillation_backup={}27 self.oscil_rate=1028 # åå¸ä¸è®¢é
29 # turtlesim_mode30 # self.pub_ = rospy.Publisher('/turtle1/cmd_vel', Twist,queue_size=1)31 # turtlebot3_burger32 self.pub_ = rospy.Publisher('/cmd_vel', Twist,queue_size=1)33 # self.pubtts_ = rospy.Publisher('voice_feedback',String,queue_size=10)34 rospy.Subscriber('voice_txt', String, self.cmd_Extraction)35 # é¢å è½½é¨å36 self.mp3_path=os.path.dirname(__file__)37 self.digit={"ä¸":1,"äº":2,"两":2,"ä¸":3,"å":4,"äº":5,"å
":6,"ä¸":7,"å
«":8,"ä¹":9,"å":10}38 # ç¼è¯æ£å表达å¼39 # self.pattern1=re.compile(r'(?P<router>å|æ£æ¹å½¢|é¿æ¹å½¢|ç©å½¢|åè¿|åå|å é|åé|左转|å³è½¬|åé|åæ¢|ç«å®|继ç»|æ¢å¤)')40 self.pattern1=re.compile(ur'(?P<router>\u5706|\u6B63\u65B9\u5F62|\u957F\u65B9\u5F62|\u77E9\u5F62|\u524D\u8FDB|\u5411\u524D|\u52A0\u901F|\u51CF\u901F|\u5DE6\u8F6C|\u53F3\u8F6C|\u540E\u9000|\u505C\u6B62|\u7ACB\u5B9A|\u7EE7\u7EED|\u6062\u590D)')41 # self.pattern2=re.compile(r'(?P<way>åå¾|ç´å¾|è¾¹é¿|åè¿|åå|åé)[^\d|ä¸|äº|两|ä¸|å|äº|å
|ä¸|å
«|ä¹|å](?P<length>\d+|ä¸|äº|两|ä¸|å|äº|å
|ä¸|å
«|ä¹|å).*(?P<direction>æ£|顺|é)*.*(?P<router>å|æ£æ¹å½¢)')42 self.pattern2=re.compile(ur'(?P<way>\u534A\u5F84|\u76F4\u5F84|\u8FB9\u957F|\u524D\u8FDB|\u5411\u524D|\u540E\u9000)\D*?(?P<length>\d+|\u4E00|\u4E8C|\u4E24|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341)[^\u6B63|\u987A|\u9006]*(?P<direction>\u6B63(?:.[^\u5F62])|\u987A|\u9006)?')43 # é¿-宽 ç±»åé¿æ¹å½¢å¹é
44 self.pattern3=re.compile(ur'(?P<way_length>\u957F)\D*?(?P<length>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341|\u4E24).*(?P<way_wide>\u5BBD)\D*?(?P<wide>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341|\u4E24)[^\u6B63|\u987A|\u9006]*(?P<direction>\u6B63|\u987A|\u9006)?')45 # 宽-é¿ ç±»åé¿æ¹å½¢å¹é
46 self.pattern4=re.compile(ur'(?P<way_wide>\u5BBD)\D*?(?P<wide>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341|\u4E24).*(?P<way_length>\u957F)\D*?(?P<length>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341|\u4E24)[^\u6B63|\u987A|\u9006]*(?P<direction>\u6B63|\u987A|\u9006)?')47 # 循ç¯æ令å¹é
pattern5=re.compile(r'(?P<notLoop>ä¸ä¸ª|走个)')48 self.pattern5=re.compile(ur'(?P<notLoop>\u8D70\u4E2A|\u4E00\u4E2A)')49 # ä¸åè·¯å¾ç±»åTwiståå¸é¢çæ§å¶50 rate = rospy.Rate(self.oscil_rate)51 while not rospy.is_shutdown():52 self.pub_.publish(self.msg)53 if self.oscillation['oscil_Flag']==1:54 key=self.oscillation['keyWord']55 self.msg=self.oscillation[key]['msg_Buff']56 self.oscillation[key]['oscil_Num']-=157 if self.oscillation[key]['oscil_Num']==0:58 self.oscillation['msg_Num']-=159 self.oscillation['keyWord']+=160 if self.oscillation['msg_Num']==0 and self.oscillation['loop_Flag']==1:61 self.oscillation=copy.deepcopy(self.oscillation_backup)62 elif self.oscillation['msg_Num']==0 and self.oscillation['loop_Flag']==0:63 self.oscillation['oscil_Flag']=264 # random_mp3=random.choice([1,2,3,4])65 # mp3_file=self.mp3_path+'/end'+str(random_mp3)+'.mp3'66 # playsound.playsound(mp3_file)67 elif self.oscillation['oscil_Flag']==2:68 self.msg=Twist()69 self.oscillation['oscil_Flag']=070 rate.sleep()71 #æ令æåå½æ°72 def cmd_Extraction(self, msg):73 rospy.loginfo(msg.data)74 #订é
æ°æ®å符串é¢å¤ç75 command_Str=msg.data76 if 'å
³é' in command_Str:77 rospy.signal_shutdown("å
³éæºå¨äºº")78 # å°è¯å¹é
è¿å¨è·¯å¾æ§å¶å
³é®è¯79 try:80 regex1=self.pattern1.search(command_Str.decode('utf-8'))81 router=regex1.group('router')82 except:83 # éææè·¯å¾æ§å¶æ令ï¼éåºåè°å½æ°84 # print('exit')85 return86 # print(router)87 # å形路å¾88 if router==u'\u5706':89 # print("flagtype=åå½¢")90 try:91 regex2=self.pattern2.search(command_Str.decode('utf-8'))92 way=regex2.group('way')93 length=regex2.group('length')94 # print(way)95 # print(length)96 except:97 return98 length=float(self.digit.get(length.encode('utf-8'),length))99 direction=self.direc(regex2)100 # print(direction)101 if way==u'\u534A\u5F84':102 radius=length103 elif way==u'\u76F4\u5F84':104 radius=length/2105 else:106 radius=0107 # print(radius)108 if radius:109 oscil_Buff=self.msg_Twist(2*radius*math.pi,1)110 self.oscillation[1]=oscil_Buff.copy()111 self.oscillation[1]['msg_Buff'].angular.z\112 =self.oscillation[1]['msg_Buff'].linear.x/radius*direction113 self.oscillation['msg_Num']=1114 self.notLoop(command_Str)115 116 # æ£æ¹å½¢117 elif router==u'\u6B63\u65B9\u5F62':118 # print("flagtype=æ£æ¹å½¢")119 try:120 regex2=self.pattern2.search(command_Str.decode('utf-8'))121 way=regex2.group('way')122 length=regex2.group('length')123 # print(way)124 # print(length)125 except:126 return127 length=float(self.digit.get(length.encode('utf-8'),length))128 direction=self.direc(regex2)129 # print(direction)130 if way==u'\u8FB9\u957F':131 length_buff=self.msg_Twist(length,1)132 angular_buff=self.msg_Twist(0.5*math.pi,2,direction)133 self.oscillation['msg_Num']=8134 for i in range(1,9,2):135 self.oscillation[i]=length_buff.copy()136 self.oscillation[i+1]=angular_buff.copy()137 self.notLoop(command_Str)138 # é¿æ¹å½¢ ç©å½¢139 elif router in (u'\u957F\u65B9\u5F62', u'\u77E9\u5F62'):140 # print("flagtype=é¿æ¹å½¢")141 try:142 regex3=self.pattern3.search(command_Str.decode('utf-8'))143 length=regex3.group('length')144 wide=regex3.group('wide')145 # print("é¿-宽")146 # print(length)147 # print(wide)148 except:149 try:150 regex3=self.pattern4.search(command_Str.decode('utf-8'))151 length=regex3.group('length')152 wide=regex3.group('wide')153 # print('宽-é¿')154 # print(length)155 # print(wide)156 except:157 return158 length=float(self.digit.get(length.encode('utf-8'),length))159 wide=float(self.digit.get(wide.encode('utf-8'),wide))160 direction=self.direc(regex3)161 if length and wide:162 length_buff=self.msg_Twist(length,1)163 wide_Buff=self.msg_Twist(wide,1)164 angular_buff=self.msg_Twist(0.5*math.pi,2,direction)165 self.oscillation['msg_Num']=8166 for i in range(1,9,4):167 self.oscillation[i]=length_buff.copy()168 self.oscillation[i+1]=angular_buff.copy()169 self.oscillation[i+2]=wide_Buff.copy()170 self.oscillation[i+3]=angular_buff.copy()171 self.notLoop(command_Str)172 # åè¿ åå173 elif router in (u'\u524D\u8FDB',u'\u5411\u524D'):174 # print("flagtype=ç´çº¿")175 try:176 regex4=self.pattern2.search(command_Str.decode('utf-8'))177 way=regex4.group('way')178 length=regex4.group('length')179 # print(way)180 # print(length)181 length=float(self.digit.get(length.encode('utf-8'),length))182 if way in (u'\u524D\u8FDB',u'\u5411\u524D'):183 length_buff=self.msg_Twist(length,1)184 self.oscillation[1]=length_buff.copy()185 self.oscillation['msg_Num']=1186 self.oscillation['oscil_Flag']=1187 except:188 # print("åè¿3")189 self.msg.linear.x = self.speed_Linear190 self.msg.angular.z = 0191 self.oscillation['oscil_Flag']=0192 # å é193 elif router==u'\u52A0\u901F':194 # print("å é11")195 if self.oscillation['oscil_Flag']!=1 and\196 self.msg.linear.x>0 and self.msg.linear.x*2<=2.5:197 if self.msg.linear.z!=0:198 self.msg.linear.x = self.msg.linear.x*2199 self.msg.angular.z = self.msg.angular.z*2200 else:201 self.msg.linear.x +=0.2202 # åé203 elif router==u'\u51CF\u901F':204 # print("åé2")205 if self.oscillation['oscil_Flag']!=1 and self.msg.linear.x-0.2>0:206 if self.msg.linear.z!=0:207 self.msg.linear.x = self.msg.linear.x/2208 self.msg.angular.z = self.msg.angular.z/2209 else:210 self.msg.linear.x -= 0.2211 # 左转212 elif router==u'\u5DE6\u8F6C':213 # print("左转4")214 if self.msg.linear.x != 0:215 if self.msg.angular.z < self.speed_Angular:216 self.msg.angular.z += 0.1217 if self.msg.angular.z > self.speed_Angular:218 self.msg.angular.z = self.speed_Angular219 self.oscillation['oscil_Flag']=0220 else:221 self.msg.angular.z = self.speed_Angular222 self.oscillation['oscil_Flag']=0223 # å³è½¬224 elif router==u'\u53F3\u8F6C': 225 # print("å³è½¬5")226 if self.msg.linear.x != 0:227 if self.msg.angular.z > -self.speed_Angular:228 self.msg.angular.z -= 0.1229 if self.msg.angular.z < -self.speed_Angular:230 self.msg.angular.z = -self.speed_Angular231 self.oscillation['oscil_Flag']=0232 else:233 self.msg.angular.z = -self.speed_Angular234 self.oscillation['oscil_Flag']=0235 # åé236 elif router==u'\u540E\u9000':237 # print("åé6")238 try:239 regex4=self.pattern2.search(command_Str.decode('utf-8'))240 way=regex4.group('way')241 length=regex4.group('length')242 # print(way)243 # print(length)244 length=float(self.digit.get(length.encode('utf-8'),length))245 if way==u'\u540E\u9000':246 length_buff=self.msg_Twist(length,1,-1)247 self.oscillation[1]=length_buff.copy()248 self.oscillation['msg_Num']=1249 self.oscillation['oscil_Flag']=1250 except:251 self.msg.linear.x = -self.speed_Linear252 self.msg.angular.z = 0253 self.oscillation['oscil_Flag']=0254 # åæ¢ ç«å®255 elif router in (u'\u505C\u6B62',u'\u7ACB\u5B9A'):256 # print("åæ¢7")257 self.oscillation['oscil_Flag']=2258 # ç»§ç» æ¢å¤259 elif router in (u'\u7EE7\u7EED',u'\u6062\u590D'):260 # print("继ç»")261 self.oscillation['oscil_Flag']=1262 # self.pubtts_.publish(self.tts)263 random_mp3=random.choice([1,2])264 mp3_file=self.mp3_path+'/ok'+str(random_mp3)+'.mp3'265 playsound.playsound(mp3_file)266 # print(mp3_file)267 # os.system("play "+mp3_file)268 print("å¤çå®æ")269 # stop the robot!270 def cleanup(self):271 twist = Twist()272 self.pub_.publish(twist)273 # å°è¯å¹é
è·¯å¾è½¨è¿¹æ¹å274 def direc(self,regex):275 try:276 direction=regex.group('direction')[0:1]277 # print(direction)278 if direction in (u'\u6B63',u'\u987A'):279 return -1280 else:281 return 1282 except:283 return 1284 # Twistæ¶æ¯ç±»å转æ¢å¤ç285 def msg_Twist(self,value,type,direction=1):286 self.oscillation = {'oscil_Flag':0,'msg_Num':0,'keyWord':1,'loop_Flag':0}287 temp={'oscil_Num':0,'msg_Buff':Twist()}288 if type==1:289 temp['oscil_Num']=(self.oscil_rate*value)//self.speed_Linear290 speed_temp=(self.oscil_rate*value)/temp['oscil_Num']291 temp['msg_Buff'].linear.x=speed_temp*direction292 elif type==2:293 temp['oscil_Num']=(self.oscil_rate* value)//self.speed_Angular294 speed_temp=(self.oscil_rate*value)/temp['oscil_Num']295 temp['msg_Buff'].angular.z=speed_temp*direction296 return temp297 # 循ç¯æ令å¹é
298 def notLoop(self,command_Str,flag=1):299 regex_notloop=self.pattern5.search(command_Str.decode('utf-8'))300 if regex_notloop:301 self.oscillation['loop_Flag']=0302 else:303 self.oscillation['loop_Flag']=1304 self.oscillation_backup=copy.deepcopy(self.oscillation)305 self.oscillation['oscil_Flag']=flag306 self.oscillation_backup['oscil_Flag']=flag307if __name__=="__main__":308 rospy.init_node('voice_cmd_node')309 try:310 cmd_vel()311 except rospy.ROSInterruptException:...
py3.py
Source:py3.py
1#!/usr/bin/env python32# -*- coding:utf-8 -*-3import roslib; roslib.load_manifest('voice')4import os, sys5import rospy6import math7import copy8import re9from geometry_msgs.msg import Twist #è¿å¨é度ç»æä½ç±»å10from std_msgs.msg import String11# Python2.5 åå§ååä¼å é¤ sys.setdefaultencoding,éæ°è½½å
¥ 12reload(sys)13sys.setdefaultencoding('utf-8')14class cmd_vel:15 def __init__(self):16 rospy.on_shutdown(self.cleanup)17 # åå§åé»è®¤é度18 self.speed_Linear = 119 self.speed_Angular = 0.520 self.msg = Twist()21 # self.tts="好ç"22 # éè¡åæ°åmsgç¼åç±»å æ ï¼æ§å¶åå¸æ¶é´23 self.oscillation = {'oscil_Flag':0,'loop_Flag':0,'msg_Num':0,'keyWord':1,1:{'oscil_Num':0,'msg_Buff':Twist()}}24 self.oscillation_backup={}25 self.oscil_rate=1026 # åå¸ä¸è®¢é
27 self.pub_ = rospy.Publisher('/turtle1/cmd_vel', Twist,queue_size=1)28 # self.pubtts_ = rospy.Publisher('voice_feedback',String,queue_size=10)29 rospy.Subscriber('voice_txt', String, self.cmd_Extraction)30 # é¢å è½½é¨å31 self.digit={"ä¸":1,"äº":2,"两":2,"ä¸":3,"å":4,"äº":5,"å
":6,"ä¸":7,"å
«":8,"ä¹":9,"å":10}32 # ç¼è¯æ£å表达å¼33 # self.pattern1=re.compile(r'(?P<router>å|æ£æ¹å½¢|é¿æ¹å½¢|ç©å½¢|åè¿|åå|å é|åé|左转|å³è½¬|åé|åæ¢|ç«å®|继ç»|æ¢å¤)')34 self.pattern1=re.compile(r'(?P<router>\u5706|\u6B63\u65B9\u5F62|\u957F\u65B9\u5F62|\u77E9\u5F62|\u524D\u8FDB|\u5411\u524D|\u52A0\u901F|\u51CF\u901F|\u5DE6\u8F6C|\u53F3\u8F6C|\u540E\u9000|\u505C\u6B62|\u7ACB\u5B9A|\u7EE7\u7EED|\u6062\u590D)')35 # self.pattern2=re.compile(r'(?P<way>åå¾|ç´å¾|è¾¹é¿|åè¿|åå)[^\d|ä¸|äº|ä¸|å|äº|å
|ä¸|å
«|ä¹|å](?P<length>\d+|ä¸|äº|ä¸|å|äº|å
|ä¸|å
«|ä¹|å).*(?P<direction>æ£|顺|é)*.*(?P<router>å|æ£æ¹å½¢)')36 self.pattern2=re.compile(r'(?P<way>\u534A\u5F84|\u76F4\u5F84|\u8FB9\u957F|\u524D\u8FDB|\u5411\u524D)\D*(?P<length>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341)[^\u6B63|\u987A|\u9006]*(?P<direction>\u6B63(?:.[^\u5F62])|\u987A|\u9006)?')37 # é¿-宽 ç±»åé¿æ¹å½¢å¹é
38 self.pattern3=re.compile(r'(?P<way_length>\u957F)\D*(?P<length>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341).*(?P<way_wide>\u5BBD)\D*(?P<wide>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341)[^\u6B63|\u987A|\u9006]*(?P<direction>\u6B63|\u987A|\u9006)?')39 # 宽-é¿ ç±»åé¿æ¹å½¢å¹é
40 self.pattern4=re.compile(r'(?P<way_wide>\u5BBD)\D*(?P<wide>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341).*(?P<way_length>\u957F)\D*(?P<length>\d+|\u4E00|\u4E8C|\u4E09|\u56DB|\u4E94|\u516D|\u4E03|\u516B|\u4E5D|\u5341)[^\u6B63|\u987A|\u9006]*(?P<direction>\u6B63|\u987A|\u9006)?')41 # 循ç¯æ令å¹é
pattern5=re.compile(r'(?P<notLoop>ä¸ä¸ª|走个)')42 self.pattern5=re.compile(r'(?P<notLoop>\u8D70\u4E2A|\u4E00\u4E2A)')43 # ä¸åè·¯å¾ç±»åTwiståå¸é¢çæ§å¶44 rate = rospy.Rate(self.oscil_rate)45 while not rospy.is_shutdown():46 self.pub_.publish(self.msg)47 if self.oscillation['oscil_Flag']==1:48 key=self.oscillation['keyWord']49 self.msg=self.oscillation[key]['msg_Buff']50 self.oscillation[key]['oscil_Num']-=151 if self.oscillation[key]['oscil_Num']==0:52 self.oscillation['msg_Num']-=153 self.oscillation['keyWord']+=154 if self.oscillation['msg_Num']==0 and self.oscillation['loop_Flag']==1:55 self.oscillation=copy.deepcopy(self.oscillation_backup)56 elif self.oscillation['msg_Num']==0 and self.oscillation['loop_Flag']==0:57 self.oscillation['oscil_Flag']=258 elif self.oscillation['oscil_Flag']==2:59 self.msg=Twist()60 self.oscillation['oscil_Flag']=061 rate.sleep()62 #æ令æåå½æ°63 def cmd_Extraction(self, msg):64 rospy.loginfo(msg.data)65 #订é
æ°æ®å符串é¢å¤ç66 commond_Str=msg.data67 # å°è¯å¹é
è¿å¨è·¯å¾æ§å¶å
³é®è¯68 try:69 regex1=self.pattern1.search(commond_Str.decode('utf-8'))70 router=regex1.group('router')71 except:72 # éææè·¯å¾æ§å¶æ令ï¼éåºåè°å½æ°73 return74 print(router)75 # å形路å¾76 if router==u'\u5706':77 print("flagtype=åå½¢")78 try:79 regex2=self.pattern2.search(commond_Str.decode('utf-8'))80 way=regex2.group('way')81 length=regex2.group('length')82 print(way)83 print(length)84 except:85 return86 length=float(self.digit.get(length.encode('utf-8'),length))87 direction=self.direc(regex2)88 print(direction)89 if way==u'\u534A\u5F84':90 radius=length91 elif way==u'\u76F4\u5F84':92 radius=length/293 else:94 radius=095 print(radius)96 if radius:97 oscil_Buff=self.msg_Twist(2*radius*math.pi,1)98 self.oscillation[1]=oscil_Buff.copy()99 self.oscillation[1]['msg_Buff'].angular.z\100 =self.oscillation[1]['msg_Buff'].linear.x/radius*direction101 self.oscillation['msg_Num']=1102 self.notLoop(commond_Str)103 104 # æ£æ¹å½¢105 elif router==u'\u6B63\u65B9\u5F62':106 print("flagtype=æ£æ¹å½¢")107 try:108 regex2=self.pattern2.search(commond_Str.decode('utf-8'))109 way=regex2.group('way')110 length=regex2.group('length')111 print(way)112 print(length)113 except:114 return115 length=float(self.digit.get(length.encode('utf-8'),length))116 direction=self.direc(regex2)117 print(direction)118 if way==u'\u8FB9\u957F':119 length_buff=self.msg_Twist(length,1)120 angular_buff=self.msg_Twist(0.5*math.pi,2,direction)121 self.oscillation['msg_Num']=8122 for i in range(1,9,2):123 self.oscillation[i]=length_buff.copy()124 self.oscillation[i+1]=angular_buff.copy()125 self.notLoop(commond_Str)126 # é¿æ¹å½¢ ç©å½¢127 elif router in (u'\u957F\u65B9\u5F62', u'\u77E9\u5F62'):128 print("flagtype=é¿æ¹å½¢")129 try:130 regex3=self.pattern3.search(commond_Str.decode('utf-8'))131 length=regex3.group('length')132 wide=regex3.group('wide')133 print(length)134 print(wide)135 except:136 try:137 regex3=self.pattern4.search(commond_Str.decode('utf-8'))138 length=regex3.group('length')139 wide=regex3.group('wide')140 print(length)141 print(wide)142 except:143 return144 length=float(self.digit.get(length.encode('utf-8'),length))145 wide=float(self.digit.get(wide.encode('utf-8'),wide))146 direction=self.direc(regex3)147 if length and wide:148 length_buff=self.msg_Twist(length,1)149 wide_Buff=self.msg_Twist(wide,1)150 angular_buff=self.msg_Twist(0.5*math.pi,2,direction)151 self.oscillation['msg_Num']=8152 for i in range(1,9,4):153 self.oscillation[i]=length_buff.copy()154 self.oscillation[i+1]=angular_buff.copy()155 self.oscillation[i+2]=wide_Buff.copy()156 self.oscillation[i+3]=angular_buff.copy()157 self.notLoop(commond_Str)158 # åè¿ åå159 elif router in (u'\u524D\u8FDB',u'\u5411\u524D'):160 print("flagtype=ç´çº¿")161 try:162 regex4=self.pattern2.search(commond_Str.decode('utf-8'))163 way=regex4.group('way')164 length=regex4.group('length')165 print(way)166 print(length)167 length=float(self.digit.get(length.encode('utf-8'),length))168 if way in (u'\u524D\u8FDB',u'\u5411\u524D'):169 length_buff=self.msg_Twist(length,1)170 self.oscillation[1]=length_buff.copy()171 self.oscillation['msg_Num']=1172 self.oscillation['oscil_Flag']=1173 except:174 print("åè¿3")175 self.msg.linear.x = self.speed_Linear176 self.msg.angular.z = 0177 self.oscillation['oscil_Flag']=0178 # å é179 elif router==u'\u52A0\u901F':180 print("å é11")181 if self.oscillation['oscil_Flag']!=1 and\182 self.msg.linear.x>0 and self.msg.linear.x*2<=2.5:183 if self.msg.linear.z!=0:184 self.msg.linear.x = self.msg.linear.x*2185 self.msg.angular.z = self.msg.angular.z*2186 else:187 self.msg.linear.x +=0.2188 # åé189 elif router==u'\u51CF\u901F':190 print("åé2")191 if self.oscillation['oscil_Flag']!=1 and self.msg.linear.x-0.2>0:192 if self.msg.linear.z!=0:193 self.msg.linear.x = self.msg.linear.x/2194 self.msg.angular.z = self.msg.angular.z/2195 else:196 self.msg.linear.x -= 0.2197 # 左转198 elif router==u'\u5DE6\u8F6C':199 print("左转4")200 if self.msg.linear.x != 0:201 if self.msg.angular.z < self.msg.linear.x:202 self.msg.angular.z += 0.1203 self.oscillation['oscil_Flag']=0204 else:205 self.msg.angular.z = self.speed_Angular206 self.oscillation['oscil_Flag']=0207 # å³è½¬208 elif router==u'\u53F3\u8F6C': 209 print("å³è½¬5")210 if self.msg.linear.x != 0:211 if self.msg.angular.z > 0:212 self.msg.angular.z = -0.1213 self.oscillation['oscil_Flag']=0214 elif self.msg.angular.z>-self.msg.linear.x and self.msg.angular.z < 0:215 self.msg.angular.z-=0.1216 self.oscillation['oscil_Flag']=0217 else:218 self.msg.angular.z = -self.speed_Angular219 self.oscillation['oscil_Flag']=0220 # åé221 elif router==u'\u540E\u9000':222 print("åé6")223 self.msg.linear.x = -self.speed_Linear224 self.msg.angular.z = 0225 self.oscillation['oscil_Flag']=0226 # åæ¢ ç«å®227 elif router in (u'\u505C\u6B62',u'\u7ACB\u5B9A'):228 print("åæ¢7")229 self.oscillation['oscil_Flag']=2230 # ç»§ç» æ¢å¤231 elif router in (u'\u7EE7\u7EED',u'\u6062\u590D'):232 print("继ç»")233 self.oscillation['oscil_Flag']=1234 # self.pubtts_.publish(self.tts)235 print("å¤çå®æ")236 # stop the robot!237 def cleanup(self):238 twist = Twist()239 self.pub_.publish(twist)240 # å°è¯å¹é
è·¯å¾è½¨è¿¹æ¹å241 def direc(self,regex):242 try:243 direction=regex.group('direction')[0:1]244 print(direction)245 if direction in (u'\u6B63',u'\u987A'):246 return -1247 else:248 return 1249 except:250 return 1251 # Twistæ¶æ¯ç±»å转æ¢å¤ç252 def msg_Twist(self,value,type,direction=1):253 self.oscillation = {'oscil_Flag':0,'msg_Num':0,'keyWord':1,'loop_Flag':0}254 temp={'oscil_Num':0,'msg_Buff':Twist()}255 if type==1:256 temp['oscil_Num']=(self.oscil_rate*value)//self.speed_Linear257 speed_temp=(self.oscil_rate*value)/temp['oscil_Num']258 temp['msg_Buff'].linear.x=speed_temp259 elif type==2:260 temp['oscil_Num']=(self.oscil_rate* value)//self.speed_Angular261 speed_temp=(self.oscil_rate*value)/temp['oscil_Num']262 temp['msg_Buff'].angular.z=speed_temp*direction263 return temp264 # 循ç¯æ令å¹é
265 def notLoop(self,commond_Str,flag=1):266 regex_notloop=self.pattern5.search(commond_Str.decode('utf-8'))267 if regex_notloop:268 self.oscillation['loop_Flag']=0269 else:270 self.oscillation['loop_Flag']=1271 self.oscillation_backup=copy.deepcopy(self.oscillation)272 self.oscillation['oscil_Flag']=flag273 self.oscillation_backup['oscil_Flag']=flag274if __name__=="__main__":275 rospy.init_node('voice_cmd_node')276 try:277 cmd_vel()278 except rospy.ROSInterruptException:...
client.py
Source:client.py
1import socket2import struct3host = "127.0.0.1"4port = 90015# å建ä¸ä¸ªå®¢æ·ç«¯çsocket对象6client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)7# è¿æ¥æå¡ç«¯8client.connect((host, port))9sendmsg = "hello"10info_length = len(sendmsg)11total_length = 4 + info_length + 212length_buff = struct.pack("!I", total_length)13buff = length_buff + "{}\r\n".format(sendmsg).encode("utf-8")14client.send(buff)15msg = client.recv(1024)16print(msg.decode("utf-8"))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!