Ejemplo n.º 1
0
Archivo: run.py Proyecto: gcshort/MRDC
    def __init__(self):

        #setup gpio
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(12, GPIO.OUT)

        self.mcp = analog.Analog()

        self.motor2 = motor.Motor(19, 20)
        self.motor1 = motor.Motor(26, 21)

        self.sonic1 = sonic.Sonic(18, 23)
        self.sonic4 = sonic.Sonic(25, 24)
        self.sonic2 = sonic.Sonic(16, 12)
        self.sonic3 = sonic.Sonic(6, 13)
        self.sonic5 = sonic.Sonic(22, 5)

        self.sense = heading.Heading()

        self.data_status = False

        self.wpath = os.getcwd() + "/data/" + time.strftime("%H%M%S") + ".csv"
        self.file = open(self.wpath, "w")
        self.file.write("time, motor, pwm\n")

        with open('data/saved_net.pickle', 'rb') as handle:
            self.net = pickle.load(handle)


#   self.sensor_collect = open("data/sensors.txt","w")
#   self.sensor_collect.write("time, sonic1, sonic2, sonic3, sonic4, sonic5\n")

        self.data = list()
        self.collecting = False

        #setup controller values
        self.state = 0

        #create xbox controller class
        self.xboxCont = XboxController.XboxController(joystickNo=0,
                                                      deadzone=0.1,
                                                      scale=1,
                                                      invertYAxis=False)

        #setup call backs
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.A,
                                           self.aButtonCallBack)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.BACK,
                                           self.backButton)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.LTHUMBY,
                                           self.LthumbY)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.RTHUMBY,
                                           self.RthumbY)

        #start the controller
        self.xboxCont.start()

        self.running = True
Ejemplo n.º 2
0
    def __init__(self):
        print("Start")

        robohat.init()

        self.obj_to_detect = 'None'

        self.xValue = 0.0
        self.yValue = 0.0

        self.lastL = robohat.irLeft()
        self.lastR = robohat.irRight()

        self.servo_tilt = 22
        self.servo_pan = 18
        gpio.setup(self.servo_tilt, gpio.OUT)
        gpio.setup(self.servo_pan, gpio.OUT)
        self.tilt_pwm = gpio.PWM(self.servo_tilt, 200)
        self.pan_pwm = gpio.PWM(self.servo_pan, 200)
        self.tilt_pwm.start(16)  # 16-40
        self.pan_pwm.start(14)  # 14-20
        time.sleep(0.5)

        self.mode = 1  # 1-AUTONOMOUS 2-Joystick

        self.xboxCont = XboxController.XboxController(joystickNo=0,
                                                      scale=1,
                                                      invertYAxis=False)

        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.LTHUMBX,
                                           self.leftThumbX)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.LTHUMBY,
                                           self.leftThumbY)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.BACK,
                                           self.backButton)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.BACK,
                                           self.backButton)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.RB,
                                           self.rb)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.LB,
                                           self.lb)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.Y,
                                           self.buttY)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.X,
                                           self.buttX)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.A,
                                           self.buttA)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.B,
                                           self.buttB)

        self.xboxCont.start()

        self.running = True
Ejemplo n.º 3
0
Archivo: run.py Proyecto: gcshort/MRDC
    def __init__(self):

        #setup gpio ### DO WE NEED THIS? -> WALL MOTOR AND SERVO?? BETTER TO BE BOTH SERVOS?
        # GPIO.setmode(GPIO.BCM)
        # GPIO.setwarnings(False)
        # GPIO.setup(12,GPIO.OUT)
        print("initializing...")
        time.sleep(1)
        self.data_status = False

        self.data = list()
        self.collecting = False

        self.ser = serial.Serial()
        self.ser.baudrate = 9600
        self.ser.port = '/dev/ttyUSB0'

        # wrapper index for drive callback
        self.ind = 0

        #setup controller values
        self.state = 0

        #create xbox controller class ### ENSURE THAT THESE INITIAL VALUES ARE CORRECT
        self.xboxCont = XboxController.XboxController(deadzone=50,
                                                      scale=255,
                                                      invertYAxis=True)

        #setup call backs ### NEED CALLBACKS FOR DRIVING, WALL, AND HOPPER: send state value over serial port and do motor control math
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.A,
                                           self.WallCallBack)  # wall servo
        self.xboxCont.setupControlCallback(
            self.xboxCont.XboxControls.RTRIGGER,
            self.HopperCallBack)  # hopper window motor
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.LTRIGGER,
                                           self.HopperCallBack)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.LTHUMBY,
                                           self.DriveCallBack)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.LTHUMBX,
                                           self.DriveCallBack)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.RTHUMBX,
                                           self.DriveCallBack)

        #start the controller
        self.xboxCont.start()

        self.running = True

        self.ser.open()
        print('Serial port is open')
Ejemplo n.º 4
0
    def __init__(self):
        self.state = 0
        self.idle = 0
        self.speed = 0
        self.idle_threshold = 200
        self.robot = Hexapod()
        self.driver = ServoDriver()
        self.robot.connectDriver(self.driver)
        self.action = [
            True, False, False, False, False, False, False, False, False,
            False, False
        ]
        self.pad = XboxController.XboxController(
            None, deadzone=30, scale=100,
            invertYAxis=True)  #only for the pygame init...
        self.mode = self.Mode.NORMAL
        self.calibration = 0
        self.loadCalibrationData()
        self.display = Display()
        print self.robot.legs[0].coxa.getCommand()

        #Action mapping
        self.command = {
            self.Mode.NORMAL: {
                self.EventTypes.ANALOG: {
                    self.Axis.LS_X: self.moveStrafe,
                    self.Axis.LS_Y: self.moveForward,
                    self.Axis.LT: self.moveLower,
                    self.Axis.RT: self.moveRise
                },
                self.EventTypes.BUTTON: {
                    self.Button.SELECT: self.calibrationMode
                }
            },
            self.Mode.CALIBRATION: {
                self.EventTypes.ANALOG: {
                    self.Axis.LS_Y: self.adjust,
                },
                self.EventTypes.BUTTON: {
                    self.Button.SELECT: self.normalMode,
                    self.Button.Y: self.selectJoint
                }
            }
        }
Ejemplo n.º 5
0
def sendCommands(IP, PORT, protocol):
    global exitThread
    # setup tcp socket to send commands to control rover
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # xbox controller
    xbox = XboxController.XboxController()
    # read inputs from xbox one controller
    while exitThread != True:
        try:
            # get input from the xbox controller
            (l,r) = xbox.readController()
            # command to send to rover
            command = [l,r]
            # serialize data
            dataToSend = pickle.dumps(command)
        except (inputs.UnpluggedError, OSError):
            # reload inputs
            importlib.reload(inputs)
            # controller disconnected, stop the motors
            dataToSend = pickle.dumps([1500,1500])
        try:
            sock.sendto(dataToSend, (IP['rover'], PORT['command']))
        except (ConnectionResetError, OSError):
            print('lost connection to rover, reconnecting...')
Ejemplo n.º 6
0
    def _init__(self):
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(16, GPIO.OUT)
        GPIO.setup(20, GPIO.OUT)
        GPIO.setup(21, GPIO.OUT)
        self.c1 = GPIO.PWM(16, 50)
        self.c2 = GPIO.PWM(20, 50)
        self.c3 = GPIO.PWM(21, 50)

        self.xValue = 0
        self.yValue = 0
        self.pwm = 0
        self.c1.start(pwm)
        self.c2.start(pwm)
        self.c3.start(pwm)

        self.xboxCont = XboxController.XboxController(deadzone=25,
                                                      joystickNo=0,
                                                      scale=100,
                                                      invertYAxis=True)
        self.xboxCont.setupControlCallback(self.xboxCont.XboxControls.LTRIGGER,
                                           self.c1)
        self.xboxCont.start()
        self.running = True

        def c1Funct(self, xValue):
            self.xValue = xValue
            self.updateChannel()
            print "Got to c1Func"

        def updateChannel(self):
            print xValue
            if (self.xValue > 8000):
                self.pwm = 100
            elif (self.xValue > 6000):
                self.pwm = 90
            elif (self.xValue > 4000):
                self.pwm = 80
            elif (self.xValue > 2000):
                self.pwm = 60
            elif (self.xValue > 0):
                self.pwm = 50
            elif (self.xValue > -2000):
                self.pwm = 40
            elif (self.xValue > -4000):
                self.pwm = 30
            elif (self.xValue > -6000):
                self.pwm = 20
            elif (self.xValue > -8000):
                self.pwm = 10
            else:
                self.pwm = 0

            if (pwm != 0):
                self.c1.changeDutyCycle(pwm)
                print "c1 has power"
            else:
                self.c1.stop()
                print "No power"
                self.xboxCont.stop()
last = time.time()

if __name__ == '__main__':

    def controlCallBack(xboxControlId, value):
        #now = time.time()
        #if (now - last > 0.05):
        l = xboxCont.controlValues.values()
        myv = l[0:len(l) - 1] + [l[len(l) - 1][0], l[len(l) - 1][1]]
        liblo.send(target, "/xbox", *myv)
        print "%s" % myv

    xboxCont = xb.XboxController(controlCallBack,
                                 joystickNo=0,
                                 deadzone=30,
                                 scale=100,
                                 invertYAxis=True)

    try:
        xboxCont.start()
        while True:
            time.sleep(1)

    except KeyboardInterrupt:
        print "User cancelled"

    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise
Ejemplo n.º 8
0
	ltx,lty = Left Thumbstick X axis or Y axis
	"""

if __name__ == '__main__':
    #establishing socket connections
    HOST = '192.168.1.125'
    PORT = 9500
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))

    #instantiates the generic xbox Controller settings
    xboxCont = XboxController.XboxController(
        controllerCallBack=None,
        joystickNo=0,
        deadzone=0.5,
        #deadzone controls the sensitivity(minimum value the controller should pick up on)
        scale=1,
        #scale controlls map and constrain
        #Ex. when scale = 1, takes whatever input from controller and change values to
        #something between -1 and 1
        invertYAxis=True)

    def ltxCall(val):
        #pwm takes val from -1 to 1
        #and changes it from 1 to 100 for GPIO ChangeDutyCycle
        #might seem redundant to first set scale to 1, but by default it will consider
        #negative values.
        pwm = (val + 1) / .02
        pwm = round(pwm, 0)
        if pwm > 90:
            pwm = 90.0
        elif pwm < 10:
Ejemplo n.º 9
0
import blinkt
from blinkt import set_pixel, set_brightness, show, clear
import time
import XboxController

xboxCont = XboxController.XboxController(controllerCallBack=None,
                                         joystickNo=0,
                                         deadzone=0.1,
                                         scale=1,
                                         invertYAxis=False)

xboxCont.start()


def startButtonCallBack(value):
    clear()
    show()
    set_pixel(0, 238, 17, 0)
    set_brightness(1)
    show()
    time.sleep(0.5)
    clear()
    show()


xboxCont.setupControlCallback(xboxCont.XboxControls.START, startButtonCallBack)
Ejemplo n.º 10
0
#	reverseTurnRight()
#	sleep(1)
#	allStop()


def myCallBack(value):
    if value[0] == 0 and value[1] == 1:
        forwardDrive()
    elif value[0] == 0 and value[1] == -1:
        reverseDrive()
    elif value[0] == -1 and value[1] == 0:
        spinLeft()
    elif value[0] == 1 and value[1] == 0:
        SpinRight()
    elif value[0] == -1 and value[1] == 1:
        forwardTurnLeft()
    elif value[0] == 1 and value[1] == 1:
        forwardTurnRight()
    elif value[0] == -1 and value[1] == -1:
        reverseTurnLeft()
    elif value[0] == 1 and value[1] == -1:
        reverseTurnRight()
    else:
        allStop()


xboxCont = XboxController.XboxController()
xboxCont.setupControlCallback(17, myCallBack)

xboxCont.start()
Ejemplo n.º 11
0
        #PCA9685_pwm.set_pwm(servo,0,currentPos[servo])
    else:
        currentPos[servo] = currentPos[servo]+ pos*step
        if currentPos[servo]>=servoMax[servo]:
            currentPos[servo] = servoMax[servo]
        elif currentPos[servo]<=servoMin[servo]:
            currentPos[servo]=servoMin[servo]
        currentPos[servo] = int(currentPos[servo])
        #print("Servo",servo,"moves to:",currentPos[servo])
    PCA9685_pwm.set_pwm(servo,0,currentPos[servo])
    print("Servo",servo,"moves to:",currentPos[servo])
init()

xboxCont = XboxController.XboxController(
        controllerCallBack = myCallBack,
        joystickNo = 0,
        deadzone = 30,
        scale = 100,
        invertYAxis = False)
try:
    xboxCont.start()
    print("xbox controller running")
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print("user canceled")
    xboxCont.stop()
"""
def fun(button):
    if button == 0:
        val=str(var0.get())
        selection = "Pos",button," = " + val
##################################
#              MAIN              #
##################################        

from gameover_model import *
import tensorflow as tf

# global Queue
queue = []

#global command variable
commands = []

# global read XboxController
controller = XboxController()

# actions_analysis
actions_analysis = [(controller.read(),time.time())]

random_file_number = str(int(np.round(np.random.random()*1000,0)))

if __name__ == '__main__':
    
    # Generating Time Model
    time_model = generate_time_model()
    
    print('Virtual Gamepad created. \nReady to play videogames!')
    
    """
        SCREENSHOT DISPLAY
Ejemplo n.º 13
0
import XboxController
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
c1 = GPIO.PWM(16, 50)
c2 = GPIO.PWM(20, 50)
c3 = GPIO.PWM(21, 50)

xboxCont = XboxController.XboxController(controllerCallBack=None,
                                         deadzone=30,
                                         joystickNo=0,
                                         scale=100,
                                         invertYAxis=True)
"""def c1Callback(value):
	if(value > 8000):
                pwm = 100
        elif(value > 6000):
                pwm = 90
        elif(value > 4000):
                pwm = 80
        elif(value > 2000):
                pwm = 60
        elif(value > 0):
                pwm = 50
        elif(value > -2000):
                pwm = 40
Ejemplo n.º 14
0
def main():
    #IP = '127.0.0.1'
    IP = '192.168.1.100'
    server_port = 2000

    #IPv4 and tcp/ip
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((IP, server_port))

    global prevDirection
    global mode
    global freeze
    freeze = False
    prevDirection = 1
    mode = 0
    
    # map of game world goes here
    # previous position value goes here
    
    threads = []

    try:
        mode = int(sys.argv[1])
        if(mode > 2 or mode < 0):
            print 'Error! Usage: \n 0 == normal operation, 1 == run tests, 2 == add optional debug output'
            return
        if(mode == 1):
            print 'Run Tests'
        elif(mode == 2):
            print 'Running with optional debug output'              
    except:
        mode = 0

    # Attempt to start xbox controller
    try:
        xboxCont = XboxController.XboxController(controllerCallBack = None, deadzone = 30, scale = 100, invertYAxis = True)
        xboxCont.setupControlCallback(xboxCont.XboxControls.A, ACallBack)
        xboxCont.setupControlCallback(xboxCont.XboxControls.B, BCallBack)
        xboxCont.setupControlCallback(xboxCont.XboxControls.START, StartCallBack)
        xboxCont.setupControlCallback(xboxCont.XboxControls.BACK, BackCallBack)
        xboxCont.setupControlCallback(xboxCont.XboxControls.DPAD, movementCallBack)
        xboxCont.setupControlCallback(xboxCont.XboxControls.RB, RBCallBack)
        xboxCont.setupControlCallback(xboxCont.XboxControls.LB, LBCallBack)
        xboxCont.setupControlCallback(xboxCont.XboxControls.X, XCallBack)
        xboxCont.setupControlCallback(xboxCont.XboxControls.Y, YCallBack)
        xboxCont.start()
    except:
        print 'Xbox Controller unable to start. Please make sure the controller is on and restart the server.'
        sys.exit()
    
    while 1:    
        try:        
            s.listen(3)
            print ("Listening...")
            (conn, (ip,port)) = s.accept()
            conns[ip] = conn
            print (str(len(conns))+" connections are running")
            message_queues[conn] = Queue.Queue()
            t = multiClients(ip, port, conn, mode, message_queues, 'started')
            t.name = ip
            t.start()
            threads.append(t)
            for thr in threads: # todo check this actually works
                if not thr.isAlive():
                    print 'thread crashed'
                    with message_queues[conns[thr.getName()]].mutex:
                        message_queues[conns[thr.getName()]].queue.clear()
                    conns[thr.getName()].close()
                    del conns[thr.getName()]
                    threads.remove(thr)
        except KeyboardInterrupt:
            for c in conns:                
                #c.send("server is closed")
                with message_queues[conns[c]].mutex:
                    message_queues[conns[c]].queue.clear()
                    del message_queues[conns[c]]
##                threads[conns[c]].join()
##                threads.remove(conns[c])
                conns[c].close()
##            del conns[c]
##            for t in threads:
##                t.join()
##                threads.remove(t)
            xboxCont.stop()
            break
        except socket.timeout:
            pass
Ejemplo n.º 15
0
    def rightThumbY(value):
        msg = 'RY,' + str(value) + ";"
        s.sendall(msg)

    def aTrigger(value):
        msg = 'AT,' + str(value) + ";"
        s.sendall(msg)

    def bTrigger(value):
        msg = 'BT,' + str(value) + ";"
        s.sendall(msg)

    #setup xbox controller, set out the deadzone and scale, also invert the Y Axis (for some reason in Pygame negative is up - wierd!
    xboxCont = XboxController.XboxController(controlCallBack,
                                             deadzone=30,
                                             scale=100,
                                             invertYAxis=True)

    #setup the left thumb (X & Y) callbacks
    xboxCont.setupControlCallback(xboxCont.XboxControls.LTHUMBX, leftThumbX)
    xboxCont.setupControlCallback(xboxCont.XboxControls.LTHUMBY, leftThumbY)
    xboxCont.setupControlCallback(xboxCont.XboxControls.RTHUMBX, rightThumbX)
    xboxCont.setupControlCallback(xboxCont.XboxControls.RTHUMBY, rightThumbY)
    xboxCont.setupControlCallback(xboxCont.XboxControls.A, aTrigger)
    xboxCont.setupControlCallback(xboxCont.XboxControls.B, bTrigger)

    try:
        #create an AF_INET, STREAM socket (TCP)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except:
        print 'Failed to create socket. Error code: ' + str(