37
37
38
38
// Helpers
39
39
40
- static bool to_float (VALUE t , float * f ) {
41
- bool res = false;
42
-
43
- if (is_number (t )) {
44
- * f = dec_as_f (t );
45
- res = true;
46
- }
47
-
48
- return res ;
49
- }
50
-
51
- static bool get_arg_float (VALUE * args , UINT argn , float * f ) {
52
- if (argn != 1 ) {
53
- return false;
40
+ static bool is_number_all (VALUE * args , UINT argn ) {
41
+ for (UINT i = 0 ;i < argn ;i ++ ) {
42
+ if (!is_number (args [i ])) {
43
+ return false;
44
+ }
54
45
}
55
- return to_float ( args [ 0 ], f ) ;
46
+ return true ;
56
47
}
57
48
58
- #define DEC_FLOAT () float f; if (!get_arg_float(args, argn, &f)) {return enc_sym(SYM_EERROR);};
49
+ #define CHECK_NUMBER_ALL () if (!is_number_all(args, argn)) {return enc_sym(SYM_EERROR);}
50
+ #define CHECK_ARGN (n ) if (argn != n) {return enc_sym(SYM_EERROR);}
51
+ #define CHECK_ARGN_NUMBER (n ) if (argn != n || !is_number_all(args, argn)) {return enc_sym(SYM_EERROR);}
59
52
60
53
// Various commands
61
54
@@ -91,8 +84,8 @@ static VALUE ext_print(VALUE *args, UINT argn) {
91
84
}
92
85
93
86
static VALUE ext_set_servo (VALUE * args , UINT argn ) {
94
- DEC_FLOAT ()
95
- servo_simple_set_output (f );
87
+ CHECK_ARGN_NUMBER ( 1 );
88
+ servo_simple_set_output (dec_as_f ( args [ 0 ]) );
96
89
return enc_sym (SYM_TRUE );
97
90
}
98
91
@@ -118,8 +111,8 @@ static VALUE ext_get_vin(VALUE *args, UINT argn) {
118
111
}
119
112
120
113
static VALUE ext_select_motor (VALUE * args , UINT argn ) {
121
- DEC_FLOAT ()
122
- int i = roundf ( f );
114
+ CHECK_ARGN_NUMBER ( 1 );
115
+ int i = dec_as_i ( args [ 0 ] );
123
116
if (i != 0 && i != 1 && i != 2 ) {
124
117
return enc_sym (SYM_EERROR );
125
118
}
@@ -162,24 +155,22 @@ static VALUE ext_get_bms_val(VALUE *args, UINT argn) {
162
155
} else if (strcmp (name , "cell_num" ) == 0 ) {
163
156
res = enc_i (val -> cell_num );
164
157
} else if (strcmp (name , "v_cell" ) == 0 ) {
165
- float f ;
166
- if (argn != 2 || !to_float (args [1 ], & f )) {
158
+ if (argn != 2 || !is_number (args [1 ])) {
167
159
return enc_sym (SYM_EERROR );
168
160
}
169
161
170
- int c = roundf ( f );
162
+ int c = dec_as_i ( args [ 1 ] );
171
163
if (c < 0 || c >= val -> cell_num ) {
172
164
return enc_sym (SYM_EERROR );
173
165
}
174
166
175
167
res = enc_F (val -> v_cell [c ]);
176
168
} else if (strcmp (name , "bal_state" ) == 0 ) {
177
- float f ;
178
- if (argn != 2 || !to_float (args [1 ], & f )) {
169
+ if (argn != 2 || !is_number (args [1 ])) {
179
170
return enc_sym (SYM_EERROR );
180
171
}
181
172
182
- int c = roundf ( f );
173
+ int c = dec_as_i ( args [ 1 ] );
183
174
if (c < 0 || c >= val -> cell_num ) {
184
175
return enc_sym (SYM_EERROR );
185
176
}
@@ -188,12 +179,11 @@ static VALUE ext_get_bms_val(VALUE *args, UINT argn) {
188
179
} else if (strcmp (name , "temp_adc_num" ) == 0 ) {
189
180
res = enc_i (val -> temp_adc_num );
190
181
} else if (strcmp (name , "temps_adc" ) == 0 ) {
191
- float f ;
192
- if (argn != 2 || !to_float (args [1 ], & f )) {
182
+ if (argn != 2 || !is_number (args [1 ])) {
193
183
return enc_sym (SYM_EERROR );
194
184
}
195
185
196
- int c = roundf ( f );
186
+ int c = dec_as_i ( args [ 1 ] );
197
187
if (c < 0 || c >= val -> temp_adc_num ) {
198
188
return enc_sym (SYM_EERROR );
199
189
}
@@ -229,18 +219,16 @@ static VALUE ext_get_bms_val(VALUE *args, UINT argn) {
229
219
}
230
220
231
221
static VALUE ext_get_adc (VALUE * args , UINT argn ) {
222
+ CHECK_NUMBER_ALL ();
223
+
232
224
if (argn == 0 ) {
233
225
return enc_F (ADC_VOLTS (ADC_IND_EXT ));
234
226
} else if (argn == 1 ) {
235
- if (is_number (args [0 ])) {
236
- INT channel = dec_as_i (args [0 ]);
237
- if (channel == 0 ) {
238
- return enc_F (ADC_VOLTS (ADC_IND_EXT ));
239
- } else if (channel == 1 ) {
240
- return enc_F (ADC_VOLTS (ADC_IND_EXT2 ));
241
- } else {
242
- return enc_sym (SYM_EERROR );
243
- }
227
+ INT channel = dec_as_i (args [0 ]);
228
+ if (channel == 0 ) {
229
+ return enc_F (ADC_VOLTS (ADC_IND_EXT ));
230
+ } else if (channel == 1 ) {
231
+ return enc_F (ADC_VOLTS (ADC_IND_EXT2 ));
244
232
} else {
245
233
return enc_sym (SYM_EERROR );
246
234
}
@@ -252,56 +240,56 @@ static VALUE ext_get_adc(VALUE *args, UINT argn) {
252
240
// Motor set commands
253
241
254
242
static VALUE ext_set_current (VALUE * args , UINT argn ) {
255
- DEC_FLOAT ()
256
- mc_interface_set_current (f );
243
+ CHECK_ARGN_NUMBER ( 1 );
244
+ mc_interface_set_current (dec_as_f ( args [ 0 ]) );
257
245
return enc_sym (SYM_TRUE );
258
246
}
259
247
260
248
static VALUE ext_set_current_rel (VALUE * args , UINT argn ) {
261
- DEC_FLOAT ()
262
- mc_interface_set_current_rel (f );
249
+ CHECK_ARGN_NUMBER ( 1 );
250
+ mc_interface_set_current_rel (dec_as_f ( args [ 0 ]) );
263
251
return enc_sym (SYM_TRUE );
264
252
}
265
253
266
254
static VALUE ext_set_duty (VALUE * args , UINT argn ) {
267
- DEC_FLOAT ()
268
- mc_interface_set_duty (f );
255
+ CHECK_ARGN_NUMBER ( 1 );
256
+ mc_interface_set_duty (dec_as_f ( args [ 0 ]) );
269
257
return enc_sym (SYM_TRUE );
270
258
}
271
259
272
260
static VALUE ext_set_brake (VALUE * args , UINT argn ) {
273
- DEC_FLOAT ()
274
- mc_interface_set_brake_current (f );
261
+ CHECK_ARGN_NUMBER ( 1 );
262
+ mc_interface_set_brake_current (dec_as_f ( args [ 0 ]) );
275
263
return enc_sym (SYM_TRUE );
276
264
}
277
265
278
266
static VALUE ext_set_brake_rel (VALUE * args , UINT argn ) {
279
- DEC_FLOAT ()
280
- mc_interface_set_brake_current_rel (f );
267
+ CHECK_ARGN_NUMBER ( 1 );
268
+ mc_interface_set_brake_current_rel (dec_as_f ( args [ 0 ]) );
281
269
return enc_sym (SYM_TRUE );
282
270
}
283
271
284
272
static VALUE ext_set_handbrake (VALUE * args , UINT argn ) {
285
- DEC_FLOAT ()
286
- mc_interface_set_handbrake (f );
273
+ CHECK_ARGN_NUMBER ( 1 );
274
+ mc_interface_set_handbrake (dec_as_f ( args [ 0 ]) );
287
275
return enc_sym (SYM_TRUE );
288
276
}
289
277
290
278
static VALUE ext_set_handbrake_rel (VALUE * args , UINT argn ) {
291
- DEC_FLOAT ()
292
- mc_interface_set_handbrake_rel (f );
279
+ CHECK_ARGN_NUMBER ( 1 );
280
+ mc_interface_set_handbrake_rel (dec_as_f ( args [ 0 ]) );
293
281
return enc_sym (SYM_TRUE );
294
282
}
295
283
296
- static VALUE ext_set_speed (VALUE * args , UINT argn ) {
297
- DEC_FLOAT ()
298
- mc_interface_set_pid_speed (f );
284
+ static VALUE ext_set_rpm (VALUE * args , UINT argn ) {
285
+ CHECK_ARGN_NUMBER ( 1 );
286
+ mc_interface_set_pid_speed (dec_as_f ( args [ 0 ]) );
299
287
return enc_sym (SYM_TRUE );
300
288
}
301
289
302
290
static VALUE ext_set_pos (VALUE * args , UINT argn ) {
303
- DEC_FLOAT ()
304
- mc_interface_set_pid_pos (f );
291
+ CHECK_ARGN_NUMBER ( 1 );
292
+ mc_interface_set_pid_pos (dec_as_f ( args [ 0 ]) );
305
293
return enc_sym (SYM_TRUE );
306
294
}
307
295
@@ -362,6 +350,88 @@ static VALUE ext_get_fault(VALUE *args, UINT argn) {
362
350
return enc_i (mc_interface_get_fault ());
363
351
}
364
352
353
+ // CAN-commands
354
+
355
+ static VALUE ext_can_current (VALUE * args , UINT argn ) {
356
+ CHECK_NUMBER_ALL ();
357
+
358
+ if (argn == 2 ) {
359
+ comm_can_set_current (dec_as_i (args [0 ]), dec_as_f (args [1 ]));
360
+ } else if (argn == 3 ) {
361
+ comm_can_set_current_off_delay (dec_as_i (args [0 ]), dec_as_f (args [1 ]), dec_as_f (args [2 ]));
362
+ } else {
363
+ return enc_sym (SYM_EERROR );
364
+ }
365
+
366
+ return enc_sym (SYM_TRUE );
367
+ }
368
+
369
+ static VALUE ext_can_current_rel (VALUE * args , UINT argn ) {
370
+ CHECK_NUMBER_ALL ();
371
+
372
+ if (argn == 2 ) {
373
+ comm_can_set_current_rel (dec_as_i (args [0 ]), dec_as_f (args [1 ]));
374
+ } else if (argn == 3 ) {
375
+ comm_can_set_current_rel_off_delay (dec_as_i (args [0 ]), dec_as_f (args [1 ]), dec_as_f (args [2 ]));
376
+ } else {
377
+ return enc_sym (SYM_EERROR );
378
+ }
379
+
380
+ return enc_sym (SYM_TRUE );
381
+ }
382
+
383
+ static VALUE ext_can_duty (VALUE * args , UINT argn ) {
384
+ CHECK_ARGN_NUMBER (2 );
385
+ comm_can_set_duty (dec_as_i (args [0 ]), dec_as_f (args [1 ]));
386
+ return enc_sym (SYM_TRUE );
387
+ }
388
+
389
+ static VALUE ext_can_brake (VALUE * args , UINT argn ) {
390
+ CHECK_ARGN_NUMBER (2 );
391
+ comm_can_set_current_brake (dec_as_i (args [0 ]), dec_as_f (args [1 ]));
392
+ return enc_sym (SYM_TRUE );
393
+ }
394
+
395
+ static VALUE ext_can_brake_rel (VALUE * args , UINT argn ) {
396
+ CHECK_ARGN_NUMBER (2 );
397
+ comm_can_set_current_brake_rel (dec_as_i (args [0 ]), dec_as_f (args [1 ]));
398
+ return enc_sym (SYM_TRUE );
399
+ }
400
+
401
+ static VALUE ext_can_rpm (VALUE * args , UINT argn ) {
402
+ CHECK_ARGN_NUMBER (2 );
403
+ comm_can_set_rpm (dec_as_i (args [0 ]), dec_as_f (args [1 ]));
404
+ return enc_sym (SYM_TRUE );
405
+ }
406
+
407
+ static VALUE ext_can_pos (VALUE * args , UINT argn ) {
408
+ CHECK_ARGN_NUMBER (2 );
409
+ comm_can_set_pos (dec_as_i (args [0 ]), dec_as_f (args [1 ]));
410
+ return enc_sym (SYM_TRUE );
411
+ }
412
+
413
+ // Math
414
+
415
+ static VALUE ext_sin (VALUE * args , UINT argn ) {
416
+ CHECK_ARGN_NUMBER (1 )
417
+ return enc_F (sinf (dec_as_f (args [0 ])));
418
+ }
419
+
420
+ static VALUE ext_cos (VALUE * args , UINT argn ) {
421
+ CHECK_ARGN_NUMBER (1 )
422
+ return enc_F (cosf (dec_as_f (args [0 ])));
423
+ }
424
+
425
+ static VALUE ext_atan (VALUE * args , UINT argn ) {
426
+ CHECK_ARGN_NUMBER (1 )
427
+ return enc_F (atanf (dec_as_f (args [0 ])));
428
+ }
429
+
430
+ static VALUE ext_pow (VALUE * args , UINT argn ) {
431
+ CHECK_ARGN_NUMBER (2 )
432
+ return enc_F (powf (dec_as_f (args [0 ]), dec_as_f (args [1 ])));
433
+ }
434
+
365
435
void lispif_load_vesc_extensions (void ) {
366
436
// Various commands
367
437
extensions_add ("print" , ext_print );
@@ -383,7 +453,7 @@ void lispif_load_vesc_extensions(void) {
383
453
extensions_add ("set-brake-rel" , ext_set_brake_rel );
384
454
extensions_add ("set-handbrake" , ext_set_handbrake );
385
455
extensions_add ("set-handbrake-rel" , ext_set_handbrake_rel );
386
- extensions_add ("set-speed " , ext_set_speed );
456
+ extensions_add ("set-rpm " , ext_set_rpm );
387
457
extensions_add ("set-pos" , ext_set_pos );
388
458
389
459
// Motor get commands
@@ -398,4 +468,19 @@ void lispif_load_vesc_extensions(void) {
398
468
extensions_add ("get-dist" , ext_get_dist );
399
469
extensions_add ("get-batt" , ext_get_batt );
400
470
extensions_add ("get-fault" , ext_get_fault );
471
+
472
+ // CAN-comands
473
+ extensions_add ("canset-current" , ext_can_current );
474
+ extensions_add ("canset-current-rel" , ext_can_current_rel );
475
+ extensions_add ("canset-duty" , ext_can_duty );
476
+ extensions_add ("canset-brake" , ext_can_brake );
477
+ extensions_add ("canset-brake-rel" , ext_can_brake_rel );
478
+ extensions_add ("canset-rpm" , ext_can_rpm );
479
+ extensions_add ("canset-pos" , ext_can_pos );
480
+
481
+ // Math
482
+ extensions_add ("sin" , ext_sin );
483
+ extensions_add ("cos" , ext_cos );
484
+ extensions_add ("atan" , ext_atan );
485
+ extensions_add ("pow" , ext_pow );
401
486
}
0 commit comments