美文网首页
Python & Create2

Python & Create2

作者: matheecs | 来源:发表于2017-02-14 05:20 被阅读0次

USB 地址

/dev/tty.usbserial-DA01NU2A

OOP

对象(object)可以修改类(class)属性值。
类属性被所有同一类及其子类的对象共享。
类属性值的改变会影响所有的对象。

《Python快速教程》

Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象。我们已经看到了内置的比如说list,还有tuple等等。它们用起来很方便。在Python的标准库里,还有大量可以用于操作系统互动,Internet开发,多线程,文本处理的对象。而在所有的这些的这些的基础上,又有很多外部的库包,定义了更丰富的对象,比如numpy, tkinter, django等用于科学计算,GUI开发,web开发的库,定义了各种各样的对象。对于一般用户来说,使用这些库,要比自己去从头开始容易得多。我们要开始攀登巨人的肩膀了。

Difference between _, __ and __xx__ in Python

super( )

Python 2.7:

super(ChildB, self).__init__()
class BumpsAndWheelDrop(PackedBinaryData):
    def __init__(self, data):
        super(BumpsAndWheelDrop, self).__init__(data)

Python 3.0:

super().__init__()
class BumpsAndWheelDrop(PackedBinaryData):
    def __init__(self, data):
        super().__init__(data)

How to remove __MACOSX from zip archives

zip -d your-archive.zip "__MACOSX*"

ZipCleaner 1.0

Python 内置函数

Paste_Image.png

iRobot Roomba 655 Pet Series Motherboard Replacement

Paste_Image.png

CreateScratchX

Python 程序

import serial
import time
from struct import *

def wakeRobot(ser):
    ser.rts = True
    time.sleep(0.1)
    ser.rts = False
    time.sleep(1)

def resetRobotAndWait(ser):
    ser.reset_input_buffer()
    ser.write('\x07')
    sBuffer = "dummy string"
    while len(sBuffer) is not 0:
        sBuffer = ser.readline()
        print sBuffer.strip()

irobot = serial.Serial(port='/dev/tty.usbserial-DA01NU2A', 
    baudrate=115200, timeout = 3.0)
wakeRobot(irobot)
irobot.write(pack('B',128))
print "Start iRobot..."

irobot.write(pack('B',131))
time.sleep(.1)

irobot.write(pack('B'+'c'*4, 164,'1','2','3','4'))
irobot.write(pack('B'*3+'cB'*5,140,0,5,'C',16,'H',24,'J',8,'L',16,'O',32))
irobot.write(pack('B'*2,141, 0))
time.sleep(1.6)


# '>h', '>H'
irobot.write(pack('B'*2,142, 21))
print "char:", unpack('B',irobot.read(1))
irobot.write(pack('B'*2,142, 22))
print "volt:", unpack('>H',irobot.read(2))
irobot.write(pack('B'*2,142, 23))
print "curr:", unpack('>h',irobot.read(2))
irobot.write(pack('B'*2,142, 24))
print "temp:", unpack('b',irobot.read(1))
irobot.write(pack('B'*2,142, 25))
print "batt:", unpack('>H',irobot.read(2))
irobot.write(pack('B'*2,142, 26))
print "Batt:", unpack('>H',irobot.read(2))

irobot.write(pack('B'*2,142, 3))
print "G3:", unpack('>BHhbHH',irobot.read(10))

irobot.write(pack('B'*2,142, 107))
print "G107:", unpack('>h'+'h'*3+'B',irobot.read(9))

irobot.write(pack('B',128))
print "Pause iRobot..."
irobot.close()

程序中调用了 structpack()unpack()功能对串口通信起到了简化作用。特别注意程序的大小端问题('>h','>H')。

Paste_Image.png

相关文章

网友评论

      本文标题:Python & Create2

      本文链接:https://www.haomeiwen.com/subject/tlfxwttx.html