Skip to content

Commit

Permalink
FW 3.9: Final float serialization update, new option to invert motor …
Browse files Browse the repository at this point in the history
…direction, UUID readout
  • Loading branch information
vedderb committed Nov 6, 2016
1 parent 5dbc94c commit bb2bfa3
Show file tree
Hide file tree
Showing 26 changed files with 219 additions and 202 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
=== FW 3.9 ===
* Configuration option for inverting the motor direction.
* STM32 96-bit unique ID in firmware version command.

=== FW 3.8 ===
* Communication protocol update for floating point variables. This breaks almost all compatibility with old firmwares.

=== FW 3.7 ===
* Delay after app and motor conf write.
- Fixes NRF bug.
Expand Down
13 changes: 4 additions & 9 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *
* return 0.0;
* }
*
* *e = ceilf(logf(fabsf(f)) / logf(2.0));
* *e = ceilf(log2f(fabsf(f)));
* float res = f / powf(2.0, (float)*e);
*
* if (res >= 1.0) {
Expand All @@ -88,7 +88,7 @@ void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *
* return f * powf(2.0, (float)e);
* }
*
* 8388608.0 is 2^23, which scales the result to fit within 24 bits.
* 8388608.0 is 2^23, which scales the result to fit within 23 bits if sig_abs < 1.0.
*
* This should be a relatively fast and efficient way to serialize
* floating point numbers in a fully defined manner.
Expand All @@ -99,14 +99,9 @@ void buffer_append_float32_auto(uint8_t* buffer, float number, int32_t *index) {
float sig_abs = fabsf(sig);
uint32_t sig_i = 0;

// Exponent range -126 to +127. This addition makes the bits look similar
// to actual floats on most system, so a direct memcpy can be done if you
// are lazy or know what you are doing. Otherwise, this manual encoding and
// decoding hopefully is portable and safe.
e += 126;

if (sig_abs >= 0.5) {
sig_i = (uint32_t)((sig_abs - 0.5f) * 2.0f * 8388608.0f);
e += 126;
}

uint32_t res = ((e & 0xFF) << 23) | (sig_i & 0x7FFFFF);
Expand Down Expand Up @@ -163,11 +158,11 @@ float buffer_get_float32_auto(const uint8_t *buffer, int32_t *index) {
int e = (res >> 23) & 0xFF;
uint32_t sig_i = res & 0x7FFFFF;
bool neg = res & (1 << 31);
e -= 126;

float sig = 0.0;
if (e != 0 || sig_i != 0) {
sig = (float)sig_i / (8388608.0 * 2.0) + 0.5;
e -= 126;
}

if (neg) {
Expand Down
Binary file modified build_all/410_o_411_o_412/VESC_0005ohm.bin
Binary file not shown.
Binary file modified build_all/410_o_411_o_412/VESC_default.bin
Binary file not shown.
Binary file modified build_all/410_o_411_o_412/VESC_servoout.bin
Binary file not shown.
Binary file modified build_all/410_o_411_o_412/VESC_ws2811.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_0005ohm.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_33k.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_default.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_servoout.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_ws2811.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_ws2811_33k.bin
Binary file not shown.
Binary file modified build_all/48/VESC_0005ohm.bin
Binary file not shown.
Binary file modified build_all/48/VESC_default.bin
Binary file not shown.
Binary file modified build_all/48/VESC_servoout.bin
Binary file not shown.
Binary file modified build_all/48/VESC_ws2811.bin
Binary file not shown.
Binary file modified build_all/60/VESC_default.bin
Binary file not shown.
Binary file modified build_all/60/VESC_servoout.bin
Binary file not shown.
Binary file modified build_all/60/VESC_ws2811.bin
Binary file not shown.
Binary file modified build_all/DAS_RS/VESC_default.bin
Binary file not shown.
323 changes: 164 additions & 159 deletions commands.c

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions conf_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ void conf_general_get_default_mc_configuration(mc_configuration *conf) {
conf->m_current_backoff_gain = MCCONF_M_CURRENT_BACKOFF_GAIN;
conf->m_encoder_counts = MCCONF_M_ENCODER_COUNTS;
conf->m_sensor_port_mode = MCCONF_M_SENSOR_PORT_MODE;
conf->m_invert_direction = MCCONF_M_INVERT_DIRECTION;
}

/**
Expand Down Expand Up @@ -411,7 +412,7 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut
}

// Wait one second for things to get ready after
// the fault disapears. (will fry things otherwise...)
// the fault disappears. (will fry things otherwise...)
chThdSleepMilliseconds(1000);

// Disable timeout
Expand Down Expand Up @@ -446,9 +447,9 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut
// Release the motor and wait a few commutations
mc_interface_lock_override_once();
mc_interface_set_current(0.0);
int tacho = mcpwm_get_tachometer_value(0);
int tacho = mc_interface_get_tachometer_value(0);
for (int i = 0;i < 2000;i++) {
if ((mcpwm_get_tachometer_value(0) - tacho) < 3) {
if ((mc_interface_get_tachometer_value(0) - tacho) < 3) {
chThdSleepMilliseconds(1);
} else {
ok_steps++;
Expand All @@ -458,9 +459,9 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut

// Average the cycle integrator for 50 commutations
mcpwm_read_reset_avg_cycle_integrator();
tacho = mcpwm_get_tachometer_value(0);
tacho = mc_interface_get_tachometer_value(false);
for (int i = 0;i < 3000;i++) {
if ((mcpwm_get_tachometer_value(0) - tacho) < 50) {
if ((mc_interface_get_tachometer_value(false) - tacho) < 50) {
chThdSleepMilliseconds(1);
} else {
ok_steps++;
Expand Down Expand Up @@ -488,11 +489,11 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut

// Average the cycle integrator for 100 commutations
mcpwm_read_reset_avg_cycle_integrator();
tacho = mcpwm_get_tachometer_value(0);
tacho = mc_interface_get_tachometer_value(0);
float rpm_sum = 0.0;
float rpm_iterations = 0.0;
for (int i = 0;i < 3000;i++) {
if ((mcpwm_get_tachometer_value(0) - tacho) < 100) {
if ((mc_interface_get_tachometer_value(0) - tacho) < 100) {
rpm_sum += mc_interface_get_rpm();
rpm_iterations += 1;
chThdSleepMilliseconds(1);
Expand Down
14 changes: 8 additions & 6 deletions conf_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@

// Firmware version
#define FW_VERSION_MAJOR 3
#define FW_VERSION_MINOR 7
#define FW_VERSION_MINOR 9

#include "datatypes.h"

/*
* Settings
*/
#define SYSTEM_CORE_CLOCK 168000000

// Settings and parameters to override
//#define VIN_R1 33000.0
//#define VIN_R1 39200.0
Expand Down Expand Up @@ -136,6 +131,13 @@
#define AS5047_USE_HW_SPI_PINS 0
#endif

/*
* Settings / Macros
*/
#define SYSTEM_CORE_CLOCK 168000000
#define STM32_UUID ((uint32_t *)0x1FFF7A10)
#define STM32_UUID_8 ((uint8_t *)0x1FFF7A10)

// Functions
void conf_general_init(void);
void conf_general_get_default_app_configuration(app_configuration *conf);
Expand Down
1 change: 1 addition & 0 deletions datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ typedef struct {
float m_current_backoff_gain;
uint32_t m_encoder_counts;
sensor_port_mode m_sensor_port_mode;
bool m_invert_direction;
} mc_configuration;

// Applications to use
Expand Down
45 changes: 24 additions & 21 deletions mc_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include "encoder.h"
#include <math.h>

// Macros
#define DIR_MULT (m_conf.m_invert_direction ? -1.0 : 1.0)

// Global variables
volatile uint16_t ADC_Value[HW_ADC_CHANNELS];
volatile int ADC_curr_norm_value[3];
Expand Down Expand Up @@ -305,11 +308,11 @@ void mc_interface_set_duty(float dutyCycle) {
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_duty(dutyCycle);
mcpwm_set_duty(DIR_MULT * dutyCycle);
break;

case MOTOR_TYPE_FOC:
mcpwm_foc_set_duty(dutyCycle);
mcpwm_foc_set_duty(DIR_MULT * dutyCycle);
break;

default:
Expand All @@ -325,11 +328,11 @@ void mc_interface_set_duty_noramp(float dutyCycle) {
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_duty_noramp(dutyCycle);
mcpwm_set_duty_noramp(DIR_MULT * dutyCycle);
break;

case MOTOR_TYPE_FOC:
mcpwm_foc_set_duty_noramp(dutyCycle);
mcpwm_foc_set_duty_noramp(DIR_MULT * dutyCycle);
break;

default:
Expand All @@ -345,11 +348,11 @@ void mc_interface_set_pid_speed(float rpm) {
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_pid_speed(rpm);
mcpwm_set_pid_speed(DIR_MULT * rpm);
break;

case MOTOR_TYPE_FOC:
mcpwm_foc_set_pid_speed(rpm);
mcpwm_foc_set_pid_speed(DIR_MULT * rpm);
break;

default:
Expand All @@ -367,11 +370,11 @@ void mc_interface_set_pid_pos(float pos) {
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_pid_pos(pos);
mcpwm_set_pid_pos(DIR_MULT * m_position_set);
break;

case MOTOR_TYPE_FOC:
mcpwm_foc_set_pid_pos(pos);
mcpwm_foc_set_pid_pos(DIR_MULT * m_position_set);
break;

default:
Expand All @@ -387,11 +390,11 @@ void mc_interface_set_current(float current) {
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_current(current);
mcpwm_set_current(DIR_MULT * current);
break;

case MOTOR_TYPE_FOC:
mcpwm_foc_set_current(current);
mcpwm_foc_set_current(DIR_MULT * current);
break;

default:
Expand All @@ -407,11 +410,11 @@ void mc_interface_set_brake_current(float current) {
switch (m_conf.motor_type) {
case MOTOR_TYPE_BLDC:
case MOTOR_TYPE_DC:
mcpwm_set_brake_current(current);
mcpwm_set_brake_current(DIR_MULT * current);
break;

case MOTOR_TYPE_FOC:
mcpwm_foc_set_brake_current(current);
mcpwm_foc_set_brake_current(DIR_MULT * current);
break;

default:
Expand Down Expand Up @@ -471,7 +474,7 @@ float mc_interface_get_duty_cycle_set(void) {
break;
}

return ret;
return DIR_MULT * ret;
}

float mc_interface_get_duty_cycle_now(void) {
Expand All @@ -491,7 +494,7 @@ float mc_interface_get_duty_cycle_now(void) {
break;
}

return ret;
return DIR_MULT * ret;
}

float mc_interface_get_sampling_frequency_now(void) {
Expand Down Expand Up @@ -531,7 +534,7 @@ float mc_interface_get_rpm(void) {
break;
}

return ret;
return DIR_MULT * ret;
}

/**
Expand Down Expand Up @@ -667,7 +670,7 @@ float mc_interface_get_tot_current_directional(void) {
break;
}

return ret;
return DIR_MULT * ret;
}

float mc_interface_get_tot_current_directional_filtered(void) {
Expand All @@ -687,7 +690,7 @@ float mc_interface_get_tot_current_directional_filtered(void) {
break;
}

return ret;
return DIR_MULT * ret;
}

float mc_interface_get_tot_current_in(void) {
Expand Down Expand Up @@ -747,7 +750,7 @@ int mc_interface_get_tachometer_value(bool reset) {
break;
}

return ret;
return DIR_MULT * ret;
}

int mc_interface_get_tachometer_abs_value(bool reset) {
Expand Down Expand Up @@ -814,7 +817,7 @@ float mc_interface_read_reset_avg_id(void) {
float res = m_motor_id_sum / m_motor_id_iterations;
m_motor_id_sum = 0.0;
m_motor_id_iterations = 0.0;
return res;
return DIR_MULT * res; // TODO: DIR_MULT?
}

/**
Expand All @@ -827,7 +830,7 @@ float mc_interface_read_reset_avg_iq(void) {
float res = m_motor_iq_sum / m_motor_iq_iterations;
m_motor_iq_sum = 0.0;
m_motor_iq_iterations = 0.0;
return res;
return DIR_MULT * res;
}

float mc_interface_get_pid_pos_set(void) {
Expand All @@ -851,7 +854,7 @@ float mc_interface_get_pid_pos_now(void) {
break;
}

return ret;
return DIR_MULT * ret;
}

float mc_interface_get_last_sample_adc_isr_duration(void) {
Expand Down
3 changes: 3 additions & 0 deletions mcconf/mcconf_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,8 @@
#ifndef MCCONF_M_SENSOR_PORT_MODE
#define MCCONF_M_SENSOR_PORT_MODE SENSOR_PORT_MODE_HALL // The mode of the hall_encoder port
#endif
#ifndef MCCONF_M_INVERT_DIRECTION
#define MCCONF_M_INVERT_DIRECTION false // Invert the motor direction
#endif

#endif /* MCCONF_DEFAULT_H_ */

0 comments on commit bb2bfa3

Please sign in to comment.