我正在为一个项目使用 MSP432,我想知道如何在增量编码器达到指定计数时生成中断。中断应阻止电机朝特定方向移动。
背景:我正在使用增量编码器来控制有刷电机。当有刷电机向左或向右移动时,它会机械连接到一个增量编码器,该编码器会计算脉冲数或“咔嗒声”。增量式编码器有效地控制了电机的运动极限。也就是说,如果编码器在正确的方向上读取到 20 个脉冲,则电机应该停止。我的项目有两种由 switch-case 语句控制的操作模式。第一种是常规模式,第二种是用户可以使用操纵杆控制电机的模式。无论程序处于哪种模式,电机都应在达到运动极限时停止。
伪代码:
Case: Routine Button Mode
{
// Motor executes right, left, right movement routine
digitalWrite(directionMotor,RIGHT); // telling motor what direction to go
analogWrite(pwm_motor2,60); // telling motor to activate at 60% PWM
if(encoder_count == Motion_Limit)
analogWrite(pwm_motor,0); // tell motor to stop
// change direction
digitalWrite(directionMotor,LEFT); // telling motor what direction to go
analogWrite(pwm_motor2,60); // telling motor to activate at 60% PWM
}
Case: Joystick_Control
{
while(analogRead<10) // Joystick is pushed to the left
{
digitalWrite(directionMotor,LEFT); // telling motor what direction to go
analogWrite(pwm_motor2,60); // telling motor to activate at 60% PWM
if(encoder_count == Motion_Limit)
analogWrite(pwm_motor,0); // tell motor to stop
}
while(analogRead>1000) // Joystick is pushed to the right
{
digitalWrite(directionMotor,RIGHT); // telling motor what direction to go
analogWrite(pwm_motor2,60); // telling motor to activate at 60% PWM
if(encoder_count == Motion_Limit)
analogWrite(pwm_motor,0); // tell motor to stop
}
} // end case statement
同样,无论程序处于何种运行模式,它都应该在达到计数时停止。即使已达到运动限制,程序仍应允许操纵杆控制驱动电机远离运动限制。也就是说,如果 count == 20 在右极限上,我仍然可以向左驱动电机。本质上,编码器应该在运行的所有时刻跟踪电机。
问题: 1. 如何在 MSP432 上声明中断? 2. 我可以使用增量编码器作为中断吗?我发现的大多数示例都使用输出高或低信号的按钮作为中断标志。我不确定我能否用编码器做同样的事情
最佳答案
您的代码看起来很像 Arduino 代码,要附加中断,您应该使用 Arduino attachInterrupt() 函数。如果您使用不同的高级支持 库那么它的文档应该包括一些中断示例。
关于你问题的实质。
您的增量编码器应该有两条线,一条表示向左运动,一根表示向右运动。
您需要按照这些行的指示向上和向下更改全局变量 encoder_count。这绝对应该使用每行的中断来完成。中断应按照编码器数据表中的规定在边沿转换时触发。 button edge和encoder edge上的触发没有区别(只是按钮比较乱,需要去抖动,找一个没有去抖动的例子)。
如果在增量计数时准确停止电机非常重要,您可以测试增量编码器增量/减量中断中的值,并在需要时禁用电机。
然而,将测试作为主循环的一部分进行可能就足够了。 (旁注:请记住,当操纵杆居中时,您需要进行操作。)
我还建议创建一个函数来控制电机。这抽象了实现细节,允许您的主循环专注于更高级别的功能。确保电机控制始终由一个函数完成还可以让您保证始终应用您的限制。
关于c - MSP432 中断与编码器计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53384417/