Difference between revisions of "Getting Started with Triode-Car"

From Banana Pi Wiki
Jump to: navigation, search
Line 9: Line 9:
 
[[香蕉派_Triode-Car]]
 
[[香蕉派_Triode-Car]]
  
=使用LM393控制小车=
+
=Control With LM393 Chip=
  
==硬件介绍==
+
==Hardware Introduction==
  
在Triode-Car中,有两种模式,其中一个是利用自带的LM393控制小车,另一个则是利用BPI:Bit/Mirco:bit控制小车,可以通过小车上面的SW2按键(如图)切换控制方式。
+
There is a button to switch between two modes on the Triode-Car. One of which is using the built-in LM393 comparator chip, and the other is with the Micro: bit or BPI: bit.
  
在LM393控制小车的模式中,只有一个功能,即巡线,它由几大部分组成,包括了:巡线检测电路,电压比较电路,驱动电路。
+
When using the built-in LM393 chip, it has only one function, to track dark lines, it consists of the Line-Following Circuit, Voltage Comparing Circuit, and the Motor Circuit.
  
===巡线检测电路===
+
===Line-Following Circuit===
  
在Triode-Car的底部,有两个巡线的检测电路,每一路包含了一个光敏电阻和一个LED。当LED的光照到黑色和白色的材料上面时,由于反光率的不同,光敏的阻值会发生明显的变化。
+
The Triode-Car uses two pairs of photoresistor and LED on the bottom side to enable its line tracking feature. When the LED light reflecs on bright and dark surfaces, the resistance of the photoresistor will change drastically.
  
在Triode-Car中,光敏电阻使用的是GL5506,这个型号的电阻会随着光照的加强而减小,亮电阻(10Lux)是2-5KΩ,暗电阻是0.2MΩ。(亮电阻:用400-600Lux光照射2小时后,在标准光源(色温2854K)10Lux光下的测试值。暗电阻:关闭10Lux光照后第10秒的阻值。)
+
The photoresistor used on Triode-Car is the GL5506, this photoresistor can have a resistance as high as 0.2MΩ in the dark(10Lux), while in the light(400~600Lux), it can have a resistance as low as a 2-5KΩ.
  
===电压比较电路===
+
(Bright resistance: Tested after shining with 400~600Lux light for 2 hours, then shine it with a standard 10Lux, with a color temperature of 2854K light source.) (Dark resistance: Tested on the 10th second after shutting off the 10Lux light source.)
 +
 
 +
===Voltage Comparing Circuit===
 
[[File:LM393_sch.jpg|300px]]
 
[[File:LM393_sch.jpg|300px]]
  
电压比较电路主要由LM393和一些电阻组成,LM393是双路电压比较器集成电路,有两个独立的电压比较器构成。它的作用是,比较两个输入电压,并根据两路电压的高低改变输出电压的高低。当比较器的正向(正极)大于反向(负极)电压时,输出高电平,当比较器的正向(正极)小于反向(负极)电压时,输出低电平。由于LM393使用的是集电极开路输出,所以需要在输出端加上上拉电阻。
+
The Voltage Comparing Circuit includes the LM393 chip and several resistors. The LM393 consists of two independent precision voltage comparators with a low offset voltage. Its use is to compare two input voltages, then changes the output voltage accordingly. When the positive voltage from the comparator is higher than reverse voltage, the output will output high signal. When the positive voltage is lower than reverse voltage, it will output low signal. The LM393's output is open collector type, so a pull high resistor is needed here.
  
===驱动电路===
+
===Motor Circuit===
 
   
 
   
电机驱动电路的主角则是三极管,Triode-Car使用的是S8550,它是一种低电压,大电流,小信号的PNP型硅三极管。最大集电极电流为1.5 A。它可以被用来当成一个低电平触发的开关使用。
+
The S8550 PNP triode plays an important role in the motor circuit. It uses a small base current and a negative base voltage to control a larger emitter-collector current. With a maximum collector current of 1.5A, it can be used as a low level switch.
  
==控制原理==
+
==Control Circuit==
  
 
[[File:Triode_line_l.jpg|500px]]
 
[[File:Triode_line_l.jpg|500px]]
  
如图所示,当小车偏离轨道向左时,右边的光敏电阻会来到黑色跑道的上方,光敏电阻阻值变高,而左边的光敏电阻处于白色地面,此时,LL点的电压会比LR点的电压低,此时,比较器U1A的正向电压高于反向电压,1号引脚输出高电平。同理,U1B的正向电压低于反向电压,7号引脚输出低电平。从而控制左边电机正转,小车右拐,R6和D2离开黑色跑道,直到左边的传感器探测到黑跑道,原理与前面类似。小车在前进过程中不断重复以上动作,从而达到前进的目的。
+
As the picture shown above, when the car shifts towards the left, the right photoresistor will be directly on top of the line, therefore, its resistance will increase. Conversely, the left resistor, will shift away from the line, its resistance will decrease. At this point, the voltage from LL will be lower than LR. The U1A comparator's positive voltage will be higher than reverse voltage, triggering pin 1 to pull high. Conversely, U1B's positive voltage will be lower than reverse voltage, pin 7 will pull low, making the left motor move. The car shifts right, R6 and D2 moves away from the dark line until the left sensors detect the dark line. The concept is similar as before, the car repeats the actions above to progress forward.
 +
 
 +
=Use MakeCode to Control the Triode-Car=
  
=Triode-Car 配合Micro:Bit 使用MakeCode环境编程=
+
This is a tutorial on how to control the Triode-Car with Micro:bit Makecode.
  
==基础教程==
+
==Beginner Tutorial==
  
===导入扩展积木&用microbit按钮控制电机===
+
===Adding the Triode-Car Extention to Control with Micro: bit===
  
打开MakeCode,点击"Advanced",再点击最下面的"Extensions",在上方搜索栏中输入 https://github.com/bpi-steam/pxt-triodecar (右键此连接并复制),点击搜索出来的项目,即可导入Triode-Car专用扩展积木。
+
It is recommended for beginners to install the extension blocks by typing in the extensions search bar: " https://github.com/bpi-steam/pxt-triodecar" to add extentions. Click to import the Triode-Car extension pack.
  
扩展积木便于初学者理解程序功能并上手使用。
+
1. As the blocks shown, the car will stop after bootup or reset.
 +
2. Pressing down button A and B at the same time will make the car go forward.
 +
3. Press A to turn Right.
 +
4. Press B to turn Left.
  
例程如下图,启动或复位时停车,同时按下AB按钮直行,按下A按钮右转,按下B按钮左转。
 
  
 
[[File:Triode-Car_motor_control_1.png|300px]]
 
[[File:Triode-Car_motor_control_1.png|300px]]
  
例程项目文件:https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_motor_control_1.hex
+
Example link:https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_motor_control_1.hex
  
项目文件下载到本地后可导入MakeCode中查看和再编辑,也可直接通过USB烧录到Micro:Bit中直接运行。
+
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.
  
 
----
 
----

Revision as of 23:49, 29 June 2021

BPI Triode-Car 配合 micro:bit 使用
BPI Triode-Car 配合BPI-Bit STEAM 教育开发板 使用
Triode-Car 配合 BPI:AI AI开发板使用

产品信息

香蕉派_Triode-Car

Control With LM393 Chip

Hardware Introduction

There is a button to switch between two modes on the Triode-Car. One of which is using the built-in LM393 comparator chip, and the other is with the Micro: bit or BPI: bit.

When using the built-in LM393 chip, it has only one function, to track dark lines, it consists of the Line-Following Circuit, Voltage Comparing Circuit, and the Motor Circuit.

Line-Following Circuit

The Triode-Car uses two pairs of photoresistor and LED on the bottom side to enable its line tracking feature. When the LED light reflecs on bright and dark surfaces, the resistance of the photoresistor will change drastically.

The photoresistor used on Triode-Car is the GL5506, this photoresistor can have a resistance as high as 0.2MΩ in the dark(10Lux), while in the light(400~600Lux), it can have a resistance as low as a 2-5KΩ.

(Bright resistance: Tested after shining with 400~600Lux light for 2 hours, then shine it with a standard 10Lux, with a color temperature of 2854K light source.) (Dark resistance: Tested on the 10th second after shutting off the 10Lux light source.)

Voltage Comparing Circuit

LM393 sch.jpg

The Voltage Comparing Circuit includes the LM393 chip and several resistors. The LM393 consists of two independent precision voltage comparators with a low offset voltage. Its use is to compare two input voltages, then changes the output voltage accordingly. When the positive voltage from the comparator is higher than reverse voltage, the output will output high signal. When the positive voltage is lower than reverse voltage, it will output low signal. The LM393's output is open collector type, so a pull high resistor is needed here.

Motor Circuit

The S8550 PNP triode plays an important role in the motor circuit. It uses a small base current and a negative base voltage to control a larger emitter-collector current. With a maximum collector current of 1.5A, it can be used as a low level switch.

Control Circuit

Triode line l.jpg

As the picture shown above, when the car shifts towards the left, the right photoresistor will be directly on top of the line, therefore, its resistance will increase. Conversely, the left resistor, will shift away from the line, its resistance will decrease. At this point, the voltage from LL will be lower than LR. The U1A comparator's positive voltage will be higher than reverse voltage, triggering pin 1 to pull high. Conversely, U1B's positive voltage will be lower than reverse voltage, pin 7 will pull low, making the left motor move. The car shifts right, R6 and D2 moves away from the dark line until the left sensors detect the dark line. The concept is similar as before, the car repeats the actions above to progress forward.

Use MakeCode to Control the Triode-Car

This is a tutorial on how to control the Triode-Car with Micro:bit Makecode.

Beginner Tutorial

Adding the Triode-Car Extention to Control with Micro: bit

It is recommended for beginners to install the extension blocks by typing in the extensions search bar: " https://github.com/bpi-steam/pxt-triodecar" to add extentions. Click to import the Triode-Car extension pack.

1. As the blocks shown, the car will stop after bootup or reset. 2. Pressing down button A and B at the same time will make the car go forward. 3. Press A to turn Right. 4. Press B to turn Left.


Triode-Car motor control 1.png

Example link:https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_motor_control_1.hex

Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.


调整电机转速

Triode-Car专用扩展积木中有可单独控制左右电机转速的积木,可进行10级调速。

例程如下图所示,按一次A按钮转速加1档,按一次B按钮转速减1档,Micro:Bit显示当前挡位数值。

Triode-Car motor control 2.png

其中"forever"积木会在"on start"积木执行完后开始无限循环执行其内部的积木,而在每次循环的间隙则可执行其他事件处理程序,如on button A/B pressed。

加入了一个"if"判断积木,若变量"speed"值小于0或大于10时,将变量"speed"值设为0,这样可以将值限定在0到10范围内不会溢出而报错。

例程项目文件:https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_motor_control_2.hex

项目文件下载到本地后可导入MakeCode中查看和再编辑,也可直接通过USB烧录到Micro:Bit中直接运行。


采集巡线检测电路的电压模拟信号

硬件原理参考“巡线检测电路”中的介绍。

Triode-Car专用扩展积木中有可采集巡线检测电路的电压模拟信号的"read left/right line tracking sensor"积木。

Micro:Bit引脚对0至3.3V电压测量精度为10bit即2^10=1024级,所以调用"read left/right line tracking sensor"积木从对应引脚读到的电压模拟值将为0至1023。

例程如下图所示,在程序中,每间隔100ms通过USB串口将读取到的左右两个传感器的电压模拟值发送给电脑,在MakeCode中打开控制台即可查看实时接收到的信息。

Triode-car read LDR.png

例程项目文件:https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_read_LDR.hex

项目文件下载到本地后可导入MakeCode中查看和再编辑,也可直接通过USB烧录到Micro:Bit中直接运行。


蓝牙控制

micro:bit支持蓝牙通讯,应用蓝牙扩展积木进行编程,在支持蓝牙通讯的Apple或Android设备上安装APP即可通过蓝牙无线控制。

适用iPhone或iPad: 官方APP micro:bit App Store链接

适用Android手机或平板:第三方APP microbit-blue Github链接或 [https://play.google.com/store/apps/details?id=com.kitronik.blemove 第三方APP Kitronik Move Google Play链接]

高阶教程

校准巡线检测电路

为了能以较高的灵敏度应用巡线检测电路,我们需要手动对两个光敏电阻对应连接的可调电阻进行微调,使其工作在灵敏度较高的区间内,且应该尽量使相同光照强度下输出的电压模拟量保持相等。

我们可以直接应用基础教程中的“采集巡线检测电路的电压模拟信号”教程中所示的例程来在电脑上输出采集到的电压模拟量数值,然后使用螺丝刀对可调电阻进行调节。

在Micro:Bit所能测量的0-1023级电压模拟量范围内,越靠近中间值,光敏电阻对光照强弱变化的响应灵敏度就越高。

所以对巡线检测电路校准时,应在稳定的环境光下,尽量将两个可调电阻调节到靠近中间值512,并尽量使二者在相同光照强度下输出的电压模拟量的差值减小。

以上实行校准步骤都是建立在保持Micro:Bit与PC连接,Micro:Bit与Triode-Car连接的前提下的,但实际应用中,为了提高使用时的灵敏度,最好直接在实际应用的场景下进行校准。

我们不一定总有条件在实际应用的场景下还能保持Micro:Bit与PC连接。在不能连接PC的时候,就需要提前写好一个可以正确的指引我们校准巡线检测电路的程序。

分析校准的步骤:

  1. 选择先对左侧的可调电阻进行手动调整,使其并联的光敏电阻在Micro:Bit对应引脚上输出的电压模拟量接近中间值。
  2. 在上一个条件满足的前提下调整右侧的可调电阻,使其并联的光敏电阻在Micro:Bit对应引脚上输出的电压模拟量接近另一个光敏电阻。

在程序上这显然是可以通过"if"条件判断来完成的,而对于实际进行手动校准的人,则是需要得到对应条件下使Micro:Bit显示不同图形使人也能得到条件满足的反馈。

例程如下图所示。

Triode-car LDR calibration 1.png

由于例程较为复杂,所以给出一些必要的说明:

  1. 将程序整体放入一个"function"自定义函数中,这有利于我们从认知上在大量积木中区分某一部分的功能,方便后续调用或维护。
  2. 整体由两个"while"循环积木组成,加入了循环条件,这样可以在可调电阻校准完成后改变循环条件退出循环。
  3. 在进入"while"循环之前,使Micro:Bit显示对应的方向指示,给人以直观的行动目标,确认当前应该要进行手动调整的可调电阻。
  4. 第一个"while"循环积木中的程序用于校准左侧光敏电阻,其中"if"判断条件为,左侧光敏电阻输出的电压模拟量大于等于450小于等于550。
  5. 当满足第4条中的"if"判断条件后,使Micro:Bit显示一个表示正确的图形,给人以视觉上反馈,此时人应该停止对左侧的可调电阻的调节,随后延时1000ms再一次进行相同的"if"判断条件,用以消除手动调整可能产生的抖动而带来的误差,当再次确认条件满足时,改变控制这个"while"循环的循环条件以退出循环,执行下一步。
  6. 第二个"while"循环积木中的程序用于校准右侧光敏电阻,其中"if"判断条件为,左右两侧光敏电阻输出的电压模拟量相减,其差值的绝对值小于等于25。

例程项目文件:https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_LDR_calibration_2.hex

项目文件下载到本地后可导入MakeCode中查看和再编辑,也可直接通过USB烧录到Micro:Bit中直接运行。


巡线行驶

在“校准巡线检测电路”之后,我们即可开始对巡线检测电路进行有效利用。

根据“巡线检测电路”中所介绍的原理,巡线需要利用“线路”与其两侧路面对光线不同的反射率进行实时的光照强度检测,在程序中读取左右两个光敏电阻的电压模拟值,对数值进行比对,以此判断Triode-Car行进路线是否发生偏移以及偏移方向,进而对左右两个电机的启停进行控制,修正Triode-Car的行驶方向,达成沿着“线路”行驶的目的。

例程如下图所示。

Triode-car Line Follower.png

由于例程较为复杂,所以给出一些必要的说明:

  1. "on button A pressed"积木用于控制循迹程序的启动和停止,每按一次按钮A,其中设置的变量就会改变一次状态。
  2. "forever"积木将重复执行其内部的程序,每次循环结束或在循环中执行到"show"或"pause"积木时会让出线程允许其他的"forever"积木或事件处理程序运行,所以此处三个"forever"积木与一个"on button A pressed"积木可以共同在后台运行,这样使系统具备同时执行多个程序的能力,一般将此称作“多工”。
  3. 第一个"forever"积木内的程序用于循环读取左右两个光敏电阻电压模拟值,然后在多级"if"条件积木中进行判断,满足相应条件时改变变量,该变量用于控制电机。
  4. 第二个"forever"积木内的多级"if"条件积木对第一个"forever"积木内改变的变量值进行判断,直接输出控制信号来控制左右电机的启停和转速。
  5. 第一,二个"forever"积木内的循环条件即为"on button A pressed"积木控制的变量,变量为"true"值时才会循环执行。
  6. 第三个"forever"积木略有巧思,其内部的循环仅在用于控制电机的变量发生变化时才会执行一次,对应改变LED当前应当显示的内容。

例程项目文件:https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_Line_Follower.hex

项目文件下载到本地后可导入MakeCode中查看和再编辑,也可直接通过USB烧录到Micro:Bit中直接运行。

Triode-Car 配合BPI:Bit 使用Arduino环境编程

基础教程

高阶教程

Triode-Car 配合BPI:Bit 使用MicroPython环境编程

基础教程

高阶教程