我正在使用Arduino
ServoTimer2库(因为我需要在同一个草图中使用
VirtualWire库.我不知道这个问题是否也存在于常规
Servo库中,但我认为值得一提.
每当我调用servo.attach(引脚)时,伺服从其当前位置移动到其范围的正中间(1500微秒).这是一个问题,因为我的Arduino需要定期安装和拆卸伺服系统并重新安装到中心,这违背了我的项目目的.
我读到在伺服.attach(引脚)之前立即调用servo.write(xxxx)会导致它默认为xxxx而不是1500,但这对我来说似乎不起作用(我甚至不理解如果能工作如果引脚未连接……).有任何想法吗?
*
编辑:
我的解决方案
感谢djUniversal的回答,我让它工作,以便伺服在连接时不会移动.如果其他人想要做同样的事情,我所做的只是进入ServoTimer2.cpp并注释掉initISR方法的前3行.它现在看起来像这样:
static void initISR()
{
// for(uint8_t i=1; i <= NBR_CHANNELS; i++) { // channels start from 1
// writeChan(i, DEFAULT_PULSE_WIDTH); // store default values
// }
servos[FRAME_SYNC_INDEX].counter = FRAME_SYNC_DELAY; // store the frame sync period
Channel = 0; // clear the channel index
ISRCount = 0; // clear the value of the ISR counter;
/* setup for timer 2 */
TIMSK2 = 0; // disable interrupts
TCCR2A = 0; // normal counting mode
TCCR2B = _BV(CS21); // set prescaler of 8
TCNT2 = 0; // clear the timer2 count
TIFR2 = _BV(TOV2); // clear pending interrupts;
TIMSK2 = _BV(TOIE2) ; // enable the overflow interrupt
isStarted = true; // flag to indicate this initialisation code has been executed
}
一旦我这样做并重新编译,伺服器在连接时立即停止移动,现在只有在写入时才会移动.
最佳答案 ServoTimer2定义了一个起始脉冲:
#define DEFAULT_PULSE_WIDTH 1500
在.h文件中.每次伺服对象初始化时,它都被用作通道的起始点.常规的伺服库具有相同的机制.
如果您想要执行您要执行的操作,则需要修改库文件以满足您的需要,否则这是不可能的.
为什么需要保持拆卸伺服系统?你有什么可以改变的吗?