def setup(): global api, bw, fw, buzzer, led_green, led_red, mqtt_distance, mqtt_speed, ua # 1. PiCar setup. picar.setup() ua = Ultrasonic_Avoidance.Ultrasonic_Avoidance(20) fw = front_wheels.Front_Wheels(db='config') bw = back_wheels.Back_Wheels(db='config') fw.turning_max = 45 # 2. GPIO pins for buzzer and LED. GPIO.setmode(GPIO.BCM) # GPIO.BCM mode. GPIO.setup(PIN_BUZZER, GPIO.OUT) # Buzzer pin. GPIO.setup(PIN_LED, GPIO.OUT) # LED pins. GPIO.output(PIN_LED, GPIO.LOW) # Set LED pins to low to turn off LEDs. buzzer = GPIO.PWM(PIN_BUZZER, 440) # Set buzzer frequency to 440Hz. led_red = GPIO.PWM(PIN_LED[0], 2000) # Set LED frequencies to 2KHz. led_green = GPIO.PWM(PIN_LED[1], 2000) buzzer.start(0) led_red.start(0) led_green.start(0) # 3. Ubidots. api = ApiClient(token='A1E-priJupwmfAGtVeOjcslK9wAW16HJzO') mqtt_distance = api.get_variable('59e93a20c03f9748c6bc3d54') mqtt_speed = api.get_variable('5a092816c03f9706c0205e88')
def ParktheBatmobile(): finished = False timer = 0 ua = Ultrasonic_Avoidance.Ultrasonic_Avoidance(20) frontWheels.turn_left() frontWheels.turn_straight() distance = ua.get_distance() print "distance: %scm" % distance backWheels.backward() time.sleep(0.5) frontWheels.turn_right() backWheels.backward() backWheels.speed = backward_speed time.sleep(1.15) frontWheels.turn_left() time.sleep(1.1) backWheels.stop() time.sleep(1) frontWheels.turn_straight() backWheels.forward() backWheels.speed = forward_speed time.sleep(0.4) backWheels.stop()
def LookForSpot(): ua = Ultrasonic_Avoidance.Ultrasonic_Avoidance(20) frontWheels.turn_straight() found = False start = False obstacleEnd = False timer = 0 time.sleep(1) while (found == False): backWheels.forward() backWheels.speed = forward_speed distance = ua.get_distance() if (distance < 20): start = True if (start == True): print "Obstacle" time.sleep(0.2) distance = ua.get_distance() if (distance > 20): obstacleEnd = True while (obstacleEnd == True): print "Empty Space" time.sleep(0.1) distance = ua.get_distance() timer += 1 if (distance < 50): obstacleEnd = False print "New Obstacle" print "Distance %scm" % distance if (timer > 7): found = True else: start = False continue backWheels.stop() print "SPOT FOUND" time.sleep(1) print "CREEPING FORWARD" backWheels.forward() backWheels.speed = forward_speed time.sleep(0.4) print "READY TO PARK" backWheels.stop() ParktheBatmobile()
def __init__(self): # 0 = random direction, 1 = force left, 2 = force right, 3 = orderdly self.force_turning = 0 picar.setup() self.ua = Ultrasonic_Avoidance.Ultrasonic_Avoidance(20) self.fw = front_wheels.Front_Wheels(db='config') self.bw = back_wheels.Back_Wheels(db='config') self.fw.turning_max = 45 self.forward_speed = 70 self.backward_speed = 70 self.back_distance = 10 self.turn_distance = 20 self.timeout = 10 self.last_angle = 90 self.last_dir = 0 self.command = xavier_command.STOP
max_off_track_count = 40 delay = 0.0005 fw = front_wheels.Front_Wheels(db='config') bw = back_wheels.Back_Wheels(db='config') lf = Line_Follower.Line_Follower() lf.references = REFERENCES fw.ready() bw.ready() fw.turning_max = 45 # Picar Ultrasonic init UA = Ultrasonic_Avoidance.Ultrasonic_Avoidance(20) # PICAR ORDERS def order(value): if "SPEED" in value.upper(): speed = value.split(':') bw.speed = int(speed[1]) bw.forward() if value.upper() == 'L': print("left") fw.turn(int(90 - turning_angle)) if value.upper() == 'R': print("right") fw.turn(int(90 + turning_angle)) if value.upper() == 'S':
import time import picar picar.setup() # D0~D7 to BCM number D0 = 17 D1 = 18 D2 = 27 D3 = 22 D4 = 23 D5 = 24 D6 = 25 D7 = 4 ua = Ultrasonic_Avoidance.Ultrasonic_Avoidance(D0) lf = Line_Follower.Line_Follower() fw = front_wheels.Front_Wheels(db='config') bw = back_wheels.Back_Wheels(db='config') adc = ADC() lf.read_analog = adc.read gate_value = 50 # less then the normal, will act forward_speed = 90 lf_status_last = [0, 0, 0] a_step = 20 b_step = 40 FLASH_line_DELAY = 50
from SunFounder_Ultrasonic_Avoidance import Ultrasonic_Avoidance from picar import front_wheels from picar import back_wheels import picar import sys picar.setup() ua_left = Ultrasonic_Avoidance.Ultrasonic_Avoidance(20) ua_right = Ultrasonic_Avoidance.Ultrasonic_Avoidance(16) ua_front = Ultrasonic_Avoidance.Ultrasonic_Avoidance(12) fw = front_wheels.Front_Wheels(db='config') bw = back_wheels.Back_Wheels(db='config') forward_speed = 0 correction = 0 ls = [] rs = [] def stop(): bw.stop() fw.turn_straight() def reading_sides(): print('...measuring left...') for x in range(0, 10): left_storage = ua_left.get_distance()
def __init__(self): self.UA = Ultrasonic_Avoidance.Ultrasonic_Avoidance(20) self.bus = smbus.SMBus(1)