本文整理汇总了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
示例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