-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.cpp
More file actions
123 lines (107 loc) · 4.88 KB
/
Copy pathtest.cpp
File metadata and controls
123 lines (107 loc) · 4.88 KB
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
// L10 / CAN / 左手 —— 读取版本、力矩、速度、状态与触觉(带 TX/RX 日志)
#include <array>
#include <cstdint>
#include "_win_console_utf8.h"
#include "LinkerHandApi.h"
#include "CommFactory.h"
// 格式化当前时间,用于 TX/RX 日志
std::string getCurrentTime() {
auto now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()) % 1000;
std::tm* tm = std::localtime(&now_time);
char buffer[80];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm);
std::ostringstream oss;
oss << buffer << "." << std::setfill('0') << std::setw(3) << now_ms.count();
return oss.str();
}
int main() {
try {
// 初始化灵巧手
LinkerHandApi hand(LINKER_HAND::L10, HAND_TYPE::LEFT);
// 创建 CAN 总线对象
std::shared_ptr<Communication::ICanBus> bus = Communication::CommFactory::createCanBus("can0", 1000000);
hand.setCanTxCallback([bus](uint32_t can_id, const uint8_t *data, uintptr_t data_len) -> int32_t {
static int tx_count = 0;
tx_count++;
std::cout << "\033[32m[TX #" << tx_count << "]\033[0m "
<< getCurrentTime()
<< " | CAN_ID: 0x" << std::hex << std::setw(8) << std::setfill('0') << can_id
<< std::dec << " (" << can_id << ")"
<< " | Len: " << data_len
<< " | Data: ";
for (uintptr_t i = 0; i < data_len; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)data[i] << " ";
}
std::cout << std::dec << std::endl;
std::vector<uint8_t> data_vec(data, data + data_len);
bus->send(data_vec, can_id);
return 0;
});
hand.setCanRxCallback([bus](uint32_t* can_id_out, uint8_t* data_out, uint8_t* len_out) -> int32_t {
auto frame = bus->recv();
if (frame.can_id == 0 && frame.can_dlc == 0) {
return -1;
}
*can_id_out = frame.can_id;
*len_out = frame.can_dlc;
memcpy(data_out, frame.data, frame.can_dlc);
static int rx_count = 0;
rx_count++;
std::cout << "\033[34m[RX #" << rx_count << "]\033[0m "
<< getCurrentTime()
<< " | CAN_ID: 0x" << std::hex << std::setw(8) << std::setfill('0') << frame.can_id
<< std::dec << " (" << frame.can_id << ")"
<< " | DLC: " << (int)frame.can_dlc
<< " | Data: ";
for (int i = 0; i < frame.can_dlc; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)frame.data[i] << " ";
}
std::cout << std::dec << std::endl;
return 0;
});
for (size_t i = 0; i < 3; ++i) {
std::cout << hand.getVersion() << std::endl;
hand.setTorque({200,200,200,200,200,200,200,200,200,200});
hand.setSpeed({200,200,200,200,200,200,200,200,200,200});
std::vector<uint8_t> torque = hand.getTorque();
std::cout << "torque size:" << torque.size() << std::endl;
for (size_t j = 0; j < torque.size(); j++) {
std::cout << (int)torque[j] << " ";
}
std::cout << std::endl;
std::vector<uint8_t> speed = hand.getSpeed();
std::cout << "speed size:" << speed.size() << std::endl;
for (size_t j = 0; j < speed.size(); j++) {
std::cout << (int)speed[j] << " ";
}
std::cout << std::endl;
std::vector<uint8_t> state = hand.getPosition();
std::cout << "state size:" << state.size() << std::endl;
for (size_t j = 0; j < state.size(); j++) {
std::cout << (int)state[j] << " ";
}
std::cout << std::endl;
std::vector<std::vector<std::vector<uint8_t>>> touch_mats = hand.getForce();
// 手指名称表,顺序对应 touch_mats[0..4]
const std::array<const char*, 5> finger_name = {
"THUMB_TOUCH", "INDEX_TOUCH", "MIDDLE_TOUCH", "RING_TOUCH", "LITTLE_TOUCH"
};
for (size_t n = 0; n < touch_mats.size(); ++n) {
std::cout << finger_name[n] << ":\n";
for (const auto &row : touch_mats[n]) {
for (uint8_t val : row)
std::cout << std::setw(2) << static_cast<int>(val) << ' ';
std::cout << '\n';
}
std::cout << '\n';
}
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}