2025-09-27 02:53:41 +00:00
# ifndef FEETECH_H
# define FEETECH_H
2025-09-30 16:35:00 +00:00
# include <vector>
2025-09-27 02:53:41 +00:00
# include <Arduino.h>
# include <HardwareSerial.h>
// SCS & HLS big-endian (high byte first)
// STS little-endian (low byte first)
2025-10-30 17:44:19 +00:00
//
2025-09-27 02:53:41 +00:00
class Feetech {
public :
2025-10-04 10:29:07 +00:00
enum FeetechMode {
2025-10-30 17:44:19 +00:00
MODE_SCS = 5 ,
MODE_SMSA = 6 ,
MODE_SMSB = 8 ,
MODE_STS = 9
2025-10-04 10:29:07 +00:00
} ;
2025-10-30 17:44:19 +00:00
2025-09-27 02:53:41 +00:00
Feetech ( HardwareSerial & serial , int DE_PIN , int RE_PIN , int TX_PIN , int RX_PIN ) ;
void begin ( ) ;
void sendPing ( uint8_t id ) ;
2025-10-04 10:29:07 +00:00
void clearEcho ( uint8_t length ) ;
2025-09-27 02:53:41 +00:00
uint16_t getModel ( uint8_t id ) ;
2025-11-02 10:41:56 +00:00
uint16_t getMajorModel ( uint8_t id ) ;
2025-09-27 02:53:41 +00:00
uint8_t getID ( uint8_t id ) ;
uint8_t setID ( uint8_t id , uint8_t newId ) ;
2025-11-02 06:22:30 +00:00
uint8_t changeID ( uint8_t id , uint8_t newId ) ;
2025-09-27 02:53:41 +00:00
uint8_t getBaudRate ( uint8_t id ) ;
uint16_t getMinAngleLimit ( uint8_t id ) ;
2025-10-04 10:29:07 +00:00
uint16_t setMinAngleLimit ( uint8_t id , uint16_t limit ) ;
2025-09-27 02:53:41 +00:00
uint16_t getMaxAngleLimit ( uint8_t id ) ;
2025-10-04 10:29:07 +00:00
uint16_t setMaxAngleLimit ( uint8_t id , uint16_t limit ) ;
2025-09-27 02:53:41 +00:00
uint8_t getCWDeadZone ( uint8_t id ) ;
uint8_t getCCWDeadZone ( uint8_t id ) ;
uint16_t getOffset ( uint8_t id ) ;
uint8_t getMode ( uint8_t id ) ;
uint8_t getTorqueEnable ( uint8_t id ) ;
uint8_t getAcceleration ( uint8_t id ) ;
uint16_t getGoalPosition ( uint8_t id ) ;
uint16_t getGoalTime ( uint8_t id ) ;
uint16_t getGoalSpeed ( uint8_t id ) ;
uint8_t getLock ( uint8_t id ) ;
uint8_t setLock ( uint8_t id , uint8_t lockEnabled ) ;
uint16_t getPosition ( uint8_t id ) ;
int16_t getSpeed ( uint8_t id ) ;
uint16_t getLoad ( uint8_t id ) ;
uint8_t getTemperature ( uint8_t id ) ;
2025-09-30 16:35:00 +00:00
2025-09-27 02:53:41 +00:00
uint8_t getMoving ( uint8_t id ) ;
2025-10-05 12:00:35 +00:00
uint16_t getCurrent ( uint8_t id ) ;
2025-09-27 02:53:41 +00:00
2025-10-05 12:00:35 +00:00
uint8_t getVoltage ( uint8_t id ) ;
2025-09-27 02:53:41 +00:00
void sendRequest ( uint8_t id , uint8_t instruction , uint8_t byteCount ) ;
2025-10-04 10:29:07 +00:00
uint8_t sendWritePos ( uint8_t id , uint16_t position ) ;
2025-11-02 06:22:30 +00:00
void syncWritePos ( uint8_t * ids , uint16_t * positions , uint16_t * speeds , uint8_t count ) ;
2025-09-27 02:53:41 +00:00
void write1Byte ( uint8_t id , byte instruction , uint8_t data ) ;
void write2Bytes ( uint8_t id , byte instruction , uint16_t data ) ;
void pingAll ( std : : vector < uint8_t > & successfulAddresses ) ;
void waitOnReply ( unsigned long timeout ) ;
uint8_t waitOnData1Byte ( unsigned long timeout ) ;
uint16_t waitOnData2Bytes ( unsigned long timeout ) ;
void sendData ( const byte * data , size_t length ) ;
size_t receiveData ( byte * buffer , size_t bufferSize ) ;
void setModeReceive ( ) ;
void setModeTransmit ( ) ;
2025-10-04 10:29:07 +00:00
uint16_t flipBytes ( uint16_t value ) ;
2025-09-27 02:53:41 +00:00
void update ( ) ;
void testRequest ( ) ;
2025-10-04 10:29:07 +00:00
uint8_t enableTorque ( uint8_t id ) ;
uint8_t disableTorque ( uint8_t id ) ;
void setFeetechMode ( FeetechMode newMode ) ;
2025-11-02 06:22:30 +00:00
void setFeetechMode ( uint8_t majorModel ) ;
void setFeetechMode ( uint16_t model ) ;
2025-09-27 02:53:41 +00:00
static const byte PING = 0x01 ; // QUERY THE WORKING STATUS
static const byte READ_DATA = 0x02 ; // READ DATA
static const byte WRITE_DATA = 0x03 ; // WRITE DATA
static const byte REGWRITE_DATA = 0x04 ; // QUEUES MOVES FOR ACTION COMMAND
static const byte ACTION = 0x05 ; // TRIGGERS REG WRITE WRITES
static const byte SYNCWRITE_DATA = 0x83 ; // SIMULTANEOUS CONTROL OF MULTIPLE SERVOS
static const byte RESET = 0x06 ; // RESET TO FACTORY DEFAULT
static const byte BROADCAST_ID = 0xFE ;
// MEMORY TABLE LOCATIONS SMS-STS
static const byte REQUEST_MODEL = 0x03 ; // 2 bytes
static const byte REQUEST_ID = 0x05 ; // 1 byte
static const byte REQUEST_BAUD_RATE = 0x06 ; // 1 byte
static const byte REQUEST_MIN_ANGLE_LIMIT = 0x09 ; // 2 bytes
static const byte REQUEST_MAX_ANGLE_LIMIT = 0x0B ; // 2 bytes
static const byte REQUEST_CW_DEAD_ZONE = 0x1A ; // 1 byte
static const byte REQUEST_CCW_DEAD_ZONE = 0x1B ; // 1 byte
2025-10-05 12:00:35 +00:00
// REQUEST OFFSET AND MODE NOT IN SCS?????
2025-10-30 17:44:19 +00:00
static const byte REQUEST_OFFSET = 0x1F ; // 2 bytes
static const byte REQUEST_MODE = 0x21 ; // 1 byte
2025-09-27 02:53:41 +00:00
static const byte REQUEST_TORQUE_ENABLE = 0x28 ; // 1 byte
static const byte REQUEST_ACCELERATION = 0x29 ; // 1 byte
static const byte REQUEST_GOAL_POSITION = 0x2A ; // 2 byte
static const byte REQUEST_GOAL_TIME = 0x2C ; // 2 byte
static const byte REQUEST_GOAL_SPEED = 0x2E ; // 2 byte
2025-10-05 12:00:35 +00:00
2025-10-30 17:44:19 +00:00
static const byte REQUEST_LOCK_SCS = 0x30 ; // 1 byte
static const byte REQUEST_LOCK_SMS_STS = 0x37 ; // 1 byte
2025-09-27 02:53:41 +00:00
static const byte REQUEST_POSITION = 0x38 ; // 2 bytes
static const byte REQUEST_CURRENT_SPEED = 0x3A ; // 2 bytes
2025-09-30 16:35:00 +00:00
static const byte REQUEST_CURRENT_LOAD = 0x3C ; // 2 bytes
static const byte REQUEST_VOLTAGE = 0x3E ; // 1 byte
2025-09-27 02:53:41 +00:00
2025-11-02 10:41:56 +00:00
static const byte REQUEST_TEMPERATURE = 0x3F ; // 1 byte
static const byte REQUEST_STATUS = 0x41 ; // 1 byte Bit0, Bit1, Bit2, Bit3, Bit4, and Bit5 are set to 1 to indicate the occurrence of a corresponding error (no voltage, no temperature, no overload). A corresponding bit of 0 indicates no error.
2025-09-30 16:35:00 +00:00
static const byte REQUEST_MOVING = 0x42 ; // 1 byte
2025-09-27 02:53:41 +00:00
static const byte REQUEST_CURRENT_CURRENT = 0x45 ; // 2 bytes
// BAUD RATES (stored as 1 byte)
static const byte SMS_STS_1M = 0 ;
static const byte SMS_STS_0_5M = 1 ;
static const byte SMS_STS_250K = 2 ;
static const byte SMS_STS_128K = 3 ;
static const byte SMS_STS_115200 = 4 ;
static const byte SMS_STS_76800 = 5 ;
static const byte SMS_STS_57600 = 6 ;
static const byte SMS_STS_38400 = 7 ;
2025-09-30 16:35:00 +00:00
uint8_t lastSentPacket [ 32 ] ;
int lastSentLength = 0 ;
2025-10-30 17:44:19 +00:00
bool filterEcho = true ; // Filters out echo caused by having simple diode/resistor TTL splitter
2025-09-30 16:35:00 +00:00
2025-09-27 02:53:41 +00:00
private :
HardwareSerial & serial ; // Reference to the HardwareSerial object
uint8_t DE_PIN = 20 ;
uint8_t RE_PIN = 10 ;
uint8_t TX_PIN = 21 ;
uint8_t RX_PIN = 9 ;
2025-10-04 10:29:07 +00:00
FeetechMode feetechMode = MODE_SCS ;
2025-09-27 02:53:41 +00:00
} ;
# endif // FEETECH_H