Python & Create2

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 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,699评论 6 427
  • 前言 Python的创始人为Guido van Rossum。1989年圣诞节期间,在阿姆斯特丹,Guido为了打...
    依依玖玥阅读 8,931评论 6 37
  • 环境管理管理Python版本和环境的工具。p–非常简单的交互式python版本管理工具。pyenv–简单的Pyth...
    MrHamster阅读 9,235评论 1 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,297评论 25 709
  • 低头族~你我他无一幸免,朋友圈里的鸡汤文不如群里的红包惹眼,有木有? 谁说“一分也是爱”?抢得惊喜爱得勉强,挤出来...
    嘻哈眉阅读 3,434评论 1 3