void dxl_tx_packet() { unsigned char i; unsigned char TxNumByte, RealTxNumByte; unsigned char checksum = 0; if( giBusUsing == 1 ) return; giBusUsing = 1; if( gbInstructionPacket[LENGTH] > (MAXNUM_TXPARAM+2) ) { gbCommStatus = COMM_TXERROR; giBusUsing = 0; return; } if( gbInstructionPacket[INSTRUCTION] != INST_PING && gbInstructionPacket[INSTRUCTION] != INST_READ && gbInstructionPacket[INSTRUCTION] != INST_WRITE && gbInstructionPacket[INSTRUCTION] != INST_REG_WRITE && gbInstructionPacket[INSTRUCTION] != INST_ACTION && gbInstructionPacket[INSTRUCTION] != INST_RESET && gbInstructionPacket[INSTRUCTION] != INST_SYNC_WRITE && gbInstructionPacket[INSTRUCTION] != INST_SYNC_READ) { gbCommStatus = COMM_TXERROR; giBusUsing = 0; return; } gbInstructionPacket[0] = 0xff; gbInstructionPacket[1] = 0xff; for( i=0; i<(gbInstructionPacket[LENGTH]+1); i++ ) checksum += gbInstructionPacket[i+2]; gbInstructionPacket[gbInstructionPacket[LENGTH]+3] = ~checksum; if( gbCommStatus == COMM_RXTIMEOUT || gbCommStatus == COMM_RXCORRUPT ) dxl_hal_clear(); TxNumByte = gbInstructionPacket[LENGTH] + 4; RealTxNumByte = dxl_hal_tx( (unsigned char*)gbInstructionPacket, TxNumByte ); if( TxNumByte != RealTxNumByte ) { gbCommStatus = COMM_TXFAIL; giBusUsing = 0; return; } if( gbInstructionPacket[INSTRUCTION] == INST_READ ) dxl_hal_set_timeout( gbInstructionPacket[PARAMETER+1] + 6 ); else if ( gbInstructionPacket[INSTRUCTION] == INST_SYNC_READ ) dxl_hal_set_timeout( gbInstructionPacket[PARAMETER+1] + 6 ); else dxl_hal_set_timeout( 6 ); gbCommStatus = COMM_TXSUCCESS; }
// Sends the data over opened DXL serial line. // Returns false if not all the bytes were sent. bool TXPacket(uint8_t* data, int num_bytes) { assert(num_bytes < MAX_PACKET_BYTES); // PrintPacket(data, num_bytes); FinalPacketConsistencyCheck(data, num_bytes); int num_transmitted_bytes = dxl_hal_tx(data, num_bytes); if(num_bytes != num_transmitted_bytes) { return false; } else { return true; } }
void dxl_tx_packet() { unsigned char i; unsigned char TxNumByte, RealTxNumByte; unsigned char checksum = 0; if( giBusUsing == 1 ) return; giBusUsing = 1; if( gbInstructionPacket[LENGTH] > (MAXNUM_TXPARAM+2) ) { gbCommStatus = COMM_TXERROR; giBusUsing = 0; return; } if( gbInstructionPacket[INSTRUCTION] != INST_PING && gbInstructionPacket[INSTRUCTION] != INST_READ && gbInstructionPacket[INSTRUCTION] != INST_WRITE && gbInstructionPacket[INSTRUCTION] != INST_REG_WRITE && gbInstructionPacket[INSTRUCTION] != INST_ACTION && gbInstructionPacket[INSTRUCTION] != INST_RESET && gbInstructionPacket[INSTRUCTION] != INST_SYNC_WRITE && gbInstructionPacket[INSTRUCTION] != INST_SYNC_READ ) { gbCommStatus = COMM_TXERROR; giBusUsing = 0; return; } gbInstructionPacket[0] = 0xff; gbInstructionPacket[1] = 0xff; for( i=0; i<(gbInstructionPacket[LENGTH]+1); i++ ) checksum += gbInstructionPacket[i+2]; gbInstructionPacket[gbInstructionPacket[LENGTH]+3] = ~checksum; if( gbCommStatus == COMM_RXTIMEOUT || gbCommStatus == COMM_RXCORRUPT ) dxl_hal_clear(); TxNumByte = gbInstructionPacket[LENGTH] + 4; #ifdef DEBUG_PRINT printf("TX(%d):", TxNumByte); for (int i=0; i < TxNumByte; i++) { printf(" %2x", gbInstructionPacket[i]); if ((i & 0xff) == 0xff) printf("\n"); } #endif RealTxNumByte = dxl_hal_tx( (unsigned char*)gbInstructionPacket, TxNumByte ); #ifdef DEBUG_PRINT printf(" - %d\n", RealTxNumByte); #endif if( TxNumByte != RealTxNumByte ) { gbCommStatus = COMM_TXFAIL; giBusUsing = 0; return; } if( gbInstructionPacket[INSTRUCTION] == INST_READ ) dxl_hal_set_timeout( gbInstructionPacket[PARAMETER+1] + 6 ); else if( gbInstructionPacket[INSTRUCTION] == INST_SYNC_READ ) // BUGBUG:: probably need to expand more here dxl_hal_set_timeout((gbInstructionPacket[LENGTH] - 4) * gbInstructionPacket[PARAMETER+1] + 6 ); else dxl_hal_set_timeout( 6 ); gbCommStatus = COMM_TXSUCCESS; }
// Send an instruction packet void dxl_tx_packet() { unsigned char i; unsigned char TxNumByte, RealTxNumByte; unsigned char checksum = 0; // do nothing if bus is busy if( giBusUsing == 1 ) return; // set bus as busy giBusUsing = 1; // check packet does not exceed maximum number of parameters if( gbInstructionPacket[LENGTH] > (MAXNUM_TXPARAM+2) ) { gbCommStatus = COMM_TXERROR; giBusUsing = 0; return; } // check instruction is valid if( gbInstructionPacket[INSTRUCTION] != INST_PING && gbInstructionPacket[INSTRUCTION] != INST_READ && gbInstructionPacket[INSTRUCTION] != INST_WRITE && gbInstructionPacket[INSTRUCTION] != INST_REG_WRITE && gbInstructionPacket[INSTRUCTION] != INST_ACTION && gbInstructionPacket[INSTRUCTION] != INST_RESET && gbInstructionPacket[INSTRUCTION] != INST_SYNC_WRITE ) { gbCommStatus = COMM_TXERROR; giBusUsing = 0; return; } // create the packet header (2x 0xFF) gbInstructionPacket[0] = 0xff; gbInstructionPacket[1] = 0xff; // calculate the checksum for( i=0; i<(gbInstructionPacket[LENGTH]+1); i++ ) checksum += gbInstructionPacket[i+2]; gbInstructionPacket[gbInstructionPacket[LENGTH]+3] = ~checksum; // if timeout or corrupt clear the buffer if( gbCommStatus == COMM_RXTIMEOUT || gbCommStatus == COMM_RXCORRUPT ) dxl_hal_clear(); // transfer the packet TxNumByte = gbInstructionPacket[LENGTH] + 4; RealTxNumByte = dxl_hal_tx( (unsigned char*)gbInstructionPacket, TxNumByte ); // check that all bytes were sent if( TxNumByte != RealTxNumByte ) { // communication failure - not all bytes sent gbCommStatus = COMM_TXFAIL; giBusUsing = 0; return; } // for read instructions we expect a reply within the timeout period if( gbInstructionPacket[INSTRUCTION] == INST_READ ) dxl_hal_set_timeout( gbInstructionPacket[PARAMETER+1] + 6 ); else dxl_hal_set_timeout( 6 ); gbCommStatus = COMM_TXSUCCESS; }