|
好久沒(méi)更新技術(shù)類日志了,今天更新下,這是一個(gè)韓國(guó)robotics機(jī)器人Dynamixel數(shù)字舵機(jī)的驅(qū)動(dòng),采用C語(yǔ)言編寫(xiě),支持Dynamixel 通信協(xié)議 ,案例是AVR單片。實(shí)際上可以采用FPGA,例如SOPC系統(tǒng)上,可控制小型機(jī)器人。



在這也拆解下CM-5的控制器,是一片atmega128A 哦!哈哈。這PCB做工一般,感覺(jué)韓國(guó)人是科技領(lǐng)域爆發(fā)戶,但我們有什么資格嘲笑棒子呢,至少人家都能造CPU了,我們呢?



#include <avr/io.h>
#include <util/delay.h>
void InitUart0(void)
{
UCSR0A = 0x02; // 設(shè)置為倍速模式
UBRR0H = 0;
UBRR0L = 1;
UCSR0B = (1<<RXEN)|(1<<TXEN);// 接收器與發(fā)送器使能
UCSR0C = (3<<UCSZ0);
DDRE &= ~_BV(PE0); // 初始化RX 端口默認(rèn)方向?yàn)檩斎?/font>
PORTE &= ~_BV(PE0); // 初始化RX 端口默認(rèn)狀態(tài)為高阻
DDRE |= _BV(PE1); // 初始化TX 端口默認(rèn)方向?yàn)檩敵?/font>
PORTE |= _BV(PE1); // 初始化TX 端口默認(rèn)狀態(tài)為高電平
DDRA |= _BV(PA0); // 初始化使能端口狀態(tài)方向?yàn)檩敵?/font>
PORTA &= ~_BV(PA0); // 初始化使能端口狀態(tài)為RX 狀態(tài)
DDRA |= _BV(PA1); // 初始化使能端口狀態(tài)方向?yàn)檩敵?/font>
PORTA |= _BV(PA1); // 初始化使能端口狀態(tài)方為T(mén)X 狀態(tài)
}
void SendUart0Byte(unsigned char data)
{
while ( !( UCSR0A & (1<<UDRE)) );// 等待發(fā)送緩沖器為空
UDR0 = data;/* 將數(shù)據(jù)放入緩沖器,發(fā)送數(shù)據(jù)*/
}
void SetServoLimit(unsigned char id, unsigned short int cw_limit, unsigned short int ccw_limit)
{
unsigned short int temp_ccw = 0; // 臨時(shí)速度,用于進(jìn)行方向判別
unsigned short int temp_cw = 0;
unsigned char temp_ccw_h = 0; // 待發(fā)送數(shù)據(jù)h 位
unsigned char temp_ccw_l = 0; // 待發(fā)送數(shù)據(jù)l 位
unsigned char temp_cw_h = 0;
unsigned char temp_cw_l = 0;
unsigned char temp_sum = 0; // 校驗(yàn)和寄存變量
if (ccw_limit > 1023)
{
temp_ccw = 1023; // 限制速度值在可用范圍內(nèi)
}
else
{
temp_ccw = ccw_limit;
}
if (cw_limit > 1023)
{
temp_cw = 1023;
}
else
{
temp_cw = cw_limit;
}
temp_ccw_h = (unsigned char)(temp_ccw >> 8);
temp_ccw_l = (unsigned char)temp_ccw; // 將16bit 數(shù)據(jù)拆為2個(gè)8bit 數(shù)據(jù)
temp_cw_h = (unsigned char)(temp_cw >> 8);
temp_cw_l = (unsigned char)temp_cw; // 將16bit 數(shù)據(jù)拆為2個(gè)8bit 數(shù)據(jù)
PORTA &= ~_BV(PA1);
PORTA |= _BV(PA0); // 使總線處于主機(jī)發(fā)送狀態(tài)
UCSR0A |= (1<<TXC0); // 清除UART0寫(xiě)完成標(biāo)志
SendUart0Byte(0xFF); // 發(fā)送啟動(dòng)符號(hào)0xFF
SendUart0Byte(0xFF); // 發(fā)送啟動(dòng)符號(hào)0xFF
SendUart0Byte(id); // 發(fā)送id
SendUart0Byte(7); // 發(fā)送數(shù)據(jù)長(zhǎng)度為參數(shù)長(zhǎng)度+2,參數(shù)長(zhǎng)度為3
SendUart0Byte(0x03); // 命令數(shù)據(jù)為“WRITE DATA”
SendUart0Byte(0x06); // 舵機(jī)控制寄存器首地址
SendUart0Byte(temp_cw_l); // 發(fā)送順時(shí)針位置限制低位
SendUart0Byte(temp_cw_h); // 發(fā)送順時(shí)針位置限制高位
SendUart0Byte(temp_ccw_l); // 發(fā)送逆時(shí)針位置限制低位
SendUart0Byte(temp_ccw_h); // 發(fā)送逆時(shí)針位置限制高位
temp_sum = id + 7 + 0x03 + 0x06 + temp_cw_l + temp_cw_h + temp_ccw_l + temp_ccw_h;
temp_sum = ~temp_sum; // 計(jì)算校驗(yàn)和
SendUart0Byte(temp_sum); // 發(fā)送校驗(yàn)和
while ( !( UCSR0A & (1<<TXC0)) ) // 等待發(fā)送完成
{ // (Waiting for finishing sending)
;
}
PORTA |= _BV(PA1);
PORTA &= ~_BV(PA0); // 使總線處于主機(jī)接收狀態(tài)
_delay_ms(2); //送完成后,總線會(huì)被從機(jī)占用,反饋應(yīng)答數(shù)據(jù),所以進(jìn)行延時(shí)
}
void SetServoPosition(unsigned char id, unsigned short int position, unsigned short int
velocity)
{
unsigned short int temp_velocity = 0; // 臨時(shí)速度,用于進(jìn)行方向判別
unsigned short int temp_position = 0;
unsigned char temp_velocity_h = 0; // 待發(fā)送數(shù)據(jù)h 位
unsigned char temp_velocity_l = 0; // 待發(fā)送數(shù)據(jù)l 位
unsigned char temp_position_h = 0;
unsigned char temp_position_l = 0;
unsigned char temp_sum = 0; // 校驗(yàn)和寄存變量
if (velocity > 1023)
{
temp_velocity = 1023; // 限制速度值在可用范圍內(nèi)
}
else
{
temp_velocity = velocity;
}
if (position > 1023)
{
temp_position = 1023;
}
else
{
temp_position = position;
}
temp_velocity_h = (unsigned char)(temp_velocity >> 8);
// 將16bit 數(shù)據(jù)拆為2個(gè)8bit 數(shù)據(jù)
temp_velocity_l = (unsigned char)temp_velocity;
temp_position_h = (unsigned char)(temp_position >> 8);
// 將16bit 數(shù)據(jù)拆為2個(gè)8bit 數(shù)據(jù)
temp_position_l = (unsigned char)temp_position;
PORTA &= ~_BV(PA1);
PORTA |= _BV(PA0); // 使總線處于主機(jī)發(fā)送狀態(tài)
UCSR0A |= (1<<TXC0); // 清除UART0寫(xiě)完成標(biāo)志
SendUart0Byte(0xFF); // 發(fā)送啟動(dòng)符號(hào)0xFF
SendUart0Byte(0xFF);
SendUart0Byte(id); // 發(fā)送id
SendUart0Byte(7); // 發(fā)送數(shù)據(jù)長(zhǎng)度為參數(shù)長(zhǎng)度+2,參數(shù)長(zhǎng)度為3
SendUart0Byte(0x03); // 命令數(shù)據(jù)為“WRITE DATA”
SendUart0Byte(0x1E); // 舵機(jī)控制寄存器首地址
SendUart0Byte(temp_position_l); // 發(fā)送速度數(shù)據(jù)低位
SendUart0Byte(temp_position_h); // 發(fā)送速度數(shù)據(jù)高位
SendUart0Byte(temp_velocity_l); //發(fā)送位置低字節(jié)
SendUart0Byte(temp_velocity_h); // 發(fā)送位置高字節(jié)
temp_sum = id + 7 + 0x03 + 0x1E + temp_position_l + temp_position_h + temp_velocity_l +
temp_velocity_h;
temp_sum = ~temp_sum; // 計(jì)算校驗(yàn)和
SendUart0Byte(temp_sum); // 發(fā)送校驗(yàn)和 (Send the checksum)
while ( !( UCSR0A & (1<<TXC0)) ) // 等待發(fā)送完成
{ // (Waiting for finishing sending)
;
}
PORTA |= _BV(PA1);
PORTA &= ~_BV(PA0); // 使總線處于主機(jī)接收狀態(tài)
_delay_ms(2); // 發(fā)送完成后,總線會(huì)被從機(jī)占用,反饋應(yīng)答數(shù)據(jù),所以進(jìn)行延時(shí)
}
void SetServoVelocity(unsigned char id, signed short int velocity)
{
unsigned char temp_sign = 0; // 臨時(shí)符號(hào),用于進(jìn)行方向判別
unsigned short int temp_velocity = 0; // 臨時(shí)速度,用于進(jìn)行方向判別
unsigned char temp_value_h = 0; // 待發(fā)送數(shù)據(jù)h 位
unsigned char temp_value_l = 0; // 待發(fā)送數(shù)據(jù)l 位
unsigned char temp_sum = 0; // 校驗(yàn)和寄存變量
if (velocity < 0)
{
temp_velocity = -velocity; // 如果為負(fù)數(shù),則取絕對(duì)值
temp_sign = 1; // 設(shè)置負(fù)數(shù)符號(hào)標(biāo)志
}
else
{
temp_velocity = velocity;
temp_sign = 0; // 設(shè)置正數(shù)符號(hào)標(biāo)志
}
if (temp_velocity > 1023)
{
temp_velocity = 1023; // 限制速度值在可用范圍內(nèi)
}
temp_velocity |= (temp_sign << 10);
temp_value_h = (unsigned char)(temp_velocity >> 8);
// 將16bit 數(shù)據(jù)拆為2個(gè)8bit 數(shù)據(jù)
temp_value_l = (unsigned char)temp_velocity;
PORTA &= ~_BV(PA1);
PORTA |= _BV(PA0); // 使總線處于主機(jī)發(fā)送狀態(tài)
UCSR0A |= (1<<TXC0); // 清除UART0寫(xiě)完成標(biāo)志
SendUart0Byte(0xFF); // 發(fā)送啟動(dòng)符號(hào)0xFF
SendUart0Byte(0xFF); // 發(fā)送啟動(dòng)符號(hào)0xFF
SendUart0Byte(id); // 發(fā)送id
SendUart0Byte(5); // 發(fā)送數(shù)據(jù)長(zhǎng)度為參數(shù)長(zhǎng)度+2,參數(shù)長(zhǎng)度為3
SendUart0Byte(0x03); // 命令數(shù)據(jù)為“WRITE DATA”
SendUart0Byte(0x20); // 舵機(jī)控制寄存器首地址
SendUart0Byte(temp_value_l); // 發(fā)送速度數(shù)據(jù)低位
SendUart0Byte(temp_value_h); // 發(fā)送速度數(shù)據(jù)高位
temp_sum = id + 5 + 0x03 + 0x20 + temp_value_l + temp_value_h;
temp_sum = ~temp_sum; // 計(jì)算校驗(yàn)和
SendUart0Byte(temp_sum); // 發(fā)送校驗(yàn)和
while ( !( UCSR0A & (1<<TXC0)) ) // 等待發(fā)送完成
{
;
}
PORTA |= _BV(PA1);
PORTA &= ~_BV(PA0); // 使總線處于主機(jī)接收狀態(tài)
_delay_ms(2); // 發(fā)送完成后,總線會(huì)被從機(jī)占用,反饋應(yīng)答數(shù)據(jù),所以進(jìn)行延時(shí)
}
int main(void)
{
InitUart0();
SetServoLimit(2,0,1023);
while(1)
{
_delay_ms(1000); //延時(shí)1s
SetServoPosition(2, 1000, 500); //控制舵機(jī)以500的速度運(yùn)動(dòng)到1000的位置
_delay_ms(1000); //延時(shí)1s
SetServoPosition(2, 200, 100); //控制舵機(jī)以100的速度運(yùn)動(dòng)到200的位置
}
}
|
|