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

问题描述

我正在使用 Arduino ServoTimer2 库(因为我还需要使用 VirtualWire 库在同一个sketch中.不知道这个问题是否也存在于常规的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 方法的前 3 行.现在看起来像这样:

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 文件中.每次伺服对象初始化时,它都用作通道的起点.常规伺服库具有相同的机制.

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