-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
128 lines (113 loc) · 2.97 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <string>
#include <iostream>
#include "dynamixel/Dynamixel.h"
int main(int argc, char** argv) {
bool serialFeedback = false;
int motorId = 1;
int numBytes = 1;
int iData = 2;
std::string command = "Get";
std::string address = "Position";
std::string motorType = "MX28";
std::string portName = "/dev/ttyO5";
int baudRate = 9600;
// motor objects
Dynamixel* motor;
AX12 ax12;
MX28 mx28;
std::vector<byte> data;
std::vector<byte> recvData;
// parse command line args
for (int i=1; i<argc; i++) {
if (!strcmp(argv[i],"--baudRate")) {
baudRate = atoi(argv[++i]);
}
else if (!strcmp(argv[i],"--serialFeedback")) {
serialFeedback = true;
}
else if (!strcmp(argv[i],"--motorId")) {
motorId = atoi(argv[++i]);
}
else if (!strcmp(argv[i],"--numBytes")) {
numBytes = atoi(argv[++i]);
}
else if (!strcmp(argv[i],"--command")) {
command = argv[++i];
}
else if (!strcmp(argv[i],"--address")) {
address = argv[++i];
}
else if (!strcmp(argv[i],"--portName")) {
portName = argv[++i];
}
else if (!strcmp(argv[i],"--motorType")) {
motorType = argv[++i];
}
else if (!strcmp(argv[i],"--data")) {
iData = std::strtoul(argv[++i], 0, 10);
}
}
if (numBytes == 1) {
data.push_back(iData);
}
else if (numBytes == 2) {
byte h, l;
Utils::ConvertToHL(iData, &h, &l);
data.push_back(l);
data.push_back(h);
}
SerialPort port;
std::cout << "Connecting to: " <<
portName << ":" << baudRate << std::endl;
if (port.connect((char *)portName.c_str(), baudRate) != 0) {
std::cout << "Success\n";
// configure the motor objects
if (motorType == "AX12") {
ax12 = AX12(motorId, &port);
motor = new AX12(motorId, &port);
}
else if (motorType == "MX28") {
mx28 = MX28(motorId, &port);
motor = new MX28(motorId, &port);
}
else {
std::cout << "Error: motor type not supported!\n";
return -1;
}
motor->Configure();
motor->SetSerialFeedback(serialFeedback);
// For debugging only:
byte buffer[1024];
int length = motor->
FormatCommand(motor->GetCommand(command),
motor->GetAddress(address),
data,
buffer);
std::cout<< "buffer: " <<
Utils::PrintBuffer(buffer, length) << std::endl;
// end for debugging
int retVal;
retVal = motor->SendReceiveCommand(command,
address,
data,
&recvData);
int recvVal = 0;
if (recvData.size() == 1) {
recvVal = recvData[0];
}
else if (recvData.size() == 2) {
recvVal = Utils::ConvertFromHL(recvData[0],recvData[1]);
}
std::cout << "received: " <<
retVal << " : " << recvVal << std::endl;
std::cout << "position: " <<
motor->getPosition() << std::endl;
}
else {
std::cout << "Couldn't open " <<
portName << " at baudRate " <<
baudRate << std::endl;
return -1;
}
return 0;
}