def state(): button.check() if button.active(): return button.state() global last_state if GPIO: current_distance = ultrasonic.distance() # the sensor can only measure up to 4 meters, so we skip too big values if not current_distance or current_distance > 450: return last_state distances.append(current_distance) l = sorted(list(distances)) index = int(len(l) / 2) distance = l[index] state_str = "P" if last_state else "L" logger.info("distance: (%d/%d/%d) %s % 4.1f % 4.1f", MIN_DISTANCE, MAX_DISTANCE, WINDOW_SIZE, state_str, distance, current_distance) if last_state == INCOMING and distance > MAX_DISTANCE: last_state = OUTGOING return OUTGOING if last_state == OUTGOING and distance < MIN_DISTANCE: last_state = INCOMING return INCOMING return last_state else: # TODO: make a button try: with open("/tmp/trigger", 'r') as f: contents = f.read() return "incoming" in contents except: return False
def checkHuman(): dis = ultrasonic.distance(US_TRIG1, US_ECHO1) print(dis) if dis < 10: return True elif dis > 10: return False
def AutoLand(): #ultrasonic.distance() returns distance in cm. 11cm is height from ground. 12 should be good then to drop LandPID = PID(-0.5, 0.1, 0.05, setpoint=12) LandPID.sample_time = 0.01 pid.output_limits = (300, 325) # output value will be between 0 and 10 #Current distance away distance = ultrasonic.distance() while distance > 12: control = LandPID(distance) #Change throttle - control needs to be between 240-420 talktopi(ZERO_VALUE, ZERO_VALUE, control, ZERO_VALUE) #update distance distance = ultrasonic.distance() #Turn off motors talktopi(ZERO_VALUE, ZERO_VALUE, MIN_VALUE, ZERO_VALUE) time.sleep(1)
def main(): while True: dist = ultrasonic.distance() print("Measured Distance = %.1f cm" % dist) #time.sleep(1) driveFORWARD() if (dist < 50): applyBRAKE()
def send_distance(): d = distance() # ignore random big readings if d < 2000: wall = d < 50 print("w: %s forward?: %s" % (wall, driver.trav_fwd)) server.send_message_to_all("distance:%f" % d) if wall and driver.trav_fwd == True: print("Should stop!") driver.reverse() sleep(0.5) driver.stop()
def check_percent_garbage(typeGb, height, trig, echo): while True: dis = ultrasonic.distance(trig, echo) percent = 100 - (dis / height) * 100 print("percent", percent) # return 100-(dis/heightAtEmpty)*100 # print('checkpercent') try: firebase.updateTrashPercent(typeGb, percent) except: print('error posting firebase') time.sleep(3)
def forward_avoid_obstacle(): dist = distance() print("Measured Distance = {:.2f} cm".format(dist)) if (dist < 25): backward() time.sleep(0.2) p1.ChangeDutyCycle(41) #50 p2.ChangeDutyCycle(48) #57 turnLeft() time.sleep(0.2) return else: p1.ChangeDutyCycle(31) #41 p2.ChangeDutyCycle(35) #48 forward() time.sleep(0.08)
def forward_avoid_obstacle(): '''前进并避障函数 遇到障碍物时先后退再左转''' dist = distance() print("Measured Distance = {:.2f} cm".format(dist)) if(dist < 25 ): backward() #延时0.2s time.sleep(0.2) #改变占空比百分值 p1.ChangeDutyCycle(41)#50 p2.ChangeDutyCycle(48)#57 turnLeft() time.sleep(0.2) return else: p1.ChangeDutyCycle(31)#41 p2.ChangeDutyCycle(35)#48 forward() time.sleep(0.08)
def get_ultrasonic_sensor_status(): return ultrasonic.distance()
#import movement import ultrasonic #import time import random import controller import speakers #start = time.time() #speakers.play_file("~/RaspTank/warning.mp3") while True: controller.open_hand() while ultrasonic.distance() > 0.4: #print(ultrasonic.distance()) controller.move_forward(0.01) speakers.play_file("./warning.mp3") #left = bool(random.getrandbits(1)) left = bool(random.getrandbits(1)) controller.close_hand() if (left): while ultrasonic.distance() <= 0.7: controller.turn_left(130) else: while ultrasonic.distance() <= 0.7: controller.turn_right(130) #controller.turn_right(130) #controller.turn_right(90) #
def new_client(client, server): print("New client connected, id: %d" % client['id']) server.send_message_to_all("Hey all, a new client has joined") server.send_message_to_all("distance:%f" % distance())