def color_follow():
    global pan_angle_color, tilt_angle_color
    Vilib.color_detect_switch(True)
    status = [Vilib.color_detect_object('x'), Vilib.color_detect_object('y')]
    size = [
        Vilib.color_detect_object('width'),
        Vilib.color_detect_object('height')
    ]
    time.sleep(0.005)
    #on left -1 on right 1
    if status[0] == -1:
        pan_angle_color = pan_angle_color + 1
        set_camera_servo1_angle(min(90, pan_angle_color))
        set_dir_servo_angle(max(-45, -pan_angle_color))
    elif status[0] == 1:
        pan_angle_color = pan_angle_color - 1
        set_camera_servo1_angle(max(-90, pan_angle_color))
        set_dir_servo_angle(min(45, -pan_angle_color))
    if status[1] == -1:
        tilt_angle_color = tilt_angle_color + 1
        set_camera_servo2_angle(min(45, tilt_angle_color))
    elif status[1] == 1:
        tilt_angle_color = tilt_angle_color - 1
        set_camera_servo2_angle(max(-45, tilt_angle_color))
    elif 0 < size[0] < 100 or 0 < size[1] < 100:
        forward(50)
    else:
        stop()
def color_detect(color):
    if color == 'close':
        print("Color detect off!")
        Vilib.color_detect_switch(False)
    else:
        print("detecting color :" + color)
        Vilib.color_detect(color)
Example #3
0
import time
from vilib import Vilib
# from ezblock import Servo,PWM
# from robothat import *
import numpy as np
# from th_test import task_start
Vilib.camera_start(web_func=True)
# Vilib.human_detect_switch(True)
Vilib.color_detect_switch(True)
Vilib.detect_color_name('blue')

while True:
    # print(Vilib.human_detect_object('number'))
    # print(Vilib.human_detect_object('y'))
    start_time = time.time()
    time.sleep(10)
    Vilib.detect_color_name('blue')
    time.sleep(10)
    Vilib.detect_color_name('red')
    # print("  ")
    # count+=1

    # for i in range(1000):
    #     pass
    #     # print("end")
    #     # pass

    # this_time = time.time() - start_time
    # print(round(this_time,3))
    # all_time += this_time
    # avge_time = round(all_time/float(count),3)
Example #4
0
    async def main_func(self):
        while 1:
            self.gs_list = car.get_adc_value()
            if self.recv_dict['CD'][0] == 'on':
                if self.is_on_edge(int(self.recv_dict['CD'][1]),self.gs_list):
                    car.backward(50)
                    time.sleep(0.5)
                    car.stop()

            if self.recv_dict['TL'][0] =='on':
                # print("line_follow on")
                car.ref = int(self.recv_dict['TL'][1])
                car.line_follow()

            if self.recv_dict['OA'] == 'on':
                # print("ultrasonic on")
                distance = self.us.read()
                if distance > 40 or distance == -2:
                    car.set_dir_servo_angle(0)
                    car.forward(50)
                elif distance < 10:
                    car.set_dir_servo_angle(-40)
                    car.backward(50)
                    time.sleep(0.5)
                elif distance < 30:
                    car.set_dir_servo_angle(40)
                    car.forward(50)
                    time.sleep(0.5)
                    
            
            if self.recv_dict['HT'] == 'on':
                Vilib.human_detect_switch(True)
            
            if self.recv_dict['HT'] == 'off':
                Vilib.human_detect_switch(False)
                
            if self.recv_dict['HF'] == 'on':
                car.human_follow()
            
            if self.recv_dict['CT'] == 'on':
                Vilib.color_detect_switch(True)
            
            if self.recv_dict['CT'] == 'off':
                Vilib.color_detect_switch(False)
            
            if self.recv_dict['CF'] == 'on':
                car.color_follow()
            
            if self.recv_dict['SS'][0] == 'on':
                music.sound_effect_threading(self.sound_name[int(self.recv_dict['SS'][1])], float(self.recv_dict['SS'][2]))
                self.recv_dict['SS'][0] = 'off' 
            
            if self.recv_dict['SM'][0] == 'on':
                temp = int(self.recv_dict['SM'][1])
                if self.current_music != temp:
                    music.music_stop()
                    self.music_flag = True
                    self.current_music = temp
                if self.music_flag:
                    try:
                        music_file = self.music_name[self.current_music]
                        volume = float(self.recv_dict['SM'][2])
                        music.background_music(music_file, volume=volume) 
                    except IndexError:
                        print("no such music file: ", int(self.recv_dict['SM'][1]) + 1)
                    self.music_flag = False
                # self.recv_dict['SM'][0] = 'off' 
            elif self.recv_dict['SM'][0] == 'off':
                if self.music_flag == False:
                    music.music_stop()
                    self.music_flag = True
            
            # if self.recv_dict['SL'][0] == 'on':
            #     if self.found_name(self.record_name[int(self.recv_dict['SL'][1])]):
            #         self.send_dict['FF'] = 'True'
            #         print("play")
            #         music.record_play(self.record_name[int(self.recv_dict['SL'][1])],volume = float(self.recv_dict['SL'][2]))
            #         self.recv_dict['SL'][0] = 'off'
            #     else:
            #         self.send_dict['FF'] = 'False'
                
                
            if self.recv_dict['TT'][0] == 'on':
                self.tts.say(self.recv_dict['TT'][1]) 
                self.recv_dict['TT'][0] = 'off'  
            
        
            await asyncio.sleep(0.01)