Commit 79dcac66 authored by Benjamin Vedder's avatar Benjamin Vedder

Updated to firmware 2.16 and implemented CAN forwarding for bldc_interface

parent 9e97202d
/*
Copyright 2015 Benjamin Vedder benjamin@vedder.se
Copyright 2016 Benjamin Vedder benjamin@vedder.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -19,8 +19,7 @@
* bldc_interface.c
*
* Compatible Firmware Versions
* 1.3
* 1.4
* 2.16
*
*/
......@@ -30,6 +29,7 @@
// Private variables
static unsigned char send_buffer[1024];
static int32_t can_fwd_vesc = -1;
// Private variables for received data
static mc_values values;
......@@ -48,12 +48,17 @@ static float dec_adc;
static float dec_adc_voltage;
static float dec_chuk;
// Private functions
void send_packet_no_fwd(unsigned char *data, unsigned int len);
static void fwd_can_append(uint8_t *data, int32_t *ind);
// Function pointers
static void(*send_func)(unsigned char *data, unsigned int len) = 0;
static void(*forward_func)(unsigned char *data, unsigned int len) = 0;
// Function pointers for received data
static void(*rx_value_func)(mc_values *values) = 0;
static void(*rx_printf_func)(char *str) = 0;
static void(*rx_fw_func)(int major, int minor) = 0;
static void(*rx_rotor_pos_func)(float pos) = 0;
static void(*rx_mcconf_func)(mc_configuration *conf) = 0;
......@@ -66,10 +71,34 @@ static void(*rx_dec_chuk_func)(float val) = 0;
static void(*rx_mcconf_received_func)(void) = 0;
static void(*rx_appconf_received_func)(void) = 0;
/**
* Initialize bldc_interface.
*
* @param func
* A function to be used when sending packets. Null (0) means that no packets will be sent.
*/
void bldc_interface_init(void(*func)(unsigned char *data, unsigned int len)) {
can_fwd_vesc = -1;
send_func = func;
}
/**
* Enable or disable can forwarding to other VESCs.
*
* @param vesc_id
* The VESC ID to forward to. Setting this to -1 disables this feature.
*/
void bldc_interface_set_forward_can(int32_t vesc_id) {
can_fwd_vesc = vesc_id;
}
/**
* Provide a function to forward received data to instead of processing it and calling handlers.
* This will also prevent data from being sent.
*
* @param func
* The forward function. Null (0) to disable forwarding.
*/
void bldc_interface_set_forward_func(void(*func)(unsigned char *data, unsigned int len)) {
forward_func = func;
}
......@@ -159,7 +188,10 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
break;
case COMM_PRINT:
// TODO
if (rx_printf_func) {
data[len] = '\0';
rx_printf_func((char*)data);
}
break;
case COMM_SAMPLE_PRINT:
......@@ -180,6 +212,7 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
break;
case COMM_GET_MCCONF:
case COMM_GET_MCCONF_DEFAULT:
ind = 0;
mcconf.pwm_mode = (mc_pwm_mode)data[ind++];
mcconf.comm_mode = (mc_comm_mode)data[ind++];
......@@ -220,6 +253,31 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
ind += 8;
mcconf.hall_sl_erpm = buffer_get_float32(data, 1000.0, &ind);
mcconf.foc_current_kp = buffer_get_float32(data, 1e5, &ind);
mcconf.foc_current_ki = buffer_get_float32(data, 1e5, &ind);
mcconf.foc_f_sw = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_dt_us = buffer_get_float32(data, 1e6, &ind);
mcconf.foc_encoder_inverted = data[ind++];
mcconf.foc_encoder_offset = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_encoder_ratio = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sensor_mode = (mc_foc_sensor_mode)data[ind++];
mcconf.foc_pll_kp = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_pll_ki = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_motor_l = buffer_get_float32(data, 1e8, &ind);
mcconf.foc_motor_r = buffer_get_float32(data, 1e5, &ind);
mcconf.foc_motor_flux_linkage = buffer_get_float32(data, 1e5, &ind);
mcconf.foc_observer_gain = buffer_get_float32(data, 1e0, &ind);
mcconf.foc_duty_dowmramp_kp = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_duty_dowmramp_ki = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_openloop_rpm = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sl_openloop_hyst = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sl_openloop_time = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sl_d_current_duty = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sl_d_current_factor = buffer_get_float32(data, 1e3, &ind);
memcpy(mcconf.foc_hall_table, data + ind, 8);
ind += 8;
mcconf.foc_hall_sl_erpm = buffer_get_float32(data, 1000.0, &ind);
mcconf.s_pid_kp = buffer_get_float32(data, 1000000.0, &ind);
mcconf.s_pid_ki = buffer_get_float32(data, 1000000.0, &ind);
mcconf.s_pid_kd = buffer_get_float32(data, 1000000.0, &ind);
......@@ -228,6 +286,7 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
mcconf.p_pid_kp = buffer_get_float32(data, 1000000.0, &ind);
mcconf.p_pid_ki = buffer_get_float32(data, 1000000.0, &ind);
mcconf.p_pid_kd = buffer_get_float32(data, 1000000.0, &ind);
mcconf.p_pid_ang_div = buffer_get_float32(data, 1e5, &ind);
mcconf.cc_startup_boost_duty = buffer_get_float32(data, 1000000.0, &ind);
mcconf.cc_min_current = buffer_get_float32(data, 1000.0, &ind);
......@@ -238,6 +297,7 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
mcconf.m_duty_ramp_step = buffer_get_float32(data, 1000000.0, &ind);
mcconf.m_duty_ramp_step_rpm_lim = buffer_get_float32(data, 1000000.0, &ind);
mcconf.m_current_backoff_gain = buffer_get_float32(data, 1000000.0, &ind);
mcconf.m_encoder_counts = buffer_get_uint32(data, &ind);
if (rx_mcconf_func) {
rx_mcconf_func(&mcconf);
......@@ -245,6 +305,7 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
break;
case COMM_GET_APPCONF:
case COMM_GET_APPCONF_DEFAULT:
ind = 0;
appconf.controller_id = data[ind++];
appconf.timeout_msec = buffer_get_uint32(data, &ind);
......@@ -296,6 +357,16 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
appconf.app_chuk_conf.tc = data[ind++];
appconf.app_chuk_conf.tc_max_diff = buffer_get_float32(data, 1000.0, &ind);
appconf.app_nrf_conf.speed = (NRF_SPEED)data[ind++];
appconf.app_nrf_conf.power = (NRF_POWER)data[ind++];
appconf.app_nrf_conf.crc_type = (NRF_CRC)data[ind++];
appconf.app_nrf_conf.retry_delay = (NRF_RETR_DELAY)data[ind++];
appconf.app_nrf_conf.retries = data[ind++];
appconf.app_nrf_conf.channel = data[ind++];
memcpy(appconf.app_nrf_conf.address, data + ind, 3);
ind += 3;
appconf.app_nrf_conf.send_crc_ack = data[ind++];
if (rx_appconf_func) {
rx_appconf_func(&appconf);
}
......@@ -316,6 +387,22 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
}
break;
case COMM_DETECT_MOTOR_R_L: {
// TODO!
} break;
case COMM_DETECT_MOTOR_FLUX_LINKAGE: {
// TODO!
} break;
case COMM_DETECT_ENCODER: {
// TODO!
} break;
case COMM_DETECT_HALL_FOC: {
// TODO!
} break;
case COMM_GET_DECODED_PPM:
ind = 0;
dec_ppm = buffer_get_float32(data, 1000000.0, &ind);
......@@ -330,6 +417,7 @@ void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
ind = 0;
dec_adc = buffer_get_float32(data, 1000000.0, &ind);
dec_adc_voltage = buffer_get_float32(data, 1000000.0, &ind);
// TODO for adc2
if (rx_dec_adc_func) {
rx_dec_adc_func(dec_adc, dec_adc_voltage);
......@@ -377,6 +465,10 @@ void bldc_interface_set_rx_value_func(void(*func)(mc_values *values)) {
rx_value_func = func;
}
void bldc_interface_set_rx_printf_func(void(*func)(char *str)) {
rx_printf_func = func;
}
void bldc_interface_set_rx_fw_func(void(*func)(int major, int minor)) {
rx_fw_func = func;
}
......@@ -419,50 +511,67 @@ void bldc_interface_set_rx_appconf_received_func(void(*func)(void)) {
}
// Setters
void bldc_interface_terminal_cmd(char* cmd) {
int32_t send_index = 0;
int len = strlen(cmd);
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_TERMINAL_CMD;
memcpy(send_buffer + send_index, cmd, len);
send_index += len;
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_set_duty_cycle(float dutyCycle) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_SET_DUTY;
buffer_append_float32(send_buffer, dutyCycle, 100000.0, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_set_current(float current) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_SET_CURRENT;
buffer_append_float32(send_buffer, current, 1000.0, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_set_current_brake(float current) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_SET_CURRENT_BRAKE;
buffer_append_float32(send_buffer, current, 1000.0, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_set_rpm(int rpm) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_SET_RPM;
buffer_append_int32(send_buffer, rpm, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_set_pos(float pos) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_SET_POS;
buffer_append_float32(send_buffer, pos, 1000000.0, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_set_servo_pos(float pos) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_SET_SERVO_POS;
buffer_append_float16(send_buffer, pos, 1000.0, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_set_mcconf(const mc_configuration *mcconf) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_SET_MCCONF;
send_buffer[send_index++] = mcconf->pwm_mode;
......@@ -504,6 +613,31 @@ void bldc_interface_set_mcconf(const mc_configuration *mcconf) {
send_index += 8;
buffer_append_float32(send_buffer,mcconf->hall_sl_erpm, 1000, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_current_kp, 1e5, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_current_ki, 1e5, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_f_sw, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_dt_us, 1e6, &send_index);
send_buffer[send_index++] = mcconf->foc_encoder_inverted;
buffer_append_float32(send_buffer, mcconf->foc_encoder_offset, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_encoder_ratio, 1e3, &send_index);
send_buffer[send_index++] = mcconf->foc_sensor_mode;
buffer_append_float32(send_buffer, mcconf->foc_pll_kp, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_pll_ki, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_motor_l, 1e8, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_motor_r, 1e5, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_motor_flux_linkage, 1e5, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_observer_gain, 1e0, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_duty_dowmramp_kp, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_duty_dowmramp_ki, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_openloop_rpm, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_sl_openloop_hyst, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_sl_openloop_time, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_sl_d_current_duty, 1e3, &send_index);
buffer_append_float32(send_buffer, mcconf->foc_sl_d_current_factor, 1e3, &send_index);
memcpy(send_buffer + send_index, mcconf->foc_hall_table, 8);
send_index += 8;
buffer_append_float32(send_buffer,mcconf->foc_hall_sl_erpm, 1000, &send_index);
buffer_append_float32(send_buffer,mcconf->s_pid_kp, 1000000, &send_index);
buffer_append_float32(send_buffer,mcconf->s_pid_ki, 1000000, &send_index);
buffer_append_float32(send_buffer,mcconf->s_pid_kd, 1000000, &send_index);
......@@ -512,6 +646,7 @@ void bldc_interface_set_mcconf(const mc_configuration *mcconf) {
buffer_append_float32(send_buffer,mcconf->p_pid_kp, 1000000, &send_index);
buffer_append_float32(send_buffer,mcconf->p_pid_ki, 1000000, &send_index);
buffer_append_float32(send_buffer,mcconf->p_pid_kd, 1000000, &send_index);
buffer_append_float32(send_buffer,mcconf->p_pid_ang_div, 1e5, &send_index);
buffer_append_float32(send_buffer,mcconf->cc_startup_boost_duty, 1000000, &send_index);
buffer_append_float32(send_buffer,mcconf->cc_min_current, 1000, &send_index);
......@@ -522,14 +657,15 @@ void bldc_interface_set_mcconf(const mc_configuration *mcconf) {
buffer_append_float32(send_buffer,mcconf->m_duty_ramp_step, 1000000, &send_index);
buffer_append_float32(send_buffer,mcconf->m_duty_ramp_step_rpm_lim, 1000000, &send_index);
buffer_append_float32(send_buffer,mcconf->m_current_backoff_gain, 1000000, &send_index);
buffer_append_uint32(send_buffer, mcconf->m_encoder_counts, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_set_appconf(const app_configuration *appconf) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_SET_APPCONF;
send_buffer[send_index++] = appconf->controller_id;
buffer_append_uint32(send_buffer, appconf->timeout_msec, &send_index);
buffer_append_float32(send_buffer, appconf->timeout_brake_current, 1000.0, &send_index);
......@@ -580,84 +716,118 @@ void bldc_interface_set_appconf(const app_configuration *appconf) {
send_buffer[send_index++] = appconf->app_chuk_conf.tc;
buffer_append_float32(send_buffer, appconf->app_chuk_conf.tc_max_diff, 1000.0, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_buffer[send_index++] = appconf->app_nrf_conf.speed;
send_buffer[send_index++] = appconf->app_nrf_conf.power;
send_buffer[send_index++] = appconf->app_nrf_conf.crc_type;
send_buffer[send_index++] = appconf->app_nrf_conf.retry_delay;
send_buffer[send_index++] = appconf->app_nrf_conf.retries;
send_buffer[send_index++] = appconf->app_nrf_conf.channel;
memcpy(send_buffer + send_index, appconf->app_nrf_conf.address, 3);
send_index += 3;
send_buffer[send_index++] = appconf->app_nrf_conf.send_crc_ack;
send_packet_no_fwd(send_buffer, send_index);
}
// Getters
void bldc_interface_get_fw_version(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_FW_VERSION;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_get_values(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_GET_VALUES;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_get_mcconf(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_GET_MCCONF;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_get_appconf(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_GET_APPCONF;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_get_decoded_ppm(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_GET_DECODED_PPM;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_get_decoded_adc(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_GET_DECODED_ADC;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_get_decoded_chuk(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_GET_DECODED_CHUK;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
// Other functions
void bldc_interface_detect_motor_param(float current, float min_rpm, float low_duty) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_DETECT_MOTOR_PARAM;
buffer_append_float32(send_buffer, current, 1000.0, &send_index);
buffer_append_float32(send_buffer, min_rpm, 1000.0, &send_index);
buffer_append_float32(send_buffer, low_duty, 1000.0, &send_index);
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_reboot(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_REBOOT;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
void bldc_interface_send_alive(void) {
int32_t send_index = 0;
fwd_can_append(send_buffer, &send_index);
send_buffer[send_index++] = COMM_ALIVE;
bldc_interface_send_packet(send_buffer, send_index);
send_packet_no_fwd(send_buffer, send_index);
}
// Helpers
const char* bldc_interface_fault_to_string(mc_fault_code fault) {
switch (fault) {
case FAULT_CODE_NONE: return "FAULT_CODE_NONE"; break;
case FAULT_CODE_OVER_VOLTAGE: return "FAULT_CODE_OVER_VOLTAGE"; break;
case FAULT_CODE_UNDER_VOLTAGE: return "FAULT_CODE_UNDER_VOLTAGE"; break;
case FAULT_CODE_DRV8302: return "FAULT_CODE_DRV8302"; break;
case FAULT_CODE_ABS_OVER_CURRENT: return "FAULT_CODE_ABS_OVER_CURRENT"; break;
case FAULT_CODE_OVER_TEMP_FET: return "FAULT_CODE_OVER_TEMP_FET"; break;
case FAULT_CODE_OVER_TEMP_MOTOR: return "FAULT_CODE_OVER_TEMP_MOTOR"; break;
default: return "FAULT_UNKNOWN"; break;
case FAULT_CODE_NONE: return "FAULT_CODE_NONE";
case FAULT_CODE_OVER_VOLTAGE: return "FAULT_CODE_OVER_VOLTAGE";
case FAULT_CODE_UNDER_VOLTAGE: return "FAULT_CODE_UNDER_VOLTAGE";
case FAULT_CODE_DRV8302: return "FAULT_CODE_DRV8302";
case FAULT_CODE_ABS_OVER_CURRENT: return "FAULT_CODE_ABS_OVER_CURRENT";
case FAULT_CODE_OVER_TEMP_FET: return "FAULT_CODE_OVER_TEMP_FET";
case FAULT_CODE_OVER_TEMP_MOTOR: return "FAULT_CODE_OVER_TEMP_MOTOR";
default: return "Unknown fault";
}
}
// Private functions
void send_packet_no_fwd(unsigned char *data, unsigned int len) {
if (!forward_func) {
bldc_interface_send_packet(data, len);
}
}
static void fwd_can_append(uint8_t *data, int32_t *ind) {
if (can_fwd_vesc >= 0) {
data[*ind++] = COMM_FORWARD_CAN;
data[*ind++] = can_fwd_vesc;
}
}
/*
Copyright 2015 Benjamin Vedder benjamin@vedder.se
Copyright 2016 Benjamin Vedder benjamin@vedder.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -15,13 +15,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* bldc_interface.h
*
* Created on: 6 aug 2015
* Author: benjamin
*/
#ifndef BLDC_INTERFACE_H_
#define BLDC_INTERFACE_H_
......@@ -29,12 +22,14 @@
// interface functions
void bldc_interface_init(void(*func)(unsigned char *data, unsigned int len));
void bldc_interface_set_forward_can(int32_t vesc_id);
void bldc_interface_set_forward_func(void(*func)(unsigned char *data, unsigned int len));
void bldc_interface_send_packet(unsigned char *data, unsigned int len);
void bldc_interface_process_packet(unsigned char *data, unsigned int len);
// Function pointer setters
void bldc_interface_set_rx_value_func(void(*func)(mc_values *values));
void bldc_interface_set_rx_printf_func(void(*func)(char *str));
void bldc_interface_set_rx_fw_func(void(*func)(int major, int minor));
void bldc_interface_set_rx_rotor_pos_func(void(*func)(float pos));
void bldc_interface_set_rx_mcconf_func(void(*func)(mc_configuration *conf));
......@@ -48,6 +43,7 @@ void bldc_interface_set_rx_mcconf_received_func(void(*func)(void));
void bldc_interface_set_rx_appconf_received_func(void(*func)(void));
// Setters
void bldc_interface_terminal_cmd(char* cmd);
void bldc_interface_set_duty_cycle(float dutyCycle);
void bldc_interface_set_current(float current);
void bldc_interface_set_current_brake(float current);
......
/*
Copyright 2012-2014 Benjamin Vedder benjamin@vedder.se
Copyright 2012-2016 Benjamin Vedder benjamin@vedder.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -15,13 +15,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* datatypes.h
*
* Created on: 14 sep 2014
* Author: benjamin
*/
#ifndef DATATYPES_H_
#define DATATYPES_H_
......@@ -29,14 +22,6 @@
#include <stdbool.h>
#include "ch.h"
// Data types
typedef enum {
MC_STATE_OFF = 0,
MC_STATE_DETECTING,
MC_STATE_RUNNING,
MC_STATE_FULL_BRAKE,
} mc_state;
typedef enum {
PWM_MODE_NONSYNCHRONOUS_HISW = 0, // This mode is not recommended
PWM_MODE_SYNCHRONOUS, // The recommended and most tested mode
......@@ -54,9 +39,16 @@ typedef enum {
SENSOR_MODE_HYBRID
} mc_sensor_mode;
typedef enum {
FOC_SENSOR_MODE_SENSORLESS = 0,
FOC_SENSOR_MODE_ENCODER,
FOC_SENSOR_MODE_HALL
} mc_foc_sensor_mode;
typedef enum {
MOTOR_TYPE_BLDC = 0,
MOTOR_TYPE_DC,
MOTOR_TYPE_FOC
} mc_motor_type;
typedef enum {
......@@ -69,24 +61,28 @@ typedef enum {
FAULT_CODE_OVER_TEMP_MOTOR
} mc_fault_code;
typedef enum {
CONTROL_MODE_DUTY = 0,
CONTROL_MODE_SPEED,
CONTROL_MODE_CURRENT,
CONTROL_MODE_CURRENT_BRAKE,
CONTROL_MODE_POS,
CONTROL_MODE_NONE
} mc_control_mode;
// VESC Types
typedef struct {
float cycle_int_limit;
float cycle_int_limit_running;
float cycle_int_limit_max;
float comm_time_sum;
float comm_time_sum_min_rpm;
int32_t comms;
uint32_t time_at_comm;
} mc_rpm_dep_struct;
float v_in;
float temp_mos1;
float temp_mos2;
float temp_mos3;
float temp_mos4;
float temp_mos5;
float temp_mos6;
float temp_pcb;
float current_motor;
float current_in;
float rpm;
float duty_now;
float amp_hours;
float amp_hours_charged;
float watt_hours;
float watt_hours_charged;
int32_t tachometer;
int tachometer_abs;
mc_fault_code fault_code;
} mc_values;
typedef struct {
// Switching and drive
......@@ -132,6 +128,30 @@ typedef struct {
// Hall sensor
int8_t hall_table[8];
float hall_sl_erpm;
// FOC
float foc_current_kp;
float foc_current_ki;
float foc_f_sw;
float foc_dt_us;
float foc_encoder_offset;
bool foc_encoder_inverted;
float foc_encoder_ratio;
float foc_motor_l;
float foc_motor_r;
float foc_motor_flux_linkage;
float foc_observer_gain;
float foc_pll_kp;
float foc_pll_ki;
float foc_duty_dowmramp_kp;
float foc_duty_dowmramp_ki;
float foc_openloop_rpm;
float foc_sl_openloop_hyst;
float foc_sl_openloop_time;
float foc_sl_d_current_duty;
float foc_sl_d_current_factor;
mc_foc_sensor_mode foc_sensor_mode;
uint8_t foc_hall_table[8];
float foc_hall_sl_erpm;
// Speed PID
float s_pid_kp;
float s_pid_ki;
......@@ -141,6 +161,7 @@ typedef struct {
float p_pid_kp;
float p_pid_ki;
float p_pid_kd;
float p_pid_ang_div;
// Current controller
float cc_startup_boost_duty;
float cc_min_current;
......@@ -151,6 +172,7 @@ typedef struct {
float m_duty_ramp_step;
float m_duty_ramp_step_rpm_lim;
float m_current_backoff_gain;
uint32_t m_encoder_counts;
} mc_configuration;
// Applications to use
......@@ -201,6 +223,7 @@ typedef enum {
ADC_CTRL_TYPE_CURRENT_REV_BUTTON,
ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_CENTER,
ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_BUTTON,
ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_ADC,
ADC_CTRL_TYPE_DUTY,
ADC_CTRL_TYPE_DUTY_REV_CENTER,
ADC_CTRL_TYPE_DUTY_REV_BUTTON
......@@ -244,6 +267,62 @@ typedef struct {
float tc_max_diff;
} chuk_config;
// NRF Datatypes
typedef enum {
NRF_SPEED_250K = 0,
NRF_SPEED_1M,
NRF_SPEED_2M
} NRF_SPEED;
typedef enum {
NRF_POWER_M18DBM = 0,
NRF_POWER_M12DBM,
NRF_POWER_M6DBM,
NRF_POWER_0DBM
} NRF_POWER;
typedef enum {
NRF_AW_3 = 0,
NRF_AW_4,
NRF_AW_5
} NRF_AW;
typedef enum {
NRF_CRC_DISABLED = 0,
NRF_CRC_1B,
NRF_CRC_2B
} NRF_CRC;
typedef enum {
NRF_RETR_DELAY_250US = 0,
NRF_RETR_DELAY_500US,
NRF_RETR_DELAY_750US,
NRF_RETR_DELAY_1000US,
NRF_RETR_DELAY_1250US,
NRF_RETR_DELAY_1500US,
NRF_RETR_DELAY_1750US,
NRF_RETR_DELAY_2000US,
NRF_RETR_DELAY_2250US,
NRF_RETR_DELAY_2500US,
NRF_RETR_DELAY_2750US,
NRF_RETR_DELAY_3000US,
NRF_RETR_DELAY_3250US,
NRF_RETR_DELAY_3500US,
NRF_RETR_DELAY_3750US,
NRF_RETR_DELAY_4000US
} NRF_RETR_DELAY;
typedef struct {
NRF_SPEED speed;
NRF_POWER power;
NRF_CRC crc_type;
NRF_RETR_DELAY retry_delay;
unsigned char retries;
unsigned char channel;
unsigned char address[3];
bool send_crc_ack;
} nrf_config;
typedef struct {
// Settings
uint8_t controller_id;
......@@ -266,6 +345,9 @@ typedef struct {
// Nunchuk application settings
chuk_config app_chuk_conf;
// NRF application settings
nrf_config app_nrf_conf;
} app_configuration;
// Communication commands
......@@ -284,119 +366,28 @@ typedef enum {
COMM_SET_SERVO_POS,
COMM_SET_MCCONF,
COMM_GET_MCCONF,
COMM_GET_MCCONF_DEFAULT,
COMM_SET_APPCONF,
COMM_GET_APPCONF,
COMM_GET_APPCONF_DEFAULT,
COMM_SAMPLE_PRINT,
COMM_TERMINAL_CMD,
COMM_PRINT,
COMM_ROTOR_POSITION,
COMM_EXPERIMENT_SAMPLE,
COMM_DETECT_MOTOR_PARAM,
COMM_DETECT_MOTOR_R_L,
COMM_DETECT_MOTOR_FLUX_LINKAGE,
COMM_DETECT_ENCODER,
COMM_DETECT_HALL_FOC,
COMM_REBOOT,
COMM_ALIVE,
COMM_GET_DECODED_PPM,
COMM_GET_DECODED_ADC,
COMM_GET_DECODED_CHUK,
COMM_FORWARD_CAN
COMM_FORWARD_CAN,
COMM_SET_CHUCK_DATA,
COMM_CUSTOM_APP_DATA
} COMM_PACKET_ID;
// CAN commands
typedef enum {
CAN_PACKET_SET_DUTY = 0,
CAN_PACKET_SET_CURRENT,
CAN_PACKET_SET_CURRENT_BRAKE,
CAN_PACKET_SET_RPM,
CAN_PACKET_SET_POS,
CAN_PACKET_FILL_RX_BUFFER,
CAN_PACKET_FILL_RX_BUFFER_LONG,
CAN_PACKET_PROCESS_RX_BUFFER,
CAN_PACKET_PROCESS_SHORT_BUFFER,
CAN_PACKET_STATUS
} CAN_PACKET_ID;
// Logged fault data
typedef struct {
mc_fault_code fault;
float current;
float current_filtered;
float voltage;
float duty;
float rpm;
int tacho;
int cycles_running;
int pwm_cycles;
int tim_val_samp;
int tim_current_samp;
int tim_top;
int comm_step;
float temperature;
} fault_data;
// External LED state
typedef enum {
LED_EXT_OFF = 0,
LED_EXT_NORMAL,
LED_EXT_BRAKE,
LED_EXT_TURN_LEFT,
LED_EXT_TURN_RIGHT,
LED_EXT_BRAKE_TURN_LEFT,
LED_EXT_BRAKE_TURN_RIGHT,
LED_EXT_BATT
} LED_EXT_STATE;
typedef struct {
int js_x;
int js_y;
int acc_x;
int acc_y;
int acc_z;
bool bt_c;
bool bt_z;
} chuck_data;
typedef struct {
int id;
systime_t rx_time;
float rpm;
float current;
float duty;
} can_status_msg;
typedef struct {
uint8_t js_x;
uint8_t js_y;
bool bt_c;
bool bt_z;
bool bt_push;
float vbat;
} mote_state;
typedef enum {
MOTE_PACKET_BATT_LEVEL = 0,
MOTE_PACKET_BUTTONS,
MOTE_PACKET_ALIVE
} MOTE_PACKET;
typedef struct {
float v_in;
float temp_mos1;
float temp_mos2;
float temp_mos3;
float temp_mos4;
float temp_mos5;
float temp_mos6;
float temp_pcb;
float current_motor;
float current_in;
float rpm;
float duty_now;
float amp_hours;
float amp_hours_charged;
float watt_hours;
float watt_hours_charged;
int tachometer;
int tachometer_abs;
mc_fault_code fault_code;
} mc_values;
#endif /* DATATYPES_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment