a
Fork of AX12 by
Revision 4:40a609b85463, committed 2016-02-29
- Comitter:
- 452R142039
- Date:
- Mon Feb 29 08:47:07 2016 +0000
- Parent:
- 1:93ad80f5fde7
- Commit message:
- ?
Changed in this revision
diff -r 93ad80f5fde7 -r 40a609b85463 AX12.cpp --- a/AX12.cpp Wed Mar 30 16:08:02 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,470 +0,0 @@ -/* mbed AX-12+ Servo Library - * - * Copyright (c) 2010, cstyles (http://mbed.org) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "AX12.h" -#include "mbed.h" - -AX12::AX12(PinName tx, PinName rx, int ID) - : _ax12(tx,rx) { - - _ax12.baud(1000000); - _ID = ID; -} - -// Set the mode of the servo -// 0 = Positional (0-300 degrees) -// 1 = Rotational -1 to 1 speed -int AX12::SetMode(int mode) { - - if (mode == 1) { // set CR - SetCWLimit(0); - SetCCWLimit(0); - SetCRSpeed(0.0); - } else { - SetCWLimit(0); - SetCCWLimit(300); - SetCRSpeed(0.0); - } - return(0); -} - - -// if flag[0] is set, were blocking -// if flag[1] is set, we're registering -// they are mutually exclusive operations -int AX12::SetGoal(int degrees, int flags) { - - char reg_flag = 0; - char data[2]; - - // set the flag is only the register bit is set in the flag - if (flags == 0x2) { - reg_flag = 1; - } - - // 1023 / 300 * degrees - short goal = (1023 * degrees) / 300; - if (AX12_DEBUG) { - printf("SetGoal to 0x%x\n",goal); - } - - data[0] = goal & 0xff; // bottom 8 bits - data[1] = goal >> 8; // top 8 bits - - // write the packet, return the error code - int rVal = write(_ID, AX12_REG_GOAL_POSITION, 2, data, reg_flag); - - if (flags == 1) { - // block until it comes to a halt - while (isMoving()) {} - } - return(rVal); -} - - -// Set continuous rotation speed from -1 to 1 -int AX12::SetCRSpeed(float speed) { - - // bit 10 = direction, 0 = CCW, 1=CW - // bits 9-0 = Speed - char data[2]; - - int goal = (0x3ff * abs(speed)); - - // Set direction CW if we have a negative speed - if (speed < 0) { - goal |= (0x1 << 10); - } - - data[0] = goal & 0xff; // bottom 8 bits - data[1] = goal >> 8; // top 8 bits - - // write the packet, return the error code - int rVal = write(_ID, 0x20, 2, data); - - return(rVal); -} - - -int AX12::SetCWLimit (int degrees) { - - char data[2]; - - // 1023 / 300 * degrees - short limit = (1023 * degrees) / 300; - - if (AX12_DEBUG) { - printf("SetCWLimit to 0x%x\n",limit); - } - - data[0] = limit & 0xff; // bottom 8 bits - data[1] = limit >> 8; // top 8 bits - - // write the packet, return the error code - return (write(_ID, AX12_REG_CW_LIMIT, 2, data)); - -} - -int AX12::SetCCWLimit (int degrees) { - - char data[2]; - - // 1023 / 300 * degrees - short limit = (1023 * degrees) / 300; - - if (AX12_DEBUG) { - printf("SetCCWLimit to 0x%x\n",limit); - } - - data[0] = limit & 0xff; // bottom 8 bits - data[1] = limit >> 8; // top 8 bits - - // write the packet, return the error code - return (write(_ID, AX12_REG_CCW_LIMIT, 2, data)); -} - - -int AX12::SetID (int CurrentID, int NewID) { - - char data[1]; - data[0] = NewID; - if (AX12_DEBUG) { - printf("Setting ID from 0x%x to 0x%x\n",CurrentID,NewID); - } - return (write(CurrentID, AX12_REG_ID, 1, data)); - -} - - -// return 1 is the servo is still in flight -int AX12::isMoving(void) { - - char data[1]; - read(_ID,AX12_REG_MOVING,1,data); - return(data[0]); -} - - -void AX12::trigger(void) { - - char TxBuf[16]; - char sum = 0; - - if (AX12_TRIGGER_DEBUG) { - printf("\nTriggered\n"); - } - - // Build the TxPacket first in RAM, then we'll send in one go - if (AX12_TRIGGER_DEBUG) { - printf("\nTrigger Packet\n Header : 0xFF, 0xFF\n"); - } - - TxBuf[0] = 0xFF; - TxBuf[1] = 0xFF; - - // ID - Broadcast - TxBuf[2] = 0xFE; - sum += TxBuf[2]; - - if (AX12_TRIGGER_DEBUG) { - printf(" ID : %d\n",TxBuf[2]); - } - - // Length - TxBuf[3] = 0x02; - sum += TxBuf[3]; - if (AX12_TRIGGER_DEBUG) { - printf(" Length %d\n",TxBuf[3]); - } - - // Instruction - ACTION - TxBuf[4] = 0x04; - sum += TxBuf[4]; - if (AX12_TRIGGER_DEBUG) { - printf(" Instruction 0x%X\n",TxBuf[5]); - } - - // Checksum - TxBuf[5] = 0xFF - sum; - if (AX12_TRIGGER_DEBUG) { - printf(" Checksum 0x%X\n",TxBuf[5]); - } - - // Transmit the packet in one burst with no pausing - for (int i = 0; i < 6 ; i++) { - _ax12.putc(TxBuf[i]); - } - - // This is a broadcast packet, so there will be no reply - - return; -} - - -float AX12::GetPosition(void) { - - if (AX12_DEBUG) { - printf("\nGetPosition(%d)",_ID); - } - - char data[2]; - - int ErrorCode = read(_ID, AX12_REG_POSITION, 2, data); - short position = data[0] + (data[1] << 8); - float angle = (position * 300)/1024; - - return (angle); -} - - -float AX12::GetTemp (void) { - - if (AX12_DEBUG) { - printf("\nGetTemp(%d)",_ID); - } - char data[1]; - int ErrorCode = read(_ID, AX12_REG_TEMP, 1, data); - float temp = data[0]; - return(temp); -} - - -float AX12::GetVolts (void) { - if (AX12_DEBUG) { - printf("\nGetVolts(%d)",_ID); - } - char data[1]; - int ErrorCode = read(_ID, AX12_REG_VOLTS, 1, data); - float volts = data[0]/10.0; - return(volts); -} - - -int AX12::read(int ID, int start, int bytes, char* data) { - - char PacketLength = 0x4; - char TxBuf[16]; - char sum = 0; - char Status[16]; - - Status[4] = 0xFE; // return code - - if (AX12_READ_DEBUG) { - printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes); - } - - // Build the TxPacket first in RAM, then we'll send in one go - if (AX12_READ_DEBUG) { - printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); - } - - TxBuf[0] = 0xff; - TxBuf[1] = 0xff; - - // ID - TxBuf[2] = ID; - sum += TxBuf[2]; - if (AX12_READ_DEBUG) { - printf(" ID : %d\n",TxBuf[2]); - } - - // Packet Length - TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes) - sum += TxBuf[3]; // Accululate the packet sum - if (AX12_READ_DEBUG) { - printf(" Length : 0x%x\n",TxBuf[3]); - } - - // Instruction - Read - TxBuf[4] = 0x2; - sum += TxBuf[4]; - if (AX12_READ_DEBUG) { - printf(" Instruction : 0x%x\n",TxBuf[4]); - } - - // Start Address - TxBuf[5] = start; - sum += TxBuf[5]; - if (AX12_READ_DEBUG) { - printf(" Start Address : 0x%x\n",TxBuf[5]); - } - - // Bytes to read - TxBuf[6] = bytes; - sum += TxBuf[6]; - if (AX12_READ_DEBUG) { - printf(" No bytes : 0x%x\n",TxBuf[6]); - } - - // Checksum - TxBuf[7] = 0xFF - sum; - if (AX12_READ_DEBUG) { - printf(" Checksum : 0x%x\n",TxBuf[7]); - } - - // Transmit the packet in one burst with no pausing - for (int i = 0; i<8 ; i++) { - _ax12.putc(TxBuf[i]); - } - - // Wait for the bytes to be transmitted - wait (0.00002); - - // Skip if the read was to the broadcast address - if (_ID != 0xFE) { - - // Receive the Status packet 6+ number of bytes read - for (int i=0; i<(6+bytes) ; i++) { - Status[i] = _ax12.getc(); - } - - // Copy the data from Status into data for return - for (int i=0; i < Status[3]-2 ; i++) { - data[i] = Status[5+i]; - } - - if (AX12_READ_DEBUG) { - printf("\nStatus Packet\n"); - printf(" Header : 0x%x\n",Status[0]); - printf(" Header : 0x%x\n",Status[1]); - printf(" ID : 0x%x\n",Status[2]); - printf(" Length : 0x%x\n",Status[3]); - printf(" Error Code : 0x%x\n",Status[4]); - - for (int i=0; i < Status[3]-2 ; i++) { - printf(" Data : 0x%x\n",Status[5+i]); - } - - printf(" Checksum : 0x%x\n",Status[5+(Status[3]-2)]); - } - - } // if (ID!=0xFE) - - return(Status[4]); -} - - -int AX12:: write(int ID, int start, int bytes, char* data, int flag) { -// 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum - - char TxBuf[16]; - char sum = 0; - char Status[6]; - - if (AX12_WRITE_DEBUG) { - printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag); - } - - // Build the TxPacket first in RAM, then we'll send in one go - if (AX12_WRITE_DEBUG) { - printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); - } - - TxBuf[0] = 0xff; - TxBuf[1] = 0xff; - - // ID - TxBuf[2] = ID; - sum += TxBuf[2]; - - if (AX12_WRITE_DEBUG) { - printf(" ID : %d\n",TxBuf[2]); - } - - // packet Length - TxBuf[3] = 3+bytes; - sum += TxBuf[3]; - - if (AX12_WRITE_DEBUG) { - printf(" Length : %d\n",TxBuf[3]); - } - - // Instruction - if (flag == 1) { - TxBuf[4]=0x04; - sum += TxBuf[4]; - } else { - TxBuf[4]=0x03; - sum += TxBuf[4]; - } - - if (AX12_WRITE_DEBUG) { - printf(" Instruction : 0x%x\n",TxBuf[4]); - } - - // Start Address - TxBuf[5] = start; - sum += TxBuf[5]; - if (AX12_WRITE_DEBUG) { - printf(" Start : 0x%x\n",TxBuf[5]); - } - - // data - for (char i=0; i<bytes ; i++) { - TxBuf[6+i] = data[i]; - sum += TxBuf[6+i]; - if (AX12_WRITE_DEBUG) { - printf(" Data : 0x%x\n",TxBuf[6+i]); - } - } - - // checksum - TxBuf[6+bytes] = 0xFF - sum; - if (AX12_WRITE_DEBUG) { - printf(" Checksum : 0x%x\n",TxBuf[6+bytes]); - } - - // Transmit the packet in one burst with no pausing - for (int i = 0; i < (7 + bytes) ; i++) { - _ax12.putc(TxBuf[i]); - } - - // Wait for data to transmit - wait (0.00002); - - // make sure we have a valid return - Status[4]=0x00; - - // we'll only get a reply if it was not broadcast - if (_ID!=0xFE) { - - // response is always 6 bytes - // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum - for (int i=0; i < 6 ; i++) { - Status[i] = _ax12.getc(); - } - - // Build the TxPacket first in RAM, then we'll send in one go - if (AX12_WRITE_DEBUG) { - printf("\nStatus Packet\n Header : 0x%X, 0x%X\n",Status[0],Status[1]); - printf(" ID : %d\n",Status[2]); - printf(" Length : %d\n",Status[3]); - printf(" Error : 0x%x\n",Status[4]); - printf(" Checksum : 0x%x\n",Status[5]); - } - - - } - - return(Status[4]); // return error code -}
diff -r 93ad80f5fde7 -r 40a609b85463 AX12.h --- a/AX12.h Wed Mar 30 16:08:02 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,172 +0,0 @@ -/* mbed AX-12+ Servo Library - * - * Copyright (c) 2010, cstyles (http://mbed.org) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MBED_AX12_H -#define MBED_AX12_H - -#include "mbed.h" - -#define AX12_WRITE_DEBUG 0 -#define AX12_READ_DEBUG 0 -#define AX12_TRIGGER_DEBUG 0 -#define AX12_DEBUG 0 - -#define AX12_REG_ID 0x3 -#define AX12_REG_CW_LIMIT 0x06 -#define AX12_REG_CCW_LIMIT 0x08 -#define AX12_REG_GOAL_POSITION 0x1E -#define AX12_REG_MOVING_SPEED 0x20 -#define AX12_REG_VOLTS 0x2A -#define AX12_REG_TEMP 0x2B -#define AX12_REG_MOVING 0x2E -#define AX12_REG_POSITION 0x24 - -#define AX12_MODE_POSITION 0 -#define AX12_MODE_ROTATION 1 - -#define AX12_CW 1 -#define AX12_CCW 0 - -/** Servo control class, based on a PwmOut - * - * Example: - * @code - * #include "mbed.h" - * #include "AX12.h" - * - * int main() { - * - * AX12 myax12 (p9, p10, 1); - * - * while (1) { - * myax12.SetGoal(0); // go to 0 degrees - * wait (2.0); - * myax12.SetGoal(300); // go to 300 degrees - * wait (2.0); - * } - * } - * @endcode - */ -class AX12 { - -public: - - /** Create an AX12 servo object connected to the specified serial port, with the specified ID - * - * @param pin tx pin - * @param pin rx pin - * @param int ID, the Bus ID of the servo 1-255 - */ - AX12(PinName tx, PinName rx, int ID); - - /** Set the mode of the servo - * @param mode - * 0 = Positional, default - * 1 = Continuous rotation - */ - int SetMode(int mode); - - /** Set goal angle in integer degrees, in positional mode - * - * @param degrees 0-300 - * @param flags, defaults to 0 - * flags[0] = blocking, return when goal position reached - * flags[1] = register, activate with a broadcast trigger - * - */ - int SetGoal(int degrees, int flags = 0); - - - /** Set the speed of the servo in continuous rotation mode - * - * @param speed, -1.0 to 1.0 - * -1.0 = full speed counter clock wise - * 1.0 = full speed clock wise - */ - int SetCRSpeed(float speed); - - - /** Set the clockwise limit of the servo - * - * @param degrees, 0-300 - */ - int SetCWLimit(int degrees); - - /** Set the counter-clockwise limit of the servo - * - * @param degrees, 0-300 - */ - int SetCCWLimit(int degrees); - - // Change the ID - - /** Change the ID of a servo - * - * @param CurentID 1-255 - * @param NewID 1-255 - * - * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID. - * In this situation, only one servo should be connected to the bus - */ - int SetID(int CurrentID, int NewID); - - - /** Poll to see if the servo is moving - * - * @returns true is the servo is moving - */ - int isMoving(void); - - /** Send the broadcast "trigger" command, to activate any outstanding registered commands - */ - void trigger(void); - - /** Read the current angle of the servo - * - * @returns float in the range 0.0-300.0 - */ - float GetPosition(); - - /** Read the temperature of the servo - * - * @returns float temperature - */ - float GetTemp(void); - - /** Read the supply voltage of the servo - * - * @returns float voltage - */ - float GetVolts(void); - -private : - - SerialHalfDuplex _ax12; - int _ID; - - int read(int ID, int start, int length, char* data); - int write(int ID, int start, int length, char* data, int flag=0); - -}; - -#endif
diff -r 93ad80f5fde7 -r 40a609b85463 RX64.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RX64.cpp Mon Feb 29 08:47:07 2016 +0000 @@ -0,0 +1,470 @@ +/* mbed AX-12+ Servo Library + * + * Copyright (c) 2010, cstyles (http://mbed.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "RX64.h" +#include "mbed.h" + +RX64::RX64(PinName tx, PinName rx, int ID) + : _rx64(tx,rx) { + + _rx64.baud(57600); + _ID = ID; +} + +// Set the mode of the servo +// 0 = Positional (0-300 degrees) +// 1 = Rotational -1 to 1 speed +int RX64::SetMode(int mode) { + + if (mode == 1) { // set CR + SetCWLimit(0); + SetCCWLimit(0); + SetCRSpeed(0.0); + } else { + SetCWLimit(0); + SetCCWLimit(300); + SetCRSpeed(0.04); + } + return(0); +} + + +// if flag[0] is set, were blocking +// if flag[1] is set, we're registering +// they are mutually exclusive operations +int RX64::SetGoal(int degrees, int flags) { + + char reg_flag = 0; + char data[2]; + + // set the flag is only the register bit is set in the flag + if (flags == 0x2) { + reg_flag = 1; + } + + // 1023 / 300 * degrees + short goal = (1023 * degrees) / 300; + if (RX64_DEBUG) { + printf("SetGoal to 0x%x\n",goal); + } + + data[0] = goal & 0xff; // bottom 8 bits + data[1] = goal >> 8; // top 8 bits + + // write the packet, return the error code + int rVal = write(_ID, RX64_REG_GOAL_POSITION, 2, data, reg_flag); + + if (flags == 1) { + // block until it comes to a halt + while (isMoving()) {} + } + return(rVal); +} + + +// Set continuous rotation speed from -1 to 1 +int RX64::SetCRSpeed(float speed) { + + // bit 10 = direction, 0 = CCW, 1=CW + // bits 9-0 = Speed + char data[2]; + + int goal = (0x3ff * abs(speed)); + + // Set direction CW if we have a negative speed + if (speed < 0) { + goal |= (0x1 << 10); + } + + data[0] = goal & 0xff; // bottom 8 bits + data[1] = goal >> 8; // top 8 bits + + // write the packet, return the error code + int rVal = write(_ID, 0x20, 2, data); + + return(rVal); +} + + +int RX64::SetCWLimit (int degrees) { + + char data[2]; + + // 1023 / 300 * degrees + short limit = (1023 * degrees) / 300; + + if (RX64_DEBUG) { + printf("SetCWLimit to 0x%x\n",limit); + } + + data[0] = limit & 0xff; // bottom 8 bits + data[1] = limit >> 8; // top 8 bits + + // write the packet, return the error code + return (write(_ID, RX64_REG_CW_LIMIT, 2, data)); + +} + +int RX64::SetCCWLimit (int degrees) { + + char data[2]; + + // 1023 / 300 * degrees + short limit = (1023 * degrees) / 300; + + if (RX64_DEBUG) { + printf("SetCCWLimit to 0x%x\n",limit); + } + + data[0] = limit & 0xff; // bottom 8 bits + data[1] = limit >> 8; // top 8 bits + + // write the packet, return the error code + return (write(_ID, RX64_REG_CCW_LIMIT, 2, data)); +} + + +int RX64::SetID (int CurrentID, int NewID) { + + char data[1]; + data[0] = NewID; + if (RX64_DEBUG) { + printf("Setting ID from 0x%x to 0x%x\n",CurrentID,NewID); + } + return (write(CurrentID, RX64_REG_ID, 1, data)); + +} + + +// return 1 is the servo is still in flight +int RX64::isMoving(void) { + + char data[1]; + read(_ID,RX64_REG_MOVING,1,data); + return(data[0]); +} + + +void RX64::trigger(void) { + + char TxBuf[16]; + char sum = 0; + + if (RX64_TRIGGER_DEBUG) { + printf("\nTriggered\n"); + } + + // Build the TxPacket first in RAM, then we'll send in one go + if (RX64_TRIGGER_DEBUG) { + printf("\nTrigger Packet\n Header : 0xFF, 0xFF\n"); + } + + TxBuf[0] = 0xFF; + TxBuf[1] = 0xFF; + + // ID - Broadcast + TxBuf[2] = 0xFE; + sum += TxBuf[2]; + + if (RX64_TRIGGER_DEBUG) { + printf(" ID : %d\n",TxBuf[2]); + } + + // Length + TxBuf[3] = 0x02; + sum += TxBuf[3]; + if (RX64_TRIGGER_DEBUG) { + printf(" Length %d\n",TxBuf[3]); + } + + // Instruction - ACTION + TxBuf[4] = 0x04; + sum += TxBuf[4]; + if (RX64_TRIGGER_DEBUG) { + printf(" Instruction 0x%X\n",TxBuf[5]); + } + + // Checksum + TxBuf[5] = 0xFF - sum; + if (RX64_TRIGGER_DEBUG) { + printf(" Checksum 0x%X\n",TxBuf[5]); + } + + // Transmit the packet in one burst with no pausing + for (int i = 0; i < 6 ; i++) { + _rx64.putc(TxBuf[i]); + } + + // This is a broadcast packet, so there will be no reply + + return; +} + + +float RX64::GetPosition(void) { + + if (RX64_DEBUG) { + printf("\nGetPosition(%d)",_ID); + } + + char data[2]; + + int ErrorCode = read(_ID, RX64_REG_POSITION, 2, data); + short position = data[0] + (data[1] << 8); + float angle = (position * 300)/1024; + + return (angle); +} + + +float RX64::GetTemp (void) { + + if (RX64_DEBUG) { + printf("\nGetTemp(%d)",_ID); + } + char data[1]; + int ErrorCode = read(_ID, RX64_REG_TEMP, 1, data); + float temp = data[0]; + return(temp); +} + + +float RX64::GetVolts (void) { + if (RX64_DEBUG) { + printf("\nGetVolts(%d)",_ID); + } + char data[1]; + int ErrorCode = read(_ID, RX64_REG_VOLTS, 1, data); + float volts = data[0]/10.0; + return(volts); +} + + +int RX64::read(int ID, int start, int bytes, char* data) { + + char PacketLength = 0x4; + char TxBuf[16]; + char sum = 0; + char Status[16]; + + Status[4] = 0xFE; // return code + + if (RX64_READ_DEBUG) { + printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes); + } + + // Build the TxPacket first in RAM, then we'll send in one go + if (RX64_READ_DEBUG) { + printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); + } + + TxBuf[0] = 0xff; + TxBuf[1] = 0xff; + + // ID + TxBuf[2] = ID; + sum += TxBuf[2]; + if (RX64_READ_DEBUG) { + printf(" ID : %d\n",TxBuf[2]); + } + + // Packet Length + TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes) + sum += TxBuf[3]; // Accululate the packet sum + if (RX64_READ_DEBUG) { + printf(" Length : 0x%x\n",TxBuf[3]); + } + + // Instruction - Read + TxBuf[4] = 0x2; + sum += TxBuf[4]; + if (RX64_READ_DEBUG) { + printf(" Instruction : 0x%x\n",TxBuf[4]); + } + + // Start Address + TxBuf[5] = start; + sum += TxBuf[5]; + if (RX64_READ_DEBUG) { + printf(" Start Address : 0x%x\n",TxBuf[5]); + } + + // Bytes to read + TxBuf[6] = bytes; + sum += TxBuf[6]; + if (RX64_READ_DEBUG) { + printf(" No bytes : 0x%x\n",TxBuf[6]); + } + + // Checksum + TxBuf[7] = 0xFF - sum; + if (RX64_READ_DEBUG) { + printf(" Checksum : 0x%x\n",TxBuf[7]); + } + + // Transmit the packet in one burst with no pausing + for (int i = 0; i<8 ; i++) { + _rx64.putc(TxBuf[i]); + } + + // Wait for the bytes to be transmitted + wait (0.00002); + + // Skip if the read was to the broadcast address + if (_ID != 0xFE) { + + // Receive the Status packet 6+ number of bytes read + for (int i=0; i<(6+bytes) ; i++) { + Status[i] = _rx64.getc(); + } + + // Copy the data from Status into data for return + for (int i=0; i < Status[3]-2 ; i++) { + data[i] = Status[5+i]; + } + + if (RX64_READ_DEBUG) { + printf("\nStatus Packet\n"); + printf(" Header : 0x%x\n",Status[0]); + printf(" Header : 0x%x\n",Status[1]); + printf(" ID : 0x%x\n",Status[2]); + printf(" Length : 0x%x\n",Status[3]); + printf(" Error Code : 0x%x\n",Status[4]); + + for (int i=0; i < Status[3]-2 ; i++) { + printf(" Data : 0x%x\n",Status[5+i]); + } + + printf(" Checksum : 0x%x\n",Status[5+(Status[3]-2)]); + } + + } // if (ID!=0xFE) + + return(Status[4]); +} + + +int RX64:: write(int ID, int start, int bytes, char* data, int flag) { +// 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum + + char TxBuf[16]; + char sum = 0; + char Status[6]; + + if (RX64_WRITE_DEBUG) { + printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag); + } + + // Build the TxPacket first in RAM, then we'll send in one go + if (RX64_WRITE_DEBUG) { + printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); + } + + TxBuf[0] = 0xff; + TxBuf[1] = 0xff; + + // ID + TxBuf[2] = ID; + sum += TxBuf[2]; + + if (RX64_WRITE_DEBUG) { + printf(" ID : %d\n",TxBuf[2]); + } + + // packet Length + TxBuf[3] = 3+bytes; + sum += TxBuf[3]; + + if (RX64_WRITE_DEBUG) { + printf(" Length : %d\n",TxBuf[3]); + } + + // Instruction + if (flag == 1) { + TxBuf[4]=0x04; + sum += TxBuf[4]; + } else { + TxBuf[4]=0x03; + sum += TxBuf[4]; + } + + if (RX64_WRITE_DEBUG) { + printf(" Instruction : 0x%x\n",TxBuf[4]); + } + + // Start Address + TxBuf[5] = start; + sum += TxBuf[5]; + if (RX64_WRITE_DEBUG) { + printf(" Start : 0x%x\n",TxBuf[5]); + } + + // data + for (char i=0; i<bytes ; i++) { + TxBuf[6+i] = data[i]; + sum += TxBuf[6+i]; + if (RX64_WRITE_DEBUG) { + printf(" Data : 0x%x\n",TxBuf[6+i]); + } + } + + // checksum + TxBuf[6+bytes] = 0xFF - sum; + if (RX64_WRITE_DEBUG) { + printf(" Checksum : 0x%x\n",TxBuf[6+bytes]); + } + + // Transmit the packet in one burst with no pausing + for (int i = 0; i < (7 + bytes) ; i++) { + _rx64.putc(TxBuf[i]); + } + + // Wait for data to transmit + wait (0.00002); + + // make sure we have a valid return + Status[4]=0x00; + + // we'll only get a reply if it was not broadcast + if (_ID!=0xFE) { + + // response is always 6 bytes + // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum + for (int i=0; i < 6 ; i++) { + Status[i] = _rx64.getc(); + } + + // Build the TxPacket first in RAM, then we'll send in one go + if (RX64_WRITE_DEBUG) { + printf("\nStatus Packet\n Header : 0x%X, 0x%X\n",Status[0],Status[1]); + printf(" ID : %d\n",Status[2]); + printf(" Length : %d\n",Status[3]); + printf(" Error : 0x%x\n",Status[4]); + printf(" Checksum : 0x%x\n",Status[5]); + } + + + } + + return(Status[4]); // return error code +}
diff -r 93ad80f5fde7 -r 40a609b85463 RX64.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RX64.h Mon Feb 29 08:47:07 2016 +0000 @@ -0,0 +1,173 @@ +/* mbed AX-12+ Servo Library + * + * Copyright (c) 2010, cstyles (http://mbed.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MBED_RX64_H +#define MBED_RX64_H + +#include "mbed.h" + +#define RX64_WRITE_DEBUG 0 +#define RX64_READ_DEBUG 0 +#define RX64_TRIGGER_DEBUG 0 +#define RX64_DEBUG 0 + +#define RX64_REG_ID 0x3 +#define RX64_REG_CW_LIMIT 0x06 +#define RX64_REG_CCW_LIMIT 0x08 +#define RX64_REG_GOAL_POSITION 0x1E +#define RX64_REG_MOVING_SPEED 0x20 +#define RX64_REG_VOLTS 0x2A +#define RX64_REG_TEMP 0x2B +#define RX64_REG_MOVING 0x2E +#define RX64_REG_POSITION 0x24 + +#define RX64_MODE_POSITION 0 +#define RX64_MODE_ROTATION 1 + +#define RX64_CW 1 +#define RX64_CCW 0 + +/** Servo control class, based on a PwmOut + * + * Example: + * @code + * #include "mbed.h" + * #include "AX12.h" + * + * int main() { + * + * AX12 myax12 (p9, p10, 1); + * + * while (1) { + * myax12.SetGoal(0); // go to 0 degrees + * wait (2.0); + * myax12.SetGoal(300); // go to 300 degrees + * wait (2.0); + * } + * } + * @endcode + */ +class RX64 { + +public: + + /** Create an AX12 servo object connected to the specified serial port, with the specified ID + * + * @param pin tx pin + * @param pin rx pin + * @param int ID, the Bus ID of the servo 1-255 + */ + RX64(PinName tx, PinName rx, int ID); + + /** Set the mode of the servo + * @param mode + * 0 = Positional, default + * 1 = Continuous rotation + */ + int SetMode(int mode); + + /** Set goal angle in integer degrees, in positional mode + * + * @param degrees 0-300 + * @param flags, defaults to 0 + * flags[0] = blocking, return when goal position reached + * flags[1] = register, activate with a broadcast trigger + * + */ + int SetGoal(int degrees, int flags = 0); + + + /** Set the speed of the servo in continuous rotation mode + * + * @param speed, -1.0 to 1.0 + * -1.0 = full speed counter clock wise + * 1.0 = full speed clock wise + */ + int SetCRSpeed(float speed); + + + /** Set the clockwise limit of the servo + * + * @param degrees, 0-300 + */ + int SetCWLimit(int degrees); + + /** Set the counter-clockwise limit of the servo + * + * @param degrees, 0-300 + */ + int SetCCWLimit(int degrees); + + // Change the ID + + /** Change the ID of a servo + * + * @param CurentID 1-255 + * @param NewID 1-255 + * + * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID. + * In this situation, only one servo should be connected to the bus + */ + int SetID(int CurrentID, int NewID); + + + /** Poll to see if the servo is moving + * + * @returns true is the servo is moving + */ + int isMoving(void); + + /** Send the broadcast "trigger" command, to activate any outstanding registered commands + */ + void trigger(void); + + /** Read the current angle of the servo + * + * @returns float in the range 0.0-300.0 + */ + float GetPosition(); + + /** Read the temperature of the servo + * + * @returns float temperature + */ + float GetTemp(void); + + /** Read the supply voltage of the servo + * + * @returns float voltage + */ + float GetVolts(void); + +private : + + + SerialHalfDuplex _rx64; + int _ID; + + int read(int ID, int start, int length, char* data); + int write(int ID, int start, int length, char* data, int flag=0); + +}; + +#endif