def run(self): self.Ultrasonic=Ultrasonic() self.Pwm=Servo() credentials = pika.PlainCredentials('newuser1', 'password') connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.1.12', 5672 ,'/', credentials)) channel = connection.channel() channel.exchange_declare(exchange='PC2Pi.topic',exchange_type='topic', durable=True, auto_delete=False) channel.queue_declare(queue='PC2Pi.Processed_Data') channel.queue_bind(queue='PC2Pi.Processed_Data', exchange='PC2Pi.topic', routing_key='Processed_Data') Data_Copy = open('Data_PI.csv', 'w') Data_Copy.write("Date;Movement_Type;\n") while True: def Write_into_CSV(body): x = format(body).split("%") Data_Copy = open('Data_PI.csv', 'a') Movement_Type = int(x[1]) Data_Copy.write("%s;"%time.ctime()) Data_Copy.write("%d;"%Movement_Type) Data_Copy.close() if(Movement_Type == 5): PWM.setMotorModel(600,600,600,600) elif(Movement_Type == 1): PWM.setMotorModel(-2000,-2000,4000,4000) elif(Movement_Type == 3): PWM.setMotorModel(-1200,-1200,2000,2000) elif(Movement_Type == 7): PWM.setMotorModel(2000,2000,-1200,-1200) elif(Movement_Type == 9): PWM.setMotorModel(4000,4000,-2000,-2000) elif(Movement_Type == 0): PWM.setMotorModel(0,0,0,0) def callback(ch, method, properties, body): print('Received from PC: {}'.format(body)) Write_into_CSV(body) channel.basic_consume(queue='PC2Pi.Processed_Data', on_message_callback=callback, auto_ack=True) print('Waiting for Messages from PC') channel.start_consuming()
def __init__(self): self.tcp_flag = False self.led = Led() self.adc = ADS7830() self.servo = Servo() self.buzzer = Buzzer() self.control = Control() self.sonic = Ultrasonic() self.control.Thread_conditiona.start()
def __init__(self): self._interrupted = False self._lock = threading.RLock() self._cmdThread = None self.motor = Motor.Motor() self.sonic = Ultrasonic.Ultrasonic() self.adc = ADC.Adc() self.servo = servo.Servo() self.stop() self.pointSonic()
def __init__(self, sonicTrig, sonicEcho, servoChannel): self.trigger = sonicTrig self.echo = sonicEcho self.servoChannel = servoChannel if self._DEBUG: print self._DEBUG_INFO, "Debug on" self.servo = Servo.Servo(servoChannel, self.servoStart, self.servoStart - 1, self.servoStop) self.ultra = Ultrasonic.Ultrasonic(sonicTrig, sonicEcho)
def run(): pwm_S = Servo() pwm_S.setServoPwm('0', SERVO_ORIZON) pwm_S.setServoPwm('1', SERVO_VERTIC) ultrasonic = Ultrasonic() cli = Redis('localhost') while True: M = ultrasonic.get_distance() cli.set('ultrasonic_M', M) time.sleep(0.2) """
def __init__(self): self.PWM = Motor() self.servo = Servo() self.led = Led() self.ultrasonic = Ultrasonic() self.buzzer = Buzzer() self.adc = Adc() self.light = Light() self.infrared = Line_Tracking() self.tcp_Flag = True self.sonic = False self.Light = False self.Mode = 'one' self.endChar = '\n' self.intervalChar = '#'
time.sleep(1) PWM.setMotorModel(-1500, -1500, 2000, 2000) #Left print "The car is turning left" time.sleep(1) PWM.setMotorModel(2000, 2000, -1500, -1500) #Right print "The car is turning right" time.sleep(1) PWM.setMotorModel(0, 0, 0, 0) #Stop print "\nEnd of program" except KeyboardInterrupt: PWM.setMotorModel(0, 0, 0, 0) print "\nEnd of program" from Ultrasonic import * ultrasonic = Ultrasonic() def test_Ultrasonic(): try: pwm = Servo() pwm.setServoPwm('0', 90) while True: data = ultrasonic.get_distance() #Get the value print("Obstacle distance is " + str(data) + "CM") time.sleep(1) except KeyboardInterrupt: print "\nEnd of program" from Line_Tracking import *
def run(self): self.Ultrasonic = Ultrasonic() self.Pwm = Servo() credentials = pika.PlainCredentials('newuser1', 'password') connection = pika.BlockingConnection( pika.ConnectionParameters('192.168.1.12', 5672, '/', credentials)) channel = connection.channel() channel.exchange_declare(exchange='PC2Pi.topic', exchange_type='topic', durable=True, auto_delete=False) channel.queue_declare(queue='PC2Pi.Processed_Data') channel.queue_bind(queue='PC2Pi.Processed_Data', exchange='PC2Pi.topic', routing_key='Processed_Data') while True: self.LMR = 0x00 if GPIO.input(IR01) == True: self.LMR = (self.LMR | 4) if GPIO.input(IR02) == True: self.LMR = (self.LMR | 2) if GPIO.input(IR03) == True: self.LMR = (self.LMR | 1) self.Pwm.setServoPwm('0', 90) self.Pwm.setServoPwm('1', 90) distance = self.Ultrasonic.get_distance() IRSensor1 = (GPIO.input(IR01) == True) IRSensor2 = (GPIO.input(IR02) == True) IRSensor3 = (GPIO.input(IR03) == True) if distance > 10: if self.LMR == 2: PWM.setMotorModel(575, 575, 575, 575) elif self.LMR == 4: PWM.setMotorModel(-1500, -1500, 2500, 2500) elif self.LMR == 6: PWM.setMotorModel(-2000, -2000, 4000, 4000) elif self.LMR == 1: PWM.setMotorModel(2500, 2500, -1500, -1500) elif self.LMR == 3: PWM.setMotorModel(4000, 4000, -2000, -2000) elif self.LMR == 7: pass else: PWM.setMotorModel(0, 0, 0, 0) message1 = time.ctime() + " Distance to Obstacle: "+ str(distance)+ \ "%" + "IR Sensor1: %"+ str(IRSensor1)+"%"+ "IR Sensor2: %"+ str(IRSensor2)+"%" \ + "IR Sensor3: %"+ str(IRSensor3)+"%" channel.basic_publish( exchange='Pi2PC.topic', routing_key='Cart101.ShoppingMall.DistanceIRSensorVal', body=message1) print('Sent_1' + message1) time.sleep(timeout)
def Run_Cart4(self): #------------------------------------------Ultrasonic sensor and Servo Motor Class self.Ultrasonic = Ultrasonic() self.LED = Led() self.Buzzer = Buzzer() self.Junction = 0 #-------------------------------------------------------------------------------------- while True: self.Movement_Type = 0 Distance = self.Ultrasonic.get_distance() IR_Left = GPIO.input(IR01) IR_Mid = GPIO.input(IR02) IR_Right = GPIO.input(IR03) if (Distance > 15.0): if ((IR_Left == 0) and (IR_Mid == 1) and (IR_Right == 0)): if (self.Moved_Reverse_New != self.Moved_Reverse_Old): self.Movement_Type = 11 #Move Reverse else: self.Movement_Type = 5 #Move Forward self.Junction = 0 #Clear the Junction Variable elif ((IR_Left == 1) and (IR_Mid == 0) and (IR_Right == 0)): self.Movement_Type = 3 #Take Slight Left elif ((IR_Left == 1) and (IR_Mid == 1) and (IR_Right == 0)): self.Movement_Type = 3 #Take Slight Left elif ((IR_Left == 0) and (IR_Mid == 0) and (IR_Right == 1)): self.Movement_Type = 7 #Take Slight Right elif ((IR_Left == 0) and (IR_Mid == 1) and (IR_Right == 1)): self.Movement_Type = 7 #Take Slight Right elif ((IR_Mid == 1) and (IR_Left == 1) and (IR_Right == 1)): if (self.Junction == 0): self.Movement_Type = self.Mov_According_To_Specified_position( ) self.Junction = 1 else: pass else: self.Movement_Type = 0 #Stop Movement self.LED.ledIndex(0x20, 255, 125, 0) #Orange self.LED.ledIndex(0x40, 255, 125, 0) #Orange time.sleep(1) self.LED.ledIndex(0x60, 0, 0, 0) #Actuate the motors to move cart to the specific Coordinate self.Move_Cart(self.Movement_Type) if (self.Is_Intermediate_DestinTion_Reached()): break
from Ultrasonic import * #import Led ultrasonic=Ultrasonic() #create an object data=ultrasonic.get_distance() #Get the value print ("Obstacle distance is "+str(data)+"CM") from servo import * #import Led pwm = Servo() #create an object #Servo rotates from 30 degrees to 150 degrees for i in range(30, 150, 1) : pwm.setServoPwm('0', i) time.sleep(0.01) #Servo rotates from 150 degrees to 0 degrees for i in range(150, 30, -1) : pwm.setServoPwm('0', i) time.sleep(0.01) from Motor import * #import Motor PWM=Motor() #create an object PWM.setMotorModel(2000,2000,2000,2000) #Forward time.sleep(3) #waiting 3 second PWM.setMotorModel(0,0,0,0) #Stop
def dist(): ultrasonic = Ultrasonic() # create an object data = ultrasonic.get_distance() # Get the value print("Obstacle distance is " + str(data) + "CM") return data
def run_ultrasonic(self): IR01 = 14 IR02 = 15 IR03 = 23 GPIO.setmode(GPIO.BCM) GPIO.setup(IR01, GPIO.IN) GPIO.setup(IR02, GPIO.IN) GPIO.setup(IR03, GPIO.IN) self.LMR0 = 0x00 if GPIO.input(IR01) == False: self.LMR0 = (self.LMR0 | 4) if GPIO.input(IR02) == False: self.LMR0 = (self.LMR0 | 2) if GPIO.input(IR03) == False: self.LMR0 = (self.LMR0 | 1) if self.LMR0 > 0: self.LMR0 = 7 ttable = [[1, 0], [2, 4], [3, 5], [1, 1], [0, 0], [0, 0]] x = 40 ultra = Ultrasonic() print "Auto drive Start!" cur_state = 0 while self.automode: if cur_state == 0: self.display.show(1, "FORWARD") self.PWM.slowforward() ultra.look_forward() elif cur_state == 1: self.display.show(1, "LOOKLEFT") self.PWM.stopMotor() ultra.look_left() elif cur_state == 2: self.display.show(1, "LOOKRITE") self.PWM.stopMotor() ultra.look_right() elif cur_state == 3: self.display.show(1, "GOBACK") self.PWM.backup() elif cur_state == 4: self.display.show(1, "TURNLEFT") self.PWM.turnLeft() time.sleep(0.2) elif cur_state == 5: self.display.show(1, "TURNRITE") self.PWM.turnRight() time.sleep(0.2) else: print "Wrong state?" cur_state = 0 time.sleep(0.1) d = ultra.get_distance() self.LMR = 0x00 if GPIO.input(IR01) == False: self.LMR = (self.LMR | 4) if GPIO.input(IR02) == False: self.LMR = (self.LMR | 2) if GPIO.input(IR03) == False: self.LMR = (self.LMR | 1) if (self.LMR <> self.LMR0): cur_state = 3 else: e = 0 if d < x else 1 cur_state = ttable[cur_state][e] print "Auto drive End!"
import json import pprint import sys import threading import netifaces as ni import websocket import Servo import Ultrasonic pp = pprint.PrettyPrinter(indent=4) servo = Servo.Servo() ultra = Ultrasonic.Ultrasonic() try: import thread except ImportError: import _thread as thread def on_message(ws, message): pp.pprint(message) _message = json.loads(message) event = _message['d']['event'] if event == 'start_servo': # print(1) activate_servo()