在向前移动然后反向时从BBC MicroBit机器人获得意外输出

问题描述 投票:0回答:1

我正在使用Micro:Bit和Bit:Bot来做一些简单的事情,但是从比特:Bot电机得到了意想不到的结果。

简单地说,我想:

  1. 移动位:Bot向前1秒(开启几个绿色Neopixels)
  2. 停止电机(清除所有neopixels)
  3. 反向(打开一些Red Neopixels)

这是我的程序,用MicroPython编写:

from microbit import *
import neopixel

# pin13 gives access to the robot's neopixels.
myLightShow = neopixel.NeoPixel(pin13,12)
myLightShow[3]= (0,255,0)
myLightShow[4]= (0,225,0)
myLightShow[5]= (0,255,0)
myLightShow[9]= (0,255,0)
myLightShow[10]= (0,255,0)        
myLightShow[11]= (0,255,0)
myLightShow.show()

#for driving the motors the following pins are used:
#pin8 (left wheel) and pin12 (right wheel) sets the direction. 
#set pin to 0 for forward, set pin to 1 for reverse

# pin0 (left wheel) and pin1 (right wheel) sets speed. 0 - 1023 range
# both, therefore, are write_analog statements.

#Below, the 5 statements tell motors to go forward, at speed 300 for 1 sec
pin8.write_digital(0)
pin12.write_digital(0)
pin0.write_analog(300)
pin1.write_analog(300)
sleep(1000)

#Stop motors and clear neopixels (i.e. off)
pin0.write_analog(0)
pin1.write_analog(0)
pin8.write_digital(0)
pin12.write_digital(0)
myLightShow.clear()

# reverse at speed 350
pin8.write_digital(1)
pin12.write_digital(1)
pin0.write_analog(350)
pin1.write_analog(350)

# turn on selected neopixels and show.
myLightShow[0]= (255,0,0)
myLightShow[1]= (255,0,0)
myLightShow[2]= (255,0,0)
myLightShow[6]= (255,0,0)
myLightShow[7]= (255,0,0)
myLightShow[8]= (255,0,0)
myLightShow.show()   

当我在我的位上运行程序:bot时,它按预期向前移动1秒,然后停止(如预期的那样),然后继续向前移动!

我已经解决了这个问题多年,不知道是什么问题。

有人可以帮忙吗?谢谢

robot micropython bbc-microbit
1个回答
0
投票

添加sleep(1000)命令似乎纠正了这个问题,现在位:bot按预期移动。

© www.soinside.com 2019 - 2024. All rights reserved.