Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MX12.h@2:02f3323a107d, 2021-11-04 (annotated)
- Committer:
- denis2nis
- Date:
- Thu Nov 04 06:12:21 2021 +0000
- Revision:
- 2:02f3323a107d
- Parent:
- 1:a9ba9cf928fe
- Child:
- 3:add8b050eb86
Arrange and comment MX12::rw function
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| denis2nis | 0:7556356a8bcd | 1 | #ifndef MBED_MX12_H | 
| denis2nis | 0:7556356a8bcd | 2 | #define MBED_MX12_H | 
| denis2nis | 0:7556356a8bcd | 3 | |
| denis2nis | 1:a9ba9cf928fe | 4 | /** | 
| denis2nis | 1:a9ba9cf928fe | 5 | * @file MX12.h | 
| denis2nis | 1:a9ba9cf928fe | 6 | * @brief this header file will contain all required | 
| denis2nis | 1:a9ba9cf928fe | 7 | * definitions and basic utilities functions. | 
| denis2nis | 2:02f3323a107d | 8 | * @details comming soon | 
| denis2nis | 1:a9ba9cf928fe | 9 | * | 
| denis2nis | 1:a9ba9cf928fe | 10 | * @author Titouan Soulard | 
| denis2nis | 1:a9ba9cf928fe | 11 | * @author Bruno Denis (for comments) | 
| denis2nis | 1:a9ba9cf928fe | 12 | * | 
| denis2nis | 1:a9ba9cf928fe | 13 | */ | 
| denis2nis | 0:7556356a8bcd | 14 | |
| denis2nis | 0:7556356a8bcd | 15 | #include "mbed.h" | 
| denis2nis | 0:7556356a8bcd | 16 | |
| denis2nis | 0:7556356a8bcd | 17 | #define MX12_ANSWER_MAX_SIZE 32 | 
| denis2nis | 0:7556356a8bcd | 18 | #define MX12_MOTOR_COUNT 16 | 
| denis2nis | 0:7556356a8bcd | 19 | |
| denis2nis | 2:02f3323a107d | 20 | /* Dynamixel protocol v1.0 : Instructions | 
| denis2nis | 2:02f3323a107d | 21 | ******************************************/ | 
| denis2nis | 2:02f3323a107d | 22 | #define PROTOCOL_INSTRUCTION_PING 0x01 | 
| denis2nis | 2:02f3323a107d | 23 | #define PROTOCOL_INSTRUCTION_READ 0x02 | 
| denis2nis | 2:02f3323a107d | 24 | #define PROTOCOL_INSTRUCTION_WRITE 0x03 | 
| denis2nis | 2:02f3323a107d | 25 | #define PROTOCOL_INSTRUCTION_REG_WRITE 0x04 | 
| denis2nis | 2:02f3323a107d | 26 | #define PROTOCOL_INSTRUCTION_ACTION 0x05 | 
| denis2nis | 2:02f3323a107d | 27 | #define PROTOCOL_INSTRUCTION_FACTORY_RESET 0x06 | 
| denis2nis | 2:02f3323a107d | 28 | #define PROTOCOL_INSTRUCTION_REBOOT 0x08 | 
| denis2nis | 2:02f3323a107d | 29 | #define PROTOCOL_INSTRUCTION_SYNC_WRITE 0x83 | 
| denis2nis | 2:02f3323a107d | 30 | #define PROTOCOL_INSTRUCTION_BULK_READ 0x92 | 
| denis2nis | 2:02f3323a107d | 31 | |
| denis2nis | 2:02f3323a107d | 32 | |
| denis2nis | 2:02f3323a107d | 33 | |
| denis2nis | 2:02f3323a107d | 34 | /** | 
| denis2nis | 2:02f3323a107d | 35 | * @brief Class to communicate with Dynamixel MX12 servomotors. | 
| denis2nis | 1:a9ba9cf928fe | 36 | * | 
| denis2nis | 2:02f3323a107d | 37 | * @details | 
| denis2nis | 2:02f3323a107d | 38 | * The servomotors are daisy chained to a serial link of the target | 
| denis2nis | 2:02f3323a107d | 39 | * microcontroller. The class ensures the initialization of serial link | 
| denis2nis | 2:02f3323a107d | 40 | * and the management of communications. | 
| denis2nis | 2:02f3323a107d | 41 | * Transmission of messages to the servomotors is blocking while | 
| denis2nis | 2:02f3323a107d | 42 | * reception is non-blocking thanks to the use of an interrupt routine. | 
| denis2nis | 1:a9ba9cf928fe | 43 | * | 
| denis2nis | 1:a9ba9cf928fe | 44 | */ | 
| denis2nis | 0:7556356a8bcd | 45 | class MX12 | 
| denis2nis | 0:7556356a8bcd | 46 | { | 
| denis2nis | 0:7556356a8bcd | 47 | public: | 
| denis2nis | 0:7556356a8bcd | 48 | |
| denis2nis | 2:02f3323a107d | 49 | /** Status enum, possible status of servomotor | 
| denis2nis | 1:a9ba9cf928fe | 50 | * | 
| denis2nis | 1:a9ba9cf928fe | 51 | */ | 
| denis2nis | 0:7556356a8bcd | 52 | enum Status { | 
| denis2nis | 2:02f3323a107d | 53 | InstructionError, ///< | 
| denis2nis | 0:7556356a8bcd | 54 | OverloadError, | 
| denis2nis | 0:7556356a8bcd | 55 | ChecksumError, | 
| denis2nis | 0:7556356a8bcd | 56 | RangeError, | 
| denis2nis | 0:7556356a8bcd | 57 | OverheatingError, | 
| denis2nis | 0:7556356a8bcd | 58 | AngleLimitError, | 
| denis2nis | 0:7556356a8bcd | 59 | InputVoltageError, | 
| denis2nis | 0:7556356a8bcd | 60 | Unknown, | 
| denis2nis | 0:7556356a8bcd | 61 | Ok | 
| denis2nis | 0:7556356a8bcd | 62 | }; | 
| denis2nis | 0:7556356a8bcd | 63 | |
| denis2nis | 2:02f3323a107d | 64 | /** State enum, possible state of communication automaton | 
| denis2nis | 1:a9ba9cf928fe | 65 | * | 
| denis2nis | 1:a9ba9cf928fe | 66 | */ | 
| denis2nis | 1:a9ba9cf928fe | 67 | enum State { | 
| denis2nis | 0:7556356a8bcd | 68 | ReadingPosition, | 
| denis2nis | 0:7556356a8bcd | 69 | Writing, | 
| denis2nis | 0:7556356a8bcd | 70 | Available, | 
| denis2nis | 0:7556356a8bcd | 71 | }; | 
| denis2nis | 0:7556356a8bcd | 72 | |
| denis2nis | 0:7556356a8bcd | 73 | /** Create MX12 instance | 
| denis2nis | 0:7556356a8bcd | 74 | * | 
| denis2nis | 0:7556356a8bcd | 75 | * @param tx board pin used for transmission in UART daisy chain link | 
| denis2nis | 0:7556356a8bcd | 76 | * to servomotors | 
| denis2nis | 0:7556356a8bcd | 77 | * @param rx board pin used for reception in UART daisy chain link | 
| denis2nis | 0:7556356a8bcd | 78 | * to servomotors | 
| denis2nis | 0:7556356a8bcd | 79 | * @param baud modulation rate of UART signal, unit: Baud | 
| denis2nis | 0:7556356a8bcd | 80 | * to servomotors | 
| denis2nis | 0:7556356a8bcd | 81 | */ | 
| denis2nis | 0:7556356a8bcd | 82 | MX12(PinName tx, PinName rx, int baud=115200); | 
| denis2nis | 0:7556356a8bcd | 83 | |
| denis2nis | 0:7556356a8bcd | 84 | /** Send desired speed to a specifc servomotor | 
| denis2nis | 0:7556356a8bcd | 85 | * | 
| denis2nis | 0:7556356a8bcd | 86 | * @param mot_id a unique value in the daisy chain network to identify | 
| denis2nis | 0:7556356a8bcd | 87 | * each servomotor | 
| denis2nis | 0:7556356a8bcd | 88 | * @param speed a float between -1 and 1 that represents the percentage | 
| denis2nis | 0:7556356a8bcd | 89 | * of maximum requested speed | 
| denis2nis | 0:7556356a8bcd | 90 | */ | 
| denis2nis | 0:7556356a8bcd | 91 | void SetSpeed(unsigned char mot_id, float speed); | 
| denis2nis | 0:7556356a8bcd | 92 | |
| denis2nis | 0:7556356a8bcd | 93 | char IsAvailable(void); | 
| denis2nis | 0:7556356a8bcd | 94 | |
| denis2nis | 0:7556356a8bcd | 95 | MX12::Status GetStatus(void); | 
| denis2nis | 0:7556356a8bcd | 96 | |
| denis2nis | 1:a9ba9cf928fe | 97 | |
| denis2nis | 0:7556356a8bcd | 98 | void ReadPosition(unsigned char mot_id); | 
| denis2nis | 0:7556356a8bcd | 99 | |
| denis2nis | 0:7556356a8bcd | 100 | float GetPosition(unsigned char mot_id); | 
| denis2nis | 0:7556356a8bcd | 101 | |
| denis2nis | 0:7556356a8bcd | 102 | void PrintAnswer(); | 
| denis2nis | 0:7556356a8bcd | 103 | |
| denis2nis | 2:02f3323a107d | 104 | /** | 
| denis2nis | 2:02f3323a107d | 105 | * Build and send an instruction packet to a servomotor according | 
| denis2nis | 2:02f3323a107d | 106 | * DYNAMIXEL Protocol 1.0 (online protocol documentation | 
| denis2nis | 2:02f3323a107d | 107 | * https://emanual.robotis.com/docs/en/dxl/protocol1/) | 
| denis2nis | 2:02f3323a107d | 108 | * | 
| denis2nis | 2:02f3323a107d | 109 | * @param[in] mot_id indicates the ID of the device (servomotor) that | 
| denis2nis | 2:02f3323a107d | 110 | * should receive the Instruction Packet and process it | 
| denis2nis | 2:02f3323a107d | 111 | * @param[in] address data address in the "Control Table of RAM Area" | 
| denis2nis | 2:02f3323a107d | 112 | * of a servomotor MX12 (https://emanual.robotis.com/docs/en/dxl/mx/mx-12w/#control-table-of-ram-area). | 
| denis2nis | 2:02f3323a107d | 113 | * @param[in] len if it is a write instruction, size in bytes | 
| denis2nis | 2:02f3323a107d | 114 | * of the data to write in the "Control Table of RAM Area" | 
| denis2nis | 2:02f3323a107d | 115 | * @param[in] data array of char containing parameter of Danamixel | 
| denis2nis | 2:02f3323a107d | 116 | * protocol that are the instruction’s auxiliary data field | 
| denis2nis | 2:02f3323a107d | 117 | * | 
| denis2nis | 2:02f3323a107d | 118 | */ | 
| denis2nis | 2:02f3323a107d | 119 | void rw(unsigned char mot_id, char address, char len, char *data); | 
| denis2nis | 0:7556356a8bcd | 120 | |
| denis2nis | 1:a9ba9cf928fe | 121 | /** Interupt Routine to read in data from UART daisy chain link | 
| denis2nis | 1:a9ba9cf928fe | 122 | * (serial port) | 
| denis2nis | 1:a9ba9cf928fe | 123 | * | 
| denis2nis | 1:a9ba9cf928fe | 124 | */ | 
| denis2nis | 0:7556356a8bcd | 125 | void _ReadCallback(); | 
| denis2nis | 0:7556356a8bcd | 126 | |
| denis2nis | 0:7556356a8bcd | 127 | char _chksm; | 
| denis2nis | 0:7556356a8bcd | 128 | |
| denis2nis | 0:7556356a8bcd | 129 | private: | 
| denis2nis | 0:7556356a8bcd | 130 | |
| denis2nis | 0:7556356a8bcd | 131 | UnbufferedSerial _mx12; | 
| denis2nis | 0:7556356a8bcd | 132 | MX12::Status _status; | 
| denis2nis | 0:7556356a8bcd | 133 | char _res[MX12_ANSWER_MAX_SIZE]; | 
| denis2nis | 0:7556356a8bcd | 134 | char _res_count; | 
| denis2nis | 0:7556356a8bcd | 135 | char _len; | 
| denis2nis | 0:7556356a8bcd | 136 | MX12::State _state; | 
| denis2nis | 0:7556356a8bcd | 137 | float _angle[MX12_MOTOR_COUNT]; | 
| denis2nis | 0:7556356a8bcd | 138 | int _baud; | 
| denis2nis | 0:7556356a8bcd | 139 | }; | 
| denis2nis | 0:7556356a8bcd | 140 | |
| denis2nis | 0:7556356a8bcd | 141 | #endif /* MBED_MX12_H */ |