“Example 1 bluebit重力遥控小车”的版本间的差异
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献) |
|||
(未显示2个用户的8个中间版本) | |||
第6行: | 第6行: | ||
'''<big>Description:</big><br />''' | '''<big>Description:</big><br />''' | ||
− | 通过micro:bit重力感应遥控blue:bit小车<br /><br /> | + | 通过micro:bit重力感应遥控blue:bit小车<br /> |
+ | 使用方法:按下micro:bit A按键左右前后倾斜,通过2轴重力控制小车 | ||
+ | <br /><br /> | ||
'''<big>Connection:</big><br />''' | '''<big>Connection:</big><br />''' | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | | 小车左电机 || AN1->P14<br /> AN2->P13 | ||
+ | |- | ||
+ | | 小车右电机 || AN1->P16<br /> AN2->P15 | ||
+ | |} | ||
+ | |||
{| | {| | ||
|- | |- | ||
− | | | + | | [[文件:重力小车.png|850px|无框|左]] |
|} | |} | ||
<br /><br /> | <br /><br /> | ||
'''<big>Programs:</big><br />''' | '''<big>Programs:</big><br />''' | ||
+ | <br /> | ||
+ | [http://wiki.labplus.cn/images/0/08/Bluebit%E9%87%8D%E5%8A%9B%E5%B0%8F%E8%BD%A6.zip Bluebit重力小车程序 ] | ||
+ | * <big>microbit重力遥控端程序</big> | ||
+ | <br/> | ||
+ | :::{| | ||
+ | |- | ||
+ | |[[文件:Example car 1.png|800px|无框]] | ||
+ | |} | ||
+ | <br /> | ||
+ | <br/> | ||
+ | <pre style="color:blue"> | ||
+ | from microbit import * | ||
+ | import radio | ||
+ | radio.on() | ||
+ | radio.config(length=8, queue=3, channel=79, power=7, | ||
+ | address=0x44773311, group=0x1B, data_rate=radio.RATE_250KBIT) | ||
+ | |||
+ | msg = bytearray(5) | ||
+ | x = 0 | ||
+ | y = 0 | ||
+ | z = 0 | ||
+ | a = 0 | ||
+ | while True: | ||
+ | x = accelerometer.get_x() | ||
+ | y = accelerometer.get_y() | ||
+ | z = accelerometer.get_z() | ||
+ | if button_a.is_pressed(): | ||
+ | a=1 | ||
+ | else: | ||
+ | a=0 | ||
+ | |||
+ | x = x + 10000 | ||
+ | msg[0] = int(x / 256) | ||
+ | msg[1] = x % 256 | ||
+ | y = y + 10000 | ||
+ | msg[2] = int(y / 256) | ||
+ | msg[3] = y % 256 | ||
+ | msg[4] = a | ||
+ | radio.send_bytes(msg) | ||
+ | sleep(50) | ||
+ | </pre> | ||
+ | <br /> | ||
+ | |||
+ | * <big>blue:bit小车程序</big> | ||
+ | <br/> | ||
+ | {| | ||
+ | |- | ||
+ | |[[文件:Example blueit 1.png|1000px|无框]]||[[文件:Example bluebit1 2.png|1000px|无框]] | ||
+ | |} | ||
+ | <pre style="color:blue"> | ||
+ | from microbit import * | ||
+ | import radio | ||
+ | import math | ||
+ | |||
+ | def motion(leftSpeed, rightSpeed): | ||
+ | if leftSpeed > 1023: | ||
+ | leftSpeed = 1023 | ||
+ | if leftSpeed < -1023: | ||
+ | leftSpeed = -1023 | ||
+ | if leftSpeed == 0: | ||
+ | pin13.write_analog(1) | ||
+ | pin14.write_analog(1) | ||
+ | |||
+ | if leftSpeed > 0: | ||
+ | pin13.write_analog(1) | ||
+ | pin14.write_analog(int(leftSpeed/1.5)) | ||
+ | |||
+ | if leftSpeed < 0: | ||
+ | leftSpeed = -leftSpeed | ||
+ | pin13.write_analog(int(leftSpeed/1.5)) | ||
+ | pin14.write_analog(1) | ||
+ | |||
+ | if rightSpeed > 1023: | ||
+ | rightSpeed = 1023 | ||
+ | if rightSpeed < -1023: | ||
+ | rightSpeed = -1023 | ||
+ | if rightSpeed == 0: | ||
+ | pin15.write_analog(1) | ||
+ | pin16.write_analog(1) | ||
+ | if rightSpeed > 0: | ||
+ | pin15.write_analog(int(rightSpeed/1.5)) | ||
+ | pin16.write_analog(1) | ||
+ | if rightSpeed < 0: | ||
+ | rightSpeed = -rightSpeed | ||
+ | pin15.write_analog(1) | ||
+ | pin16.write_analog(int(rightSpeed/1.5)) | ||
+ | |||
+ | display.off() | ||
+ | radio.on() | ||
+ | radio.config(length=8, queue=20, channel=79, power=7, | ||
+ | address=0x44773311, group=0x1B, data_rate=radio.RATE_250KBIT) | ||
+ | x = 0 | ||
+ | y = 0 | ||
+ | z = 0 | ||
+ | a = 0 | ||
+ | left = 0 | ||
+ | right = 0 | ||
+ | while True: | ||
+ | msg = bytes(5) | ||
+ | msg = radio.receive_bytes() | ||
+ | if msg is not None: | ||
+ | x = msg[0]*256 + msg[1] | ||
+ | x = x - 10000 | ||
+ | y = msg[2]*256 + msg[3] | ||
+ | y = y - 10000 | ||
+ | a = msg[4] | ||
+ | #print('x=',x,'y=',y) | ||
+ | if a == 1: | ||
+ | left = int((y + x)) | ||
+ | right = int((y - x)) | ||
+ | print('left = ', left) | ||
+ | print('right = ', right) | ||
+ | motion(right, left) | ||
+ | else: | ||
+ | motion(0,0) | ||
+ | </pre> |
2018年3月30日 (五) 16:11的最新版本
Part list:
- 1x micro:bit
- 1x blue:bit-主板
- 2x blue:bit-电机驱动
Description:
通过micro:bit重力感应遥控blue:bit小车
使用方法:按下micro:bit A按键左右前后倾斜,通过2轴重力控制小车
Connection:
小车左电机 | AN1->P14 AN2->P13 |
小车右电机 | AN1->P16 AN2->P15 |
Programs:
Bluebit重力小车程序
- microbit重力遥控端程序
from microbit import * import radio radio.on() radio.config(length=8, queue=3, channel=79, power=7, address=0x44773311, group=0x1B, data_rate=radio.RATE_250KBIT) msg = bytearray(5) x = 0 y = 0 z = 0 a = 0 while True: x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() if button_a.is_pressed(): a=1 else: a=0 x = x + 10000 msg[0] = int(x / 256) msg[1] = x % 256 y = y + 10000 msg[2] = int(y / 256) msg[3] = y % 256 msg[4] = a radio.send_bytes(msg) sleep(50)
- blue:bit小车程序
from microbit import * import radio import math def motion(leftSpeed, rightSpeed): if leftSpeed > 1023: leftSpeed = 1023 if leftSpeed < -1023: leftSpeed = -1023 if leftSpeed == 0: pin13.write_analog(1) pin14.write_analog(1) if leftSpeed > 0: pin13.write_analog(1) pin14.write_analog(int(leftSpeed/1.5)) if leftSpeed < 0: leftSpeed = -leftSpeed pin13.write_analog(int(leftSpeed/1.5)) pin14.write_analog(1) if rightSpeed > 1023: rightSpeed = 1023 if rightSpeed < -1023: rightSpeed = -1023 if rightSpeed == 0: pin15.write_analog(1) pin16.write_analog(1) if rightSpeed > 0: pin15.write_analog(int(rightSpeed/1.5)) pin16.write_analog(1) if rightSpeed < 0: rightSpeed = -rightSpeed pin15.write_analog(1) pin16.write_analog(int(rightSpeed/1.5)) display.off() radio.on() radio.config(length=8, queue=20, channel=79, power=7, address=0x44773311, group=0x1B, data_rate=radio.RATE_250KBIT) x = 0 y = 0 z = 0 a = 0 left = 0 right = 0 while True: msg = bytes(5) msg = radio.receive_bytes() if msg is not None: x = msg[0]*256 + msg[1] x = x - 10000 y = msg[2]*256 + msg[3] y = y - 10000 a = msg[4] #print('x=',x,'y=',y) if a == 1: left = int((y + x)) right = int((y - x)) print('left = ', left) print('right = ', right) motion(right, left) else: motion(0,0)