云天 发表于 2021-11-8 21:57:18

Maixduino麦克纳姆轮小车

本帖最后由 云天 于 2021-11-8 22:02 编辑




https://www.bilibili.com/video/BV1nT4y197zN?share_source=copy_web

什么是麦克纳姆轮在竞赛机器人和特殊工种机器人中,全向移动经常是一个必需的功能。「全向移动」意味着可以在平面内做出任意方向平移同时自转的动作。为了实现全向移动,一般机器人会使用「全向轮」(Omni Wheel)或「麦克纳姆轮」(Mecanum Wheel)这两种特殊轮子。全向轮:麦克纳姆轮:麦克纳姆轮与普通轮子的区别在于麦克纳姆轮旋转时,由于存在斜向的从动轮,会同时产生一个斜向的力,当我们控制轮子旋转的速度与方向时,将斜向的力增强或抵消,从而实现小车的全向移动。可以完成横移、斜方向移动等普通小车无法完成的高难度动作,轮子的转动方向与小车的运动方向关系如下图:【麦克纳姆轮安装】车拆时再上图
【电机驱动L298N安装】车拆时再上图
【电机电源及灯带安装】车拆时再上图
【Maixduino 主控安装】车拆时再上图
【Mind+编程】【Micrpython代码】

# MindPlus
# maixduino
from network_esp32 import wifi
from board import board_info
from umqtt import MQTTClient
from machine import Timer
from pin import Pin
import ubinascii
import neopixel
import machine
import usocket
import time
import gc


def neopixel_range(rgb, start, end, color):
trend = -1 if end < start else 1
for i in range(start, end-1 if trend < 0 else end+1, trend):
    rgb = (color)
rgb.write()

def hostgetIP(host):
      return usocket.getaddrinfo(host,8080,0,0)

_mqtt_topic_list = []

def timer11_tick(_):
global mqtt
mqtt.check_msg()

def mqtt_callback(topic, msg):
try:
    topic = topic.decode('utf-8', 'ignore')
    _msg = msg.decode('utf-8', 'ignore')
    eval('mqtt_topic_' + bytes.decode(ubinascii.hexlify(topic)) + '("' + _msg + '")')
except:
    print((topic, msg))

def neopixel_rotate(rgb, offset):
steps = rgb.n
if offset == 0 or steps == 0:
    return
offset = (abs(offset) % steps)*(-1 if offset < 0 else 1)
c = []
if offset > 0:
    for i in range(0, offset):
      c.append(rgb)
    for i in range(steps-1, offset-1, -1):
      rgb = rgb
    for i in range(0, offset):
      rgb = c
else:
    for i in range(0, abs(offset)):
      c.append(rgb)
    for i in range(0, steps-abs(offset)):
      rgb = rgb
    for i in range(steps-abs(offset), steps):
      rgb = c
rgb.write()

def neopixel_hsl(h, s, l):
h = (abs(h) % 360)*(-1 if h < 0 else 1)
s = max(s, 0); s = min(s, 99); l = max(l, 0); l = min(l, 99)
c = (((100 - abs(2 * l - 100)) * s) << 8) // 10000
h1 = h // 60; h2 = (h - h1 * 60) * 256 // 60;
temp = abs((((h1 % 2) << 8) + h2) - 256);
x = (c * (256 - (temp))) >> 8; _r = _g = _b = 0
if h1 == 0:
    _r = c; _g = x; _b = 0
elif h1 == 1:
    _r = x; _g = c; _b = 0
elif h1 == 2:
    _r = 0; _g = c; _b = x
elif h1 == 3:
    _r = 0; _g = x; _b = c
elif h1 == 4:
    _r = x; _g = 0; _b = c
elif h1 == 5:
    _r = c; _g = 0; _b = x
m = ((l * 2 << 8) // 100 - c) // 2
r = _r + m; g = _g + m; b = _b + m
return (r, g, b)

def neopixel_rainbow(rgb, start, end, start_hue, end_hue):
steps = rgb.n
if steps == 0:
    return
if end < start:
    num = end; end = start; start = num
start = max(start, 0); start = min(start, steps)
end = max(end, 0); end = min(end, steps)
steps = end - start + 1
saturation = 100; luminance = 50; h1 = start_hue; h2 = end_hue
hDistCW = (abs((h2 + 360) - h1) % 360)*(-1 if ((h2 + 360) - h1) < 0 else 1)
hStepCW = (hDistCW * 100) // steps; hStep = hStepCW
h1_100 = h1 * 100; s1 = saturation; s2 = saturation; sDist = s2 - s1
sStep = sDist // steps; s1_100 = s1 * 100; l1 = luminance; l2 = luminance
lDist = l2 - l1; lStep = lDist // steps; l1_100 = l1 * 100
if steps == 1:
    rgb =neopixel_hsl(h1 + hStep, s1 + sStep, l1 + lStep)
else:
    rgb = neopixel_hsl(start_hue, saturation, luminance)
    for i in range(start + 1, start + steps - 1):
      h = (h1_100 + i * hStep) // 100 + 360
      s = (s1_100 + i * sStep) // 100
      l = (l1_100 + i * lStep) // 100
      rgb = neopixel_hsl(h, s, l)
    rgb = neopixel_hsl(end_hue, saturation, luminance)
rgb.write()

# 自定义函数
def back():
Pin.digital_write(board_info.PIN2, 1)
Pin.digital_write(board_info.PIN3, 0)
Pin.digital_write(board_info.PIN4, 1)
Pin.digital_write(board_info.PIN5, 0)
Pin.digital_write(board_info.PIN8, 0)
Pin.digital_write(board_info.PIN9, 1)
Pin.digital_write(board_info.PIN10, 0)
Pin.digital_write(board_info.PIN11, 1)
def forward():
Pin.digital_write(board_info.PIN2, 0)
Pin.digital_write(board_info.PIN3, 1)
Pin.digital_write(board_info.PIN4, 0)
Pin.digital_write(board_info.PIN5, 1)
Pin.digital_write(board_info.PIN8, 1)
Pin.digital_write(board_info.PIN9, 0)
Pin.digital_write(board_info.PIN10, 1)
Pin.digital_write(board_info.PIN11, 0)
def stop():
Pin.digital_write(board_info.PIN2, 0)
Pin.digital_write(board_info.PIN3, 0)
Pin.digital_write(board_info.PIN4, 0)
Pin.digital_write(board_info.PIN5, 0)
Pin.digital_write(board_info.PIN8, 0)
Pin.digital_write(board_info.PIN9, 0)
Pin.digital_write(board_info.PIN10, 0)
Pin.digital_write(board_info.PIN11, 0)
def right():
Pin.digital_write(board_info.PIN10, 1)
Pin.digital_write(board_info.PIN11, 0)
Pin.digital_write(board_info.PIN2, 1)
Pin.digital_write(board_info.PIN3, 0)
Pin.digital_write(board_info.PIN8, 0)
Pin.digital_write(board_info.PIN9, 1)
Pin.digital_write(board_info.PIN4, 0)
Pin.digital_write(board_info.PIN5, 1)
def left():
Pin.digital_write(board_info.PIN10, 0)
Pin.digital_write(board_info.PIN11, 1)
Pin.digital_write(board_info.PIN2, 0)
Pin.digital_write(board_info.PIN3, 1)
Pin.digital_write(board_info.PIN8, 1)
Pin.digital_write(board_info.PIN9, 0)
Pin.digital_write(board_info.PIN4, 1)
Pin.digital_write(board_info.PIN5, 0)

# 事件回调函数
def mqtt_topic_356e39327571424d67(_msg):
global JieGuo
if (_msg == 'G'):
    forward()
    time.sleep(0.2)
    stop()
if (_msg == 'B'):
    back()
    time.sleep(0.2)
    stop()
if (_msg == 'L'):
    left()
    time.sleep(0.2)
    stop()
if (_msg == 'R'):
    right()
    time.sleep(0.2)
    stop()
if (_msg == 'S'):
    stop()
if (_msg == 'Y'):
    JieGuo = 1
    neopixel_rainbow(ws2812_13, 0, 15, 1, 360)
if (_msg == 'N'):
    JieGuo = 0
    ws2812_13.fill((0, 0, 0))
    ws2812_13.write()


ws2812_13 = neopixel.NeoPixel(board_info.PIN13, n=16, bpp=3, timing=1)
ws2812_13.fill((0, 0, 0))
ws2812_13.write()
neopixel_range(ws2812_13, 0, 15, (0, 0, 255))
time.sleep(3)
ws2812_13.fill((0, 0, 0))
ws2812_13.write()
wifi.connect("sxs", "smj080823")
while not (wifi.isconnected()):
pass
mqtt = MQTTClient("", hostgetIP("iot.dfrobot.com.cn"), 1883, "X8jykxFnR", "u8jskbFngz", keepalive=300)
try:
mqtt.connect()
print('MQTT Connected Successful')
except:
print('MQTT Connection Failed')
mqtt.set_callback(mqtt_callback)
mqtt.subscribe("5n92uqBMg")
tim11 =Timer(Timer.TIMER2,Timer.CHANNEL3,mode=Timer.MODE_PERIODIC,period=50,callback=timer11_tick)
ShiJian = time.ticks_ms()
JieGuo = 0
neopixel_range(ws2812_13, 0, 15, (204, 0, 0))
time.sleep(3)
ws2812_13.fill((0, 0, 0))
ws2812_13.write()
while True:
if (JieGuo == 1):
    if ((time.ticks_ms() - ShiJian) > 200):
      neopixel_rotate(ws2812_13, 1)
      ShiJian = time.ticks_ms()
if Pin.digital_read(board_info.PIN6):
    gc.collect()
    neopixel_range(ws2812_13, 0, 15, (0, 0, 255))
    time.sleep(0.2)
    ws2812_13.fill((0, 0, 0))
    ws2812_13.write()
    neopixel_range(ws2812_13, 0, 15, (204, 0, 0))
    time.sleep(0.2)
    ws2812_13.fill((0, 0, 0))
    ws2812_13.write()
    machine.reset()

其中修改“mqtt = MQTTClient("", hostgetIP("iot.dfrobot.com.cn"), 1883, "X8jykxFnR", "u8jskbFngz", keepalive=300)”中的“keepalive”值改为“300”。(MQTT在建立连接的时候,我们可以传递一个 Keep Alive 参数,它的单位为秒,MQTT 协议中约定:在 1.5*Keep Alive 的时间间隔内,如果 Broker 没有收到来自 Client 的任何数据包,那么 Broker 认为它和 Client 之间的连接已经断开;同样地, 如果 Client 没有收到来自 Broker 的任何数据包,那么 Client 认为它和 Broker 之间的连接已经断开。)
【App Inventor编程】




【程序进一步优化】为实现小车连续运动,将APP中的按钮命令“点击”发送修改为“按下”发送,再增加“松开”命令发送“停止指令”。Maixduino程序中将接收命令后的"stop"指令去掉。





# MindPlus
# maixduino
from network_esp32 import wifi
from board import board_info
from umqtt import MQTTClient
from machine import Timer
from pin import Pin
import ubinascii
import neopixel
import usocket
import time


def neopixel_range(rgb, start, end, color):
trend = -1 if end < start else 1
for i in range(start, end-1 if trend < 0 else end+1, trend):
    rgb = (color)
rgb.write()

def hostgetIP(host):
      return usocket.getaddrinfo(host,8080,0,0)

_mqtt_topic_list = []

def timer11_tick(_):
global mqtt
mqtt.check_msg()

def mqtt_callback(topic, msg):
try:
    topic = topic.decode('utf-8', 'ignore')
    _msg = msg.decode('utf-8', 'ignore')
    eval('mqtt_topic_' + bytes.decode(ubinascii.hexlify(topic)) + '("' + _msg + '")')
except:
    print((topic, msg))

def neopixel_rotate(rgb, offset):
steps = rgb.n
if offset == 0 or steps == 0:
    return
offset = (abs(offset) % steps)*(-1 if offset < 0 else 1)
c = []
if offset > 0:
    for i in range(0, offset):
      c.append(rgb)
    for i in range(steps-1, offset-1, -1):
      rgb = rgb
    for i in range(0, offset):
      rgb = c
else:
    for i in range(0, abs(offset)):
      c.append(rgb)
    for i in range(0, steps-abs(offset)):
      rgb = rgb
    for i in range(steps-abs(offset), steps):
      rgb = c
rgb.write()

def neopixel_hsl(h, s, l):
h = (abs(h) % 360)*(-1 if h < 0 else 1)
s = max(s, 0); s = min(s, 99); l = max(l, 0); l = min(l, 99)
c = (((100 - abs(2 * l - 100)) * s) << 8) // 10000
h1 = h // 60; h2 = (h - h1 * 60) * 256 // 60;
temp = abs((((h1 % 2) << 8) + h2) - 256);
x = (c * (256 - (temp))) >> 8; _r = _g = _b = 0
if h1 == 0:
    _r = c; _g = x; _b = 0
elif h1 == 1:
    _r = x; _g = c; _b = 0
elif h1 == 2:
    _r = 0; _g = c; _b = x
elif h1 == 3:
    _r = 0; _g = x; _b = c
elif h1 == 4:
    _r = x; _g = 0; _b = c
elif h1 == 5:
    _r = c; _g = 0; _b = x
m = ((l * 2 << 8) // 100 - c) // 2
r = _r + m; g = _g + m; b = _b + m
return (r, g, b)

def neopixel_rainbow(rgb, start, end, start_hue, end_hue):
steps = rgb.n
if steps == 0:
    return
if end < start:
    num = end; end = start; start = num
start = max(start, 0); start = min(start, steps)
end = max(end, 0); end = min(end, steps)
steps = end - start + 1
saturation = 100; luminance = 50; h1 = start_hue; h2 = end_hue
hDistCW = (abs((h2 + 360) - h1) % 360)*(-1 if ((h2 + 360) - h1) < 0 else 1)
hStepCW = (hDistCW * 100) // steps; hStep = hStepCW
h1_100 = h1 * 100; s1 = saturation; s2 = saturation; sDist = s2 - s1
sStep = sDist // steps; s1_100 = s1 * 100; l1 = luminance; l2 = luminance
lDist = l2 - l1; lStep = lDist // steps; l1_100 = l1 * 100
if steps == 1:
    rgb =neopixel_hsl(h1 + hStep, s1 + sStep, l1 + lStep)
else:
    rgb = neopixel_hsl(start_hue, saturation, luminance)
    for i in range(start + 1, start + steps - 1):
      h = (h1_100 + i * hStep) // 100 + 360
      s = (s1_100 + i * sStep) // 100
      l = (l1_100 + i * lStep) // 100
      rgb = neopixel_hsl(h, s, l)
    rgb = neopixel_hsl(end_hue, saturation, luminance)
rgb.write()

# 自定义函数
def back():
Pin.digital_write(board_info.PIN2, 1)
Pin.digital_write(board_info.PIN3, 0)
Pin.digital_write(board_info.PIN4, 1)
Pin.digital_write(board_info.PIN5, 0)
Pin.digital_write(board_info.PIN8, 0)
Pin.digital_write(board_info.PIN9, 1)
Pin.digital_write(board_info.PIN10, 0)
Pin.digital_write(board_info.PIN11, 1)
def forward():
Pin.digital_write(board_info.PIN2, 0)
Pin.digital_write(board_info.PIN3, 1)
Pin.digital_write(board_info.PIN4, 0)
Pin.digital_write(board_info.PIN5, 1)
Pin.digital_write(board_info.PIN8, 1)
Pin.digital_write(board_info.PIN9, 0)
Pin.digital_write(board_info.PIN10, 1)
Pin.digital_write(board_info.PIN11, 0)
def stop():
Pin.digital_write(board_info.PIN2, 0)
Pin.digital_write(board_info.PIN3, 0)
Pin.digital_write(board_info.PIN4, 0)
Pin.digital_write(board_info.PIN5, 0)
Pin.digital_write(board_info.PIN8, 0)
Pin.digital_write(board_info.PIN9, 0)
Pin.digital_write(board_info.PIN10, 0)
Pin.digital_write(board_info.PIN11, 0)
def right():
Pin.digital_write(board_info.PIN10, 1)
Pin.digital_write(board_info.PIN11, 0)
Pin.digital_write(board_info.PIN2, 1)
Pin.digital_write(board_info.PIN3, 0)
Pin.digital_write(board_info.PIN8, 0)
Pin.digital_write(board_info.PIN9, 1)
Pin.digital_write(board_info.PIN4, 0)
Pin.digital_write(board_info.PIN5, 1)
def left():
Pin.digital_write(board_info.PIN10, 0)
Pin.digital_write(board_info.PIN11, 1)
Pin.digital_write(board_info.PIN2, 0)
Pin.digital_write(board_info.PIN3, 1)
Pin.digital_write(board_info.PIN8, 1)
Pin.digital_write(board_info.PIN9, 0)
Pin.digital_write(board_info.PIN4, 1)
Pin.digital_write(board_info.PIN5, 0)

# 事件回调函数
def mqtt_topic_356e39327571424d67(_msg):
global JieGuo
if (_msg == 'G'):
    forward()
if (_msg == 'B'):
    back()
if (_msg == 'L'):
    left()
if (_msg == 'R'):
    right()
if (_msg == 'S'):
    stop()
if (_msg == 'Y'):
    JieGuo = 1
    neopixel_rainbow(ws2812_13, 0, 15, 1, 360)
if (_msg == 'N'):
    JieGuo = 0
    ws2812_13.fill((0, 0, 0))
    ws2812_13.write()


ws2812_13 = neopixel.NeoPixel(board_info.PIN13, n=16, bpp=3, timing=1)
ws2812_13.fill((0, 0, 0))
ws2812_13.write()
neopixel_range(ws2812_13, 0, 15, (0, 0, 255))
time.sleep(3)
ws2812_13.fill((0, 0, 0))
ws2812_13.write()
wifi.connect("sxs", "smj080823")
while not (wifi.isconnected()):
pass
mqtt = MQTTClient("", hostgetIP("iot.dfrobot.com.cn"), 1883, "X8jykxFnR", "u8jskbFngz", keepalive=30)
try:
mqtt.connect()
print('MQTT Connected Successful')
except:
print('MQTT Connection Failed')
mqtt.set_callback(mqtt_callback)
mqtt.subscribe("5n92uqBMg")
tim11 =Timer(Timer.TIMER2,Timer.CHANNEL3,mode=Timer.MODE_PERIODIC,period=50,callback=timer11_tick)
ShiJian = time.ticks_ms()
JieGuo = 0
neopixel_range(ws2812_13, 0, 15, (204, 0, 0))
time.sleep(3)
ws2812_13.fill((0, 0, 0))
ws2812_13.write()
while True:
if (JieGuo == 1):
    if ((time.ticks_ms() - ShiJian) > 200):
      neopixel_rotate(ws2812_13, 1)
      ShiJian = time.ticks_ms()


演示:麦克纳姆大战小黑https://www.bilibili.com/video/BV1MP4y157A5?share_source=copy_web
【语音控制】
1、使用软件“MaixPy IDE”,首先将asr.py上传到Maixduino板子上。选择“工具”菜单,“发送文件到开发板”。
文中用到的ASR.py是由df团队编写(感谢李工提供),具体代码如下:

import time
import json
from Maix import GPIO, I2S
from fpioa_manager import fm
from speech_recognizer import isolated_word

class ASR:
    def __init__(self):
      self.asr = None
      self.i2s = None
      self.model = None

    def __start(self, size):
      if self.asr:
            del self.asr
      if self.i2s:
            del self.i2s
      try:
            fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)
            fm.register(18,fm.fpioa.I2S0_SCLK, force=True)
            fm.register(19,fm.fpioa.I2S0_WS, force=True)
            self.i2s = I2S(I2S.DEVICE_0)
            self.i2s.channel_config(self.i2s.CHANNEL_0, self.i2s.RECEIVER, align_mode=I2S.STANDARD_MODE)
            self.i2s.set_sample_rate(16000)
            self.asr = isolated_word(dmac=2, i2s=I2S.DEVICE_0, size=size)
            self.asr.set_threshold(0, 0, 15000)
            return True
      except:
            return False

    def newModel(self):
      return []

    def training(self, corpus, model):
      if not corpus or not self.__start(1) or not self.asr:
            return
      rc=1
      co=0
      while True:
            time.sleep_ms(100)
            if self.asr.Done == self.asr.record(0):
                model.append({'name': corpus, 'data': self.asr.get(0)})
                print("complete")
                break
            if self.asr.Speak == self.asr.state():
                if rc != -1:
                  print()
                print("speak: " + str(corpus))
                rc = -1
            else:
                if co%2==0:
                  print("\rready" + "."*rc + " "*(3-rc), end='')
                  rc += 1
                  if rc > 3:
                        rc = 1
            co += 1

    def run(self, model):
      if not model or len(model)<1 or not self.__start(len(model)) or not self.asr:
            return None
      for i in range(len(model)):
            self.asr.set(i, model['data'])
      self.model = model

    def recognize(self):
      if not self.model or len(self.model)<1 or not self.asr:
            return None
      for i in range(20):
            if self.asr.Done == self.asr.recognize():
                result = self.asr.result()
                if result:
                  return self.model]['name']
      return None

    def save(self, model, path):
      if not model or not path:
            return False
      try:
            for i in range(len(model)):
                model['data'] = list(model['data'])
                model['data'] = list(model['data'])
            with open(path, "wb") as f:
                json.dump(model,f)
                f.close()
            return True
      except:
            return False

    def load(self, path):
      if not path:
            return []
      try:
            with open(path, "rb") as f:
                model = json.load(f)
                for i in range(len(model)):
                  model['data'] = bytes(model['data'])
                  model['data'] = tuple(model['data'])
                f.close()
            return model
      except:
            return []

asr = ASR()

2、固件要使用“maixpy_v0.6.2_72_g22a8555b5_minimum_speech_with_ide_support ”


https://cn.dl.sipeed.com/shareURL/MAIX/MaixPy/release/master/maixpy_v0.6.2_72_g22a8555b5
3、训练代码

# MindPlus
# maixduino
from board import board_info

from ASR import asr
import time
from Maix import GPIO

from fpioa_manager import fm
fm.register(board_info.PIN12,fm.fpioa.GPIO0)
led_r=GPIO(GPIO.GPIO0,GPIO.OUT)
led_r.value(0)
time.sleep(1)
led_r.value(1)
model = asr.newModel()
corpus = ['打开','关闭','退出']
print('开始训练')
for order in corpus:
asr.training(order, model)
print('训练完成')
asr.save(model, "/flash/asr.json")
asr.run(asr.load("/flash/asr.json"))
while True:
result = asr.recognize()
if bool(result):
    print(result)
    if (result == '打开'):
      led_r.value(1)
    if (result == '关闭'):
      led_r.value(0)
    if (result == '退出'):
      break
fm.unregister(board_info.PIN12)



LED灯接在扩展板的12引脚上。

4、测试代码



# MindPlus
# maixduino
from board import board_info

from ASR import asr
import time
from Maix import GPIO

from fpioa_manager import fm
fm.register(board_info.PIN12,fm.fpioa.GPIO0)
led_r=GPIO(GPIO.GPIO0,GPIO.OUT)
led_r.value(1)
time.sleep(1)
led_r.value(0)
asr.run(asr.load("/flash/asr.json"))
while True:
result = asr.recognize()
if bool(result):
    print(result)
    if (result == '打开'):
      led_r.value(1)
    if (result == '关闭'):
      led_r.value(0)
    if (result == '退出'):
      break
fm.unregister(board_info.PIN12)

测试
https://www.bilibili.com/video/BV1kL4y1q7ik?share_source=copy_web
【语音控车】1.训练程序

from board import board_info

from ASR import asr
import time
from Maix import GPIO

from fpioa_manager import fm
fm.register(board_info.PIN12,fm.fpioa.GPIO0)
led_r=GPIO(GPIO.GPIO0,GPIO.OUT)
led_r.value(0)
time.sleep(1)
led_r.value(1)
model = asr.newModel()
#corpus = ['打开','关闭','退出','前进','后退','向左','向右','停止']
print('开始训练')
for order in corpus:
asr.training(order, model)
print('训练完成')
asr.save(model, "/flash/asr.json")


2、语音控车代码


# MindPlus
# maixduino

from board import board_info

from ASR import asr
import time
from Maix import GPIO

from fpioa_manager import fm
fm.register(board_info.PIN2,fm.fpioa.GPIO0)
PIN2=GPIO(GPIO.GPIO0,GPIO.OUT)
fm.register(board_info.PIN3,fm.fpioa.GPIO1)
PIN3=GPIO(GPIO.GPIO1,GPIO.OUT)
fm.register(board_info.PIN4,fm.fpioa.GPIO2)
PIN4=GPIO(GPIO.GPIO2,GPIO.OUT)
fm.register(board_info.PIN5,fm.fpioa.GPIO3)
PIN5=GPIO(GPIO.GPIO3,GPIO.OUT)
fm.register(board_info.PIN8,fm.fpioa.GPIO4)
PIN8=GPIO(GPIO.GPIO4,GPIO.OUT)
fm.register(board_info.PIN9,fm.fpioa.GPIO5)
PIN9=GPIO(GPIO.GPIO5,GPIO.OUT)
fm.register(board_info.PIN10,fm.fpioa.GPIO6)
PIN10=GPIO(GPIO.GPIO6,GPIO.OUT)
fm.register(board_info.PIN11,fm.fpioa.GPIO7)
PIN11=GPIO(GPIO.GPIO7,GPIO.OUT)
fm.register(board_info.PIN12,fm.fpioa.GPIOHS0)
led_r=GPIO(GPIO.GPIOHS0,GPIO.OUT)

# 自定义函数
def back():
Pin.digital_write(PIN2, 1)
Pin.digital_write(PIN3, 0)
Pin.digital_write(PIN4, 1)
Pin.digital_write(PIN5, 0)
Pin.digital_write(PIN8, 0)
Pin.digital_write(PIN9, 1)
Pin.digital_write(PIN10, 0)
Pin.digital_write(PIN11, 1)
def forward():
Pin.digital_write(PIN2, 0)
Pin.digital_write(PIN3, 1)
Pin.digital_write(PIN4, 0)
Pin.digital_write(PIN5, 1)
Pin.digital_write(PIN8, 1)
Pin.digital_write(PIN9, 0)
Pin.digital_write(PIN10, 1)
Pin.digital_write(PIN11, 0)
def stop():
Pin.digital_write(PIN2, 0)
Pin.digital_write(PIN3, 0)
Pin.digital_write(PIN4, 0)
Pin.digital_write(PIN5, 0)
Pin.digital_write(PIN8, 0)
Pin.digital_write(PIN9, 0)
Pin.digital_write(PIN10, 0)
Pin.digital_write(PIN11, 0)
def right():
Pin.digital_write(PIN10, 1)
Pin.digital_write(PIN11, 0)
Pin.digital_write(PIN2, 1)
Pin.digital_write(PIN3, 0)
Pin.digital_write(PIN8, 0)
Pin.digital_write(PIN9, 1)
Pin.digital_write(PIN4, 0)
Pin.digital_write(PIN5, 1)
def left():
Pin.digital_write(PIN10, 0)
Pin.digital_write(PIN11, 1)
Pin.digital_write(PIN2, 0)
Pin.digital_write(PIN3, 1)
Pin.digital_write(PIN8, 1)
Pin.digital_write(PIN9, 0)
Pin.digital_write(PIN4, 1)
Pin.digital_write(PIN5, 0)

led_r.value(1)
time.sleep(1)
led_r.value(0)
asr.run(asr.load("/flash/asr.json"))
while True:
result = asr.recognize()
if bool(result):
    print(result)
    if (result == '前进'):
      forword()
      time.sleep(0.2)
      stop()
    if (result == '后退'):
      back()
      time.sleep(0.2)
      stop()
    if (result == '向左'):
      left()
      time.sleep(0.2)
      stop()
    if (result == '向右'):
      right()
      time.sleep(0.2)
      stop()
    if (result == '打开'):
      led_r.value(1)
    if (result == '关闭'):
      led_r.value(0)
    if (result == '退出'):
      break
fm.unregister(board_info.PIN12)
fm.unregister(board_info.PIN2)
fm.unregister(board_info.PIN3)
fm.unregister(board_info.PIN4)
fm.unregister(board_info.PIN5)
fm.unregister(board_info.PIN8)
fm.unregister(board_info.PIN9)
fm.unregister(board_info.PIN10)
fm.unregister(board_info.PIN11)

测试
https://www.bilibili.com/video/BV1eq4y137gz?share_source=copy_web
演示
https://www.bilibili.com/video/BV1zQ4y1m7pw?share_source=copy_web



页: [1]
查看完整版本: Maixduino麦克纳姆轮小车