bldc/lispBM/lispif_vesc_extensions.c

522 lines
15 KiB
C
Raw Normal View History

2022-01-14 11:54:24 -08:00
/*
Copyright 2022 Benjamin Vedder benjamin@vedder.se
Copyright 2022 Joel Svensson svenssonjoel@yahoo.se
This file is part of the VESC firmware.
The VESC firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lispif.h"
#include "extensions.h"
#include "print.h"
#include "commands.h"
#include "mc_interface.h"
#include "timeout.h"
#include "servo_dec.h"
#include "servo_simple.h"
#include "encoder.h"
2022-01-14 16:25:11 -08:00
#include "comm_can.h"
#include "bms.h"
#include "utils.h"
2022-01-15 15:14:55 -08:00
#include "hw.h"
2022-01-14 11:54:24 -08:00
#include <math.h>
// Helpers
2022-01-21 01:52:56 -08:00
static bool is_number_all(lbm_value *args, lbm_uint argn) {
for (lbm_uint i = 0;i < argn;i++) {
if (!lbm_is_number(args[i])) {
2022-01-16 07:21:18 -08:00
return false;
}
2022-01-14 16:25:11 -08:00
}
2022-01-16 07:21:18 -08:00
return true;
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
#define CHECK_NUMBER_ALL() if (!is_number_all(args, argn)) {return lbm_enc_sym(SYM_EERROR);}
2022-01-16 07:21:18 -08:00
#define CHECK_ARGN(n) if (argn != n) {return enc_sym(SYM_EERROR);}
2022-01-21 01:52:56 -08:00
#define CHECK_ARGN_NUMBER(n) if (argn != n || !is_number_all(args, argn)) {return lbm_enc_sym(SYM_EERROR);}
2022-01-14 11:54:24 -08:00
// Various commands
2022-01-21 01:52:56 -08:00
static lbm_value ext_print(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
static char output[256];
2022-01-21 01:52:56 -08:00
for (lbm_uint i = 0; i < argn; i ++) {
lbm_value t = args[i];
2022-01-14 11:54:24 -08:00
2022-01-21 01:52:56 -08:00
if (lbm_is_ptr(t) && lbm_type_of(t) == LBM_PTR_TYPE_ARRAY) {
lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(t);
2022-01-14 11:54:24 -08:00
switch (array->elt_type){
2022-01-21 01:52:56 -08:00
case LBM_VAL_TYPE_CHAR:
2022-01-14 11:54:24 -08:00
commands_printf("%s", (char*)array + 8);
break;
default:
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_NIL);
2022-01-14 11:54:24 -08:00
break;
}
2022-01-21 01:52:56 -08:00
} else if (lbm_type_of(t) == LBM_VAL_TYPE_CHAR) {
if (lbm_dec_char(t) =='\n') {
2022-01-14 11:54:24 -08:00
commands_printf(" ");
} else {
2022-01-21 01:52:56 -08:00
commands_printf("%c", lbm_dec_char(t));
2022-01-14 11:54:24 -08:00
}
} else {
2022-01-21 01:52:56 -08:00
lbm_print_value(output, 256, t);
2022-01-14 11:54:24 -08:00
commands_printf("%s", output);
}
}
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_servo(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
servo_simple_set_output(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_reset_timeout(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
timeout_reset();
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_ppm(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(servodec_get_servo(0));
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_encoder(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(encoder_read_deg());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_vin(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_input_voltage_filtered());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_select_motor(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
int i = lbm_dec_as_i(args[0]);
2022-01-14 11:54:24 -08:00
if (i != 0 && i != 1 && i != 2) {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-14 11:54:24 -08:00
}
mc_interface_select_motor_thread(i);
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_selected_motor(lbm_value *args, lbm_uint argn) {
2022-01-14 16:25:11 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_i(mc_interface_motor_now());
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_bms_val(lbm_value *args, lbm_uint argn) {
lbm_value res = lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
if (argn != 1 && argn != 2) {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
char *name = lbm_dec_str(args[0]);
2022-01-14 16:25:11 -08:00
if (!name) {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
}
bms_values *val = bms_get_values();
if (strcmp(name, "v_tot") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->v_tot);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "v_charge") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->v_charge);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "i_in") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->i_in);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "i_in_ic") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->i_in_ic);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "ah_cnt") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->ah_cnt);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "wh_cnt") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->wh_cnt);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "cell_num") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_i(val->cell_num);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "v_cell") == 0) {
2022-01-21 01:52:56 -08:00
if (argn != 2 || !lbm_is_number(args[1])) {
return lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
int c = lbm_dec_as_i(args[1]);
2022-01-14 16:25:11 -08:00
if (c < 0 || c >= val->cell_num) {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->v_cell[c]);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "bal_state") == 0) {
2022-01-21 01:52:56 -08:00
if (argn != 2 || !lbm_is_number(args[1])) {
return lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
int c = lbm_dec_as_i(args[1]);
2022-01-14 16:25:11 -08:00
if (c < 0 || c >= val->cell_num) {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
res = lbm_enc_i(val->bal_state[c]);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "temp_adc_num") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_i(val->temp_adc_num);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "temps_adc") == 0) {
2022-01-21 01:52:56 -08:00
if (argn != 2 || !lbm_is_number(args[1])) {
return lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
int c = lbm_dec_as_i(args[1]);
2022-01-14 16:25:11 -08:00
if (c < 0 || c >= val->temp_adc_num) {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-14 16:25:11 -08:00
}
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->temps_adc[c]);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "temp_ic") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->temp_ic);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "temp_hum") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->temp_hum);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "hum") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->hum);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "temp_max_cell") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->temp_max_cell);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "soc") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->soc);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "soh") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->soh);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "can_id") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_i(val->can_id);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "ah_cnt_chg_total") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->ah_cnt_chg_total);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "wh_cnt_chg_total") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->wh_cnt_chg_total);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "ah_cnt_dis_total") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->ah_cnt_dis_total);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "wh_cnt_dis_total") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(val->wh_cnt_dis_total);
2022-01-14 16:25:11 -08:00
} else if (strcmp(name, "msg_age") == 0) {
2022-01-21 01:52:56 -08:00
res = lbm_enc_F(UTILS_AGE_S(val->update_time));
2022-01-14 16:25:11 -08:00
}
return res;
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_adc(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_NUMBER_ALL();
2022-01-15 15:14:55 -08:00
if (argn == 0) {
2022-01-21 01:52:56 -08:00
return lbm_enc_F(ADC_VOLTS(ADC_IND_EXT));
2022-01-15 15:14:55 -08:00
} else if (argn == 1) {
2022-01-21 01:52:56 -08:00
lbm_int channel = lbm_dec_as_i(args[0]);
2022-01-16 07:21:18 -08:00
if (channel == 0) {
2022-01-21 01:52:56 -08:00
return lbm_enc_F(ADC_VOLTS(ADC_IND_EXT));
2022-01-16 07:21:18 -08:00
} else if (channel == 1) {
2022-01-21 01:52:56 -08:00
return lbm_enc_F(ADC_VOLTS(ADC_IND_EXT2));
2022-01-15 15:14:55 -08:00
} else {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-15 15:14:55 -08:00
}
} else {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-15 15:14:55 -08:00
}
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_systime(lbm_value *args, lbm_uint argn) {
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_I(chVTGetSystemTimeX());
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_secs_since(lbm_value *args, lbm_uint argn) {
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
return lbm_enc_F(UTILS_AGE_S(lbm_dec_as_u(args[0])));
}
2022-01-14 11:54:24 -08:00
// Motor set commands
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_current(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_current(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_current_rel(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_current_rel(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_duty(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_duty(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_brake(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_brake_current(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_brake_rel(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_brake_current_rel(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_handbrake(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_handbrake(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_handbrake_rel(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_handbrake_rel(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_rpm(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_pid_speed(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_set_pos(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1);
2022-01-21 01:52:56 -08:00
mc_interface_set_pid_pos(lbm_dec_as_f(args[0]));
return lbm_enc_sym(SYM_TRUE);
2022-01-14 11:54:24 -08:00
}
// Motor get commands
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_current(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_tot_current_filtered());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_current_dir(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_tot_current_directional_filtered());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_current_in(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_tot_current_in_filtered());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_duty(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_duty_cycle_now());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_rpm(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_rpm());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_temp_fet(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_temp_fet_filtered());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_temp_mot(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_temp_motor_filtered());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_speed(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_speed());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_dist(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_distance_abs());
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_batt(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_F(mc_interface_get_battery_level(0));
2022-01-14 11:54:24 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_get_fault(lbm_value *args, lbm_uint argn) {
2022-01-14 11:54:24 -08:00
(void)args; (void)argn;
2022-01-21 01:52:56 -08:00
return lbm_enc_i(mc_interface_get_fault());
2022-01-14 11:54:24 -08:00
}
2022-01-16 07:21:18 -08:00
// CAN-commands
2022-01-21 01:52:56 -08:00
static lbm_value ext_can_current(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_NUMBER_ALL();
if (argn == 2) {
2022-01-21 01:52:56 -08:00
comm_can_set_current(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]));
2022-01-16 07:21:18 -08:00
} else if (argn == 3) {
2022-01-21 01:52:56 -08:00
comm_can_set_current_off_delay(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]), lbm_dec_as_f(args[2]));
2022-01-16 07:21:18 -08:00
} else {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_TRUE);
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_can_current_rel(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_NUMBER_ALL();
if (argn == 2) {
2022-01-21 01:52:56 -08:00
comm_can_set_current_rel(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]));
2022-01-16 07:21:18 -08:00
} else if (argn == 3) {
2022-01-21 01:52:56 -08:00
comm_can_set_current_rel_off_delay(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]), lbm_dec_as_f(args[2]));
2022-01-16 07:21:18 -08:00
} else {
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_EERROR);
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
return lbm_enc_sym(SYM_TRUE);
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_can_duty(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(2);
2022-01-21 01:52:56 -08:00
comm_can_set_duty(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]));
return lbm_enc_sym(SYM_TRUE);
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_can_brake(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(2);
2022-01-21 01:52:56 -08:00
comm_can_set_current_brake(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]));
return lbm_enc_sym(SYM_TRUE);
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_can_brake_rel(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(2);
2022-01-21 01:52:56 -08:00
comm_can_set_current_brake_rel(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]));
return lbm_enc_sym(SYM_TRUE);
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_can_rpm(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(2);
2022-01-21 01:52:56 -08:00
comm_can_set_rpm(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]));
return lbm_enc_sym(SYM_TRUE);
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_can_pos(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(2);
2022-01-21 01:52:56 -08:00
comm_can_set_pos(lbm_dec_as_i(args[0]), lbm_dec_as_f(args[1]));
return lbm_enc_sym(SYM_TRUE);
}
static lbm_value ext_can_get_current(lbm_value *args, lbm_uint argn) {
CHECK_ARGN_NUMBER(1);
can_status_msg *stat0 = comm_can_get_status_msg_id(lbm_dec_as_i(args[0]));
if (stat0) {
return lbm_enc_F(stat0->current);
} else {
return lbm_enc_sym(SYM_EERROR);
}
}
static lbm_value ext_can_get_current_dir(lbm_value *args, lbm_uint argn) {
CHECK_ARGN_NUMBER(1);
can_status_msg *stat0 = comm_can_get_status_msg_id(lbm_dec_as_i(args[0]));
if (stat0) {
return lbm_enc_F(stat0->current * SIGN(stat0->duty));
} else {
return lbm_enc_sym(SYM_EERROR);
}
2022-01-16 07:21:18 -08:00
}
// Math
2022-01-21 01:52:56 -08:00
static lbm_value ext_sin(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1)
2022-01-21 01:52:56 -08:00
return lbm_enc_F(sinf(lbm_dec_as_f(args[0])));
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_cos(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1)
2022-01-21 01:52:56 -08:00
return lbm_enc_F(cosf(lbm_dec_as_f(args[0])));
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_atan(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(1)
2022-01-21 01:52:56 -08:00
return lbm_enc_F(atanf(lbm_dec_as_f(args[0])));
2022-01-16 07:21:18 -08:00
}
2022-01-21 01:52:56 -08:00
static lbm_value ext_pow(lbm_value *args, lbm_uint argn) {
2022-01-16 07:21:18 -08:00
CHECK_ARGN_NUMBER(2)
2022-01-21 01:52:56 -08:00
return lbm_enc_F(powf(lbm_dec_as_f(args[0]), lbm_dec_as_f(args[1])));
2022-01-16 07:21:18 -08:00
}
2022-01-14 11:54:24 -08:00
void lispif_load_vesc_extensions(void) {
// Various commands
2022-01-21 01:52:56 -08:00
lbm_add_extension("print", ext_print);
lbm_add_extension("timeout-reset", ext_reset_timeout);
lbm_add_extension("get-ppm", ext_get_ppm);
lbm_add_extension("get-encoder", ext_get_encoder);
lbm_add_extension("set-servo", ext_set_servo);
lbm_add_extension("get-vin", ext_get_vin);
lbm_add_extension("select-motor", ext_select_motor);
lbm_add_extension("get-selected-motor", ext_get_selected_motor);
lbm_add_extension("get-bms-val", ext_get_bms_val);
lbm_add_extension("get-adc", ext_get_adc);
lbm_add_extension("systime", ext_systime);
lbm_add_extension("secs-since", ext_secs_since);
2022-01-14 11:54:24 -08:00
// Motor set commands
2022-01-21 01:52:56 -08:00
lbm_add_extension("set-current", ext_set_current);
lbm_add_extension("set-current-rel", ext_set_current_rel);
lbm_add_extension("set-duty", ext_set_duty);
lbm_add_extension("set-brake", ext_set_brake);
lbm_add_extension("set-brake-rel", ext_set_brake_rel);
lbm_add_extension("set-handbrake", ext_set_handbrake);
lbm_add_extension("set-handbrake-rel", ext_set_handbrake_rel);
lbm_add_extension("set-rpm", ext_set_rpm);
lbm_add_extension("set-pos", ext_set_pos);
2022-01-14 11:54:24 -08:00
// Motor get commands
2022-01-21 01:52:56 -08:00
lbm_add_extension("get-current", ext_get_current);
lbm_add_extension("get-current-dir", ext_get_current_dir);
lbm_add_extension("get-current-in", ext_get_current_in);
lbm_add_extension("get-duty", ext_get_duty);
lbm_add_extension("get-rpm", ext_get_rpm);
lbm_add_extension("get-temp-fet", ext_get_temp_fet);
lbm_add_extension("get-temp-mot", ext_get_temp_mot);
lbm_add_extension("get-speed", ext_get_speed);
lbm_add_extension("get-dist", ext_get_dist);
lbm_add_extension("get-batt", ext_get_batt);
lbm_add_extension("get-fault", ext_get_fault);
2022-01-16 07:21:18 -08:00
// CAN-comands
2022-01-21 01:52:56 -08:00
lbm_add_extension("canset-current", ext_can_current);
lbm_add_extension("canset-current-rel", ext_can_current_rel);
lbm_add_extension("canset-duty", ext_can_duty);
lbm_add_extension("canset-brake", ext_can_brake);
lbm_add_extension("canset-brake-rel", ext_can_brake_rel);
lbm_add_extension("canset-rpm", ext_can_rpm);
lbm_add_extension("canset-pos", ext_can_pos);
lbm_add_extension("canget-current", ext_can_get_current);
lbm_add_extension("canget-current-dir", ext_can_get_current_dir);
2022-01-16 07:21:18 -08:00
// Math
2022-01-21 01:52:56 -08:00
lbm_add_extension("sin", ext_sin);
lbm_add_extension("cos", ext_cos);
lbm_add_extension("atan", ext_atan);
lbm_add_extension("pow", ext_pow);
2022-01-14 11:54:24 -08:00
}