def launchSequence(distance, yawAngle): print "\n%sm away" % distance print "%s degrees\n" % yawAngle adjustYawAngle(yawAngle) adjustPitchAngle(distance) #Basic kinematics to calculate the force at which the #launching mechanism needs to propell the object at power = 0 #value of calculation go here shoot(power) pwm.stop() GPIO.cleanup()
def get_distance(): #the trig send the sound GPIO.output(TRIG, True) #suspend the execution for this "0.0001" seconds time.sleep(0.0001) #the trig stop sending sound GPIO.output(TRIG, False) while GPIO.input(ECHO) == False: #while we have not receive the echo start = time.time() #start counting time while GPIO.input(ECHO) == True: #while receiving the echo end = time.time() sig_time = end - start distance = sig_time / 0.000058 #we gonna use the v=d/t (vitesse du son) print('Distance : {} cm '.format(distance)) GPIO.cleanup() #we clean all the pins return distance
def led_blinking (pin): print ("\nBlinking 1/sec \n") for i in range (0, 61, 1): led_blink (11) GPIO.cleanup ()
def getTempperature(self, temp): from RPI import GPIO data = [] j = 0 channel = 13 #输入GPIO号 channel = int(temp) GPIO.setmode(GPIO.BCM) time.sleep(1) GPIO.setup(channel, GPIO.OUT) GPIO.output(channel, GPIO.LOW) time.sleep(0.02) GPIO.output(channel, GPIO.HIGH) GPIO.setup(channel, GPIO.IN) while GPIO.input(channel) == GPIO.LOW: continue while GPIO.input(channel) == GPIO.HIGH: continue while j < 40: k = 0 while GPIO.input(channel) == GPIO.LOW: continue while GPIO.input(channel) == GPIO.HIGH: k += 1 if k > 100: break if k < 8: data.append(0) else: data.append(1) j += 1 logger.info("sensor is working.") logger.debug(data) humidity_bit = data[0:8] humidity_point_bit = data[8:16] temperature_bit = data[16:24] temperature_point_bit = data[24:32] check_bit = data[32:40] humidity = 0 humidity_point = 0 temperature = 0 temperature_point = 0 check = 0 for i in range(8): humidity += humidity_bit[i] * 2**(7 - i) humidity_point += humidity_point_bit[i] * 2**(7 - i) temperature += temperature_bit[i] * 2**(7 - i) temperature_point += temperature_point_bit[i] * 2**(7 - i) check += check_bit[i] * 2**(7 - i) tmp = humidity + humidity_point + temperature + temperature_point if check == tmp: logger.info("temperature :", temperature, "*C, humidity :", humidity, "%") return "主人,当前家中温度" + str(temperature) + "摄氏度,湿度:百分之" + str( humidity) else: #return "抱歉主人,传感器犯了点小错" self.getTempperature(channel) GPIO.cleanup()
GPIO.setwarnings(False) # Setup pin 4 as ouput for sending the signal to the intercom and make it initially zero INTERCOM_PIN = 4 GPIO.setup(INTERCOM_PIN, GPIO.OUT) GPIO.output(INTERCOM_PIN, 0) # Start the web server WEB_SERVER_PORT = 8080 WEB_SERVER_HOST = "localhost" class DoorHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(s): s.send_response(200) s.send_header("Content-type", "text/plain") s.end_headers() GPIO.output(INTERCOM_PIN, 1) time.sleep(2) GPIO.output(INTERCOM_PIN, 0) server_class = BaseHTTPServer.HTTPServer httpd = server_class((WEB_SERVER_HOST, WEB_SERVER_PORT), DoorHandler) print time.asctime(), "Server Starts - %s:%s" % (WEB_SERVER_HOST, WEB_SERVER_PORT) try: httpd.serve_forever() except KeyboardInterrupt: pass httpd.server_close() GPIO.cleanup()
import RPI.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) PLF = 17 PLB = 22 PRF = 18 PRB = 23 duration = 0.5 GPIO.setup(PLF, GPIO.OUT) GPIO.setup(PLB, GPIO.OUT) GPIO.setup(PRF, GPIO.OUT) GPIO.setup(PRB, GPIO.OUT) GPIO.cleanup()
def cleanAndExit(): print("cleaning...") GPIO.cleanup() print("bye") sys.exit()
def my_read(signal , frame): global continue_reading print(my_message_1) continue_reading = False GPIO.cleanup()
class WallStop(): def __init__(self): self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=1) self.goahead_param = 500 self.back_param = 1000 self.sensor_values = LightSensorValues() rospy.Subscriber('/lightsensors', LightSensorValues, self.callback) self.M = 0.10 self.M1 = 0.00 self.e = 0.00 self.e1 = 0.00 self.e2 = 0.00 self.goal = 600 self.Kp = 0.0002 self.Ki = 0.000025 self.Kd = 0.00010 def callback(self, messages): self.sensor_values = messages def callBackTest(channel): print("callback") GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) GPIO.add_event_detect(pin, GPIO.FALLING, callBack=callBackTest, bouncetime=300) try: while (Trure): time.sleep(1) except KeyboardInterrpt: print("break") GPIO.cleanup() def run(self): rate = rospy.Rate(10) data = Twist() data.linear.x = 0.00 while not rospy.is_shutdown(): key = ord(getch()) if key == 65: data.linear.x = 0.1 elif key == 66: data.linear.x = -0.1 elif key == 67: data.linear.y = 0.1 elif key == 68: data.linear.y = -0.1 elif key == 13: print("Enter") break else: pause() """self.M1 = self.M self.e2 = self.e1 self.e1 = self.e self.e = self.goal - self.sensor_values.sum_all #self.M = self.M1 + self.Kp * (self.e-self.e1) #self.M = self.M1 + self.Kp * (self.e-self.e1) + self.Kd * ((self.e-self.e1) - (self.e1-self.e2)) self.M = self.M1 + self.Kp * (self.e-self.e1) + self.Ki * self.e + self.Kd * ((self.e-self.e1) - (self.e1-self.e2)) if self.sensor_values.sum_all < self.goahead_param: data.linear.x = 0.2 elif self.sensor_values.sum_all > self.back_param: data.linear.x = -0.2 else: data.linear.x = 0.0 if self.M < 0.25 and self.M > -0.25: data.linear.x = self.M #else: data.linear.x = 0.0 #print('move param_____________' + str(self.M))""" self.cmd_vel.publish(data) rate.sleep()
def destory(): Buzz.stop() # Stop the buzzer GPIO.output(Buzzer, 1) # Set Buzzer pin to High GPIO.cleanup() # Release resource
sampler_position = int(data[-3:]) #scale throttle, steer angle, and sampler position to range [-1,1] scaled_sampler_position = (90 - sampler_position)/90 #1 is max clockise, -1 max anticlockwise scaled_throttle = (90 - throttle)/90 scaled_steer_angle = (steer_angle - 90)/90 left_power = scaled_throttle + scaled_steer_angle right_power = scaled_throttle - scaled_steer_angle #Scale left_power and right_power so both stay in range [-1,1] if (left_power >= 1) or (right_power <= -1): left_power *= 1/2 right_power *= 1/2 TB1.SetMotors(left_power) TB2.SetMotors(right_power) #Sampler Control Code #Servo is neutral at 1.5ms, >1.5 Counter-Clockwise, <1.5 Clockwise pulse_width = 1.5 - scaled_sampler_position duty_cycle = pulse_width * 5 # this is the simplification of D = PW/T * 100 with f=1/T = 50Hz sampler.ChangeDutyCycle(duty_cycle) control_socket.close() GPIO.cleanup() #Check if neccessary
# The python script import RPI.GPIO as GPIO # import RPi Library GPIO.setmode(GPIO.BOARD) servo=11 GPIO.setup(servo,GPIO.OUT) pwm=GPIO.PWM(11,50) #set GPIO pin 11 to 50 Hz”frequency” pwm.start(5) for i in range (0,20): Position=input(“where do you want the servo? 0 - 108 ”) degree = 1./18.*(Position)+2 pwm.ChangeDutyCycle(degree) pwm.stop() #stop the pulse width modulations GPIO.cleanup() #to clear everything up
def End(): gpio.cleanup() return False