Example 1 bluebit重力遥控小车
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献)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)