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.
Dependents: PSL_ROBOT_lorenzo robot_lorenzo recepteur_mbed_os_6
MX12.h@0:7556356a8bcd, 2021-11-03 (annotated)
- Committer:
- denis2nis
- Date:
- Wed Nov 03 11:17:12 2021 +0000
- Revision:
- 0:7556356a8bcd
- Child:
- 1:a9ba9cf928fe
Initial commit
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 | 0:7556356a8bcd | 4 | /** Class to communicate with Dynamixel MX12 servomotors. |
denis2nis | 0:7556356a8bcd | 5 | * |
denis2nis | 0:7556356a8bcd | 6 | * |
denis2nis | 0:7556356a8bcd | 7 | */ |
denis2nis | 0:7556356a8bcd | 8 | |
denis2nis | 0:7556356a8bcd | 9 | #include "mbed.h" |
denis2nis | 0:7556356a8bcd | 10 | |
denis2nis | 0:7556356a8bcd | 11 | #define MX12_ANSWER_MAX_SIZE 32 |
denis2nis | 0:7556356a8bcd | 12 | #define MX12_MOTOR_COUNT 16 |
denis2nis | 0:7556356a8bcd | 13 | |
denis2nis | 0:7556356a8bcd | 14 | class MX12 |
denis2nis | 0:7556356a8bcd | 15 | { |
denis2nis | 0:7556356a8bcd | 16 | public: |
denis2nis | 0:7556356a8bcd | 17 | |
denis2nis | 0:7556356a8bcd | 18 | enum Status { |
denis2nis | 0:7556356a8bcd | 19 | InstructionError, |
denis2nis | 0:7556356a8bcd | 20 | OverloadError, |
denis2nis | 0:7556356a8bcd | 21 | ChecksumError, |
denis2nis | 0:7556356a8bcd | 22 | RangeError, |
denis2nis | 0:7556356a8bcd | 23 | OverheatingError, |
denis2nis | 0:7556356a8bcd | 24 | AngleLimitError, |
denis2nis | 0:7556356a8bcd | 25 | InputVoltageError, |
denis2nis | 0:7556356a8bcd | 26 | Unknown, |
denis2nis | 0:7556356a8bcd | 27 | Ok |
denis2nis | 0:7556356a8bcd | 28 | }; |
denis2nis | 0:7556356a8bcd | 29 | |
denis2nis | 0:7556356a8bcd | 30 | enum State { |
denis2nis | 0:7556356a8bcd | 31 | ReadingPosition, |
denis2nis | 0:7556356a8bcd | 32 | Writing, |
denis2nis | 0:7556356a8bcd | 33 | Available, |
denis2nis | 0:7556356a8bcd | 34 | }; |
denis2nis | 0:7556356a8bcd | 35 | |
denis2nis | 0:7556356a8bcd | 36 | /** Create MX12 instance |
denis2nis | 0:7556356a8bcd | 37 | * |
denis2nis | 0:7556356a8bcd | 38 | * @param tx board pin used for transmission in UART daisy chain link |
denis2nis | 0:7556356a8bcd | 39 | * to servomotors |
denis2nis | 0:7556356a8bcd | 40 | * @param rx board pin used for reception in UART daisy chain link |
denis2nis | 0:7556356a8bcd | 41 | * to servomotors |
denis2nis | 0:7556356a8bcd | 42 | * @param baud modulation rate of UART signal, unit: Baud |
denis2nis | 0:7556356a8bcd | 43 | * to servomotors |
denis2nis | 0:7556356a8bcd | 44 | */ |
denis2nis | 0:7556356a8bcd | 45 | MX12(PinName tx, PinName rx, int baud=115200); |
denis2nis | 0:7556356a8bcd | 46 | |
denis2nis | 0:7556356a8bcd | 47 | /** Send desired speed to a specifc servomotor |
denis2nis | 0:7556356a8bcd | 48 | * |
denis2nis | 0:7556356a8bcd | 49 | * @param mot_id a unique value in the daisy chain network to identify |
denis2nis | 0:7556356a8bcd | 50 | * each servomotor |
denis2nis | 0:7556356a8bcd | 51 | * @param speed a float between -1 and 1 that represents the percentage |
denis2nis | 0:7556356a8bcd | 52 | * of maximum requested speed |
denis2nis | 0:7556356a8bcd | 53 | */ |
denis2nis | 0:7556356a8bcd | 54 | void SetSpeed(unsigned char mot_id, float speed); |
denis2nis | 0:7556356a8bcd | 55 | |
denis2nis | 0:7556356a8bcd | 56 | char IsAvailable(void); |
denis2nis | 0:7556356a8bcd | 57 | |
denis2nis | 0:7556356a8bcd | 58 | MX12::Status GetStatus(void); |
denis2nis | 0:7556356a8bcd | 59 | |
denis2nis | 0:7556356a8bcd | 60 | void ReadPosition(unsigned char mot_id); |
denis2nis | 0:7556356a8bcd | 61 | |
denis2nis | 0:7556356a8bcd | 62 | float GetPosition(unsigned char mot_id); |
denis2nis | 0:7556356a8bcd | 63 | |
denis2nis | 0:7556356a8bcd | 64 | void PrintAnswer(); |
denis2nis | 0:7556356a8bcd | 65 | |
denis2nis | 0:7556356a8bcd | 66 | void rw(unsigned char mot_id, char adress, char len, char *data); |
denis2nis | 0:7556356a8bcd | 67 | |
denis2nis | 0:7556356a8bcd | 68 | void _ReadCallback(); |
denis2nis | 0:7556356a8bcd | 69 | |
denis2nis | 0:7556356a8bcd | 70 | char _chksm; |
denis2nis | 0:7556356a8bcd | 71 | |
denis2nis | 0:7556356a8bcd | 72 | private: |
denis2nis | 0:7556356a8bcd | 73 | |
denis2nis | 0:7556356a8bcd | 74 | UnbufferedSerial _mx12; |
denis2nis | 0:7556356a8bcd | 75 | MX12::Status _status; |
denis2nis | 0:7556356a8bcd | 76 | char _res[MX12_ANSWER_MAX_SIZE]; |
denis2nis | 0:7556356a8bcd | 77 | char _res_count; |
denis2nis | 0:7556356a8bcd | 78 | char _len; |
denis2nis | 0:7556356a8bcd | 79 | MX12::State _state; |
denis2nis | 0:7556356a8bcd | 80 | float _angle[MX12_MOTOR_COUNT]; |
denis2nis | 0:7556356a8bcd | 81 | int _baud; |
denis2nis | 0:7556356a8bcd | 82 | }; |
denis2nis | 0:7556356a8bcd | 83 | |
denis2nis | 0:7556356a8bcd | 84 | #endif /* MBED_MX12_H */ |