Example #1
0
class AI(object):
    
    def __init__(self, ship, enemy, actors):
        self.ship = ship
        self.steer = Steer(ship, actors)
        self.max_prediction_time = 0.25
        self.planet = ship.melee.planet
        self.enemy = enemy

    # Elementary steering AI 
    def update(self):
                    
        st = self.steer.collision_threat(2.5)
            
        self.range = (self.ship.body.position - self.enemy.body.position).length
        range2 = (self.ship.body.position - self.planet.body.position).length
        margin =  self.planet.radius + self.ship.radius * 2.0

        if st is None and range2 > margin:
            self.chase()
            return
        
        if st:
            self.avoid(st)
			
    def chase(self):
        st = self.steer.target(self.enemy, self.max_prediction_time)
        #p1 = self.ship.body.position
        #draw_line(p1.x, p1.y, st.x, st.y)
        st = self.ship.body.get_local_point(st)
        # Ship's heading is 180 off rigid body's heading => add pi
        angle = atan2(st.x, st.y) + pi

        if self.range < 50 and (angle < 0.05 or angle > 6.233):
            self.ship.fire()

        if angle > 0.05 and angle < 6.233:
            if angle >= 0.05 and angle < pi:
                self.ship.turn_right()
            else:
                self.ship.turn_left()
        else:
            self.ship.body.angular_velocity = 0.0
            if self.range > 5.0:
                self.ship.thrust()
        
    def avoid(self, st):
        k = self.ship.body.get_local_point(st)
        angle = atan2(k.x, k.y) + pi
        t = self.ship.body.linear_velocity.cross(st)

        if self.range < 50 and (angle < 0.05 or angle > 6.233):
            self.ship.fire()

        if t >= 0:
            self.ship.turn_right()
        else:
            self.ship.turn_left()

        self.ship.thrust()
Example #2
0
 def post(self):
         init()
         sleep_time = 0.1
         #1.使用get_argument获取url query参数
         arg = self.get_argument('k')
         if(arg=='w'):
                 forward(sleep_time)
         elif(arg=='s'):
                 reverse(sleep_time)
         elif(arg=='a'):
                 left(sleep_time)
         elif(arg=='d'):
                 right(sleep_time)
         elif(arg=='q'):
                 pivot_left(sleep_time)
         elif(arg=='e'):
                 pivot_right(sleep_time)
         elif(arg=='z'):
                 p_left(sleep_time)
         elif(arg=='x'):
                 p_right(sleep_time)
         elif(arg=='u'or arg=='i' or arg =='o' or arg == 'p'):
                 result = {'u':"可回收物",'i':"厨余垃圾",'o':"有害垃圾",'p':"其他垃圾"}
                 p = result[arg]
                 lid = Steer(p)
                 lid.open()
                 time.sleep(5)  # 打开5秒
                 lid.close()
         else:
                 return False
         self.write(arg)#向前端返回信息
Example #3
0
def ai_work():

    wakeup.detector.terminate()  # 结束监控热词

    wakeup.snowboydecoder.play_audio_file()  # ding一声
    led1.set_on()  # 开灯

    # 1.录 用户语音
    state = userrecord.record()
    if state == False:  # 唤醒后太久没说话
        led1.set_off()
        return
    led1.set_off()

    led2.set_on()
    # 2.用户语音 转 文字
    ai_text = "你说什么"
    user_text = iFlytekVoiceTrans.get_usertext()  # 获得语音的文字结果
    #user_text = duvoice.speech_to_text()

    # 3.获得 AI文字
    if user_text == '':  # 录音结果有误
        print("AI说: " + ai_text)
        baiduvoice.text_to_speech(ai_text)
    else:  # 结果无误
        result = classify.get_type(user_text)  # 从话中提取垃圾种类

        if result == False:  # 没有说任何垃圾
            ai_text = aibrain.ai_think(user_text)  # 思知机器人回答
            # 4.AI文字 转 语音
            baiduvoice.text_to_speech(ai_text)
        else:
            if result[0] != None:
                print(result[0] + '是' + result[1])
                ai_text = result[0] + '是' + result[1] + ',' + result[
                    1] + '箱已打开'  # 回答xx是xx垃圾
            else:
                print(result[1] + "箱已打开")
                ai_text = result[1] + '箱已打开'
            # 4.AI文字 转 语音
            baiduvoice.text_to_speech(ai_text)
            lid = Steer(result[1])  # 垃圾种类对应的盖子
            lid.open()
            time.sleep(5)  # 打开5秒
            lid.close()

    led2.set_off()  # 关灯
Example #4
0
 def unregister(cls, panel):
     """ Unregisters a :class:`~steer.Panel` from this dashboard. """
     from steer import Steer
     return Steer.unregister_panel(cls, panel)
Example #5
0
 def register(cls, panel):
     """ Registers a :class:`~steer.Panel` with this dashboard. """
     from steer import Steer
     return Steer.register_panel(cls, panel)
Example #6
0
 def __init__(self, ship, enemy, actors):
     self.ship = ship
     self.steer = Steer(ship, actors)
     self.max_prediction_time = 0.25
     self.planet = ship.melee.planet
     self.enemy = enemy