by sprince09 » Mon Jul 27, 2009 7:01 pm
by sprince09
Mon Jul 27, 2009 7:01 pm
Hey all,
I'm working on creating a motion sensor for the Robotis AX/RX servos for one of my projects, and I've run up against a wall with how to set up my communications. I would like to emulate the communication protocol that the AX/RX servos use so that I can run the sensor from the same communication bus as the rest of the servo network, but I can't seem to get my AVR (atmega 168 @ 20.0 MHz) to behave properly. For testing purposes, I currently have the AVR hooked up to my computer over a serial port, and I've been dancing around with it trying to send and receive packets of data. I'm using an interrupt based receive function on the AVR, and right now I'm just trying to have it echo back data packets to me as I send them. The code I have seems to work properly for single characters, but fails with multiple characters. Here's the relevant code:
- Code: Select all
volatile unsigned char packet[255] = {0};
volatile unsigned char p_index = 0;
int main(void){
initialize();
//main loop
//do nothing, just wait for incoming packets to be handled by interrupt vector
for(;;);
}
ISR(USART_RX_vect)
{
//interrupt based UART reciever
//store recieved value in packet[] and increment packet index
cli();
TCNT2 = 0;
packet[p_index++] = UDR0;
sei();
}
ISR(TIMER2_COMPA_vect){
//called when TNCT2 > timeout, indicating that a packet has finished
//being recieved, and now needs to be parsed
cli();
if(p_index > 0) read_packet();
sei();
}
void read_packet(void){
//echos back a data packet received on UART0
int i;
for(i = 0; i < p_index; i++) uart_tx(packet[i]);
//clear old packet and reset index
p_index = 0;
for(i = 0; i < 255; i++) packet[i] = 0;
}
To summarize, what I've tried to do with this code is:
- Wait for a character to arrive in the RX buffer of the AVR
- Store the received character in global array and reset a timer
- If the timer count exceeds a timeout value (set by me), the packet[] array
gets echoed back to the computer from the AVR (this only works for 1
character at a time)
- Reset packet[] array and p_index after an echo has been sent
I guess what I'm really asking is if anyone has tried to implement their own sensors or anything that are compatible with the AX/RX series of servos, and if they have, could they explain how they managed their communications?
*EDIT*
I get the proper number of characters echoed back to me from the AVR, but their values are incorrect. That's my main problem.
Hey all,
I'm working on creating a motion sensor for the Robotis AX/RX servos for one of my projects, and I've run up against a wall with how to set up my communications. I would like to emulate the communication protocol that the AX/RX servos use so that I can run the sensor from the same communication bus as the rest of the servo network, but I can't seem to get my AVR (atmega 168 @ 20.0 MHz) to behave properly. For testing purposes, I currently have the AVR hooked up to my computer over a serial port, and I've been dancing around with it trying to send and receive packets of data. I'm using an interrupt based receive function on the AVR, and right now I'm just trying to have it echo back data packets to me as I send them. The code I have seems to work properly for single characters, but fails with multiple characters. Here's the relevant code:
- Code: Select all
volatile unsigned char packet[255] = {0};
volatile unsigned char p_index = 0;
int main(void){
initialize();
//main loop
//do nothing, just wait for incoming packets to be handled by interrupt vector
for(;;);
}
ISR(USART_RX_vect)
{
//interrupt based UART reciever
//store recieved value in packet[] and increment packet index
cli();
TCNT2 = 0;
packet[p_index++] = UDR0;
sei();
}
ISR(TIMER2_COMPA_vect){
//called when TNCT2 > timeout, indicating that a packet has finished
//being recieved, and now needs to be parsed
cli();
if(p_index > 0) read_packet();
sei();
}
void read_packet(void){
//echos back a data packet received on UART0
int i;
for(i = 0; i < p_index; i++) uart_tx(packet[i]);
//clear old packet and reset index
p_index = 0;
for(i = 0; i < 255; i++) packet[i] = 0;
}
To summarize, what I've tried to do with this code is:
- Wait for a character to arrive in the RX buffer of the AVR
- Store the received character in global array and reset a timer
- If the timer count exceeds a timeout value (set by me), the packet[] array
gets echoed back to the computer from the AVR (this only works for 1
character at a time)
- Reset packet[] array and p_index after an echo has been sent
I guess what I'm really asking is if anyone has tried to implement their own sensors or anything that are compatible with the AX/RX series of servos, and if they have, could they explain how they managed their communications?
*EDIT*
I get the proper number of characters echoed back to me from the AVR, but their values are incorrect. That's my main problem.