Skip to content
Permalink
Browse files

Updates for Rover software stack.

  • Loading branch information...
tlalexander committed Mar 12, 2018
1 parent 47c0826 commit e74d3a3e4c2047b239084c152b4cbfddafbe3145
Showing with 84 additions and 14 deletions.
  1. +2 −2 appconf/appconf_default.h
  2. +20 −4 comm_can.c
  3. +22 −0 commands.c
  4. +1 −0 commands.h
  5. +3 −3 conf_general.h
  6. +5 −2 datatypes.h
  7. +27 −0 encoder.c
  8. +1 −0 encoder.h
  9. +3 −3 mcconf/mcconf_default.h
@@ -37,13 +37,13 @@
#define APPCONF_SEND_CAN_STATUS_RATE_HZ 500
#endif
#ifndef APPCONF_CAN_BAUD_RATE
#define APPCONF_CAN_BAUD_RATE CAN_BAUD_500K
#define APPCONF_CAN_BAUD_RATE CAN_BAUD_1M
#endif

// The default app is UART in case the UART port is used for
// firmware updates.
#ifndef APPCONF_APP_TO_USE
#define APPCONF_APP_TO_USE APP_UART
#define APPCONF_APP_TO_USE APP_NONE
#endif

// PPM app configureation
@@ -31,6 +31,7 @@
#include "app.h"
#include "crc.h"
#include "packet.h"
#include "encoder.h"

// Settings
#define CANDx CAND1
@@ -155,6 +156,7 @@ static THD_FUNCTION(cancom_process_thread, arg) {
uint8_t crc_low;
uint8_t crc_high;
bool commands_send;
uint8_t send_buffer[8];

for(;;) {
chEvtWaitAny((eventmask_t) 1);
@@ -179,12 +181,26 @@ static THD_FUNCTION(cancom_process_thread, arg) {
ind = 0;
mc_interface_set_current((float)buffer_get_int32(rxmsg.data8, &ind) / 1000.0);
timeout_reset();
ind = 0;
buffer_append_int32(send_buffer, encoder_cumulative_counts(), &ind);
buffer_append_float32_auto(send_buffer, mc_interface_get_rpm(), &ind);
//send_buffer[ind++] = mc_interface_get_fault();
comm_can_transmit_sid(app_get_configuration()->controller_id |
((uint32_t)CAN_PACKET_SEND_ROVER_DETAILS << 8), send_buffer, ind);


break;

case CAN_PACKET_SET_CURRENT_BRAKE:
ind = 0;
mc_interface_set_brake_current((float)buffer_get_int32(rxmsg.data8, &ind) / 1000.0);
timeout_reset();
ind = 0;
buffer_append_int32(send_buffer, encoder_cumulative_counts(), &ind);
buffer_append_float32_auto(send_buffer, mc_interface_get_rpm(), &ind);
//send_buffer[ind++] = mc_interface_get_fault();
comm_can_transmit_sid(app_get_configuration()->controller_id |
((uint32_t)CAN_PACKET_SEND_ROVER_DETAILS << 8), send_buffer, ind);
break;

case CAN_PACKET_SET_RPM:
@@ -193,10 +209,10 @@ static THD_FUNCTION(cancom_process_thread, arg) {
timeout_reset();
break;

case CAN_PACKET_SET_POS:
ind = 0;
mc_interface_set_pid_pos((float)buffer_get_int32(rxmsg.data8, &ind) / 1000000.0);
timeout_reset();
case CAN_PACKET_SEND_ROVER_DETAILS:
//ind = 0;
//mc_interface_set_pid_pos((float)buffer_get_int32(rxmsg.data8, &ind) / 1000000.0);
//timeout_reset();
break;

case CAN_PACKET_FILL_RX_BUFFER:
@@ -196,6 +196,18 @@ void commands_process_packet(unsigned char *data, unsigned int len) {
timeout_reset();
break;

case COMM_SET_CURRENT_GET_POSITION:
ind = 0;
mc_interface_set_current((float)buffer_get_int32(data, &ind) / 1000.0);
timeout_reset();
ind = 0;
send_buffer[ind++] = COMM_ROTOR_POSITION_CUMULATIVE;
buffer_append_int32(send_buffer, encoder_cumulative_counts(), &ind);
//buffer_append_float32(send_buffer, mc_interface_get_rpm(), 1e0, &ind);
//send_buffer[ind++] = mc_interface_get_fault();
commands_send_packet(send_buffer, ind);
break;

case COMM_SET_CURRENT_BRAKE:
ind = 0;
mc_interface_set_brake_current((float)buffer_get_int32(data, &ind) / 1000.0);
@@ -884,6 +896,16 @@ void commands_send_rotor_pos(float rotor_pos) {
commands_send_packet(buffer, index);
}

void commands_send_rotor_pos_counts(int32_t rotor_pos) {
uint8_t buffer[5];
int32_t index = 0;

buffer[index++] = COMM_ROTOR_POSITION_CUMULATIVE;
buffer_append_int32(buffer, (int32_t)(rotor_pos), &index);

commands_send_packet(buffer, index);
}

void commands_send_experiment_samples(float *samples, int len) {
if ((len * 4 + 1) > 256) {
return;
@@ -29,6 +29,7 @@ void commands_send_packet(unsigned char *data, unsigned int len);
void commands_process_packet(unsigned char *data, unsigned int len);
void commands_printf(const char* format, ...);
void commands_send_rotor_pos(float rotor_pos);
void commands_send_rotor_pos_counts(int32_t rotor_pos);
void commands_send_experiment_samples(float *samples, int len);
disp_pos_mode commands_get_disp_pos_mode(void);
void commands_set_app_data_handler(void(*func)(unsigned char *data, unsigned int len));
@@ -36,7 +36,7 @@
//#define WS2811_TEST 1
//#define CURR1_DOUBLE_SAMPLE 0
//#define CURR2_DOUBLE_SAMPLE 0
//#define AS5047_USE_HW_SPI_PINS 1
#define AS5047_USE_HW_SPI_PINS 1

// Disable hardware limits on configuration parameters
//#define DISABLE_HW_LIMITS
@@ -57,8 +57,8 @@
//#define HW_VERSION_46 // Also for 4.7
//#define HW_VERSION_48
//#define HW_VERSION_49
//#define HW_VERSION_410 // Also for 4.11 and 4.12
#define HW_VERSION_60
#define HW_VERSION_410 // Also for 4.11 and 4.12
//#define HW_VERSION_60
//#define HW_VERSION_R2
//#define HW_VERSION_VICTOR_R1A
//#define HW_VERSION_DAS_RS
@@ -481,7 +481,9 @@ typedef enum {
COMM_FORWARD_CAN,
COMM_SET_CHUCK_DATA,
COMM_CUSTOM_APP_DATA,
COMM_NRF_START_PAIRING
COMM_NRF_START_PAIRING,
COMM_ROTOR_POSITION_CUMULATIVE,
COMM_SET_CURRENT_GET_POSITION
} COMM_PACKET_ID;

// CAN commands
@@ -497,7 +499,8 @@ typedef enum {
CAN_PACKET_PROCESS_SHORT_BUFFER,
CAN_PACKET_STATUS,
CAN_PACKET_SET_CURRENT_REL,
CAN_PACKET_SET_CURRENT_BRAKE_REL
CAN_PACKET_SET_CURRENT_BRAKE_REL,
CAN_PACKET_SEND_ROVER_DETAILS
} CAN_PACKET_ID;

// Logged fault data
@@ -70,6 +70,8 @@ static bool index_found = false;
static uint32_t enc_counts = 10000;
static encoder_mode mode = ENCODER_MODE_NONE;
static float last_enc_angle = 0.0;
static int32_t cumulative_encoder_counts = 0;
static uint32_t last_encoder_counts = 0;

// Private functions
static void spi_transfer(uint16_t *in_buf, const uint16_t *out_buf, int length);
@@ -200,6 +202,16 @@ float encoder_read_deg(void) {
return angle;
}

int32_t encoder_cumulative_counts(void) {

if (mode==ENCODER_MODE_AS5047P_SPI)
{
return cumulative_encoder_counts;
}

return 0;
}

/**
* Reset the encoder counter. Should be called from the index interrupt.
*/
@@ -246,6 +258,21 @@ void encoder_tim_isr(void) {
spi_end();

pos &= 0x3FFF;

// Handle encoder rollover up or down.
if(last_encoder_counts < (16384 / 4) && pos > (3 * 16384 / 4))
{
cumulative_encoder_counts -= 16384;
} else
if(last_encoder_counts > (3 * 16384 / 4) && pos < (16384 / 4))
{
cumulative_encoder_counts += 16384;
}

// Now account for actual reading changes
cumulative_encoder_counts += (pos - last_encoder_counts);
last_encoder_counts = pos;

last_enc_angle = ((float)pos * 360.0) / 16384.0;
}

@@ -28,6 +28,7 @@ void encoder_init_abi(uint32_t counts);
void encoder_init_as5047p_spi(void);
bool encoder_is_configured(void);
float encoder_read_deg(void);
int32_t encoder_cumulative_counts(void);
void encoder_reset(void);
void encoder_tim_isr(void);
void encoder_set_counts(uint32_t counts);
@@ -22,7 +22,7 @@

// Default settings
#ifndef MCCONF_DEFAULT_MOTOR_TYPE
#define MCCONF_DEFAULT_MOTOR_TYPE MOTOR_TYPE_BLDC
#define MCCONF_DEFAULT_MOTOR_TYPE MOTOR_TYPE_FOC
#endif
#ifndef MCCONF_PWM_MODE
#define MCCONF_PWM_MODE PWM_MODE_SYNCHRONOUS // Default PWM mode
@@ -228,7 +228,7 @@
#define MCCONF_FOC_ENCODER_RATIO 7.0
#endif
#ifndef MCCONF_FOC_SENSOR_MODE
#define MCCONF_FOC_SENSOR_MODE FOC_SENSOR_MODE_SENSORLESS
#define MCCONF_FOC_SENSOR_MODE FOC_SENSOR_MODE_ENCODER
#endif
#ifndef MCCONF_FOC_PLL_KP
#define MCCONF_FOC_PLL_KP 2000.0
@@ -329,7 +329,7 @@
#define MCCONF_M_ENCODER_COUNTS 8192 // The number of encoder counts
#endif
#ifndef MCCONF_M_SENSOR_PORT_MODE
#define MCCONF_M_SENSOR_PORT_MODE SENSOR_PORT_MODE_HALL // The mode of the hall_encoder port
#define MCCONF_M_SENSOR_PORT_MODE SENSOR_PORT_MODE_AS5047_SPI // The mode of the hall_encoder port
#endif
#ifndef MCCONF_M_INVERT_DIRECTION
#define MCCONF_M_INVERT_DIRECTION false // Invert the motor direction

0 comments on commit e74d3a3

Please sign in to comment.
You can’t perform that action at this time.