当前位置: 首页>>代码示例>>Python>>正文


Python Arduino.sendCommand方法代码示例

本文整理汇总了Python中arduino.Arduino.sendCommand方法的典型用法代码示例。如果您正苦于以下问题:Python Arduino.sendCommand方法的具体用法?Python Arduino.sendCommand怎么用?Python Arduino.sendCommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在arduino.Arduino的用法示例。


在下文中一共展示了Arduino.sendCommand方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Neopixel

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import sendCommand [as 别名]
class Neopixel():
    
    def __init__(self):
        self.board = Arduino()
        self.board.connect()

    def reset(self):
        return self.board.sendCommand(Commands.RESET_NEO_PIXELS,0,0)

    def writePixel(self,pixel,r,g,b):
        return self.board.sendCommand(Commands.WRITE_NEO_PIXEL,0,pixel,r,g,b)
开发者ID:mattvenn,项目名称:v2,代码行数:13,代码来源:neopixel.py

示例2: Encoder

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import sendCommand [as 别名]
class Encoder():

    def __init__(self):
        self.board = Arduino()
        self.board.connect()

    def getRevsright(self):
        rightrevs = self.board.sendCommand(Commands.READ_RIGHT_ENCODER,0,-1)
        return rightrevs

    def getRevsleft(self):
        leftrevs = self.board.sendCommand(Commands.READ_LEFT_ENCODER,0,-1)
        return leftrevs
开发者ID:mattvenn,项目名称:v2,代码行数:15,代码来源:encoder.py

示例3: Ultrasound

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import sendCommand [as 别名]
class Ultrasound():
    
    def __init__(self):
        self.board = Arduino()
        self.board.connect()

    def getDistance(self):
        distance = int(self.board.sendCommand(Commands.READ_ULTRASOUND,TRIGGER_PIN,0))
        # large distances are reported as 0, so change to max distance
        if distance == 0:
            distance = 100
        # limit to 100
        elif distance > 100:
            distance = 100
        return distance
开发者ID:mattvenn,项目名称:v2,代码行数:17,代码来源:ultrasound.py

示例4: ArduinoTracker

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import sendCommand [as 别名]
class ArduinoTracker(object):
    def __init__(self, video_src, port, baud):
        self.cam = cv2.VideoCapture(video_src)
        ret, self.frame = self.cam.read()
        cv2.namedWindow('Arduino Tracker')
        cv2.setMouseCallback('Arduino Tracker', self.mouse)

        self.selection = None
        self.drag_start = None
        self.tracking_state = 0
        self.dest = None
        self.state = 'Servo: Pivot'
        self.pivot_dir = 'l' # default pivot direction left
        self.move_dir = 'f' # default move direction forward
        if not port:
            port = '/dev/tty.usbmodem1A1341'
        if not baud:
            baud = 115200
        self.robot = Arduino(port, baud)
        self.prev_angle = 0
        self.prev_dist = 0
        self.counter = 0

    def mouse(self, event, x, y, flags, param):
        '''
        Actions on mouse click.
        '''
        if event == cv2.EVENT_LBUTTONDOWN:
            self.drag_start = (x, y)
            if self.tracking_state == 1:
                self.dest = (x,y)
                self.state = 'Idle'
            else:
                self.tracking_state = 0

        if self.drag_start:
            if flags & cv2.EVENT_FLAG_LBUTTON:
                h, w = self.frame.shape[:2]
                xo, yo = self.drag_start
                x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y]))
                x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y]))
                self.selection = None
                if x1-x0 > 0 and y1-y0 > 0:
                    self.selection = (x0, y0, x1, y1)
            else:
                self.drag_start = None
                if self.selection is not None:
                    self.tracking_state = 1

    def dist(self, pt1, pt2):
        '''
        Calculate distance between two points.
        '''
        return ((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2)**0.5

    def doAction(self, diff, center):
        '''
        Handle actions and state changes
        '''

        # Test default direction
        if self.state == 'Servo: Pivot':
            self.prev_angle = diff
            self.robot.sendCommand(self.pivot_dir)
            self.state = 'Pivot: Wait'

        # Wait 4 cycles
        elif self.state == 'Pivot: Wait':
            self.counter += 1
            if self.counter == 4:
                self.counter = 0
                self.robot.sendCommand('s')
                self.state = 'Determine Direction'

        # If default pivot direction wrong, change direction
        elif self.state == 'Determine Direction':
            if (
                    (abs(diff) > abs(self.prev_angle) and abs(self.prev_angle) < 90) or 
                    (abs(diff) < abs(self.prev_angle) and abs(self.prev_angle) > 90)  
                ):
                if self.pivot_dir == 'l':
                    self.pivot_dir = 'r'
                else:
                    self.pivot_dir = 'l'
            self.robot.sendCommand(self.pivot_dir)
            self.state = 'Turning'

        # Continue turning until angle between lines is small
        elif self.state == 'Turning':
            d = self.dist(center, self.dest)
            if d < 50:
                self.state = 'Done'
                self.robot.sendCommand('s')
            if abs(diff) < 8 or abs(diff) > 172:
                self.state = 'Correct'
                self.robot.sendCommand('s')

        # Correc the angle slightly
        elif self.state == 'Correct':
            if self.pivot_dir == 'l':
#.........这里部分代码省略.........
开发者ID:zhangxingshuo,项目名称:py-robot,代码行数:103,代码来源:ArduinoTracker.py

示例5: Mission

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import sendCommand [as 别名]
class Mission():
    
    # tested
    def __init__(self):

        if pi is True:
            self.board = Arduino()
            self.board.connect()
            self.move = Motors()
            GPIO.setup(GREEN_LED_GPIO,GPIO.OUT)
            GPIO.setup(RED_LED_GPIO,GPIO.OUT)
            GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)

        # load the sample hash
        with open(rfid_hash) as fh:
            self.rfid_hash = json.load(fh)


    # won't test
    def startMission(self):
        if pi is True:
            GPIO.output(GREEN_LED_GPIO,False)
            GPIO.output(RED_LED_GPIO,True)
            print("waiting for button...")
            while GPIO.input(BUTTON) == False:
                time.sleep(0.1)
            GPIO.output(RED_LED_GPIO,False)

    # won't test
    def endMission(self):
        if pi is True:
            GPIO.output(GREEN_LED_GPIO,True)

    # tested
    def deleteData(self):
        try:
            os.unlink(data_file)
        except OSError:
            pass

    # tested
    # stores a dict as json on the disk
    # appends to the data file
    def saveData(self, sample):
        with open(data_file,'a') as fh:
            fh.write(json.dumps(sample) + "\n")

    # tested
    # returns a list of dicts
    def loadData(self):
        data = []
        try:
            with open(data_file,) as fh:
                for sample in fh.readlines():
                    data.append(json.loads(sample))
        except IOError:
            pass
        return data

    # won't test
    # fetches an RFID
    def getLocation(self):
        if pi is True:
            rfid = ""
            # keep moving until robot gets a location
            while True:
                rfid = self.board.sendCommand(Commands.READ_RFID,0,0)
                # when I catalogued the RFIDs I missed the last char!
                rfid = rfid[0:11]
                if len(rfid) == 11:
                    break
                self.move.forward(70)  
                time.sleep(0.1)
                self.move.stop()
                time.sleep(1)
            return rfid
    
    # tested
    # indexes into the sample database with the RFID and returns sample as dict
    def takeSample(self, location):
        try:
            return self.rfid_hash[location]
        except KeyError:
            raise Exception("unknown location [%s]" % location)

    # tested
    # uploads a sample dict with a team name (string)
    def uploadSample(self, sample, team):
        # fetch team ID from nane
        r = requests.get(mc_url + '/api/team/' + team)
        if r.status_code == 400:
            raise Exception(r.text)

        team_id = json.loads(r.text)['id']
        sample['team'] = str(team_id)

        # posts it
        r = requests.post(mc_url + '/api/sample', json=sample)
        if r.status_code == 400:
            return r.text
#.........这里部分代码省略.........
开发者ID:mattvenn,项目名称:v2,代码行数:103,代码来源:mission.py

示例6: Motors

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import sendCommand [as 别名]
class Motors():
    def __init__(self):
        self.board = Arduino()
        self.board.connect()

    def enable(self):
        self.board.sendCommand(Commands.WRITE_DIGITAL,7,1)	# puts motor drive into Phase/Enable mode

    def disable(self):
        self.board.sendCommand(Commands.WRITE_DIGITAL,7,0)# puts motor drive into IN/IN mode

    def forward(self, speed):
        self.enable()
        commandL = self.speedToCommand(speed)
        commandR = self.speedToCommand(speed)
        self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_DIRECTION,1)
        self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_DIRECTION,0)
        self.board.sendCommand(Commands.WRITE_PWM,LEFT_MOTOR_SPEED,commandR)
        self.board.sendCommand(Commands.WRITE_PWM,RIGHT_MOTOR_SPEED,commandL)


    def getLeft(self):
	value = self.board.sendCommand(Commands.READ_LEFT_ENCODER,0,0)
	return value

    def getRight(self):
	value = self.board.sendCommand(Commands.READ_RIGHT_ENCODER,0,0)
	return value

    def getRightDistance(self):
        value = self.board.sendCommand(Commands.READ_RIGHT_DISTANCE,0,0)
	return value
    def getLeftDistance(self):
        value = self.board.sendCommand(Commands.READ_LEFT_DISTANCE,0,0)
	return value

    def getAtPosition(self):
        value = self.board.sendCommand(Commands.AT_POSITION,0,0)
	return value

    def backward(self, speed):
        self.enable()
        commandL = self.speedToCommand(speed)
	commandR = self.speedToCommand(speed)

	self.board.sendCommand(Commands.WRITE_PWM,LEFT_MOTOR_SPEED,commandL)
        self.board.sendCommand(Commands.WRITE_PWM,RIGHT_MOTOR_SPEED,commandR)
        self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_DIRECTION,0)
        self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_DIRECTION,1)


    def leftMotor(self, speed, direction):

        self.enable()
        command = self.speedToCommand(speed)
        if(direction==1):
            self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_DIRECTION,1)
            self.board.sendCommand(Commands.WRITE_PWM,LEFT_MOTOR_SPEED,command)
        else:
            self.board.sendCommand(Commands.WRITE_PWM,LEFT_MOTOR_SPEED,command)
            self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_DIRECTION,0)



    def rightMotor(self, speed, direction):

        self.enable()
        command = self.speedToCommand(speed)
        if(direction==1):
            self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_DIRECTION,0)
            self.board.sendCommand(Commands.WRITE_PWM,RIGHT_MOTOR_SPEED,command)
        else:
            self.board.sendCommand(Commands.WRITE_PWM,RIGHT_MOTOR_SPEED,command)
            self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_DIRECTION,1)



    def stop(self):

	self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_SPEED,0)
        self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_SPEED,0)
        #self.position(0)
        #self.rotation(0)

    def speedToCommand(self,speed):
        #speed between 0-100
        command = int(speed * 2.55)
        if(command > 255):
            command = 255
        return command

     # lower level command to move
    def position(self,pos,speed,block):
        self.board.sendCommand(Commands.POSITION,DUMMY_PIN,pos)
                
        if block:
            there=int(self.getAtPosition())
            while int(self.getAtPosition()) == 0:
                print("on my way")
                sleep(0.2)
#.........这里部分代码省略.........
开发者ID:pi-bot,项目名称:v2,代码行数:103,代码来源:motors.py


注:本文中的arduino.Arduino.sendCommand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。