mbed to Dynamixel servo communication.
Dependents: dynamixel_wheel_test
Dynamixel.cpp
- Committer:
- dmgongora
- Date:
- 2015-08-19
- Revision:
- 2:32377cbec534
- Parent:
- 1:ce081666d225
- Child:
- 3:61785d105315
File content as of revision 2:32377cbec534:
#include "Dynamixel.h" #include "mbed.h" Dynamixel::Dynamixel(PinName tx, PinName rx, PinName txEnable, uint8_t motorID, int baudrate) : m_link(tx, rx), m_txEnable(txEnable), m_motorID(motorID), m_baudrate(baudrate) { m_link.baud(m_baudrate); } uint8_t Dynamixel::ping() { uint8_t elements = 6; uint8_t instructionBuffer[elements]; uint8_t statusBuffer[STATUS_PACKET_LENGTH]; if (m_link.writeable() ) { instructionBuffer[0] = 0xff; instructionBuffer[1] = 0xff; instructionBuffer[2] = m_motorID; // ID instructionBuffer[3] = 0x02; // Length instructionBuffer[4] = PING; // Instruction instructionBuffer[5] = ~(instructionBuffer[2] + instructionBuffer[3] + instructionBuffer[4]) & 0xFF; // Check sum m_txEnable = 1; // Enable Tx / Disable Rx for (int i = 0; i<= elements; i++) { m_link.putc(instructionBuffer[i]); } wait_ms(1); // fix this!!! m_txEnable = 0; // Disable Tx / Enable Rx } else { //pc.printf("Dynamixel not writeable\n"); } wait_ms(10); // fix this!!! if ( m_link.readable() && m_motorID != BROADCAST_ID ) { for (int i = 0; i < STATUS_PACKET_LENGTH; i++) { statusBuffer[i] = m_link.getc(); // Read status packet } } return statusBuffer[4]; // Return error } uint8_t Dynamixel::toggleLED(uint8_t ledState) { uint8_t elements = 8; uint8_t instructionBuffer[elements]; uint8_t statusBuffer[STATUS_PACKET_LENGTH]; if (m_link.writeable() ) { instructionBuffer[0] = 0xff; instructionBuffer[1] = 0xff; instructionBuffer[2] = m_motorID; // ID instructionBuffer[3] = 0x04; // Length instructionBuffer[4] = WRITE_DATA; // Instruction instructionBuffer[5] = ADDRESS_LED; // Parameter 1: Starting address instructionBuffer[6] = ledState; // Parameter 2: First value to be writen instructionBuffer[7] = ~(instructionBuffer[2] + instructionBuffer[3] + instructionBuffer[4] + instructionBuffer[5] + instructionBuffer[6]) & 0xFF; // Check sum m_txEnable = 1; // Enable Tx / Disable Rx for (int i = 0; i<= elements; i++) { m_link.putc(instructionBuffer[i]); } wait_ms(2); // fix this!!! m_txEnable = 0; // Disable Tx / Enable Rx } else { //pc.printf("Dynamixel not writeable\n"); } wait_ms(10); if ( m_link.readable() && m_motorID != BROADCAST_ID ) { for (int i = 0; i < STATUS_PACKET_LENGTH; i++) { statusBuffer[i] = m_link.getc(); // Read status packet } } return statusBuffer[4]; // Return error } uint8_t Dynamixel::move(uint16_t position) { // 0 to 1023 (0x3FF) uint8_t elements = 9; uint8_t instructionBuffer[elements]; uint8_t statusBuffer[STATUS_PACKET_LENGTH]; uint16_t checkSum = 0; if (m_link.writeable() ) { instructionBuffer[0] = 0xff; instructionBuffer[1] = 0xff; instructionBuffer[2] = m_motorID; // ID instructionBuffer[3] = 0x05; // Length instructionBuffer[4] = WRITE_DATA; // Instruction instructionBuffer[5] = ADDRESS_GOAL_POSITION; // Parameter 1: Starting address instructionBuffer[6] = (uint8_t) position & 0xff; // Parameter 2: First value to be writen instructionBuffer[7] = (uint8_t) (position >> 8); // Parameter 3: Second value to be writen checkSum = instructionBuffer[2] + instructionBuffer[3] + instructionBuffer[4] + instructionBuffer[5] + instructionBuffer[6] + instructionBuffer[7]; instructionBuffer[8] = (uint8_t) (~checkSum & 0xff); // Check sum /* for (int i = 0; i<= elements; i++) { pc.printf("%c ", instructionBuffer[i]); } pc.printf("\n"); */ m_txEnable = 1; // Enable Tx / Disable Rx for (int i = 0; i<= elements; i++) { m_link.putc(instructionBuffer[i]); } wait_ms(2); // fix this!!! m_txEnable = 0; // Disable Tx / Enable Rx } else { //pc.printf("Dynamixel not writeable\n"); } wait_ms(10); if ( m_link.readable() && m_motorID != BROADCAST_ID ) { for (int i = 0; i < STATUS_PACKET_LENGTH; i++) { statusBuffer[i] = m_link.getc(); // Read status packet } } return statusBuffer[4]; // Return error } uint8_t Dynamixel::setSpeed(uint16_t speed) { // 0 to 1023 (0x3FF) uint8_t elements = 9; uint8_t instructionBuffer[elements]; uint16_t checkSum = 0; uint8_t statusBuffer[STATUS_PACKET_LENGTH]; if (m_link.writeable() ) { instructionBuffer[0] = 0xff; instructionBuffer[1] = 0xff; instructionBuffer[2] = m_motorID; // ID instructionBuffer[3] = 0x05; // Length instructionBuffer[4] = WRITE_DATA; // Instruction instructionBuffer[5] = ADDRESS_MOVING_SPEED; // Parameter 1: Starting address instructionBuffer[6] = (uint8_t) speed & 0xff; // Parameter 2: First value to be writen instructionBuffer[7] = (uint8_t) (speed >> 8); // Parameter 3: Second value to be writen checkSum = instructionBuffer[2] + instructionBuffer[3] + instructionBuffer[4] + instructionBuffer[5] + instructionBuffer[6] + instructionBuffer[7]; instructionBuffer[8] = (uint8_t) (~checkSum & 0xff); // Check sum /* for (int i = 0; i<= elements; i++) { pc.printf("%c ", instructionBuffer[i]); } pc.printf("\n"); */ m_txEnable = 1; // Enable Tx / Disable Rx for (int i = 0; i<= elements; i++) { m_link.putc(instructionBuffer[i]); } wait_ms(2); // fix this!!! m_txEnable = 0; // Disable Tx / Enable Rx } else { //pc.printf("Dynamixel not writeable\n"); } wait_ms(10); if ( m_link.readable() && m_motorID != BROADCAST_ID ) { for (int i = 0; i < STATUS_PACKET_LENGTH; i++) { statusBuffer[i] = m_link.getc(); // Read status packet } } return statusBuffer[4]; // Return error }