-
Notifications
You must be signed in to change notification settings - Fork 17
/
damiao_wrist.cpp
93 lines (71 loc) · 2.78 KB
/
damiao_wrist.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
#include "damiao_mvp/damiao.h"
#include <cmath>
#include <thread> // 包含 <thread> 标头以使用 std::this_thread::sleep_for
#include <csignal>
// ros msg
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Float64MultiArray.h>
float left_wrist_q = 0;
float right_wrist_q = 0;
bool g_shutdown_requested = false;
// Signal handler for Ctrl+C
void signalHandler(int signum)
{
if (signum == SIGINT)
{
ROS_INFO("Ctrl+C received. Shutting down gracefully...");
g_shutdown_requested = true;
}
}
void h1_damiao_callback(const std_msgs::Float64MultiArray::ConstPtr& msg)
{
// ROS_INFO("I heard: [%s]", msg->data.c_str());
std::cout << "I heard: " << msg->data[0] << msg->data[1] << std::endl;
// std::cout << "I heard: " << msg->data[0] << " " << msg->data[1] << " " << msg->data[2] << " " << msg->data[3] << " " << msg->data[4] << std::endl;
left_wrist_q = msg->data[0];
right_wrist_q = msg->data[1];
}
int cnt = 0;
int main(int argc , char** argv)
{
ros::init(argc, argv, "damiao_command");
// Set up the signal handler
signal(SIGINT, signalHandler);
ros::NodeHandle n;
ros::Subscriber h1_damiao_subscriber = n.subscribe("damiao_command", 1, h1_damiao_callback);
auto serial = std::make_shared<SerialPort>("/dev/ttyACM0", B921600);
// auto serial = std::make_shared<SerialPort>("/dev/ttyACM0", B1000000);
auto dm = std::make_shared<damiao::Motor>(serial);
dm->addMotor(0x01, 0x00); // default id
dm->addMotor(0x02, 0x00); // default id
sleep(0.5);
dm->enable(0x01);
sleep(0.5);
dm->enable(0x02);
while (ros::ok() && !g_shutdown_requested)
{
ros::spinOnce();
float q = sin(std::chrono::system_clock::now().time_since_epoch().count() / 1e9);
// dm->control(0x01, 0, 0, 0, 0, 0);
// dm->control(0x02, 0, 0, 0, 0, 0);
// std::cout << q << std::endl;
// dm->control(0x01, 7.5, 0.4, left_wrist_q, 0, 0);
// // dm->control(0x02, 5, 0.3, q*12, 0, 0);
// std::this_thread::sleep_for(std::chrono::microseconds(500));
// dm->control(0x01, 7.5, 0.4, left_wrist_q, 0, 0);
dm->control(0x01, 5, 0.4, -1.5*right_wrist_q, 0, 0);
dm->control(0x02, 5, 0.4, 1.5*left_wrist_q, 0, 0);
std::cout << "sending command to left wrist: " << left_wrist_q << std::endl;
std::cout << "sending command to right wrist: " << right_wrist_q << std::endl;
auto & m1 = dm->motors[0x01];
std::cout << "m1: " << m1->state.q << " " << m1->state.dq << " " << m1->state.tau << std::endl;
std::this_thread::sleep_for(std::chrono::microseconds(500));
auto & m2 = dm->motors[0x02];
std::cout << "m2: " << m2->state.q << " " << m2->state.dq << " " << m2->state.tau << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
cnt++;
std::cout << "cnt: " << cnt << std::endl;
}
return 0;
}