|
楼主 |
发表于 2007-7-12 23:24:44
|
显示全部楼层
最初看见主坛上有帖子讨论舵机的,本来这个帖子想发在主坛的,但是觉得发在这里可以活跃一下机器人论坛。希望大家踊跃一些。帖子发了几天,看的人都不多,确实比较冷清。
下面是我用avrlib中的舵机驱动程序的demo代码,希望有前辈提供一些改进意见,可以让舵机转动更流畅。
占空比不稳定有没有可能是跟内部RC不准有关呢?
#include <avr/io.h> // include I/O definitions (port names, pin names, etc)
#include <avr/signal.h> // include "signal" names (interrupt names)
#include <avr/interrupt.h> // include interrupt support
#include "global.h" // include our global settings
#include "servo.h" // include RC servo driver library
void servoTest(void);
//----- Begin Code ------------------------------------------------------------
int main(void)
{
timerInit();
// begin servo test
servoTest();
return 0;
}
void servoTest(void)
{
u08 pos;
u08 channel;
// do some examples
// initialize RC servo system
servoInit();
// setup servo output channel-to-I/Opin mapping
// format is servoSetChannelIO( CHANNEL#, PORT, PIN );
servoSetChannelIO(0, _SFR_IO_ADDR(PORTD), PD0);
servoSetChannelIO(1, _SFR_IO_ADDR(PORTD), PD1);
servoSetChannelIO(2, _SFR_IO_ADDR(PORTD), PD2);
servoSetChannelIO(3, _SFR_IO_ADDR(PORTD), PD3);
// set port pins to output
outb(DDRD, 0x0F);
pos = 0;
#define SPEED_SERVO 1
// spin servos sequentially back and forth between their limits
while(1)
{
for(channel=0; channel<SERVO_NUM_CHANNELS; channel++)
{
for(pos=0; pos<100; pos++)
{
servoSetPosition(channel,pos);
timerPause(SPEED_SERVO);
}
}
for(channel=0; channel<SERVO_NUM_CHANNELS; channel++)
{
for(pos=100; pos>=1; pos--)
{
servoSetPosition(channel,pos);
timerPause(SPEED_SERVO);
}
}
}
} |
|