Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def notify(self, data):
def is_connected(self):
return len(self._connections) > 0

def _advertise(self, interval_us=500000):
def _advertise(self, interval_us=100000):
print("Starting advertising")
self._ble.gap_advertise(interval_us, adv_data=self._payload)

Expand Down
81 changes: 38 additions & 43 deletions code/car.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
from ssd1306 import SSD1306_I2C #从ssd1306模块中导入SSD1306_I2C子模块
import framebuf
import time

from ir_receive import IRrec
#编码盘计数
count1 = 0
count2 = 0
#遥控器指令表
IR_FORWARD=48
IR_BACKWARD=164
IR_LEFT=16
IR_RIGHT=180
IR_STOP=56
IR_LIGHT_ON=44
IR_LIGHT_OFF=26


class CAR():

Expand Down Expand Up @@ -53,7 +62,8 @@ def __init__(self):
self.t5 = Pin(39, Pin.IN, Pin.PULL_UP)

#红外接收头
self.IR = Pin(32, Pin.IN)
self.IR = IRrec(32)
self.IR.callback(self.getIR)

#屏幕
self.i2c=SoftI2C(sda=Pin(25), scl=Pin(23))
Expand Down Expand Up @@ -308,44 +318,29 @@ def T5(self):
return self.t5.value()

#红外接收解码
def getIR(self):

if (self.IR.value() == 0):
count = 0
while ((self.IR.value() == 0) and (count < 100)): #9ms
count += 1
time.sleep_us(100)
if(count < 10):
return None
count = 0
while ((self.IR.value() == 1) and (count < 50)): #4.5ms
count += 1
time.sleep_us(100)

idx = 0
cnt = 0
data = [0,0,0,0]
for i in range(0,32):
count = 0
while ((self.IR.value() == 0) and (count < 10)): #0.56ms
count += 1
time.sleep_us(100)

count = 0
while ((self.IR.value() == 1) and (count < 20)): #0: 0.56mx
count += 1 #1: 1.69ms
time.sleep_us(100)

if count > 7:
data[idx] |= 1<<cnt
if cnt == 7:
cnt = 0
idx += 1
else:
cnt += 1

if data[0]+data[1] == 0xFF and data[2]+data[3] == 0xFF: #check
return data[2]
else:
return("REPEAT")

def getIR(self,nec,add,cmd):
self.s_red=1
if cmd==IR_FORWARD:
self.forward()
self.screen()
elif cmd==IR_BACKWARD:
self.backward()
self.screen()
elif cmd==IR_LEFT:
self.turn_left(1)
self.screen()
elif cmd==IR_RIGHT:
self.turn_right(1)
self.screen()
elif cmd==IR_STOP:
self.stop()
self.screen()
elif cmd==IR_LIGHT_ON:
self.light_on()
self.screen()
elif cmd==IR_LIGHT_OFF:
self.light_off()
self.screen()
time.sleep_ms(50)
self.s_red=0
self.screen()
85 changes: 0 additions & 85 deletions code/examples/IR_control&screen.py

This file was deleted.

58 changes: 0 additions & 58 deletions code/examples/IR_control.py

This file was deleted.

9 changes: 5 additions & 4 deletions code/examples/bleremote.py → code/examples/ble&IRremote.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ def fun(tim):


while 1:
ds=pycar.getDistance()
jo=pycar.getJourney()
if quittt.value()==0:
print('quit')
break
if p.is_connected():
pycar.s_blue=1
ds=pycar.getDistance()
jo=pycar.getJourney()
pycar.screen()
print(ds)
p.notify(str(ds))
utime.sleep_ms(300)
pycar.screen()
utime.sleep_ms(1000)

67 changes: 67 additions & 0 deletions code/ir_receive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from machine import Pin
from time import ticks_us,ticks_diff

class IRrec:
def __init__(self, pin=32):
self._ic_start = 0 #每一个信号的开始时间
self._ic_last = 0 #每一t信号的结束时间
self._ic_width = 0 #每一个信号的宽度
self._sr = [0x00, 0x00, 0x00, 0x00] #整个红外信号的结构[地址,地址反码,命令,命令反码]
self._address = 0 #红外地址
self._command = 0 #红外命令
self._cb = 0 #递归??取出地址和命令值
self._ic_pin = Pin(pin, Pin.IN)
self._ic_pin.irq(trigger=Pin.IRQ_RISING, handler=self._ic_cb) #每次信号中断触发_ic_cb函数
self._id = 0 #信号个数(计数用,暂时程序里没有用到)
self._rst() #各项数据清零

def _rst(self):
self._sr = [0x00, 0x00, 0x00, 0x00]
self._ic_last = ticks_us()
self._sc = 0
self._sb = 0

def _bit(self, v):
self._sr[self._sb] = (self._sr[self._sb] >> 1)|(v << 8)
self._sc += 1
if (self._sc > 7):
self._sc = 0
self._sb += 1
if (self._sb > 3):
if ((self._sr[0] ^ self._sr[1] ^ self._sr[2] ^ self._sr[3]) == 0):#异或校验2组数据完整性 根据红外协议来讲 没问题pass 有问题清空重新来过
self._address = self._sr[0]
self._command = self._sr[2]
if (self._cb):
self._cb(self, self._address, self._command)
self._rst()

def _ic_cb(self, pin):

self._ic_start = ticks_us()#时间
icw = ticks_diff(self._ic_start, self._ic_last)
self._ic_last = self._ic_start
self._ic_width = icw
self._id += 1
if (icw > 5500):
pass
elif (icw > 4000):#一个高低信号 大于 4ms 4.5ms 是发送结束 和起始
self._rst()
elif (icw > 2500): # Repeat command#一个高低信号 大于 2.5ms 结束码2.5ms 收到 结束信号直接结束
pass
elif (icw > 1500):#一个高低信号 大于 1.5ms 有效信号处理
self._bit(1) # High bit
else:
self._bit(0) # Low bit#低8位 小于 1.5ms的 有效信号处理
# print('Low bit')


def callback(self, fn):
self._cb = fn


def nec_cb(nec, a, c):
print(a, c)

from ir_receive import IRrec
nec = IRrec(35)
nec.callback(nec_cb)