PSL_2021 / servomotor_MX12_Lorenzo

Dependents:   PSL_ROBOT_lorenzo robot_lorenzo recepteur_mbed_os_6

MX12.h

Committer:
denis2nis
Date:
2021-11-03
Revision:
1:a9ba9cf928fe
Parent:
0:7556356a8bcd
Child:
2:02f3323a107d

File content as of revision 1:a9ba9cf928fe:

#ifndef MBED_MX12_H
#define MBED_MX12_H

/**  
* @file MX12.h  
* @brief this header file will contain all required  
*        definitions and basic utilities functions. 
*  
* @author Titouan Soulard 
* @author Bruno Denis (for comments)
* 
*/ 

#include "mbed.h"

#define MX12_ANSWER_MAX_SIZE 32
#define MX12_MOTOR_COUNT 16

/** Class to communicate with Dynamixel MX12 servomotors. 
 *
 *  
 */
class MX12 
{
    public:
    
        /** Status of servomotor
         *
         */
        enum Status {
            InstructionError,
            OverloadError,
            ChecksumError,
            RangeError,
            OverheatingError,
            AngleLimitError,
            InputVoltageError,
            Unknown,
            Ok
        };
        
        /** State of communication automaton
         *
         */
         enum State {
            ReadingPosition,
            Writing,
            Available,
        };
    
        /** Create MX12 instance
         *
         * @param tx board pin used for transmission in UART daisy chain link 
         *           to servomotors 
         * @param rx board pin used for reception in UART daisy chain link 
         *           to servomotors 
         * @param baud modulation rate of UART signal, unit: Baud
         *           to servomotors 
         */
        MX12(PinName tx, PinName rx, int baud=115200);
        
        /** Send desired speed to a specifc servomotor
         *
         * @param mot_id a unique value in the daisy chain network to identify
         *               each servomotor
         * @param speed a float between -1 and 1 that represents the percentage
         *              of maximum requested speed
         */
        void SetSpeed(unsigned char mot_id, float speed);
        
        char IsAvailable(void);
        
        MX12::Status GetStatus(void);
        
        
        void ReadPosition(unsigned char mot_id);
        
        float GetPosition(unsigned char mot_id);
        
        void PrintAnswer();
        
        void rw(unsigned char mot_id, char adress, char len, char *data);
        
        /** Interupt Routine to read in data from UART daisy chain link
         *  (serial port)
         *
         */
        void _ReadCallback();
        
        char _chksm;
    
    private:
    
        UnbufferedSerial _mx12;
        MX12::Status _status;
        char _res[MX12_ANSWER_MAX_SIZE];
        char _res_count;
        char _len;
        MX12::State _state;
        float _angle[MX12_MOTOR_COUNT];
        int _baud;
};

#endif /* MBED_MX12_H */