本文介绍了无需移动即可连接Arduino伺服器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Arduino ServoTimer2 库(因为我还需要使用库.我不知道常规 Servo 库,但我认为值得一提.

I am using the Arduino ServoTimer2 library (because I need to also use the VirtualWire library in the same sketch. I don't know if this problem also exists in the regular Servo library, but I thought it was worth mentioning.

每当我调用servo.attach(pin)时,伺服器就会从其当前位置移动到其范围的确切中间位置(1500微秒).这是一个问题,因为我的Arduino需要定期安装和拆卸伺服器,然后重新设置到中心位置会破坏我的项目目标.

Any time I call servo.attach(pin), the servo moves from whatever its current position is to the exact middle of its range (1500 microseconds). This is a problem because my Arduino needs to attach and detach the servo regularly and resetting back to the center defeats the purpose of my project.

我读到在servo.attach(pin)之前立即调用servo.write(xxxx)会使它默认为xxxx而不是1500,但这似乎对我不起作用(我也不知道如果插针为零,那怎么办?没有连接...).有什么想法吗?

I read that calling servo.write(xxxx) immediately before servo.attach(pin) would cause it to default to xxxx rather than 1500, but that doesn't seem to work for me (nor do I even understand how that would work if the pin isn't attached...). Any ideas?

*

我的解决方案

由于djUniversal的回答,我使它能够正常工作,以便在连接伺服器时不会移动.万一其他人想要做同样的事情,我要做的只是进入ServoTimer2.cpp并注释掉initISR方法的前三行.现在看起来像这样:

Thanks to djUniversal's answer, I got it working so that the servo doesn't move when attached. In case anyone else is looking to do the same, all I did was go into ServoTimer2.cpp and comment out the first 3 lines of the initISR method. It now looks like this:

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
}

这样做并重新编译后,伺服器在连接时立即停止移动,现在仅在写入时才移动.

As soon as I did that and recompiled, the servo stopped moving immediately when it was attached and now moves only when it is written to.

推荐答案

ServoTimer2使用以下命令定义一个启动脉冲:

The ServoTimer2 defines a starting pulse with:

#define DEFAULT_PULSE_WIDTH  1500

.h文件中的

.每次伺服对象初始化时,都将其用作通道的起点.常规的Servo库具有相同的机制.

in the .h file. Each time the servo object initialises this is used as the starting point for the channel. The regular Servo library has the same mechanism.

如果您想做自己想做的事情,则需要修改库文件以适合您的需要,否则就无法做到.

If you wanted to do what you are trying to do you would need to modify the library files to suit your needs as this is not possible otherwise.

为什么需要保持拆卸伺服器的原因?有什么可以改变的吗?

Why is it that you need to keep detaching the servos? Is there something you can change there?

这篇关于无需移动即可连接Arduino伺服器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 13:23