Skip to content

Commit 9535c0a

Browse files
committed
first commit
0 parents  commit 9535c0a

File tree

12 files changed

+497
-0
lines changed

12 files changed

+497
-0
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.10.2)
2+
project(UnitreeMotorSDK_M80106)
3+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3")
4+
5+
include_directories("include/")
6+
link_directories(
7+
lib/
8+
)
9+
10+
#example
11+
add_executable(motorctrl example/main.cpp)
12+
target_link_libraries(motorctrl libUnitreeMotorSDK_M80106_Linux64.so)

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2016-2022 HangZhou YuShu TECHNOLOGY CO.,LTD. ("Unitree Robotics")
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# README.md
2+
3+
### Notice
4+
5+
support motor: GO-M8010-6 motor
6+
7+
not support motor: A1 motor、 B1 motor (Check A1B1 branch for support)
8+
9+
### Build
10+
```bash
11+
mkdir build
12+
cd build
13+
cmake ..
14+
make
15+
```
16+
17+
### Run
18+
Run examples with 'sudo',e.g.
19+
```bash
20+
sudo ./motorctrl
21+
```

example/main.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "serialPort/SerialPort.h"
2+
#include <unistd.h>
3+
4+
int main() {
5+
6+
SerialPort serial("/dev/ttyUSB0");
7+
MotorCmd cmd;
8+
MotorData data;
9+
10+
while(true) {
11+
cmd.motorType = MotorType::GO_M8010_6;
12+
cmd.id = 0;
13+
cmd.mode = 1;
14+
cmd.K_P = 0.0;
15+
cmd.K_W = 0.05;
16+
cmd.Pos = 0.0;
17+
cmd.W = 6.28*6.33;
18+
cmd.T = 0.0;
19+
serial.sendRecv(&cmd,&data);
20+
21+
std::cout << std::endl;
22+
std::cout << "motor.Pos: " << data.Pos << std::endl;
23+
std::cout << "motor.Temp: " << data.Temp << std::endl;
24+
std::cout << "motor.W: " << data.W << std::endl;
25+
std::cout << "motor.T: " << data.T << std::endl;
26+
std::cout << "motor.MError: " << data.MError << std::endl;
27+
std::cout << std::endl;
28+
29+
usleep(200);
30+
31+
}
32+
33+
}

include/IOPort/IOPort.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef __IOPORT_H
2+
#define __IOPORT_H
3+
4+
#include <stdint.h>
5+
#include <unistd.h>
6+
#include <vector>
7+
#include "unitreeMotor/unitreeMotor.h"
8+
9+
enum class BlockYN{
10+
YES,
11+
NO
12+
};
13+
14+
class IOPort{
15+
public:
16+
IOPort(BlockYN blockYN, size_t recvLength, size_t timeOutUs){
17+
resetIO(blockYN, recvLength, timeOutUs);
18+
}
19+
virtual ~IOPort(){}
20+
virtual size_t send(uint8_t *sendMsg, size_t sendLength) = 0;
21+
virtual size_t recv(uint8_t *recvMsg, size_t recvLength) = 0;
22+
virtual size_t recv(uint8_t *recvMsg) = 0;
23+
virtual bool sendRecv(std::vector<MotorCmd> &sendVec, std::vector<MotorData> &recvVec) = 0;
24+
void resetIO(BlockYN blockYN, size_t recvLength, size_t timeOutUs);
25+
protected:
26+
BlockYN _blockYN = BlockYN::NO;
27+
size_t _recvLength;
28+
timeval _timeout;
29+
timeval _timeoutSaved;
30+
};
31+
32+
inline void IOPort::resetIO(BlockYN blockYN, size_t recvLength, size_t timeOutUs){
33+
_blockYN = blockYN;
34+
_recvLength = recvLength;
35+
_timeout.tv_sec = timeOutUs / 1000000;
36+
_timeout.tv_usec = timeOutUs % 1000000;
37+
_timeoutSaved = _timeout;
38+
}
39+
40+
41+
#endif // z1_lib_IOPORT_H

include/crc/crc_ccitt.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#ifndef __CRC_CCITT_H
2+
#define __CRC_CCITT_H
3+
4+
/*
5+
* This mysterious table is just the CRC of each possible byte. It can be
6+
* computed using the standard bit-at-a-time methods. The polynomial can
7+
* be seen in entry 128, 0x8408. This corresponds to x^0 + x^5 + x^12.
8+
* Add the implicit x^16, and you have the standard CRC-CCITT.
9+
* https://github.com/torvalds/linux/blob/5bfc75d92efd494db37f5c4c173d3639d4772966/lib/crc-ccitt.c
10+
*/
11+
uint16_t const crc_ccitt_table[256] = {
12+
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
13+
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
14+
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
15+
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
16+
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
17+
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
18+
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
19+
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
20+
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
21+
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
22+
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
23+
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
24+
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
25+
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
26+
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
27+
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
28+
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
29+
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
30+
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
31+
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
32+
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
33+
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
34+
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
35+
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
36+
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
37+
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
38+
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
39+
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
40+
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
41+
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
42+
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
43+
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
44+
};
45+
46+
47+
static uint16_t crc_ccitt_byte(uint16_t crc, const uint8_t c)
48+
{
49+
return (crc >> 8) ^ crc_ccitt_table[(crc ^ c) & 0xff];
50+
}
51+
52+
/**
53+
* crc_ccitt - recompute the CRC (CRC-CCITT variant) for the data
54+
* buffer
55+
* @crc: previous CRC value
56+
* @buffer: data pointer
57+
* @len: number of bytes in the buffer
58+
*/
59+
inline uint16_t crc_ccitt(uint16_t crc, uint8_t const *buffer, size_t len)
60+
{
61+
while (len--)
62+
crc = crc_ccitt_byte(crc, *buffer++);
63+
return crc;
64+
}
65+
66+
67+
#endif

include/serialPort/SerialPort.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef __SERIALPORT_H
2+
#define __SERIALPORT_H
3+
4+
/*
5+
High frequency serial communication,
6+
Not that common, but useful for motor communication.
7+
*/
8+
#include <termios.h>
9+
#include <sys/select.h>
10+
#include <string>
11+
#include <string.h>
12+
#include <stdint.h>
13+
#include <fcntl.h>
14+
#include <sys/ioctl.h>
15+
#include <linux/serial.h>
16+
#include <unistd.h>
17+
#include <iostream>
18+
19+
#include "serialPort/include/errorClass.h"
20+
#include "unitreeMotor/unitreeMotor.h"
21+
#include "IOPort/IOPort.h"
22+
23+
enum class bytesize_t{
24+
fivebits,
25+
sixbits,
26+
sevenbits,
27+
eightbits
28+
};
29+
30+
enum class parity_t{
31+
parity_none,
32+
parity_odd,
33+
parity_even,
34+
parity_mark,
35+
parity_space
36+
};
37+
38+
enum class stopbits_t{
39+
stopbits_one,
40+
stopbits_two,
41+
stopbits_one_point_five
42+
};
43+
44+
enum class flowcontrol_t{
45+
flowcontrol_none,
46+
flowcontrol_software,
47+
flowcontrol_hardware
48+
};
49+
50+
class SerialPort : public IOPort{
51+
public:
52+
SerialPort(const std::string &portName,
53+
size_t recvLength = 16,
54+
uint32_t baudrate = 4000000,
55+
size_t timeOutUs = 20000,
56+
BlockYN blockYN = BlockYN::NO,
57+
bytesize_t bytesize = bytesize_t::eightbits,
58+
parity_t parity = parity_t::parity_none,
59+
stopbits_t stopbits = stopbits_t::stopbits_one,
60+
flowcontrol_t flowcontrol = flowcontrol_t::flowcontrol_none);
61+
virtual ~SerialPort();
62+
void resetSerial(size_t recvLength = 16,
63+
uint32_t baudrate = 4000000,
64+
size_t timeOutUs = 20000,
65+
BlockYN blockYN = BlockYN::NO,
66+
bytesize_t bytesize = bytesize_t::eightbits,
67+
parity_t parity = parity_t::parity_none,
68+
stopbits_t stopbits = stopbits_t::stopbits_one,
69+
flowcontrol_t flowcontrol = flowcontrol_t::flowcontrol_none);
70+
size_t send(uint8_t *sendMsg, size_t sendLength);
71+
size_t recv(uint8_t *recvMsg, size_t recvLength);
72+
size_t recv(uint8_t *recvMsg);
73+
bool sendRecv(uint8_t *sendMsg, uint8_t *recvMsg, size_t sendLength);
74+
bool sendRecv(MotorCmd* sendMsg, MotorData* recvMsg);
75+
bool sendRecv(std::vector<MotorCmd> &sendVec, std::vector<MotorData> &recvVec);
76+
77+
private:
78+
void _open();
79+
void _set();
80+
void _close();
81+
size_t _nonBlockRecv(uint8_t *recvMsg, size_t readLen);
82+
std::string _portName;
83+
uint32_t _baudrate;
84+
bytesize_t _bytesize;
85+
parity_t _parity;
86+
stopbits_t _stopbits;
87+
flowcontrol_t _flowcontrol;
88+
bool _xonxoff;
89+
bool _rtscts;
90+
int _fd;
91+
fd_set _rSet;
92+
93+
};
94+
95+
96+
97+
98+
#endif // SERIALPORT_H
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef __ERRORCLASS_H
2+
#define __ERRORCLASS_H
3+
4+
#include <string>
5+
#include <exception>
6+
#include <stdexcept>
7+
#include <sstream>
8+
#include <cstring>
9+
10+
#define THROW(exceptionClass, message) throw exceptionClass(__FILE__, \
11+
__LINE__, (message) )
12+
13+
class IOException : public std::exception
14+
{
15+
// Disable copy constructors
16+
IOException& operator=(const IOException&);
17+
std::string file_;
18+
int line_;
19+
std::string e_what_;
20+
int errno_;
21+
public:
22+
explicit IOException (std::string file, int line, int errnum)
23+
: file_(file), line_(line), errno_(errnum) {
24+
std::stringstream ss;
25+
#if defined(_WIN32) && !defined(__MINGW32__)
26+
char error_str [1024];
27+
strerror_s(error_str, 1024, errnum);
28+
#else
29+
char * error_str = strerror(errnum);
30+
#endif
31+
ss << "IO Exception (" << errno_ << "): " << error_str;
32+
ss << ", file " << file_ << ", line " << line_ << ".";
33+
e_what_ = ss.str();
34+
}
35+
explicit IOException (std::string file, int line, const char * description)
36+
: file_(file), line_(line), errno_(0) {
37+
std::stringstream ss;
38+
ss << "IO Exception: " << description;
39+
ss << ", file " << file_ << ", line " << line_ << ".";
40+
e_what_ = ss.str();
41+
}
42+
virtual ~IOException() throw() {}
43+
IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
44+
45+
int getErrorNumber () const { return errno_; }
46+
47+
virtual const char* what () const throw () {
48+
return e_what_.c_str();
49+
}
50+
};
51+
52+
#endif // ERRORCLASS_H

0 commit comments

Comments
 (0)