-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_mort.cpp
More file actions
325 lines (280 loc) · 12.2 KB
/
debug_mort.cpp
File metadata and controls
325 lines (280 loc) · 12.2 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
******************************************************************************
* @file debug_mort.cpp
* @author mortamar@andrew.cmu.edu
* @version 3.0
* @date January-2025
* @brief Jan 2022:Changed communication structure to be compatible with Keil Studio.
Debug functions live in this file and also the base communication management functions.
Jan 2025: had to change com structure again, for some reason now only RawSerial works.
Probably deprecating libraries...
Also had to increase the baud rate to 115200 for the graphics rendering and teleoperation to work
properly.
******************************************************************************
*/
#include "haplink_encoders.h"
#include "delta_thumb.h"
#include "hand_virtual_environment.h"
#include "main.h"
#include "mbed.h"
#include "debug_mort.h"
#include "nucleo_led.h"
#include "haplink_communication.h"
#include "haplink_position.h"
#include "haplink_virtual_environments.h"
#include "haplink_motors.h"
#include "haplink_fsr.h"
#include "haplink_time.h"
RawSerial pc(USBTX, USBRX);
//communication variables:
uint8_t receivechar;
int receivedMessage = 0;
uint8_t communicationBuf[20];
uint8_t sendBuffer[10];
uint8_t messagesize = 0;
/*-- Functions to communicate with processing and render graphics,
change the print to match what you need for your environment.
You can also change the argument so you send processing what you need--*/
/*******************************************************************************
* @name printProcessingComm1DOF
* @brief this is just an example function to send to show the communication structure to processing.
this function assumes that what you want to send to processing is one parameter as well
as the position of the 1DOF Haplink handle.
* @param a parameter to send to processing.
* @retval none.
*/
void printProcessingComm1DOF( double parameter )
{
//debugprintHelloWorld();
if ((returnMessageAcknowledged() > 0) && (returnDataHasBeenRequested() > 0))
{
clearMessageAcknowledged();
clearDataHasBeenRequested();
pc.printf("%f\t%f\t l",(float)getXH(), (float)(parameter));
//only for debugging:
//pc.printf("got message \n");
//if you did not want to send a parameter for example:
//serial.printf("%f\t l",(float)getXH());
}
}
/*******************************************************************************
* @name printProcessingComm2DOF
* @brief this is just an example function to send to show the communication structure to processing.
this function assumes that what you want to send to processing is one parameter as well
as the position of the 2DOF Haplink cursor and Proxy for rendering Outside of a Box.
* @param a parameter to send to processing.
* @retval none.
*/
void printProcessingComm2DOF( double parameter )
{
if ((returnMessageAcknowledged() > 0) && (returnDataHasBeenRequested() > 0))
{
clearMessageAcknowledged();
clearDataHasBeenRequested();
pc.printf("%f\t%f\t%f\t%f\t%f\t l", (float)getRx(), (float)getRy(), (float)getProxyX(), (float)getProxyY(), (float)parameter);
////pc.printf("%f\t%f\t l", (float)getRx(), (float)getRy());
}
}
/*******************************************************************************
* @name printProcessingCommFinger1
* @brief this is just an example function to send to show the communication structure to processing.
this function assumes that what you want to send to processing is the position of the 2DOF Finger1 cursor
* @param a parameter to send to processing.
* @retval none.
*/
void printProcessingCommFinger1(void)
{
if ((returnMessageAcknowledged() > 0) && (returnDataHasBeenRequested() > 0))
{
clearMessageAcknowledged();
clearDataHasBeenRequested();
pc.printf("%f\t%f\t", getXf1_global(), getYf1_global());
////pc.printf("%f\t%f\t l", (float)getRx(), (float)getRy());
}
}
/*******************************************************************************
* @name printProcessingCommFinger2
* @brief this is just an example function to send to show the communication structure to processing.
this function assumes that what you want to send to processing is the position of the 2DOF Finger2 cursor.
* @param a parameter to send to processing.
* @retval none.
*/
void printProcessingCommFinger2(void)
{
if ((returnMessageAcknowledged() > 0) && (returnDataHasBeenRequested() > 0))
{
clearMessageAcknowledged();
clearDataHasBeenRequested();
pc.printf("%f\t%f\t", getXf2_global(), getYf2_global());
////pc.printf("%f\t%f\t l", (float)getRx(), (float)getRy());
}
}
/*******************************************************************************
* @name printTeleoperationComm
* @brief this is just an example function to send to show the communication structure for teleoperation.
this function assumes that all you want to send is the position of the handle or cursor
* @param none.
* @retval none.
*/
void printTeleoperationComm( void )
{
if (returnTeleOperationHasBeenRequested() > 0)
{
//Communication for teleoperation:
#ifdef DOF_1 //teleoperation in 1-DOF
get_xH_packed( sendBuffer );
//writeBlock(sendBuffer,4) ;
pc.printf("%c%c%c%c",sendBuffer[0],sendBuffer[1],sendBuffer[2],sendBuffer[3]);
#else //teleoperation in 2-DOF
get_rx_ry_packed( sendBuffer );
//serial.writeBlock(sendBuffer,6) ;
pc.printf("%c%c%c%c%c%c",sendBuffer[0],sendBuffer[1],sendBuffer[2],sendBuffer[3], sendBuffer[4], sendBuffer[5]);
#endif
clearTeleOperationHasBeenRequested();
}
}
/*-- Functions to print to help you debug, feel free to add your own
The idea behind this functions is to call them to print useful things to the screen
to help you see that your device is working properly. Just remember that prints will
ALWAYS cause delays and affect the behavior of your device. And if they happen too
frequently, your device may not work at all.-- */
void debugprintFingerMotorCounts()
{
pc.printf("Motor 4 Encoder Counts: %i\n", getCountsSensor4());
pc.printf("Motor 5 Encoder Counts: %i\n", getCountsSensor5());
pc.printf("Motor 6 Encoder Counts: %i\n", getCountsSensor6());
pc.printf("Motor 7 Encoder Counts: %i\n", getCountsSensor7());
}
void printDebugDeltaThumb( void )
{
pc.printf("xH=%lf, dXh=%lf, ForceH=%lf, Torque=%lf\r\n", getXH(), getDxH(), getForceH(), getTorqueMotor1());
}
void printProcessingHapticHand( void )
{
// pc.printf("%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t l", (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, getThumbX(), getThumbY(), getThumbZ(), (float)0.0, (float)0.0, (float)0.0, (float)0.0);
// pc.printf("%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t l", (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0);
pc.printf("%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t \n", (float)deltaThumbX, getThumbY(), getThumbZ(), (float)getXf1_global(), (float)getYf1_global(), (float)NORMAL_ZF1, (float)getXf2_global(), (float)getYf2_global(), (float)NORMAL_ZF2, (float)getSphereX(), (float)getSphereY(), (float)getSphereZ(), (float)getSphereRadius());
// if ((returnMessageAcknowledged() > 0) && (returnDataHasBeenRequested() > 0))
// {
// clearMessageAcknowledged();
// clearDataHasBeenRequested();
// // pc.printf("%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t l", getThumbX(), getThumbY(), getThumbZ(), (float)getRx1(), (float)getRy1(), (float)45.41, (float)getRx2(), (float)getRy2(), (float)86.59, (float)getSphereX(), (float)getSphereY(), (float)getSphereZ(), (float)getSphereRadius());
// pc.printf("%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t l", (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0, (float)0.0);
// ////pc.printf("%f\t%f\t l", (float)getRx(), (float)getRy());
// }
}
void printDebug1DOFAllParameters( void )
{
pc.printf("t=%lf, FSR1=%u, FSR2=%u, xH=%lf, dXh=%lf, ForceH=%lf, Torque=%lf\r\n",getTime_ms(), queryFSR1value(), queryFSR2value(), getXH(), getDxH(), getForceH(), getTorqueMotor1());
}
void printDebug2DOFAllParameters( void )
{
pc.printf("Rx: %lf, Ry: %lf, ThetaA: %lf, ThetaB: %lf, ProxyX: %lf, ProxyY: %lf \n", getRx(), getRy(), getThetaADeg(), getThetaBDeg(),getProxyX(),getProxyY());
}
void printDebugFinger1Parameters( void )
{
pc.printf("rx = %lf, ry = %lf, Torque M4 = %lf Nm, Torque M5 = %lf Nm, theta_a_deg = %lf, theta_b_deg = %lf \n", getRx1(), getRy1(), getTorqueMotor4(), getTorqueMotor5(), getThetaA1_deg(), getThetaB1_deg());
}
void printDebugFinger2Parameters( void )
{
pc.printf("rx = %lf, ry = %lf, Torque M6 = %lf Nm, Torque M7 = %lf Nm, theta_a_deg = %lf, theta_b_deg = %lf \n", getRx2(), getRy2(), getTorqueMotor6(), getTorqueMotor7(), getThetaA2_deg(), getThetaB2_deg());
}
void printComBuffer( void )
{
pc.printf("message: %u , %u \n", communicationBuf[0], communicationBuf[1]);
}
void debugprint(uint16_t number)
{
pc.printf("Got to %u\n",number);
}
void debugprintHelloWorld( void )
{
pc.printf("Hello! Haptics to see you! \n");
}
void debugprint2byteValue(uint16_t number)
{
pc.printf("Value = %u\n",number);
}
void debugprintRegister( uint32_t registerval)
{
pc.printf("Register Value %u\n", registerval);
}
void debugprintEncoderCounts( int32_t encodercounts)
{
pc.printf("Encoder Counts %i\n", encodercounts);
}
void debugprintDouble( double number)
{
pc.printf("double number %lf\n", number);
}
void debugprintStarterCode( void )
{
pc.printf("Dear Math, please grow up and solve your own problems.\n");
}
void debugprinttruesusb( void )
{
pc.printf("l");
}
/*--Functions to manage the two way communication with processing, do not change! --*/
void manageIncommingMessage( void )
{
int response = 0;
response = decodeMessage( &communicationBuf[0] );
//Only if debugging
//pc.printf("Message received is: %i \n", response);
}
//function that gets called to receive messages from the computer via USB
void receiveMessageCallback( void)
{
int i;
// Note: you need to actually read from the serial to clear the RX interrupt
//pc.printf("%c\n", pc.getc());
receivechar = pc.getc();
i = 0;
if (receivechar == 'p') //receiving position from teleoperation
{
communicationBuf[0] = receivechar;
receivechar = pc.getc(); //get first digit
communicationBuf[1] = receivechar;
receivechar = pc.getc(); //get second digit
communicationBuf[2] = receivechar;
receivechar = pc.getc(); //get the terminating character
i = 3;
#ifdef DOF_2
communicationBuf[3] = receivechar;
receivechar = pc.getc(); //get the second digit
communicationBuf[4] = receivechar;
receivechar = pc.getc(); //get the terminating character
#endif
}
else
{
while (receivechar != 'l')
{
communicationBuf[i] = receivechar;
receivechar = pc.getc();
i = i+1;
}
//printf("Received Message\n"); //don't print so soon, buffer is not ready.
}
//only uncomment when debugging with Arduino, for some reason HAVE to do the newline thing
//receivechar = pc.getc();
receivechar = '0';
receivedMessage = receivedMessage + 1;
messagesize = i;
}
int checkReceiveMessage( void )
{
return receivedMessage;
}
void clearReceiveMessage( void )
{
receivedMessage = 0;
}
void initCommunication( void )
{
resetCommunicationVariables();
pc.baud(115200);
pc.attach(&receiveMessageCallback);
}
//EOF