Example #1
0
def main():
    # Local functions in main function
    global nextLocationIndex
    global locations
    global roverMotor
    botCentered = False
    locationAccuracy = 0.00001

    print("Setting devices...")
    compass = Compass("/dev/ttyACM0")
    # roverMotor = Motor("/dev/ttyACM2")
    roverGps = RoverGps()
    # roverMotor.resetAllMotors()
    print("All device set!")
    sleep(2)

    # Set the bot to point at next location
    while not botCentered:
        if centerBot(compass, 0, roverGps, roverMotor, 10):
            botCentered = True
        # sleep(0.1)
        # os.system("clear")
    # botCentered=False
    # roverMotor.moveMotor("stop")
    os.system("clear")
    print("Rover centered!")
    # roverMotor.resetAllMotors()
    sleep(2)

    print("Locations:", locations)
    input('Press anything to continue!')

    # =========
    # Main Loop
    # =========
    while nextLocationIndex < len(locations):
        # roverMotor.resetAllMotors()
        try:
            currentLocation = roverGps.getGpsData()
            # print(roverGps)
        except ValueError:
            print("GPS not set")
            continue
        if currentLocation[0] == None:
            print("GPS no location")
            continue

        if abs(currentLocation[0] -
               locations[nextLocationIndex][0]) < locationAccuracy and abs(
                   currentLocation[1] -
                   locations[nextLocationIndex][1]) < locationAccuracy:
            # roverMotor.moveMotor("stop")
            nextLocationIndex += 1
            if nextLocationIndex >= len(locations):
                break
            print(locations)
            print("Location Reached!", currentLocation)
            print("Press any key to continue")
            input()
            # sleep(2)
            # Center bot to north
            botCentered = False
            while not botCentered:
                # os.system("clear")

                if centerBot(compass, 0, roverGps, roverMotor, 20):
                    print()
                    botCentered = True
            botCentered = False
            print("Press anything to continue to location",
                  locations[nextLocationIndex])
            input()
            continue

        slope = getSlope(currentLocation)
        # Move the rover to this slope
        while not botCentered:
            # os.system("clear")
            slope = getSlope(currentLocation)

            if centerBot(compass, slope, roverGps, roverMotor, 15):
                botCentered = True
            # sleep(0.5)
        if not centerBot(compass, slope, roverGps, roverMotor, 15):
            print()
            botCentered = False

        # # Move bot forward
        # # os.system("clear")
        try:
            print("Move Forward",
                  roverGps.getGpsData(), locations[nextLocationIndex], slope,
                  compass.getCompassAngle())
            # roverMotor.moveMotor('forward')
        except ValueError:
            print("Compass Value error")
    print("Finished!")
Example #2
0
def main():
    try:
        Gps = Gps()
        compass = Compass()
        global nextLocationIndex
        global locations
    except Exception as e:
        print(str(e))
        print("Error in creating the objects")
        return
    try:
        while 1:
            try:
                for _i in range(len(locations)):
                    Reached = False
                    while(not Reached):
                        currentLocation = Gps.getGpsData()
                        slope = getSlope(currentLocation)
                        print(centerBot(compass, slope, Gps, 10))
                        Reached = isReached(Gps)
                        if(Reached):
                            sock.send("27".encode())
                            input("Reached "+str(nextLocationIndex) +
                                  " Press anything to continue")
                        sleep(0.1)
                    nextLocationIndex += 1
            except Exception as e:
                print(str(e))
            PORT = '/dev/ttyUSB0'
            BUFFER_SIZE =  3

            ser = serial.Serial(PORT)
            command = -1
            ignoreCommand = False
            x = -1
            toAngle = 0

            try:
                while True:
                    try:
                        data = ser.read(BUFFER_SIZE)
                        currentAngle = compass.getCompassAngle()
                        if(not ignoreCommand):
                            x=parseCommand(data)
                            print("\n")
                            if(x==27):
                                toAngle = currentAngle
                                print("Stop")
                            elif(x==87):
                                toAngle = currentAngle
                                print("Forward")
                            elif(x==65):
                                toAngle = (currentAngle - 90 + 360)%360
                                ignoreCommand = True
                                print("Left")
                            elif(x==68):
                                toAngle = (currentAngle + 90 + 360)%360
                                ignoreCommand = True
                                print("Right")
                            else:
                                print("Command Not Found ")
                        sock.send(str(x).encode())
                        os.system(cmd)
                    except Exception as e:
                        print(e)
                        ser.close()
                        ser = serial.Serial(PORT)
            except Exception as e:
                print(e) 
            finally:
                ser.close()
    except Exception as e:
        print(str(e))
    finally:
        global sock
        sock.close()
Example #3
0
def main():
	roverGps="dummy"
	global nextLocationIndex
	global locations
	botCentered = False
	locationAccuracy=0.00001
	# locationAccuracy=2
	initializedCurrentLocation=False

	print("Setting devices...")
	compass = Compass("/dev/ttyACM0")
	# roverMotor = Motor("/dev/ttyACM0")
#	roverGps = RoverGps()
	# roverMotor.resetAllMotors()
	print("All device set!")
	#sleep(10)

	# Set the bot to point at north
	while not botCentered:
		
		print("Centering Rover!",end=": ")
		if centerBot(compass, 0, roverGps, 10):
			botCentered=True
		# sleep(0.1)
		# os.system("clear")
	# botCentered=False
	
	os.system("clear")
	print("Rover centered!")
	# roverMotor.resetAllMotors()
	sleep(2)
	
	setScaledLocations()
	print(locations)
	sleep(2)
	
	while nextLocationIndex < len(locations):
	# while True:
		# roverMotor.resetAllMotors()
		try:
			currentLocation = readGPS()
#			currentLocation = roverGps.getGpsData()
			# print(roverGps)
			print(readGPS())
#			print(roverGps.getGpsData())
		except ValueError:
			print("GPS not set")
			continue
		if currentLocation[0]==None:
			print("GPs no locayion")
			continue
			
		if abs(currentLocation[0]-locations[nextLocationIndex][0])<locationAccuracy and  abs(currentLocation[1]-locations[nextLocationIndex][1])<locationAccuracy:
			nextLocationIndex+=1
			if nextLocationIndex>=len(locations):
				break
			print(locations)
			print("Location Reached!", currentLocation)
			print("Press any key to continue")
			input()
			# sleep(2)
			# Center bot to north
			botCentered=False
			while not botCentered:
				# os.system("clear")
				print("Centering Rover!",end=": ")
				if centerBot(compass, 0, roverGps, 20):
					print()
					botCentered=True
			botCentered=False
			print("Press anything to continue to location", locations[nextLocationIndex])
			input()
			continue
		
		slope = getSlope(currentLocation)
		# Move the rover to this slope    
		while not botCentered:
			# os.system("clear")
			slope = getSlope(currentLocation)
			try:
				print("Centering Rover ", end=': ')
			except ValueError:
				print("Value Error")
			if centerBot(compass, slope, roverGps, 15):
				botCentered=True
			# sleep(0.5)
		if not centerBot(compass, slope, roverGps, 15):
			print()
			botCentered=False

		# Move bot forward
		# os.system("clear")
		try:
			print("Move Forward", readGPS(), slope, compass.getCompassAngle(), abs(slope)-abs(compass.getCompassAngle()))
			#roverMotor.forwardMotor()
		except ValueError:
			print("Compass Value error")
	print("Finished!")
Example #4
0
def main():
    # Local functions in main function
    global nextLocationIndex
    global locations
    global roverMotor
    botCentered = False
    locationAccuracy = 0.000005

    print("Setting devices...")
    compass = Compass("/dev/ttyACM0")
    roverMotor = Motor(('192.168.43.94', 12345))
    try:
        roverGps = RoverGps()
    except OSError:
        print("GPSD not started!")
        exit(-1)
    roverMotor.moveMotor('stop')
    print("All device set!")
    sleep(1)
    roverMotor.moveMotor('stop')
    # Set the bot to point at next location
    while not (170 < compass.getCompassAngle()
               and compass.getCompassAngle() < 180):
        print(compass.getCompassAngle())

    # os.system("clear")
    botCentered = False

    # Rotate Rover Once
    # angle = compass.getCompassAngle()-100
    # while compass.getCompassAngle()>angle or compass.getCompassAngle()<angle:
    # 	print("Calibration!")
    # 	roverMotor.moveMotor('left')

    roverMotor.moveMotor("stop")
    os.system("clear")
    print("Rover centered!")
    roverMotor.moveMotor('stop')
    sleep(2)
    # roverMotor.resetAllMotors()
    print("Locations:", locations)
    # input('Press anything to continue!')
    sleep(2)

    # =========
    # Main Loop
    # =========
    while nextLocationIndex < len(locations):
        # roverMotor.resetAllMotors()
        try:
            currentLocation = roverGps.getGpsData()
            # print(roverGps)
        except ValueError:
            print("GPS not set")
            continue
        if currentLocation[0] == None:
            print("GPS no location")
            continue

        if abs(currentLocation[0] -
               locations[nextLocationIndex][0]) < locationAccuracy and abs(
                   currentLocation[1] -
                   locations[nextLocationIndex][1]) < locationAccuracy:
            roverMotor.moveMotor("stop")
            nextLocationIndex += 1
            if nextLocationIndex >= len(locations):
                break
            print(locations)
            print("Location Reached!", currentLocation)
            print("Coninue to next location")
            # input()
            sleep(2)
            # Center bot to north
            botCentered = False
            while not botCentered:
                # os.system("clear")

                if centerBot(compass, 0, roverGps, roverMotor, 20):
                    print()
                    botCentered = True
            botCentered = False
            print("Continue to location", locations[nextLocationIndex])
            # input()
            continue

        slope = getSlope(currentLocation)
        # Move the rover to this slope
        while not botCentered:
            # os.system("clear")
            slope = getSlope(currentLocation)

            if centerBot(compass, slope, roverGps, roverMotor, 10):
                botCentered = True
            # sleep(0.5)
        if not centerBot(compass, slope, roverGps, roverMotor, 10):
            print()
            botCentered = False

        # # Move bot forward
        # # os.system("clear")
        try:
            print("Move Forward",
                  roverGps.getGpsData(), locations[nextLocationIndex], slope,
                  compass.getCompassAngle())
            # roverMotor.moveMotor('forward')
            roverMotor.moveMotor('forward')
        except ValueError:
            print("Compass Value error")
    roverMotor.moveMotor('stop')
    print("Finished!")