Exemplo n.º 1
0
def get_num_lines():
    # based on rpi distance sensor
    dist = ultrasonic.get_distance()

    dist = clamp_distance(dist)

    return dist
Exemplo n.º 2
0
def on_message(client, userdata, msg):

    if msg.topic == 'joystick/left_right':
        lr_val = float(msg.payload.decode())
        if lr_val > 0.15:
            print('Go right : ', lr_val)
            motl.forward(speed=lr_val)
            motr.backward(speed=lr_val)
        elif lr_val < -0.15:
            print('Go left : ', -lr_val)
            motl.backward(speed=-lr_val)
            motr.forward(speed=-lr_val)

    if msg.topic == 'joystick/forward_reverse':
        fwrev_val = float(msg.payload.decode())
        if fwrev_val > 0.15:
            if us.get_distance() < 50:
                print('[STOP] Obstacle detected')
                motl.stop()
                motr.stop()
                return
            else:
                print('Go back : ', fwrev_val)
                motl.backward(speed=fwrev_val)
                motr.backward(speed=fwrev_val)

        elif fwrev_val < -0.15:
            print('Go forward : ', -fwrev_val)
            motl.forward(speed=-fwrev_val)
            motr.forward(speed=-fwrev_val)

    if msg.topic == 'joystick/stop':
        print('Motor Stop')
        motl.stop()
        motr.stop()
Exemplo n.º 3
0
def set_range(samples=3):
    total = 0
    count = 0
    while (count < samples):
        range = US.get_distance()
        total = total + range
        count = count + 1

    avg = total / samples
    print "Range set to %.2f" % avg
    return avg
import json
from ultrasonic import get_distance
from rc_car import move_left, move_right, move_forward
from camera import capture
from reward import get_reward
from server import init_params, get_action
import time

r_init = init_params([30 * 30], 3)

for i in range(0):
    st = time.time()
    
    distance = 401
    while distance >= 400 and distance <= 3000:
        distance = get_distance()
    reward = get_reward(distance, 60)
    print(distance, reward)
    terminate = False
    if distance < 60:
        terminate = True

    state = capture()
    print(state.shape)
    action = get_action(state.tolist(), reward, terminate)
    print(action)
    
    duration = 0.5
    
    if distance > 60:
        if action == 0:
Exemplo n.º 5
0
    lr_pos = lr_pos_r
    fwrev_pos = fwrev_pos_r
    lr_pos_r = 0
    fwrev_pos_r = 0
    time.sleep(0.1)
    if lr_pos == 0 and fwrev_pos < 0:  #forward
        motl.forward(speed=-fwrev_pos)
        motr.forward(speed=-fwrev_pos)

        l_stop_val = -fwrev_pos
        r_stop_val = -fwrev_pos

        print('Forward : ', -fwrev_pos)

    elif lr_pos == 0 and fwrev_pos > 0:  #reverse
        if us.get_distance() < 50:
            print('[STOP] Obstacle detected')
            motor_stop()
        else:
            print('Reverse : ', fwrev_pos)
            motl.backward(speed=fwrev_pos)
            motr.backward(speed=fwrev_pos)

            l_stop_val = fwrev_pos
            r_stop_val = fwrev_pos

    elif lr_pos < 0 and fwrev_pos == 0:  #left
        motl.backward(-lr_pos)
        motr.forward(-lr_pos)

        l_stop_val = -lr_pos
Exemplo n.º 6
0
import RPi.GPIO as GPIO
import time
import ultrasonic as US

GPIO.setmode(GPIO.BOARD)

US.setup(16, 11)

distance = US.get_distance()

print "Distance (cm): %.1f" % distance

GPIO.cleanup()
Exemplo n.º 7
0
from gpiozero import Motor
import ultrasonic as us

while True:
    print(us.get_distance())
Exemplo n.º 8
0
def is_in_range():
    current_range = US.get_distance()
    return (current_range < max_range)