Exemplo n.º 1
0
def Keyborad_control():
    while True:
        global power_val
        key = readkey()
        if key == '6':
            if power_val <= 90:
                power_val += 10
                print("power_val:", power_val)
        elif key == '4':
            if power_val >= 10:
                power_val -= 10
                print("power_val:", power_val)
        if key == 'w':
            fc.forward(power_val)
        elif key == 'a':
            fc.turn_left(power_val)
        elif key == 's':
            fc.backward(power_val)
        elif key == 'd':
            fc.turn_right(power_val)
        else:
            fc.stop()
        if key == 'q':
            print("quit")
            break
Exemplo n.º 2
0
def main():
    while True:
        scan_list = fc.scan_step(23)
        # print(scan_list)
        if not scan_list:
            continue

        scan_list = [str(i) for i in scan_list]
        scan_list = "".join(scan_list)
        paths = scan_list.split("2")
        length_list = []
        for path in paths:
            length_list.append(len(path))
        # print(length_list)
        if max(length_list) == 0:
            fc.stop() 
        else:
            i = length_list.index(max(length_list))
            pos = scan_list.index(paths[i])
            pos += (len(paths[i]) - 1) / 2
            # pos = int(pos)
            delta = len(scan_list) / 3
            # delta *= us_step/abs(us_step)
            if pos < delta:
                fc.turn_left(speed)
            elif pos > 2 * delta:
                fc.turn_right(speed)
            else:
                if scan_list[int(len(scan_list)/2-1)] == "0":
                    fc.backward(speed)
                else:
                    fc.forward(speed)
Exemplo n.º 3
0
def main():
    time.sleep(5)

    while True:
        scan_list = fc.scan_step(buf)
        if not scan_list:
            continue
        tmp = scan_list[2:8]
        tmpL = scan_list[2:5]
        tmpR = scan_list[5:8]
        print(tmp)
        if tmp != [2, 2, 2, 2, 2, 2]:
            print("Back Off")
            fc.backward(pow)
            time.sleep(bak)

            if sum(tmpL) > sum(tmpR):
                print("Turn Left")
                fc.turn_left(pow * 2)
            else:
                print("Turn Right")
                fc.turn_right(pow * 2)

        else:
            fc.forward(pow)
Exemplo n.º 4
0
async def main_func():
    global recv_dict, send_dict, gs_list
    while 1:
        gs_list = fc.get_grayscale_list()

        if recv_dict['CD'][0] == 'on':
            if fc.is_on_edge(recv_dict['CD'][1], gs_list):
                fc.backward(20)
                time.sleep(0.5)
                fc.stop()

        if recv_dict['TL'][0] == 'on':
            if fc.get_line_status(recv_dict['TL'][1], gs_list) == 0:
                fc.forward(recv_dict['PW'])
            elif fc.get_line_status(recv_dict['TL'][1], gs_list) == -1:
                fc.turn_left(recv_dict['PW'])
            elif fc.get_line_status(recv_dict['TL'][1], gs_list) == 1:
                fc.turn_right(recv_dict['PW'])

        if recv_dict['OA'] == 'on':
            scan_list = fc.scan_step(35)
            if scan_list:
                tmp = scan_list[3:7]
                if tmp != [2, 2, 2, 2]:
                    fc.turn_right(recv_dict['PW'])
                else:
                    fc.forward(recv_dict['PW'])

        elif recv_dict['OF'] == 'on':
            scan_list = fc.scan_step(23)

            if scan_list != False:
                scan_list = [str(i) for i in scan_list]
                scan_list = "".join(scan_list)
                paths = scan_list.split("2")
                length_list = []
                for path in paths:
                    length_list.append(len(path))
                if max(length_list) == 0:
                    fc.stop()
                else:
                    i = length_list.index(max(length_list))
                    pos = scan_list.index(paths[i])
                    pos += (len(paths[i]) - 1) / 2
                    delta = len(scan_list) / 3
                    if pos < delta:
                        fc.turn_left(recv_dict['PW'])
                    elif pos > 2 * delta:
                        fc.turn_right(recv_dict['PW'])
                    else:
                        if scan_list[int(len(scan_list) / 2 - 1)] == "0":
                            fc.backward(recv_dict['PW'])
                        else:
                            fc.forward(recv_dict['PW'])

        elif recv_dict['RD'] == 'on':
            fc.scan_step(35)

        await asyncio.sleep(0.01)
Exemplo n.º 5
0
def move(direction):
    if direction == b'w':
        fc.forward(10)
    elif direction == b'a':
        fc.turn_left(10)
    elif direction == b's':
        fc.backward(10)
    elif direction == b'd':
        fc.turn_right(10)
Exemplo n.º 6
0
Arquivo: car.py Projeto: voidTM/Pi-Car
 def turn(self, angle: int, power: int = 10):
     print(angle)
     if angle < 0:
         self.turn_right(abs(angle))
     elif angle > 1:
         self.turn_left(angle)
     else:
         while(fc.get_distance_at(0) < turn_dist):
             fc.backward(2)
Exemplo n.º 7
0
def mbackward():
    speed4 = fc.Speed(25)
    speed4.start()
    fc.backward(15)
    x = 0
    for i in range(3):
        time.sleep(0.1)

    speed4.deinit()
    fc.stop()
Exemplo n.º 8
0
def move_and_detect(direction, distance, speed, camera, input_height,
                    input_width, interpreter, labels, threshold):
    print("Move in", direction, "direction for", distance, "cm.")

    distance_travelled = 0

    if direction == 'w':
        fc.forward(10)
    elif direction == 'a':
        fc.turn_left(10)
    elif direction == 's':
        fc.backward(10)
    elif direction == 'd':
        fc.turn_right(10)
    else:
        fc.stop()
        return

    stop_sign_detected = False
    should_stop = True
    while distance_travelled < distance:

        stop = False
        start_time = time.monotonic()
        image = capture_frame(camera, input_height, input_width)
        results = detect_objects(interpreter, image, threshold)

        if (should_stop):
            saw_stop_sign = "stop sign" in get_detected_object_labels(
                results, labels)
            if (saw_stop_sign):
                print("Stop sign detected")
            if (stop_sign_detected and not saw_stop_sign):
                stop = True
                stop_sign_detected = False
            elif (not stop_sign_detected and saw_stop_sign):
                stop_sign_detected = True

        elapsed_seconds = (time.monotonic() - start_time)

        print("Elapsed time (ms): ", elapsed_seconds * 1000)
        print_object_labels(results, labels)

        distance_travelled += speed() * elapsed_seconds

        if (stop):
            print("Stop for 3s")
            fc.stop()
            time.sleep(3)
            should_stop = True
            stop = False
            fc.forward(10)

    fc.stop()
Exemplo n.º 9
0
def Remote_control(control_flag, speed=50):
    speed = int(speed)
    if control_flag == 'forward':
        fc.forward(speed)
    elif control_flag == 'backward':
        fc.backward(speed)
    elif control_flag == 'turn_left':
        fc.turn_left(speed)
    elif control_flag == 'turn_right':
        fc.turn_right(speed)
    else:
        fc.stop()
Exemplo n.º 10
0
Arquivo: car.py Projeto: voidTM/Pi-Car
    def drive_backward(self, distance = 10, power = 5):
        self.trip_meter.reset()
        fc.backward(power)

        while(self.trip_meter.distance < distance):
            continue
        
        fc.stop()

        actually_traveled = self.trip_meter.distance

        return actually_traveled
Exemplo n.º 11
0
def state2(tick, clear=20, speed=10):
    fc.backward(speed)
    time.sleep(0.50)
    fc.backward(0)

    # pick random angle
    angles = [22.5 * i for i in range(1, 16)]
    angle = random.choice(angles)
    if angle > 180: angle = angle - 360

    print("tick {0} state 2 rotating angle {1:5.2f}".format(tick, angle))
    turnAngle(angle)
    return state1
Exemplo n.º 12
0
 def move25():
     speed4 = Speed(25)
     speed4.start()
     # time.sleep(2)
     fc.backward(100)
     x = 0
     for i in range(1):
         time.sleep(0.1)
         speed = speed4()
         x += speed * 0.1
         print("%smm/s"%speed)
     print("%smm"%x)
     speed4.deinit()
     fc.stop()
Exemplo n.º 13
0
def main():
    time.sleep(5)

    fc.forward(pow)
    time.sleep(3)

    fc.backward(pow)
    time.sleep(2)

    fc.turn_left(pow)
    time.sleep(2)

    fc.turn_right(pow)
    time.sleep(2)

    fc.stop()
Exemplo n.º 14
0
def main():
    while True:
        scan_list = scan_step1(40)
        if not scan_list:
            continue
        tmp = scan_list[3:7]
        # if tmp == [0,0,0,0]:
        #     fc.backward(speed)

        if 1 not in tmp:
            print(tmp)
            fc.forward(speed)
        else:
            print(tmp)
            fc.stop()
            time.sleep(0.3)
            fc.backward(speed)
            time.sleep(0.3)
            fc.turn_right(speed)
Exemplo n.º 15
0
def move(direction, distance):
    print("Move in", direction, "direction for", distance, "cm.")
    speed4 = Speed(25)
    speed4.start()
    time.sleep(2)

    distance_travelled = 0

    if direction == 'w':
        print("w")
        fc.forward(5)
    elif direction == 'a':
        fc.turn_left(10)
        #fc.forward(10)
    elif direction == 's':
        fc.backward(10)
    elif direction == 'd':
        fc.turn_right(10)
        #fc.forward(10)
    elif direction == "wa":
        print("wa")
        fc.turn_left(5)
        #fc.forward(10)
    elif direction == "wd":
        print("wd")
        fc.turn_right(5)
        #fc.forward(10)
    else:
        speed4.deinit()
        fc.stop()
        return

    while distance_travelled < distance:
        time.sleep(0.1)
        distance_travelled += speed4() * 0.1
        print("travelled", distance_travelled, "cm")
        #print("eval 1: ", distance_travelled < distance )
    
    speed4.deinit()
    fc.stop()
Exemplo n.º 16
0
def move(direction, distance, speed):
    print("Move in", direction, "direction for", distance, "cm.")

    distance_travelled = 0

    if direction == 'w':
        fc.forward(10)
    elif direction == 'a':
        fc.turn_left(10)
    elif direction == 's':
        fc.backward(10)
    elif direction == 'd':
        fc.turn_right(10)
    else:
        fc.stop()
        return

    while distance_travelled < distance:
        time.sleep(0.1)
        distance_travelled += speed() * 0.1

    fc.stop()
Exemplo n.º 17
0
def main():

    reset_servo(-65)
    while True:
       #reset_servo(-65)
        scan_list = fc.scan_step(25)
        if not scan_list:
            continue

        tmp = scan_list[3:7]
        print(tmp)
        if tmp == [2,2,2,2]:        
            fc.forward(speed)
          #  reset_servo(-65)

        else:
            fc.stop()
            time.sleep(0.5)
            fc.backward(100)
            time.sleep(0.5)
            fc.stop()
            time.sleep(0.5)
            fc.turn_right(50)