-
Notifications
You must be signed in to change notification settings - Fork 14
/
debug.cpp
218 lines (200 loc) · 6.4 KB
/
debug.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <signal.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "VREPClient.hpp"
/**
* VREPClient instance
* Globale variable
*/
static VREPClient VREP;
using namespace std;
static void print_image(unsigned char* image, int* resolution)
{
FILE* f = fopen("cam.ppm","w");
fprintf(f,"P3\n%d %d\n255\n",resolution[0],resolution[1]);
for(int i=resolution[0]-1; i>=0; i--)
{
for(int j=0; j<resolution[1]; j++)
{
int index = 3*(i * resolution[1] + j);
fprintf(f,"%hhu %hhu %hhu ", image[index], image[index+1], image[index+2]);
}
fprintf(f,"\n");
}
fclose(f);
return;
}
static void exiting(bool success = true)
{
//Close server connection
VREP.stop();
VREP.disconnect();
if (success) {
exit(EXIT_SUCCESS);
} else {
exit(EXIT_FAILURE);
}
}
static void signal_handler(int sig, siginfo_t *siginfo, void *context)
{
cout << endl << "Exiting..." << endl;
exiting();
}
static void attachSignalHandler()
{
struct sigaction action;
bzero(&action, sizeof(action));
action.sa_sigaction = &signal_handler;
action.sa_flags = SA_SIGINFO;
if (sigaction(SIGINT, &action, NULL) < 0) {
cerr << "Unable to register signal handler" << endl;
exit(EXIT_FAILURE);
}
}
static void displayInitialState()
{
//Display founded motors
size_t countMotors = VREP.countMotors();
cout << "Registered motors: " << countMotors << endl;
for (size_t i=0;i<countMotors;i++) {
cout << "[" << i << "] ";
cout << VREP.getMotor(i).getName() << " ";
cout << "minPos=" << VREP.getMotor(i).getMinPos() << " ";
cout << "maxPos=" << VREP.getMotor(i).getMaxPos() << " ";
cout << "TorqueMax=" << VREP.getMotor(i).getTorqueMax() << endl;
}
//And force sensors
size_t countForceSensors = VREP.countForceSensors();
cout << "Registered force sensors: " << countForceSensors << endl;
for (size_t i=0;i<countForceSensors;i++) {
cout << "[" << i << "] ";
cout << VREP.getForceSensor(i).getName() << endl;
}
//And vision sensors
size_t countVisionSensors = VREP.countVisionSensors();
cout << "Registered vision sensors: " << countVisionSensors << endl;
for (size_t i=0;i<countVisionSensors;i++) {
cout << "[" << i << "] ";
cout << VREP.getVisionSensor(i).getName() << endl;
}
}
static void displayState()
{
size_t countMotors = VREP.countMotors();
//Display motor states
for (size_t i=0;i<countMotors;i++) {
cout << " #[" << i << "] ";
cout << VREP.getMotor(i).getName() << " ";
cout << "pos=" << VREP.getMotor(i).readPos() << " ";
cout << "torque=" << VREP.getMotor(i).readTorque() << endl;
}
size_t countForceSensors = VREP.countForceSensors();
//Display force sensors value
for (size_t i=0;i<countForceSensors;i++) {
cout << " *[" << i << "] ";
cout << VREP.getForceSensor(i).getName() << " ";
cout << "force=" << VREP.getForceSensor(i).readForceNorm() << " ";
cout << "torque=" << VREP.getForceSensor(i).readTorqueNorm() << endl;
}
size_t countVisionSensors = VREP.countVisionSensors();
//Display vision sensors value
for (size_t i=0;i<countVisionSensors;i++) {
cout << " *[" << i << "] ";
cout << VREP.getVisionSensor(i).getName() << " ";
}
//Display accelerometer sensor
cout << " - Accelerometer ";
cout << "X=" << VREP.readAccelerometerX() << " ";
cout << "Y=" << VREP.readAccelerometerY() << " ";
cout << "Z=" << VREP.readAccelerometerZ() << endl;
//Display position tracker
cout << " - Position tracker ";
cout << "X=" << VREP.readPositionTrackerX() << " ";
cout << "Y=" << VREP.readPositionTrackerY() << " ";
cout << "Z=" << VREP.readPositionTrackerZ() << endl;
}
double PI = 3.14159265359;
double sinParams[20][3] = {
//phase, amplitude, offset
{0, 0, 0}, // Hanche D 1
{0, 0.2, -0.2}, // Hanche D 2
{0, 0, 0}, // Cuisse D
{0, 0, 0}, // Genou D
//{PI/2, 0.2, 0.2}, // Cheville D
{0, -0.2, 0.2}, // Cheville D
{0, 0, 0}, // Pied D
{0, 0, 0}, // Hanche G 1
{0, 0, 0}, // Hanche G 2
{0, -0.8, 0.8}, // Cuisse G
{0, 0.8, -0.8}, // Genou G
{0, 0, 0}, // Cheville G
{0, -0.1, 0.1}, // Pied G
{0, 0, 0}, // Epaule D
{0, -1, -1}, // Bras D
{0, 0, 0}, // Coude D
{0, 0, 0}, // Epaule G
{0, 0, 1.57}, // Bras G
{0, 0, 0}, // Coude G
{0, 0, 0}, // Cou
{0, 0, 0} // Tete
};
static void moveMotorsStep(double t)
{
int n = 20;
for (size_t i = 0; i < n; i++) {
double phase = sinParams[i][0];
double amplitude = sinParams[i][1];
double offset = sinParams[i][2];
double pos = amplitude * sin(t + phase) + offset;
VREP.getMotor(i).writePos(pos);
}
}
int main(int argc, char* argv[])
{
//Network parameters
int port = 0;
char* ip = NULL;
//Parse input arguments
if (argc != 3) {
cerr << "Bad usage. Usage: ./command [ip address] [port number]" << endl;
cerr << "Provide network parameters to connect to V-REP server" << endl;
return EXIT_FAILURE;
} else {
ip = argv[1];
port = atoi(argv[2]);
}
//Signal attaching
attachSignalHandler();
try {
//Connection to V-REP
cout << "Connecting to V-REP server " << ip << ":" << port << endl;
VREP.connect(ip, port);
//Display initial state
displayInitialState();
//Main Loop
cout << "Starting simulation" << endl;
VREP.start();
for (double t=0;t<60.0;t+=0.050) {
//Display state
cout << "Simulation step t=" << t << endl;
displayState();
//Do next step
VREP.nextStep();
//Compute motors move
moveMotorsStep(t);
print_image(VREP.getVisionSensor(0).getImage(),
VREP.getVisionSensor(0).getResolution());
}
//End simulation
cout << "Stopping simulation" << endl;
VREP.stop();
} catch (string str) {
cerr << "Exception error: " << str << endl;
exiting(false);
}
exiting();
}