当我尝试使用中断时,Raspberry Pi Pico 锁定
我正在尝试使用编码器来跟踪机器人上三个轮子的运动,但是一旦任何电机移动机器人“锁定”,它就会停止响应命令,停止打印到串行监视器,并且只是保持旋转它的轮子直到我把它关掉。我删除了除了跟踪一个编码器的代码之外的所有内容,并尝试用手转动轮子来解决问题,但它仍然锁定。更奇怪的是,现在它会开始旋转其中一个轮子,即使我已经删除了任何应该让它这样做的代码,即使是错误的。
由于我对 python 不熟悉,所以我使用 Arduino IDE 对 pico 进行编程,但我找不到任何使用 pico 中断的信息或故障排除提示(不假设您使用的是 micropython)。
这是我用来尝试查找问题的简化代码。它要做的就是跟踪编码器已经执行了多少步,并每秒将其打印到串行监视器。我尝试过移除串口并让它点亮 LED,但这并没有帮助。
int encA = 10;
int encB = 11;
int count = 0;
int timer = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(encA),readEncoder,RISING);
timer = millis();
}
void loop() {
// put your main code here, to run repeatedly:
if (timer - millis() > 5000) {
Serial.println(count);
timer = millis();
}
}
void readEncoder() {
int bVal = digitalRead(encB);
if (bVal == 0) {
count--;
}
else{
count++;
}
}
I'm trying to use encoders to track the movement of three wheels on a robot, but as soon as any of the motors move the robot "locks up", it stops responding to commands, stops printing to the serial monitor, and just keeps spinning its wheels until I turn it off. I cut out everything except just the code to track one encoder and tried turning the wheel by hand to sus out the problem, but it still locked up. And even more strangely, now it will start spinning one of the wheels even though I've removed any code that should have it do that, even by mistake.
I used the Arduino IDE to program the pico since I've got no familiarity with python, but I can't find any information or troubleshooting tips for using interrupts with the pico that don't assume you're using micropython.
Here's the simplified code I'm using to try to find the problem. All it's meant to do is keep track of how many steps the encoder has made and print that to the serial monitor every second. Ive tried removing the serial and having it light up LEDs instead but that didn't help.
int encA = 10;
int encB = 11;
int count = 0;
int timer = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(encA),readEncoder,RISING);
timer = millis();
}
void loop() {
// put your main code here, to run repeatedly:
if (timer - millis() > 5000) {
Serial.println(count);
timer = millis();
}
}
void readEncoder() {
int bVal = digitalRead(encB);
if (bVal == 0) {
count--;
}
else{
count++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pi Pico 的映射函数 digitalPinToInterrupt 是否有效?
您可以尝试只使用与 pi 对应的中断号吗?
https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__irq。 html
您的示例中编码器的引脚错误(也许您错误地复制和粘贴了)?
GitHub 上有类似的代码,您可以修改并尝试。
https://github.com/jumejume1/Arduino/blob/master/ ROTARY_ENCODER/ROTARY_ENCODER.ino
它可能会帮助您找到解决方案。
还,
https://www.arduino.cc/reference/en/libraries/rpi_pico_timerinterrupt/< /a>
Does the mapping function digitalPinToInterrupt for the Pi Pico work?
Can you try just using the interrupt number that corresponds to the pi?
https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__irq.html
You have the wrong pin to encoder in your example (maybe you incorrectly copy and pasted)?
There is similar code on GitHub that you could modify and try instead.
https://github.com/jumejume1/Arduino/blob/master/ROTARY_ENCODER/ROTARY_ENCODER.ino
It might help you find a solution.
Also,
https://www.arduino.cc/reference/en/libraries/rpi_pico_timerinterrupt/
中断号对应于引脚(除非您重新分配它或禁用它),因此对于引脚 11,代码可以是:
这有效:
请参阅: https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__irq.html 用于禁用、启用等。
The interrupt number corresponds to the pin (unless you have reassigned it or disabled it) so for pin 11 the code can be:
This works:
See: https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__irq.html for disable, enable etc.