-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdxl_loopback.c
117 lines (106 loc) · 2.88 KB
/
dxl_loopback.c
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
#include <dirent.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "communications.h"
#include "high_level_commands.h"
bool KILL_SIGNAL = false;
void KillSwitch() {
KILL_SIGNAL = true;
bool result = TorqueDisable(BROADCAST_ID);
}
void PositionLoopback(int usb2ax_index, uint8_t master_id, uint8_t slave_id) {
int baud_num = 3;
if (InitDXL(usb2ax_index, baud_num) == 0) {
printf("Failed to open device: %d, baud num: %d\n", usb2ax_index, baud_num);
TerminateDXL();
return;
}
bool result = TorqueEnable(slave_id);
uint16_t pos;
while (!KILL_SIGNAL) {
result = ReadPosition(master_id, &pos);
if (result) {
result = SetPosition(slave_id, pos);
}
usleep(1000);
}
TorqueDisable(slave_id);
TerminateDXL();
}
// Enables torque, centers servo, waits until movement stops, disables torque.
void CenterPosition(int usb2ax_index, int dxl_id) {
int baud_num = 3;
if (InitDXL(usb2ax_index, baud_num) == 0) {
printf("Failed to open device: %d, baud num: %d\n", usb2ax_index, baud_num);
TerminateDXL();
return;
}
bool result = TorqueEnable(dxl_id);
bool moving = false;
if (result) {
printf("Enabled torque\n");
result = SetPosition(dxl_id, 512);
moving = true;
if (result) {
printf("Set position\n");
while (moving) {
usleep(10000);
ReadMovingStatus(dxl_id, &moving);
}
printf("Done moving\n");
}
}
result = TorqueDisable(dxl_id);
if (result) {
printf("Disabled torque\n");
}
TerminateDXL();
}
void ListDevices() {
DIR* dir;
struct dirent* ent;
char* devices_dir = "/dev/";
char* expected_prefix = "ttyACM";
int i;
if ((dir = opendir(devices_dir)) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strlen(expected_prefix) <= strlen(ent->d_name) &&
memcmp(ent->d_name, expected_prefix, strlen(expected_prefix)) == 0) {
printf("%s\n", ent->d_name);
}
}
closedir(dir);
} else {
printf("Couldn't find device directory: %s\n", devices_dir);
}
}
int main(int argc, char* argv[]) {
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = KillSwitch;
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
int device_num = 0; // Default to device ID 0
if (argc >= 2) {
char* end;
long value = strtol(argv[1], &end, 10);
if (!(end == argv[1] || *end != '\0' || errno == ERANGE)) {
// PositionLoopback(value, 1, 2);
CenterPosition(value, 1);
} else {
printf("Arg 1 (%s) was not recognized as an integer device number.\n",
argv[1]);
}
} else {
printf("Enter USB2AX ttyACM* number as argument 1!\n");
printf("Here are some possible device ID's from /dev/*.\n");
ListDevices();
}
return 0;
}