當前位置: 首頁>>代碼示例>>Python>>正文


Python Protocol.encode方法代碼示例

本文整理匯總了Python中protocol.Protocol.encode方法的典型用法代碼示例。如果您正苦於以下問題:Python Protocol.encode方法的具體用法?Python Protocol.encode怎麽用?Python Protocol.encode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在protocol.Protocol的用法示例。


在下文中一共展示了Protocol.encode方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: ServoCtrl

# 需要導入模塊: from protocol import Protocol [as 別名]
# 或者: from protocol.Protocol import encode [as 別名]
class ServoCtrl(ChassisDev):

    TYPE = 0x82
    # 12 bytes data package format in struct, only first byte valid
    pack_pmt = "4B2f"


    def __init__(self, serial_dev):
        self.protocol = Protocol(self.TYPE, self.pack_pmt)

        self.serial_dev = serial_dev

        ChassisDev.__init__(self, "Servo",
                            sub_topic = "/robot_chassis/Servo",
                            sub_msg_class = Byte)

    def start_subscribe(self):

        def cb_func(pwm_data):
            self.dev_write(self.pwm_to_encoder(pwm_data.data))

        ChassisDev.start_sub(self, cb_func)

    def pwm_to_encoder(self, pwm):
        """ construct the payload
        """
        return [pwm, 0,0,0,0,0]

    def dev_write(self, data):
        """ write data to serial
        """
        self.serial_dev.write(self.protocol.encode(data))
        print "Servo: Write to serial", data
開發者ID:CaoTaMaBi,項目名稱:open-robot,代碼行數:35,代碼來源:servo_ctrl.py

示例2: MoveCtrl

# 需要導入模塊: from protocol import Protocol [as 別名]
# 或者: from protocol.Protocol import encode [as 別名]
class MoveCtrl(ChassisDev):

    TYPE = 0x81
    # 12 bytes data package format in struct
    pack_pmt = "3f"

    # Chassis
    VEL_TRANS = 329.4531
    L = 0.105

    def __init__(self, serial_dev):
        self.protocol = Protocol(self.TYPE, self.pack_pmt)

        self.serial_dev = serial_dev

        ChassisDev.__init__(self, "move_ctrl",
                            sub_topic = "cmd_vel",
                            sub_msg_class = Twist)

    def start_subscribe(self):

        def cb_func(twist_data):
            self.dev_write(self.vel_to_encoder(twist_data.linear.x,
                                          twist_data.linear.y,
                                          twist_data.angular.z))

        ChassisDev.start_sub(self, cb_func)

    def vel_to_encoder(self, linear_x, linear_y, angular_z):
        """ calculate speed for three wheels
        """
        wheel_left = ( linear_x * 0.8660254 - linear_y * 0.5 - \
                           angular_z * self.L ) * self.VEL_TRANS
	wheel_back =( linear_y - angular_z * self.L ) * self.VEL_TRANS
	wheel_right=(-linear_x * 0.8660254 -linear_y * 0.5 - \
                          angular_z*self.L)*self.VEL_TRANS;
        return [wheel_left, wheel_right, wheel_back]

    def dev_write(self, data):
        """ write data to serial
        """
        self.serial_dev.write(self.protocol.encode(data))
        print "Write to serial", data
開發者ID:CaoTaMaBi,項目名稱:open-robot,代碼行數:45,代碼來源:move_ctrl.py


注:本文中的protocol.Protocol.encode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。