Telesensorics / Dynamixel

Dependents:   RobotArmDemo

Fork of Dynamixel by robot arm demo team

DynamixelBus.h

Committer:
jepickett
Date:
2015-12-10
Revision:
0:35c27ebd9e8e
Child:
4:fe2a6b66cb85

File content as of revision 0:35c27ebd9e8e:

/* 
Copyright (c) 2015 Jonathan Pickett & Microsoft. Some appropriate open source license.
*/

#ifndef __DYNAMIXEL_BUS_H__
#define __DYNAMIXEL_BUS_H__

#include "mbed.h"

#include <vector>
using namespace std;


enum InstructionTable
{
    instructionPing            = 0x1,          // no action. obtain status packet.
    instructionRead            = 0x2,          // write values in control table
    instructionWrite           = 0x3,          // read values in control table
    instructionWriteAction     = 0x4,          // write actions to be performed upon triggering
    instructionTriggerAction   = 0x5,          // trigger stored actions
    instructionReset           = 0x6,          // changes ALL control table vaalues to factory default
    instructionSyncWrite       = 0x83          // write to many servos at once
};


enum StatusCodes
{
    statusValid       = 0x80,       // REVIEW: Not in spec. I am overloading this for Ping, such that a 0 return value = unit not found
    instructionError  = 0x40,
    overloadError     = 0x20,
    checksumError     = 0x10,
    rangeError        = 0x08,
    overheatingError  = 0x04,
    angleLimitError   = 0x02,
    inputVoltageError = 0x01
};

typedef unsigned char StatusCode;

typedef vector<unsigned char> CommBuffer;

typedef unsigned char ServoId;

typedef pair< ServoId, CommBuffer > SyncIdDataPair;
    
class DynamixelBus
{
public:
    DynamixelBus( PinName txUartPin, PinName rxUartPin, PinName txSelectPin, PinName rxSelectPin, int baud );
    
    StatusCode  Ping( ServoId id );
    StatusCode  Read( ServoId id, unsigned char controlTableStart, unsigned char bytesToRead, CommBuffer& data );
    StatusCode  Write( ServoId id, unsigned char controlTableStart, const CommBuffer& dataToWrite );
    StatusCode  WriteAction( ServoId id, unsigned char controlTableStart, const CommBuffer& dataToWrite );
    void        TriggerAction( ServoId id );
    StatusCode  HardReset( ServoId id );
    StatusCode  SyncWrite( unsigned char controlTableStart, unsigned char bytesToWrite, vector<SyncIdDataPair>& data );

private:
    unsigned char CalculateTxPacketChecksum( CommBuffer& packet );
    StatusCode send_ping( ServoId id );
    StatusCode send_write( ServoId id, unsigned char controlTableStart, const CommBuffer& dataToWrite );
    StatusCode send_read( ServoId id, unsigned char controlTableStart, unsigned char bytesToRead, CommBuffer& data );
        
private:
    Serial      _uart;      // The uart the servos live on.
    DigitalOut _txSelect;   // Turn on to enable the tri-state buffer before sending data over the bus. Turn off when data has been sent.
    DigitalOut _rxSelect;   // Turn on to enable the tri-state buffer before receiving data over the bus. Turn off when data has been received.
    
    float _replyDelay;      // The delay time between command being sent and response being sent by the servo.
    int _baud;              // baud of all connected servos
    float _responseTimeout;
};

#endif // __DYNAMIXEL_BUS_H__