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


Python Bus.sync_write方法代码示例

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


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

示例1: __init__

# 需要导入模块: from bus import Bus [as 别名]
# 或者: from bus.Bus import sync_write [as 别名]
class BioloidController:

    def __init__(self, useLogger = False):
        self.id = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        self.pose = [512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512]
        self.nextPose = [512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512]
        self.speed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        self.interpolating = False
        self.playing = False
        self.servoCount = 12
        self.lastFrame = pyb.millis()
        self.port = UART_Port(1, 1000000)
        self.bus = Bus(self.port, show=Bus.SHOW_PACKETS)
        # Bus.SHOW_NONE
        # Bus.SHOW_COMMANDS
        # Bus.SHOW_PACKETS
        if useLogger:
            self.logger = Logger('sync_log.txt')
        else:
            self.logger = None

    # Load a pose into nextPose
    def loadPose(self, poseArray):
        for i in range(self.servoCount):
            self.nextPose[i] = (poseArray[i]) # << BIOLOID_SHIFT)
            #print ('loadPose[', self.id[i], '] = ', self.nextPose[i])

    def isLogging(self):
        return self.logger is not None

    # read the current robot's pose
    def readPose(self):
        for i in range(self.servoCount):
            self.pose[i] = (self.readTwoByteRegister(self.id[i], AX_GOAL_POSITION)) # << BIOLOID_SHIFT)
            #print ('readPose[', self.id[i], '] = ', self.pose[i])
            pyb.delay(25)

    def writePose(self):
        values = []
        logging = self.isLogging()
        if logging:
            logValues = []
        for i in range(self.servoCount):
            poseValue = int(self.pose[i])
            values.append(struct.pack('<H', poseValue))
            if logging:
                logValues.append(poseValue)
        self.bus.sync_write(self.id, AX_GOAL_POSITION, values)
        if logging:
            self.logger.log(logValues)

    def slowMoveServoTo(self, deviceId, targetPosition, speed = SLOW_SERVO_MOVE_SPEED, scanFunction = None):
        oldSpeed = self.readTwoByteRegister(deviceId, AX_MOVING_SPEED)
        currentPosition = self.readTwoByteRegister(deviceId, AX_PRESENT_POSITION)
        self.writeTwoByteRegister(deviceId, AX_MOVING_SPEED, speed)
        self.writeTwoByteRegister(deviceId, AX_GOAL_POSITION, targetPosition)
        done = False
        scanCount = 0
        lastPosition = 0
        startTime = pyb.millis()
        while abs(currentPosition - targetPosition) > 5:
            currentPosition = self.readTwoByteRegister(deviceId, AX_PRESENT_POSITION)
            if scanFunction is not None:
                if currentPosition != lastPosition:
                    scanCount += 1
                    lastPosition = currentPosition
                    scanFunction(currentPosition, scanCount)
            pyb.delay(1)
        self.writeTwoByteRegister(deviceId, AX_MOVING_SPEED, oldSpeed)
        if scanFunction is not None:
            scanFunction(targetPosition, scanCount + 1)
        print("Elapsed Time: %d" % (pyb.millis() - startTime))

    def rampServoTo(self, deviceId, targetPosition):
        currentPosition = self.readTwoByteRegister(deviceId, AX_PRESENT_POSITION) # present position
        if targetPosition > currentPosition:
            stepDelta = 1
            stepAccel = 2
            comparison = lambda: targetPosition > (currentPosition + stepDelta)
        else:
            stepDelta = -1
            stepAccel = -2
            comparison = lambda: currentPosition > (targetPosition - stepDelta)
        while comparison():
            movePosition = currentPosition + stepDelta
            stepDelta += stepAccel
            self.setPosition(deviceId, movePosition)
            currentPosition = self.readTwoByteRegister(deviceId, AX_PRESENT_POSITION) # present position
            pyb.delay(25)
        self.setPosition(deviceId, targetPosition)

    def setPosition(self, deviceId, position):
        self.writeTwoByteRegister(deviceId, AX_GOAL_POSITION, position)

    def writeData(self, deviceId, controlTableIndex, byteData):
        try:
            result = self.bus.write(deviceId, controlTableIndex, byteData)
        except BusError as ex:
            if ex.get_error_code() == ErrorCode.CHECKSUM:
                print ("CHECKSUM Error - retrying...")
#.........这里部分代码省略.........
开发者ID:JonHylands,项目名称:roz,代码行数:103,代码来源:BioloidController.py


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