Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/vesc interface #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Node/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build/
Build/
Drivers
Makefile
.mxproject
Expand Down
15 changes: 14 additions & 1 deletion Node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,20 @@ set(STM32CUBEMX_GENERATED_FILES
startup_stm32f334x8.s
)

set(STM32_USER_CREATED_FILES
Core/Src/vesc_packet.c
)

set(EXTERNAL_FILES
External/Src/bldc_interface_uart.c
External/Src/bldc_interface.c
External/Src/buffer.c
External/Src/crc.c
External/Src/packet.c
)

set(EXECUTABLE ${PROJECT_NAME}.out)
add_executable(${EXECUTABLE} ${STM32CUBEMX_GENERATED_FILES})
add_executable(${EXECUTABLE} ${STM32CUBEMX_GENERATED_FILES} ${EXTERNAL_FILES})

target_compile_definitions(${EXECUTABLE} PRIVATE
-DUSE_HAL_DRIVER
Expand All @@ -53,6 +65,7 @@ target_compile_definitions(${EXECUTABLE} PRIVATE

target_include_directories(${EXECUTABLE} PRIVATE
Core/Inc
External/Inc
Drivers/STM32F3xx_HAL_Driver/Inc
Drivers/CMSIS/Device/ST/STM32F3xx/Include
Drivers/CMSIS/Include
Expand Down
37 changes: 35 additions & 2 deletions Node/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "bldc_interface.h"
#include "bldc_interface_uart.h"
#include "can.h"
#include "i2c.h"
#include "spi.h"
#include "stm32f3xx_hal_usart.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
Expand Down Expand Up @@ -58,7 +61,20 @@ void SystemClock_Config(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
static void send_packet_motor_1(unsigned char* data, unsigned int len)
{
HAL_UART_Transmit(&huart1, data, len, 0);
}

static void send_packet_motor_2(unsigned char* data, unsigned int len)
{
HAL_UART_Transmit(&huart2, data, len, 0);
}

static void send_packet_motor_3(unsigned char* data, unsigned int len)
{
HAL_UART_Transmit(&huart3, data, len, 0);
}
/* USER CODE END 0 */

/**
Expand All @@ -68,7 +84,9 @@ void SystemClock_Config(void);
int main(void)
{
/* USER CODE BEGIN 1 */

BldcInterface motor1;
BldcInterface motor2;
BldcInterface motor3;
/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/
Expand Down Expand Up @@ -98,16 +116,31 @@ int main(void)
MX_TIM17_Init();
MX_I2C1_Init();
MX_SPI1_Init();

/* USER CODE BEGIN 2 */
bldc_interface_uart_init(&motor1, send_packet_motor_1);
bldc_interface_uart_init(&motor2, send_packet_motor_2);
bldc_interface_uart_init(&motor3, send_packet_motor_3);

uint8_t motor1_data;
uint8_t motor2_data;
uint8_t motor3_data;
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

// TODO receive more than one byte at a time
// TODO Error handling before processing bytes
HAL_UART_Receive(&huart1, &motor1_data, 1, 0);
HAL_UART_Receive(&huart2, &motor2_data, 1, 0);
HAL_UART_Receive(&huart3, &motor3_data, 1, 0);

bldc_interface_uart_process_byte(&motor1, motor1_data);
bldc_interface_uart_process_byte(&motor2, motor2_data);
bldc_interface_uart_process_byte(&motor3, motor3_data);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
Expand Down
121 changes: 121 additions & 0 deletions Node/External/Inc/bldc_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2016-2017 Benjamin Vedder [email protected]

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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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/>.
*/

#ifndef BLDC_INTERFACE_H_
#define BLDC_INTERFACE_H_

#include "datatypes.h"

typedef struct BldcInterface
{
// Private variables
unsigned char send_buffer[1024];

// Private variables for received data
mc_values values;
int fw_major;
int fw_minor;
float rotor_pos;
mc_configuration mcconf;
app_configuration appconf;
float detect_cycle_int_limit;
float detect_coupling_k;
signed char detect_hall_table[8];
signed char detect_hall_res;
float dec_ppm;
float dec_ppm_len;
float dec_adc;
float dec_adc_voltage;
float dec_chuk;

// Function pointers
void(*send_func)(unsigned char *data, unsigned int len);
void(*forward_func)(unsigned char *data, unsigned int len);

// Function pointers for received data
void(*rx_value_func)(mc_values *values);
void(*rx_printf_func)(char *str);
void(*rx_fw_func)(int major, int minor);
void(*rx_rotor_pos_func)(float pos);
void(*rx_mcconf_func)(mc_configuration *conf);
void(*rx_appconf_func)(app_configuration *conf);
void(*rx_detect_func)(float cycle_int_limit, float coupling_k,
const signed char *hall_table, signed char hall_res);
void(*rx_dec_ppm_func)(float val, float ms);
void(*rx_dec_adc_func)(float val, float voltage);
void(*rx_dec_chuk_func)(float val);
void(*rx_mcconf_received_func)(void);
void(*rx_appconf_received_func)(void);
void(*motor_control_set_func)(motor_control_mode mode, float value);
void(*values_requested_func)(void);
} BldcInterface;

// interface functions
void bldc_interface_init(BldcInterface* interface, void(*func)(unsigned char *data, unsigned int len));
void bldc_interface_set_forward_func(BldcInterface* interface, void(*func)(unsigned char *data, unsigned int len));
void bldc_interface_send_packet(BldcInterface* interface, unsigned char *data, unsigned int len);
void bldc_interface_process_packet(BldcInterface* interface, unsigned char *data, unsigned int len);

// Function pointer setters
void bldc_interface_set_rx_value_func(BldcInterface* interface, void(*func)(mc_values *values));
void bldc_interface_set_rx_printf_func(BldcInterface* interface, void(*func)(char *str));
void bldc_interface_set_rx_fw_func(BldcInterface* interface, void(*func)(int major, int minor));
void bldc_interface_set_rx_rotor_pos_func(BldcInterface* interface, void(*func)(float pos));
void bldc_interface_set_rx_mcconf_func(BldcInterface* interface, void(*func)(mc_configuration *conf));
void bldc_interface_set_rx_appconf_func(BldcInterface* interface, void(*func)(app_configuration *conf));
void bldc_interface_set_rx_detect_func(BldcInterface* interface, void(*func)(float cycle_int_limit, float coupling_k,
const signed char *hall_table, signed char hall_res));
void bldc_interface_set_rx_dec_ppm_func(BldcInterface* interface, void(*func)(float val, float ms));
void bldc_interface_set_rx_dec_adc_func(BldcInterface* interface, void(*func)(float val, float voltage));
void bldc_interface_set_rx_dec_chuk_func(BldcInterface* interface, void(*func)(float val));
void bldc_interface_set_rx_mcconf_received_func(BldcInterface* interface, void(*func)(void));
void bldc_interface_set_rx_appconf_received_func(BldcInterface* interface, void(*func)(void));

void bldc_interface_set_sim_control_function(BldcInterface* interface, void(*func)(motor_control_mode mode, float value));
void bldc_interface_set_sim_values_func(BldcInterface* interface, void(*func)(void));

// Setters
void bldc_interface_terminal_cmd(BldcInterface* interface, char* cmd);
void bldc_interface_set_duty_cycle(BldcInterface* interface, float dutyCycle);
void bldc_interface_set_current(BldcInterface* interface, float current);
void bldc_interface_set_current_brake(BldcInterface* interface, float current);
void bldc_interface_set_rpm(BldcInterface* interface, int rpm);
void bldc_interface_set_pos(BldcInterface* interface, float pos);
void bldc_interface_set_handbrake(BldcInterface* interface, float current);
void bldc_interface_set_servo_pos(BldcInterface* interface, float pos);
void bldc_interface_set_mcconf(BldcInterface* interface, const mc_configuration *mcconf);
void bldc_interface_set_appconf(BldcInterface* interface, const app_configuration *appconf);

// Getters
void bldc_interface_get_fw_version(BldcInterface* interface);
void bldc_interface_get_values(BldcInterface* interface);
void bldc_interface_get_mcconf(BldcInterface* interface);
void bldc_interface_get_appconf(BldcInterface* interface);
void bldc_interface_get_decoded_ppm(BldcInterface* interface);
void bldc_interface_get_decoded_adc(BldcInterface* interface);
void bldc_interface_get_decoded_chuk(BldcInterface* interface);

// Other functions
void bldc_interface_detect_motor_param(BldcInterface*, float current, float min_rpm, float low_duty);
void bldc_interface_reboot(BldcInterface*);
void bldc_interface_send_alive(BldcInterface*);
void send_values_to_receiver(BldcInterface*, mc_values *values);

// Helpers
const char* bldc_interface_fault_to_string(mc_fault_code fault);

#endif /* BLDC_INTERFACE_H_ */
37 changes: 37 additions & 0 deletions Node/External/Inc/bldc_interface_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2015 Benjamin Vedder [email protected]

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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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/>.
*/

/*
* bldc_interface_uart.h
*
* Created on: 9 okt 2015
* Author: benjamin
*/

#ifndef BLDC_INTERFACE_UART_H_
#define BLDC_INTERFACE_UART_H_

// Includes
#include "packet.h" // For the MAX_PACKET_LEN define
#include "bldc_interface.h"

// Functions
void bldc_interface_uart_init(BldcInterface* interface, void(*func)(unsigned char *data, unsigned int len));
void bldc_interface_uart_process_byte(BldcInterface* interface, unsigned char b);
void bldc_interface_uart_run_timer(BldcInterface* interface);

#endif /* BLDC_INTERFACE_UART_H_ */
44 changes: 44 additions & 0 deletions Node/External/Inc/buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2012-2018 Benjamin Vedder [email protected]

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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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/>.
*/

#ifndef BUFFER_H_
#define BUFFER_H_

#include <stdint.h>

void buffer_append_int16(uint8_t* buffer, int16_t number, int32_t *index);
void buffer_append_uint16(uint8_t* buffer, uint16_t number, int32_t *index);
void buffer_append_int32(uint8_t* buffer, int32_t number, int32_t *index);
void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index);
void buffer_append_int64(uint8_t* buffer, int64_t number, int32_t *index);
void buffer_append_uint64(uint8_t* buffer, uint64_t number, int32_t *index);
void buffer_append_float16(uint8_t* buffer, float number, float scale, int32_t *index);
void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *index);
void buffer_append_double64(uint8_t* buffer, double number, double scale, int32_t *index);
void buffer_append_float32_auto(uint8_t* buffer, float number, int32_t *index);
int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index);
uint16_t buffer_get_uint16(const uint8_t *buffer, int32_t *index);
int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index);
uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index);
int64_t buffer_get_int64(const uint8_t *buffer, int32_t *index);
uint64_t buffer_get_uint64(const uint8_t *buffer, int32_t *index);
float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index);
float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index);
double buffer_get_double64(const uint8_t *buffer, double scale, int32_t *index);
float buffer_get_float32_auto(const uint8_t *buffer, int32_t *index);

#endif /* BUFFER_H_ */
33 changes: 33 additions & 0 deletions Node/External/Inc/crc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2012-2014 Benjamin Vedder [email protected]

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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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/>.
*/

/*
* crc.h
*
* Created on: 26 feb 2012
* Author: benjamin
*/

#ifndef CRC_H_
#define CRC_H_

/*
* Functions
*/
unsigned short crc16(unsigned char *buf, unsigned int len);

#endif /* CRC_H_ */
Loading