Source

papl_robwork / src / stub.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
  Copyright (c) 2012 Anders Lau Olsen.
  See LICENSE file for terms and conditions.
*/

#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>

#include <papl_robwork.h>
#include <stdio.h>
#include <string.h>

/*----------------------------------------------------------------------*
  Value accessors
 *----------------------------------------------------------------------*/

#define WorkCell_val(v) (*((RW_WorkCell **) Data_custom_val(v)))
#define State_val(v) (*((RW_State **) Data_custom_val(v)))
#define Device_val(v) (*((RW_Device **) Data_custom_val(v)))
#define Frame_val(v) (*((RW_Frame **) Data_custom_val(v)))
#define FKTable_val(v) (*((RW_FKTable **) Data_custom_val(v)))
#define TimedStatePath_val(v) (*((RW_TimedStatePath **) Data_custom_val(v)))

/*----------------------------------------------------------------------*
  Finalizers
 *----------------------------------------------------------------------*/

static void finalize_WorkCell(value model)
{
    robwork_freeWorkCell(WorkCell_val(model));
}

static void finalize_State(value state)
{
    robwork_freeState(State_val(state));
}

static void finalize_Device(value device)
{
    robwork_freeDevice(Device_val(device));
}

static void finalize_Frame(value frame)
{
    robwork_freeFrame(Frame_val(frame));
}

static void finalize_FKTable(value table)
{
    robwork_freeFKTable(FKTable_val(table));
}

static void finalize_TimedStatePath(value path)
{
    robwork_freeTimedStatePath(TimedStatePath_val(path));
}

/*----------------------------------------------------------------------*
  Value setup
 *----------------------------------------------------------------------*/

static struct custom_operations ops_WorkCell = {
  "org.bergsoe.caml_robwork_workcell",
  finalize_WorkCell,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

static struct custom_operations ops_State = {
  "org.bergsoe.caml_robwork_state",
  finalize_State,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

static struct custom_operations ops_Device = {
  "org.bergsoe.caml_robwork_device",
  finalize_Device,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

static struct custom_operations ops_Frame = {
  "org.bergsoe.caml_robwork_frame",
  finalize_Frame,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

static struct custom_operations ops_FKTable = {
  "org.bergsoe.caml_robwork_fktable",
  finalize_FKTable,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

static struct custom_operations ops_TimedStatePath = {
  "org.bergsoe.caml_robwork_timedstatepath",
  finalize_TimedStatePath,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

/*----------------------------------------------------------------------*
  Allocators
 *----------------------------------------------------------------------*/

#define alloc_WorkCell() alloc_custom(&ops_WorkCell, sizeof(RW_WorkCell *), 0, 1)
#define alloc_State() alloc_custom(&ops_State, sizeof(RW_State *), 0, 1)
#define alloc_Device() alloc_custom(&ops_Device, sizeof(RW_Device *), 0, 1)
#define alloc_Frame() alloc_custom(&ops_Frame, sizeof(RW_Frame *), 0, 1)
#define alloc_FKTable() alloc_custom(&ops_FKTable, sizeof(RW_FKTable *), 0, 1)
#define alloc_TimedStatePath() alloc_custom(&ops_TimedStatePath, sizeof(RW_TimedStatePath *), 0, 1)

/*----------------------------------------------------------------------*
  Data verification.
 *----------------------------------------------------------------------*/

value caml_robwork_isValidDevice(value device_value)
{
    CAMLparam1(device_value);

    const RW_Device* device = Device_val(device_value);

    CAMLreturn(Val_bool(robwork_isValidDevice(device)));
}

value caml_robwork_isValidFrame(value frame_value)
{
    CAMLparam1(frame_value);

    const RW_Frame* frame = Frame_val(frame_value);

    CAMLreturn(Val_bool(robwork_isValidFrame(frame)));
}

value caml_robwork_isValidWorkCell(value workcell_value)
{
    CAMLparam1(workcell_value);

    const RW_WorkCell* workcell = WorkCell_val(workcell_value);

    CAMLreturn(Val_bool(robwork_isValidWorkCell(workcell)));
}

value caml_robwork_getWorkCellErrorSize(value workcell_value)
{
    CAMLparam1(workcell_value);
    const RW_WorkCell* workcell = WorkCell_val(workcell_value);
    CAMLreturn(Val_int(robwork_getWorkCellErrorSize(workcell)));
}

value caml_robwork_getWorkCellError(value workcell_value, value error_value)
{
    CAMLparam2(workcell_value, error_value);

    const RW_WorkCell* workcell = WorkCell_val(workcell_value);
    char* error = String_val(error_value);

    const char* error_msg = robwork_getWorkCellError(workcell);
    const int len = robwork_getWorkCellErrorSize(workcell);
    memcpy(error, error_msg, len);
    CAMLreturn(Val_unit);
}

/*----------------------------------------------------------------------*
  Workcell operations
 *----------------------------------------------------------------------*/

value caml_robwork_loadWorkCell(value name_value)
{
    CAMLparam1(name_value);

    const char* name = String_val(name_value);

    value workcell = alloc_WorkCell();
    WorkCell_val(workcell) = robwork_loadWorkCell(name);
    CAMLreturn(workcell);
}

value caml_robwork_getState(value workcell_value)
{
    CAMLparam1(workcell_value);

    const RW_WorkCell* workcell = WorkCell_val(workcell_value);

    value state = alloc_State();
    State_val(state) = robwork_getState(workcell);
    CAMLreturn(state);
}

value caml_robwork_getDevice(value workcell_value, value name_value)
{
    CAMLparam2(workcell_value, name_value);

    const RW_WorkCell* workcell = WorkCell_val(workcell_value);
    const char* name = String_val(name_value);

    value device = alloc_Device();
    Device_val(device) = robwork_getDevice(workcell, name);
    CAMLreturn(device);
}

value caml_robwork_getFrame(value workcell_value, value name_value)
{
    CAMLparam2(workcell_value, name_value);

    const RW_WorkCell* workcell = WorkCell_val(workcell_value);
    const char* name = String_val(name_value);

    value frame = alloc_Frame();
    Frame_val(frame) = robwork_getFrame(workcell, name);
    CAMLreturn(frame);
}

/*----------------------------------------------------------------------*
  Device operations
 *----------------------------------------------------------------------*/

value caml_robwork_getDOF(value device_value)
{
    CAMLparam1(device_value);
    const RW_Device* device = Device_val(device_value);

    CAMLreturn(Val_int(robwork_getDOF(device)));
}

value caml_robwork_getLower(value device_value, value q_value)
{
    CAMLparam2(device_value, q_value);

    const RW_Device* device = Device_val(device_value);
    double* q = (double*)q_value;
    robwork_getLower(device, q);

    CAMLreturn(Val_unit);
}

value caml_robwork_getUpper(value device_value, value q_value)
{
    CAMLparam2(device_value, q_value);

    const RW_Device* device = Device_val(device_value);
    double* q = (double*)q_value;
    robwork_getUpper(device, q);

    CAMLreturn(Val_unit);
}

value caml_robwork_getVelocityLimits(value device_value, value q_value)
{
    CAMLparam2(device_value, q_value);

    const RW_Device* device = Device_val(device_value);
    double* q = (double*)q_value;
    robwork_getVelocityLimits(device, q);

    CAMLreturn(Val_unit);
}

value caml_robwork_getEnd(value device_value)
{
    CAMLparam1(device_value);
    const RW_Device* device = Device_val(device_value);

    value frame = alloc_Frame();
    Frame_val(frame) = robwork_getEnd(device);
    CAMLreturn(frame);
}

/*----------------------------------------------------------------------*
  State operations
 *----------------------------------------------------------------------*/

value caml_robwork_copyState(value state_value)
{
    CAMLparam1(state_value);
    const RW_State* org_state = State_val(state_value);

    value state = alloc_State();
    State_val(state) = robwork_copyState(org_state);
    CAMLreturn(state);
}

value caml_robwork_getFKTable(value state_value)
{
    CAMLparam1(state_value);
    const RW_State* state = State_val(state_value);

    value table = alloc_FKTable();
    FKTable_val(table) = robwork_getFKTable(state);
    CAMLreturn(table);
}

/*----------------------------------------------------------------------*
  FKTable operations
 *----------------------------------------------------------------------*/

value caml_robwork_getTransform(
    value table_value,
    value frame_value,
    value result_value)
{
    CAMLparam3(table_value, frame_value, result_value);
    const RW_FKTable* table = FKTable_val(table_value);
    const RW_Frame* frame = Frame_val(frame_value);
    double* result = (double*)result_value;

    robwork_getTransform(table, frame, result);
    CAMLreturn(Val_unit);
}

/*----------------------------------------------------------------------*
  Device and state operations
 *----------------------------------------------------------------------*/

value caml_robwork_setQ(
    value state_value,
    value device_value,
    value q_value)
{
    CAMLparam3(state_value, device_value, q_value);

    RW_State* state = State_val(state_value);
    const RW_Device* device = Device_val(device_value);
    const double* q = (double*)q_value;

    robwork_setQ(state, device, q);
    CAMLreturn(Val_unit);
}

value caml_robwork_getQ(
    value state_value,
    value device_value,
    value q_value)
{
    CAMLparam3(state_value, device_value, q_value);

    const RW_State* state = State_val(state_value);
    const RW_Device* device = Device_val(device_value);
    double* q = (double*)q_value;

    robwork_getQ(state, device, q);
    CAMLreturn(Val_unit);
}

/*----------------------------------------------------------------------*
  Frame operations
 *----------------------------------------------------------------------*/

value caml_robwork_gripFrame(
    value item_value,
    value gripper_value,
    value state_value)
{
    CAMLparam3(item_value, gripper_value, state_value);

    RW_Frame* item = Frame_val(item_value);
    RW_Frame* gripper = Frame_val(gripper_value);
    RW_State* state = State_val(state_value);

    CAMLreturn(Val_bool(robwork_gripFrame(item, gripper, state)));
}

/*----------------------------------------------------------------------*
  Collision detection
 *----------------------------------------------------------------------*/

value caml_robwork_inCollision(value workcell_value, value state_value)
{
    CAMLparam2(workcell_value, state_value);
    const RW_WorkCell* workcell = WorkCell_val(workcell_value);
    const RW_State* state = State_val(state_value);

    CAMLreturn(Val_bool(robwork_inCollision(workcell, state)));    
}

/*----------------------------------------------------------------------*
  (Time, State) path operations
 *----------------------------------------------------------------------*/

value caml_robwork_makeTimedStatePath()
{
    CAMLparam0();
    value path = alloc_TimedStatePath();
    TimedStatePath_val(path) = robwork_makeTimedStatePath();
    CAMLreturn(path);
}

value caml_robwork_addTimedState(
    value path_value, value time_value, value state_value)
{
    CAMLparam3(path_value, time_value, state_value);

    RW_TimedStatePath* path = TimedStatePath_val(path_value);
    double time = Double_val(time_value);
    const RW_State* state = State_val(state_value);

    robwork_addTimedState(path, time, state);

    CAMLreturn(Val_unit);
}

value caml_robwork_writeTimedStatePath(
    value workcell_value,
    value path_value,
    value file_value)
{
    CAMLparam3(workcell_value, path_value, file_value);

    const RW_WorkCell* workcell = WorkCell_val(workcell_value);
    const RW_TimedStatePath* path = TimedStatePath_val(path_value);
    const char* file = String_val(file_value);

    CAMLreturn(Val_bool(robwork_writeTimedStatePath(workcell, path, file)));
}