使用V7RC進行ePy-Lite藍芽控制
V7RC程式下載
l V7RC - Google Play 應用程式 https://play.google.com/store/apps/details?id=com.v7idea.v7rcliteandroidsdkversion&hl=zh_TW
l 在App Store 上的「V7RC」
- Apple https://apps.apple.com/tw/app/v7rc/id1390983964
V7RC-Protocol說明
GitHub https://github.com/v7rc/V7RC-Protocol
micro:bit 與 V7RC智能遙控 Data Protocol https://v7idea.blogspot.com/2019/05/microbit-v7rc-data-protocol.html
範例程式
Python - ePy-Lite_BLE_V7RC.py
|
""" ePy-Lite_BLE_V7RC.py """ from
machine import Switch #Get button KEY library from
machine import Pin, LED, UART from
machine import Timer from
machine import I2C import utime import math import ure as re import
ssd1306 DEBUG = True DISPLAY
= True srt = b'' srv = b'' sr2 = b'' ss8 = b'' led = b'' le2 = b'' fal = b'' ch1 = 1500 ch2 = 1500 ch3 = 1500 ch4 = 1500 KeyADone
= None def V7RC_read(): global srt,srv,sr2,ss8,led,le2,fal data = ble.readline() if data != b'': fal = re.search(r'FAL\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d#', data) #
FAL1500150015001500# srv = re.search(r'SRV\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d#', data) #
SRV1500150015001500# -> C1, C2, C3, C4 PWM訊號 srt = re.search(r'SRT\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d#', data) #
SRT1500150015001500# -> C1, C2, C3, C4 PWM訊號 sr2 = re.search(r'SR2\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d#', data) #
SR21500100018002000# -> C5, C6, C7, C8 PWM訊號 ss8 = re.search(r'SS8\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w#', data) #
SS89696969696969669# -> 簡易PWM. 可同時使用8個PWM led = re.search(r'LED\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w#', data) #
LEDFFFAFFFAFFFAFFFA# -> 控制前方的LED le2 = re.search(r'LE2\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w#', data) #
LE2FFFAFFFAFFFAFFFA# -> 控制前方的LE2 return (True) return (False) def key_int(): global
KeyADone KeyADone = True def tick3(timer): global
TimerDone TimerDone = True #
Start Function if
__name__ == '__main__': ledY = LED('ledy') ledR = LED('ledr') ledY.off() ledR.off() ledRgb = LED(LED.RGB) KeyA =
Switch('keya') #Create button A KeyA.callback(key_int) KeyADone = False timer = Timer(3,freq = 10) timer.callback(tick3) TimerDone = False LEDR=bytearray(4) LEDG=bytearray(4) LEDB=bytearray(4) LEDM=bytearray(4) LE2R=bytearray(4) LE2G=bytearray(4) LE2B=bytearray(4) LE2M=bytearray(4) SS8M=bytearray(8) i2c = I2C(0,I2C.MASTER) if(DISPLAY): oled=ssd1306.SSD1306_I2C(128, 64, i2c) try: ble = UART(1, 115200,
timeout=10, read_buf_len=64) except: ble.deinit() ble = UART(1, 115200,
timeout=10, read_buf_len=64) ble.write('!CCMD@') utime.sleep_ms( 150 ) ble.write('!CCMD@') utime.sleep_ms( 150 ) ble.write('AT\\r\\n') utime.sleep_ms( 50 ) print(ble.readline()) ble.write('AT+NAME=?\\r\\n') utime.sleep_ms(50) ble.write('AT+MODE_DATA\\r\\n') print(ble.readline()) print(ble.readline()) while True: if
TimerDone == True: ledY.toggle() TimerDone = False if
V7RC_read() == True: ledR.on() #
SRV1500150015001500# -> C1, C2, C3, C4 PWM訊號 if srv: # 車輛 if len(srv.group(0)) != 20 : print(srv) break ch1=int(srv.group(0)[3 :7 ]) ch2=int(srv.group(0)[7 :11]) ch3=int(srv.group(0)[11:15]) ch4=int(srv.group(0)[15:19]) if(DEBUG): print("SRV
C1:{},C2:{},C3:{},C4:{},".format(ch1,ch2,ch3,ch4)) if(DISPLAY): oled.text("SRV
C1:" + str(ch1),0,0,1) oled.text("SRV
C2:" + str(ch2),0,10,1) oled.text("SRV
C3:" + str(ch3),0,20,1) oled.text("SRV
C4:" + str(ch4),0,30,1) oled.show() #
SRT1500150015001500# -> C1, C2, C3, C4 PWM訊號 if srt: # 坦克 if len(srt.group(0)) != 20 : print(srt) break ch1=int(srt.group(0)[3 :7 ]) ch2=int(srt.group(0)[7 :11]) ch3=int(srt.group(0)[11:15]) ch4=int(srt.group(0)[15:19]) if(DEBUG): print("SRT
C1:{},C2:{},C3:{},C4:{},".format(ch1,ch2,ch3,ch4)) if(DISPLAY): oled.text("SRT
C1:" + str(ch1),0,0,1) oled.text("SRT
C2:" + str(ch2),0,10,1) oled.text("SRT
C3:" + str(ch3),0,20,1) oled.text("SRT
C4:" + str(ch4),0,30,1) oled.show() #
SR21500100018002000# -> C5, C6, C7, C8 PWM訊號 if sr2: # ch5=int(sr2.group(0)[3 :7 ]) ch6=int(sr2.group(0)[7 :11]) ch7=int(sr2.group(0)[11:15]) ch8=int(sr2.group(0)[15:19]) if(DEBUG): print("SR2
C5:{},C6:{},C7:{},C8:{},".format(ch5,ch6,ch7,ch8)) if(DISPLAY): oled.text("SR2
C5:" + str(ch5),0,0,1) oled.text("SR2
C6:" + str(ch6),0,10,1) oled.text("SR2
C7:" + str(ch7),0,20,1) oled.text("SR2
C8:" + str(ch8),0,30,1) oled.show() #
SS89696969696969669# -> 簡易PWM. 可同時使用8個PWM if ss8: # 專家/BOT SS8M[0]=int(ss8.group(0)[3 :5 ],16) SS8M[1]=int(ss8.group(0)[5 :7 ],16) SS8M[2]=int(ss8.group(0)[7 :9 ],16) SS8M[3]=int(ss8.group(0)[9 :11],16) SS8M[4]=int(ss8.group(0)[11:13],16) SS8M[5]=int(ss8.group(0)[13:15],16) SS8M[6]=int(ss8.group(0)[15:17],16) SS8M[7]=int(ss8.group(0)[17:19],16) if(DEBUG): print("SS8:{},{},{},{},{},{},{},{}".format(SS8M[0],SS8M[1],SS8M[2],SS8M[3],SS8M[4],SS8M[5],SS8M[6],SS8M[7])) # #
LEDFFFAFFFAFFFAFFFA# -> 控制前方的LED if led: if len(led.group(0)) != 20 : print(led) break LEDstr=str(led.group(0)[3:-1],'utf-8') LEDR[0]=int(LEDstr[0 ],16) LEDG[0]=int(LEDstr[1 ],16) LEDB[0]=int(LEDstr[2 ],16) LEDM[0]=int(LEDstr[3 ],16) LEDR[1]=int(LEDstr[4 ],16) LEDG[1]=int(LEDstr[5 ],16) LEDB[1]=int(LEDstr[6 ],16) LEDM[1]=int(LEDstr[7 ],16) LEDR[2]=int(LEDstr[8 ],16) LEDG[2]=int(LEDstr[9 ],16) LEDB[2]=int(LEDstr[10],16) LEDM[2]=int(LEDstr[11],16) LEDR[3]=int(LEDstr[12],16) LEDG[3]=int(LEDstr[13],16) LEDB[3]=int(LEDstr[14],16) LEDM[3]=int(LEDstr[15],16) if(DEBUG): print("LED1:{},{},{},{},".format(LEDR[0],LEDG[0],LEDB[0],LEDM[0])) print("LED2:{},{},{},{},".format(LEDR[1],LEDG[1],LEDB[1],LEDM[1])) print("LED3:{},{},{},{},".format(LEDR[2],LEDG[2],LEDB[2],LEDM[2])) print("LED4:{},{},{},{},".format(LEDR[3],LEDG[3],LEDB[3],LEDM[3])) for i in range(4): ledRgb.rgb_write(int(i+1),LEDR[i]<<4,LEDG[i]<<4,LEDB[i]<<4) #
LE2FFFAFFFAFFFAFFFA# -> 控制前方的LE2 if le2: if len(le2.group(0)) != 20 : print(le2) break LE2str=str(le2.group(0)[3:-1],'utf-8') LE2R[0]=int(LE2str[0 ],16) LE2G[0]=int(LE2str[1 ],16) LE2B[0]=int(LE2str[2 ],16) LE2M[0]=int(LE2str[3 ],16) LE2R[1]=int(LE2str[4 ],16) LE2G[1]=int(LE2str[5 ],16) LE2B[1]=int(LE2str[6 ],16) LE2M[1]=int(LE2str[7 ],16) LE2R[2]=int(LE2str[8 ],16) LE2G[2]=int(LE2str[9 ],16) LE2B[2]=int(LE2str[10],16) LE2M[2]=int(LE2str[11],16) LE2R[3]=int(LE2str[12],16) LE2G[3]=int(LE2str[13],16) LE2B[3]=int(LE2str[14],16) LE2M[3]=int(LE2str[15],16) if(DEBUG): print("LE21:{},{},{},{},".format(LE2R[0],LE2G[0],LE2B[0],LE2M[0])) print("LE22:{},{},{},{},".format(LE2R[1],LE2G[1],LE2B[1],LE2M[1])) print("LE23:{},{},{},{},".format(LE2R[2],LE2G[2],LE2B[2],LE2M[2])) print("LE24:{},{},{},{},".format(LE2R[3],LE2G[3],LE2B[3],LE2M[3])) for i in range(4, 8): ledRgb.rgb_write(int(i+1),LE2R[i-4]<<4,LE2G[i-4]<<4,LE2B[i-4]<<4) ledR.off() if
KeyADone == True: #Press A Key break utime.sleep_ms(10) ble.deinit() KeyA.callback(None) timer.callback(None) timer.deinit() ledY.off() |
Python - ePy-Lite_BLE_V7RC_Car.py
|
""" ePy-Lite_BLE_V7RC_Car.py ePy-Lite
L9110 H-bridge ----------------- P10
B-IA P11
B-IB GND
VSS VIN
VCC P12
A-IA P13
A-IB """ from
machine import Switch #Get button KEY library from
machine import Pin, LED, UART, PWM from
machine import Timer,RTC import utime import math import ure as re DEBUG = True srt = b'' srv = b'' sr2 = b'' ss8 = b'' led = b'' le2 = b'' fal = b'' ch1 = 1500 ch2 = 1500 ch3 = 1500 ch4 = 1500 KeyADone
= None class Motor: def __init__(self): self.motorR_pwmA
= PWM(Pin.epy.PWM2, freq = 10000, duty = 0) self.motorR_pwmB
= PWM(Pin.epy.PWM3, freq = 10000, duty = 0) self.motorL_pwmA
= PWM(Pin.epy.PWM0, freq = 10000, duty = 0) self.motorL_pwmB
= PWM(Pin.epy.PWM1, freq = 10000, duty = 0) def forward(self): self.motorR_pwmA.duty(100) self.motorR_pwmB.duty(0) self.motorL_pwmA.duty(100) self.motorL_pwmB.duty(0) def turnLeft(self): self.motorR_pwmA.duty(100) self.motorR_pwmB.duty(0) self.motorL_pwmA.duty(50) self.motorL_pwmB.duty(0) def turnRight(self): self.motorR_pwmA.duty(50) self.motorR_pwmB.duty(0) self.motorL_pwmA.duty(100) self.motorL_pwmB.duty(0) def backward(self): self.motorR_pwmA.duty(0) self.motorR_pwmB.duty(100) self.motorL_pwmA.duty(0) self.motorL_pwmB.duty(100) def backLeft(self): self.motorR_pwmA.duty(0) self.motorR_pwmB.duty(100) self.motorL_pwmA.duty(0) self.motorL_pwmB.duty(50) def backRight(self): self.motorR_pwmA.duty(0) self.motorR_pwmB.duty(50) self.motorL_pwmA.duty(0) self.motorL_pwmB.duty(100) def stop(self): self.motorR_pwmA.duty(0) self.motorR_pwmB.duty(0) self.motorL_pwmA.duty(0) self.motorL_pwmB.duty(0) def V7RC_read(): global srt,srv,sr2,ss8,led,le2,fal data = ble.readline() if data != b'': fal = re.search(r'FAL\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d#', data) #
FAL1500150015001500# srv = re.search(r'SRV\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d#', data) #
SRV1500150015001500# -> C1, C2, C3, C4 PWM訊號 srt = re.search(r'SRT\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d#', data) #
SRT1500150015001500# -> C1, C2, C3, C4 PWM訊號 sr2 = re.search(r'SR2\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d#', data) #
SR21500100018002000# -> C5, C6, C7, C8 PWM訊號 ss8 = re.search(r'SS8\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w#', data) #
SS89696969696969669# -> 簡易PWM. 可同時使用8個PWM led = re.search(r'LED\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w#', data) #
LEDFFFAFFFAFFFAFFFA# -> 控制前方的LED le2 = re.search(r'LE2\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w#', data) #
LE2FFFAFFFAFFFAFFFA# -> 控制前方的LE2 return (True) return (False) def XY2LR(X, Y): if Y > 1600: if X > 1600: Car.turnRight() elif X < 1400: Car.turnLeft() else: Car.forward() elif Y < 1400: if X > 1600: Car.backRight() elif X < 1400: Car.backLeft() else: Car.backward() else : Car.stop() def key_int(): global
KeyADone KeyADone = True def tick3(timer): global
TimerDone TimerDone = True def rtctick(rtctimer): global
TimerDone TimerDone = True #
Start Function if
__name__ == '__main__': ledY = LED('ledy') ledR = LED('ledr') ledG = LED('ledg') ledY.off() ledR.off() ledG.on() ledRgb = LED(LED.RGB) KeyA =
Switch('keya') #Create button A KeyA.callback(key_int) KeyADone = False rtc = RTC() rtc.tickcallback(rtctick) # timer = Timer(3,freq = 10) # timer.callback(tick3) TimerDone = False Car = Motor() LEDR=bytearray(4) LEDG=bytearray(4) LEDB=bytearray(4) LEDM=bytearray(4) LE2R=bytearray(4) LE2G=bytearray(4) LE2B=bytearray(4) LE2M=bytearray(4) try: ble = UART(1, 115200,
timeout=10, read_buf_len=64) except: ble.deinit() ble = UART(1, 115200,
timeout=10, read_buf_len=64) ble.write('!CCMD@') utime.sleep_ms( 150 ) ble.write('!CCMD@') utime.sleep_ms( 150 ) ble.write('AT\\r\\n') utime.sleep_ms( 50 ) print(ble.readline()) ble.write('AT+NAME=?\\r\\n') utime.sleep_ms(50) ble.write('AT+MODE_DATA\\r\\n') print(ble.readline()) print(ble.readline()) while True: if
TimerDone == True: # XY2LR(ch1, ch2) ledY.toggle() TimerDone = False if
V7RC_read() == True: ledR.on() #
SRV1500150015001500# -> C1, C2, C3, C4 PWM訊號 if srv: # 車輛 if len(srv.group(0)) != 20 : print(srv) break ch1=int(srv.group(0)[3 :7 ]) ch2=int(srv.group(0)[7 :11]) ch3=int(srv.group(0)[11:15]) ch4=int(srv.group(0)[15:19]) if(DEBUG): print("SRV
C1:{},C2:{},C3:{},C4:{},".format(ch1,ch2,ch3,ch4)) XY2LR(ch1, ch2) #
SRT1500150015001500# -> C1, C2, C3, C4 PWM訊號 if srt: # 坦克 if len(srt.group(0)) != 20 : print(srt) break ch1=int(srt.group(0)[3 :7 ]) ch2=int(srt.group(0)[7 :11]) ch3=int(srt.group(0)[11:15]) ch4=int(srt.group(0)[15:19]) if(DEBUG): print("SRT
C1:{},C2:{},C3:{},C4:{},".format(ch1,ch2,ch3,ch4)) XY2LR(ch1, ch2) #
SR21500100018002000# -> C5, C6, C7, C8 PWM訊號 if sr2: # ch5=int(sr2.group(0)[3 :7 ]) ch6=int(sr2.group(0)[7 :11]) ch7=int(sr2.group(0)[11:15]) ch8=int(sr2.group(0)[15:19]) if(DEBUG): print("SR2
C5:{},C6:{},C7:{},C8:{},".format(ch5,ch6,ch7,ch8)) #
SS89696969696969669# -> 簡易PWM. 可同時使用8個PWM if ss8: # 專家/BOT SS8M[0]=int(ss8.group(0)[3 :5 ],16) SS8M[1]=int(ss8.group(0)[5 :7 ],16) SS8M[2]=int(ss8.group(0)[7 :9 ],16) SS8M[3]=int(ss8.group(0)[9 :11],16) SS8M[4]=int(ss8.group(0)[11:13],16) SS8M[5]=int(ss8.group(0)[13:15],16) SS8M[6]=int(ss8.group(0)[15:17],16) SS8M[7]=int(ss8.group(0)[17:19],16) if(DEBUG): print("SS8:{},{},{},{},{},{},{},{}".format(SS8M[0],SS8M[1],SS8M[2],SS8M[3],SS8M[4],SS8M[5],SS8M[6],SS8M[7])) # #
LEDFFFAFFFAFFFAFFFA# -> 控制前方的LED if led: if len(led.group(0)) != 20 : print(led) break LEDstr=str(led.group(0)[3:-1],'utf-8') LEDR[0]=int(LEDstr[0 ],16) LEDG[0]=int(LEDstr[1 ],16) LEDB[0]=int(LEDstr[2 ],16) LEDM[0]=int(LEDstr[3 ],16) LEDR[1]=int(LEDstr[4 ],16) LEDG[1]=int(LEDstr[5 ],16) LEDB[1]=int(LEDstr[6 ],16) LEDM[1]=int(LEDstr[7 ],16) LEDR[2]=int(LEDstr[8 ],16) LEDG[2]=int(LEDstr[9 ],16) LEDB[2]=int(LEDstr[10],16) LEDM[2]=int(LEDstr[11],16) LEDR[3]=int(LEDstr[12],16) LEDG[3]=int(LEDstr[13],16) LEDB[3]=int(LEDstr[14],16) LEDM[3]=int(LEDstr[15],16) if(DEBUG): print("LED1:{},{},{},{},".format(LEDR[0],LEDG[0],LEDB[0],LEDM[0])) print("LED2:{},{},{},{},".format(LEDR[1],LEDG[1],LEDB[1],LEDM[1])) print("LED3:{},{},{},{},".format(LEDR[2],LEDG[2],LEDB[2],LEDM[2])) print("LED4:{},{},{},{},".format(LEDR[3],LEDG[3],LEDB[3],LEDM[3])) for i in range(4): ledRgb.rgb_write(int(i+1),LEDR[i]<<4,LEDG[i]<<4,LEDB[i]<<4) #
LE2FFFAFFFAFFFAFFFA# -> 控制前方的LE2 if le2: if len(le2.group(0)) != 20 : print(le2) break LE2str=str(le2.group(0)[3:-1],'utf-8') LE2R[0]=int(LE2str[0 ],16) LE2G[0]=int(LE2str[1 ],16) LE2B[0]=int(LE2str[2 ],16) LE2M[0]=int(LE2str[3 ],16) LE2R[1]=int(LE2str[4 ],16) LE2G[1]=int(LE2str[5 ],16) LE2B[1]=int(LE2str[6 ],16) LE2M[1]=int(LE2str[7 ],16) LE2R[2]=int(LE2str[8 ],16) LE2G[2]=int(LE2str[9 ],16) LE2B[2]=int(LE2str[10],16) LE2M[2]=int(LE2str[11],16) LE2R[3]=int(LE2str[12],16) LE2G[3]=int(LE2str[13],16) LE2B[3]=int(LE2str[14],16) LE2M[3]=int(LE2str[15],16) if(DEBUG): print("LE21:{},{},{},{},".format(LE2R[0],LE2G[0],LE2B[0],LE2M[0])) print("LE22:{},{},{},{},".format(LE2R[1],LE2G[1],LE2B[1],LE2M[1])) print("LE23:{},{},{},{},".format(LE2R[2],LE2G[2],LE2B[2],LE2M[2])) print("LE24:{},{},{},{},".format(LE2R[3],LE2G[3],LE2B[3],LE2M[3])) for i in range(4, 8): ledRgb.rgb_write(int(i+1),LE2R[i-4]<<4,LE2G[i-4]<<4,LE2B[i-4]<<4) ledR.off() if
KeyADone == True: #Press A Key break utime.sleep_ms(100) ble.deinit() KeyA.callback(None) timer.callback(None) timer.deinit() ledY.off() |
程式執行結果
SRV (基本PWM控制)
|
Command:SRV1808182015001500#,Index:0,Length:20 SRV C1:1808,C2:1820,C3:1500,C4:1500, |
LED:20 Byte;顯示第一組四個LED燈
|
Command:LEDFFFA0FFA00FAFF0A#,Index:0,Length:20 LED LED1:15,15,15,10, LED2:0,15,15,10, LED3:0,0,15,10, LED4:15,15,0,10, |
LE2:20 Byte; 顯示第二組四個LED燈
|
Command:LE2EFFAF0FA778A076A#,Index:0,Length:20 LE2 LE21:14,15,15,10, LE22:15,0,15,10, LE23:7,7,8,10, LE24:0,7,6,10, |
V7RC RGB LED
https://www.youtube.com/watch?v=XhoB9F1QR3w
沒有留言:
張貼留言