“Car:bit”的版本间的差异

来自Labplus盛思维基百科
跳转至: 导航搜索
引脚定义
循迹小车
 
(未显示2个用户的22个中间版本)
第1行: 第1行:
 
== 概述 ==
 
== 概述 ==
 +
car:bit是以micro:bit作主控的小车。内置超声波、模拟光线、蜂鸣器、测速、全彩RGB灯、循迹等模块。可用于超声波测距、循线、寻光、无线遥控小车。
  
 
== 技术参数 ==
 
== 技术参数 ==
 +
* 供电方式:3.7V 18650锂电池
 +
* Micro USB接口:可充电
 +
* PCI插槽,可接入micro:bit作为主控
 +
* 内置模拟光线
 +
* 超声波测距 3cm~300cm  精度±1cm
 +
* 支持两轮测速
  
== 引脚定义 ==
+
== 使用说明 ==
{| class="wikitable"
+
<br />
 +
<br />
 +
[[文件:Carbit.jpg|600px|无框|居中]]
 +
[[文件:Carbit 1.jpg|700px|无框|居中]]
 +
<br />
 +
<br />
 +
* <big>car:bit快速入门</big>
 +
<big>Step1.</big>[http://wiki.labplus.cn/index.php?title=Micro:bit%E7%BC%96%E7%A8%8B%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8 教程:microbit怎么编程下载?]<br />
 +
<big>Step2.</big>在使用电机驱动函数和测速函数时,应在先程序前定义[http://wiki.labplus.cn/images/6/62/Carbit_driver.zip car:bit驱动]<br />
 +
<big>Step3.</big>简要使用说明
 +
{| class="wikitable"  
 
|-
 
|-
! 内置  !! micro:bit引脚
+
! style="background:#7dc2f5"|car:bit
 +
! style="background:#7dc2f5"|micro:bit引脚
 +
! style="background:#7dc2f5"|Python示例
 
|-
 
|-
| 无源蜂鸣器 || P0
+
| B2电机 ||I2C || setLeftMotor(value) <br /><small>前进 value:0~100;后退 value:0~-100</small>
 
|-
 
|-
| 光线传感器 || P1
+
| B1电机 ||I2C || setRightMotor(value) <br /> <small>前进 value:0~100;后退 value:0~-100</small>
 
|-
 
|-
| 声音传感器 || P2
+
| 蜂鸣器 ||P0 || import music <br />music.play(music.NYAN)
 
|-
 
|-
| 全彩RGB灯 || P16
+
| W1/W2/W3/W4 RGB灯 ||P8 || <small>#W1显示红色</small> <br />import neopixel <br />np=neopixel.NeoPixel(pin8,4) <br /> np[0]=  (255,0,0) <br /> np.show()
 +
|-
 +
| 模拟光线 Q3 ||P1 || pin1.read_analog()
 +
|-
 +
| 模拟光线 Q4 ||P2 || pin2.read_analog()
 +
|-
 +
| 测速 U11||I2C || spd = getSpeed()<br />print(spd[0]+spd[1]*256) #left Speed
 +
|-
 +
| 测速 U10||I2C || spd = getSpeed()<br />print(spd[2]+spd[3]*256) #Right Speed
 +
|-
 +
| 循迹红外对管 U18/U17/U14/U13/U6/U5||U18->P16<br />U17->P15<br />U14->P2<br />U13->P1<br />U6->P14<br />U5->P13
 +
||pin1.read_digital() <br /><small>更详细循迹编程可参考循迹例程</small>
 +
|-
 +
| 超声波||I2C || Distance()
 +
|}
 +
 
 +
{| style=" background-color:#ffffcc;" cellpadding="1"
 +
| 1.P1/P2与模拟光线引脚有复用,所以不能同时使用。在使用时需要通过切换开关,选择「循迹」和「光线」<br /> 2.如果18650锂电池重新装载的话,第一次需要激活下锂电池才能供电正常!激活方式:装上18650电池后接上microUSB接口,电源开关拨到ON
 +
|}
 +
 
 +
== 应用例程 ==
 +
=== 循迹小车 ===
 +
<br/>
 +
[http://wiki.labplus.cn/images/0/06/%E5%BE%AA%E8%BF%B9%E5%B0%8F%E8%BD%A6.zip car:bit-循迹小车例程]
 +
<pre style="color:blue">
 +
'''
 +
循迹小车(基于PID算法)
 +
    使用时需调整以下参数
 +
1、PID参数kp ki kd
 +
2、直线行走速度值(影响行走速度) k_speed
 +
3、最大限速值(影向转弯效果) speedMax
 +
4、速度调整系数(影响转弯速度) speedScale
 +
5、转弯调整延时(影向转弯调整时间) turnDelay
 +
'''
 +
 
 +
from microbit import *
 +
kp = 1
 +
ki = 1
 +
kd = 1
 +
preError = 0
 +
integral = 0
 +
delays = 1
 +
leftMotorSpeed = 0
 +
rightMotorSpeed = 0
 +
k_speed = 50
 +
speedMax = 75
 +
turnDelay = 10
 +
 
 +
def setLeftMotor(speed):
 +
    i2c.write(0x10, bytearray([0x01, speed]))
 +
   
 +
def setRightMotor(speed):
 +
    i2c.write(0x10, bytearray([0x02, speed]))
 +
 
 +
def getSpeed():
 +
    motorSpeed = i2c.read(0x10,4)
 +
    return motorSpeed
 +
 
 +
def pidInit(kp_ = 0, ki_ = 0, kd_  = 0,delays_ = 1):
 +
    global kp, ki,kd, delays
 +
    kp = kp_
 +
    ki = ki_
 +
    kd = kd_
 +
    delays = delays_
 +
   
 +
def getError():
 +
    err = 0
 +
    varLeft1 = pin1.read_digital()
 +
    varLeft2 = pin14.read_digital()
 +
    varLeft3 = pin13.read_digital()
 +
    varRight1 = pin2.read_digital()
 +
    varRight2 = pin15.read_digital()
 +
    varRight3 = pin16.read_digital()
 +
    if (varLeft1 == 1 and varRight1 == 0 and varLeft2 == 0):
 +
        err = 1
 +
    elif (varLeft2 == 1 and varLeft1 == 1):
 +
        err = 2
 +
    elif (varLeft2 == 1 and varLeft1 == 0 and varLeft3 == 0):
 +
        err = 3
 +
    elif (varLeft3 == 1 and varLeft2 == 1):
 +
        err = 4
 +
    elif (varLeft3 == 1 and varLeft2 == 0):
 +
        err = 5
 +
    elif (varRight1 == 1 and varLeft1 == 0 and varRight2 == 0):
 +
        err = -1
 +
    elif (varRight1 == 1 and varRight2 == 1):
 +
        err = -2
 +
    elif (varRight2 == 1 and varRight1 == 0 and varRight3 == 0):
 +
        err = -3
 +
    elif (varRight3 == 1 and varRight2 == 1):
 +
        err = -4
 +
    elif (varRight3 == 1 and varRight2 == 0):
 +
        err = -5
 +
    #print(err)
 +
    return err
 +
 
 +
def pidControl():
 +
    global preError,integral,delays,leftMotorSpeed,rightMotorSpeed
 +
    global kd, ki, kd
 +
   
 +
    speedScale = 1
 +
    error = getError()
 +
    if (error != 0):
 +
        speedScale = 2/abs(error)
 +
        if speedScale > 1:
 +
            speedScale = 1
 +
    integral = integral + error
 +
    derivative = error - preError
 +
    output = (int)(kp*error + ki*integral*delays + kd*derivative/delays)
 +
    print(output)
 +
    preError = error
 +
    leftMotorSpeed = int((k_speed - output)*speedScale)
 +
    rightMotorSpeed = int((k_speed + output)*speedScale)
 +
 
 +
    if leftMotorSpeed < -speedMax:
 +
        leftMotorSpeed = -speedMax
 +
    elif leftMotorSpeed > speedMax:
 +
        leftMotorSpeed = speedMax
 +
    if rightMotorSpeed < -speedMax:
 +
        rightMotorSpeed = -speedMax
 +
    elif rightMotorSpeed > speedMax:
 +
        rightMotorSpeed = speedMax 
 +
    setLeftMotor(leftMotorSpeed)
 +
    setRightMotor(rightMotorSpeed)
 +
   
 +
    sleep(abs(error)*turnDelay)
 +
 
 +
# test code
 +
pidInit(kp_ = 8, ki_ = 0.0, kd_ = 15, delays_ = 1)
 +
#setLeftMotor(60)
 +
#setRightMotor(60)
 +
 
 +
while True:
 +
    pidControl()
 +
</pre>
 +
 
 +
== 版本历史记录 ==
 +
 
 +
{|  border="1" cellspacing="0" align="left" cellpadding="0" width="60%" style="text-align:center;"
 +
|- style="text-align:center;background-color:#6fa8dc;color:#fffff;"
 +
!width="10%"|Version !!width="15%"| Date !! Note  <small>[+]新增[-]删除[^]修复</small>
 +
|-
 +
| V1.4 ||2018/04/24 || style="text-align:left"|
 
|}
 
|}

2018年8月24日 (五) 16:29的最新版本

概述

car:bit是以micro:bit作主控的小车。内置超声波、模拟光线、蜂鸣器、测速、全彩RGB灯、循迹等模块。可用于超声波测距、循线、寻光、无线遥控小车。

技术参数

  • 供电方式:3.7V 18650锂电池
  • Micro USB接口:可充电
  • PCI插槽,可接入micro:bit作为主控
  • 内置模拟光线
  • 超声波测距 3cm~300cm 精度±1cm
  • 支持两轮测速

使用说明



Carbit.jpg
Carbit 1.jpg



  • car:bit快速入门

Step1.教程:microbit怎么编程下载?
Step2.在使用电机驱动函数和测速函数时,应在先程序前定义car:bit驱动
Step3.简要使用说明

car:bit micro:bit引脚 Python示例
B2电机 I2C setLeftMotor(value)
前进 value:0~100;后退 value:0~-100
B1电机 I2C setRightMotor(value)
前进 value:0~100;后退 value:0~-100
蜂鸣器 P0 import music
music.play(music.NYAN)
W1/W2/W3/W4 RGB灯 P8 #W1显示红色
import neopixel
np=neopixel.NeoPixel(pin8,4)
np[0]= (255,0,0)
np.show()
模拟光线 Q3 P1 pin1.read_analog()
模拟光线 Q4 P2 pin2.read_analog()
测速 U11 I2C spd = getSpeed()
print(spd[0]+spd[1]*256) #left Speed
测速 U10 I2C spd = getSpeed()
print(spd[2]+spd[3]*256) #Right Speed
循迹红外对管 U18/U17/U14/U13/U6/U5 U18->P16
U17->P15
U14->P2
U13->P1
U6->P14
U5->P13
pin1.read_digital()
更详细循迹编程可参考循迹例程
超声波 I2C Distance()
1.P1/P2与模拟光线引脚有复用,所以不能同时使用。在使用时需要通过切换开关,选择「循迹」和「光线」
2.如果18650锂电池重新装载的话,第一次需要激活下锂电池才能供电正常!激活方式:装上18650电池后接上microUSB接口,电源开关拨到ON

应用例程

循迹小车


car:bit-循迹小车例程

'''
循迹小车(基于PID算法)
    使用时需调整以下参数
	1、PID参数kp ki kd
	2、直线行走速度值(影响行走速度) k_speed 
	3、最大限速值(影向转弯效果) speedMax
	4、速度调整系数(影响转弯速度) speedScale
	5、转弯调整延时(影向转弯调整时间) turnDelay
'''

from microbit import *
kp = 1
ki = 1
kd = 1
preError = 0
integral = 0
delays = 1
leftMotorSpeed = 0
rightMotorSpeed = 0
k_speed = 50
speedMax = 75
turnDelay = 10

def setLeftMotor(speed):
    i2c.write(0x10, bytearray([0x01, speed]))
    
def setRightMotor(speed): 
    i2c.write(0x10, bytearray([0x02, speed]))

def getSpeed():
    motorSpeed = i2c.read(0x10,4)
    return motorSpeed

def pidInit(kp_ = 0, ki_ = 0, kd_  = 0,delays_ = 1):
    global kp, ki,kd, delays
    kp = kp_
    ki = ki_
    kd = kd_
    delays = delays_
    
def getError():
    err = 0
    varLeft1 = pin1.read_digital()
    varLeft2 = pin14.read_digital()
    varLeft3 = pin13.read_digital()
    varRight1 = pin2.read_digital()
    varRight2 = pin15.read_digital()
    varRight3 = pin16.read_digital()
    if (varLeft1 == 1 and varRight1 == 0 and varLeft2 == 0):
        err = 1
    elif (varLeft2 == 1 and varLeft1 == 1):
        err = 2
    elif (varLeft2 == 1 and varLeft1 == 0 and varLeft3 == 0):
        err = 3
    elif (varLeft3 == 1 and varLeft2 == 1):
        err = 4
    elif (varLeft3 == 1 and varLeft2 == 0):
        err = 5
    elif (varRight1 == 1 and varLeft1 == 0 and varRight2 == 0):
        err = -1
    elif (varRight1 == 1 and varRight2 == 1):
        err = -2
    elif (varRight2 == 1 and varRight1 == 0 and varRight3 == 0):
        err = -3
    elif (varRight3 == 1 and varRight2 == 1):
        err = -4
    elif (varRight3 == 1 and varRight2 == 0):
        err = -5
    #print(err)
    return err

def pidControl():
    global preError,integral,delays,leftMotorSpeed,rightMotorSpeed
    global kd, ki, kd
    
    speedScale = 1
    error = getError()
    if (error != 0):
        speedScale = 2/abs(error)
        if speedScale > 1:
            speedScale = 1
    integral = integral + error
    derivative = error - preError
    output = (int)(kp*error + ki*integral*delays + kd*derivative/delays)
    print(output)
    preError = error
    leftMotorSpeed = int((k_speed - output)*speedScale)
    rightMotorSpeed = int((k_speed + output)*speedScale)

    if leftMotorSpeed < -speedMax:
        leftMotorSpeed = -speedMax
    elif leftMotorSpeed > speedMax:
        leftMotorSpeed = speedMax
    if rightMotorSpeed < -speedMax:
        rightMotorSpeed = -speedMax
    elif rightMotorSpeed > speedMax:
        rightMotorSpeed = speedMax   
    setLeftMotor(leftMotorSpeed)
    setRightMotor(rightMotorSpeed)
    
    sleep(abs(error)*turnDelay)

# test code
pidInit(kp_ = 8, ki_ = 0.0, kd_ = 15, delays_ = 1)
#setLeftMotor(60)
#setRightMotor(60)

while True:
    pidControl()

版本历史记录

Version Date Note [+]新增[-]删除[^]修复
V1.4 2018/04/24