Update Docs; Add 3D-Models for flash and Motor driver; Update Readme; Check program code for exceeding size in CMakeLists.txt; Moved measure() function to core1.c/core1.h; Added direction enum typedef; Update Control Loop Block Diagram;

This commit is contained in:
gab-k committed 2024-12-25 21:47:52 +01:00
1 parent 7d2c80574a
commit acdbf3cffe
25 files changed
+1195 -632

No files matched your search

+69 -5
View File
@@ -53,7 +53,9 @@ pico_set_program_name(RP2040-Decoder "RP2040-Decoder")
pico_set_program_version(RP2040-Decoder "1.0")
# LOGLEVEL>0 enables debugging via stdio, this can be done either via USB or alternatively,
# Logging configuration:
#
# LOGLEVEL>0 enables logging messages via stdio, this can be done either via USB or alternatively,
# directly with the UART TX/RX pins (see pin definition in RP2040-Decoder-board.h).
#
# In order to use stdio via USB please set STDIO_USB_ENABLED to 1 below
@@ -62,9 +64,9 @@ pico_set_program_version(RP2040-Decoder "1.0")
# Additional Notes:
# - When logging is disabled (LOGLEVEL 0), all logging functions will be completely excluded during compilation.
# - Be cautious: Logging can alter timing behavior. For example, the PID controller is time critical,
# limit the frequency of log outputs—such as printing only every 100th iteration.
# - Note that with UART logging enabled, PICO_DEFAULT_UART_TX_PIN and PICO_DEFAULT_UART_RX_PIN are disabled for decoder functions
# because they are used for UART communication. This is automatically configured for you below.
# limit the frequency of log outputs—such as logging only every 100th iteration.
# - Note that with UART logging enabled, PICO_DEFAULT_UART_TX_PIN and PICO_DEFAULT_UART_RX_PIN are disabled for decoder functions.
# These pins are used for UART communication. Disablign is done automatically in RP2040-Decoder-board.h or RP2040-Decoder-board-legacy.h respectively.
set(LOGLEVEL 1)
# Additionally, the LOG_WAIT parameter can be configured to make the controller wait for user input via stdio before continuing.
# This is useful for ensuring that all stdio logging outputs are captured.
@@ -82,7 +84,8 @@ target_compile_definitions( RP2040-Decoder PRIVATE
LOGLEVEL=${LOGLEVEL}
LOG_WAIT=${LOG_WAIT}
STDIO_UART_ENABLED=${STDIO_UART_ENABLED}
STDIO_USB_ENABLED=${STDIO_USB_ENABLED})
STDIO_USB_ENABLED=${STDIO_USB_ENABLED}
)
# Warn when logging is enabled (LOGLEVEL>0) and neither stdio is enabled
if (LOGLEVEL GREATER 0 AND (STDIO_USB_ENABLED EQUAL 0 AND STDIO_UART_ENABLED EQUAL 0))
message(WARNING "\nWARNING: Logging (LOGLEVEL>0) enabled with both STDIO_USB_ENABLED and STDIO_UART_ENABLED disabled!")
@@ -107,3 +110,64 @@ target_include_directories(RP2040-Decoder PRIVATE ${CMAKE_CURRENT_LIST_DIR})
# create map/bin/hex file etc.
pico_add_extra_outputs(RP2040-Decoder)
# Program size check
#
# Check for program size exceeding limits as CVs are stored at the end of flash, the program could theoretically overwrite this.
# In practice the program size is usually only about 100KiB.
# Extract the directory of the C compiler and construct the path to the 'arm-none-eabi-size' tool
get_filename_component(C_COMPILER_DIR ${CMAKE_C_COMPILER} DIRECTORY)
set(SIZE_TOOL ${C_COMPILER_DIR}/arm-none-eabi-size)
# Add custom command to check the flash size after build
add_custom_command(TARGET RP2040-Decoder POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Checking flash size for $<TARGET_FILE:RP2040-Decoder>"
COMMAND ${SIZE_TOOL} $<TARGET_FILE:RP2040-Decoder> > size_output.txt
)
# Read the size output file
file(READ "${CMAKE_BINARY_DIR}/size_output.txt" SIZE_OUTPUT)
# Print the content of SIZE_OUTPUT
message(STATUS "arm-none-eabi-size output:\n${SIZE_OUTPUT}")
# Use regex to match all numbers
string(REGEX MATCHALL "[0-9]+" MATCHED_VALUES "${SIZE_OUTPUT}")
# Get third element from list of matches, this corresponds with dec table value in "size_output.txt"
list(GET MATCHED_VALUES 3 PROGRAM_SIZE_DEC)
# Check for success of program size extraction from "size_output.txt"
if(PROGRAM_SIZE_DEC)
message(STATUS "PROGRAM_SIZE_DEC: ${PROGRAM_SIZE_DEC} bytes")
else()
message(FATAL_ERROR "Could not extract program size from size_output.txt")
endif()
# Path to the header file that defines PICO_FLASH_SIZE_BYTES
set(FLASH_CONFIG_HEADER ${CMAKE_SOURCE_DIR}/${PICO_BOARD}.h)
# Read the contents of the header file into a variable
file(READ ${FLASH_CONFIG_HEADER} HEADER_CONTENT)
# Extract the value of PICO_FLASH_SIZE_BYTES using a regular expression
string(REGEX MATCHALL "#define PICO_FLASH_SIZE_BYTES [0-9]+" MACRO_MATCH "${HEADER_CONTENT}")
string(REGEX MATCHALL "[0-9]+" PICO_FLASH_SIZE_BYTES "${MACRO_MATCH}")
# Check for success of PICO_FLASH_SIZE_BYTES extraction from board header file
if(PICO_FLASH_SIZE_BYTES)
message(STATUS "PICO_FLASH_SIZE_BYTES: ${PICO_FLASH_SIZE_BYTES} bytes")
else()
message(FATAL_ERROR "Failed to find PICO_FLASH_SIZE_BYTES in the header file.")
endif()
# Sector size is always 4096 bytes
set(FLASH_SECTOR_SIZE 4096)
message(STATUS "FLASH_SECTOR_SIZE: ${FLASH_SECTOR_SIZE} bytes")
# Subtract flash sector size from flash size
math(EXPR FLASH_TARGET_OFFSET "${PICO_FLASH_SIZE_BYTES} - ${FLASH_SECTOR_SIZE}")
message(STATUS "FLASH_TARGET_OFFSET: ${FLASH_TARGET_OFFSET} bytes")
if(PROGRAM_SIZE_DEC GREATER FLASH_TARGET_OFFSET)
message(FATAL_ERROR "Program size exceeds allowed flash size. Flash sector containing the CVs would be overwritten! Please reduce the size of the program.")
endif()
+3 -1
View File
@@ -40,9 +40,11 @@
// --- FLASH ---
// Flash size is 8 MiB for W25Q64JVSSIQ if you decide to use another capacity flash IC change the size to the corresponding value
// 8MiB = (8 * 1024 * 1024) bytes = 8388608 bytes
#ifndef PICO_FLASH_SIZE_BYTES
#define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024)
#define PICO_FLASH_SIZE_BYTES 8388608
#endif
// Offset from base address used for saving CV_ARRAY_FLASH - in this case one sector size from the end of flash
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE)
+3 -1
View File
@@ -43,9 +43,11 @@
// --- FLASH ---
// Flash size is 8 MiB for W25Q64JVSSIQ if you decide to use another capacity flash IC change the size to the corresponding value
// 8MiB = (8 * 1024 * 1024) bytes = 8388608 bytes
#ifndef PICO_FLASH_SIZE_BYTES
#define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024)
#define PICO_FLASH_SIZE_BYTES 8388608
#endif
// Offset from base address used for saving CV_ARRAY_FLASH - in this case one sector size from the end of flash
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE)
+6 -5
View File
@@ -316,8 +316,8 @@ void set_outputs(const uint32_t functions_to_set_bitmask) {
uint32_t temp_mask = 1;
for (uint8_t i = 0; i < 32; i++) {
if (temp_mask & functions_to_set_bitmask) {
const bool direction = get_direction_of_speed_step(speed_step_target);
outputs_to_set |= get_32bit_CV(i * 8 + 260 - 4 * direction);
const direction_t dir = get_direction_of_speed_step(speed_step_target);
outputs_to_set |= get_32bit_CV(i * 8 + 260 - 4 * dir);
}
temp_mask <<= 1;
}
@@ -444,7 +444,7 @@ void instruction_evaluation(const uint8_t number_of_bytes, const uint8_t *const
speed_step_target = byte_array[command_byte_start_index - 1];
// Check for direction change
const bool direction_changed = get_direction_of_speed_step(speed_step_target) !=
get_direction_of_speed_step(speed_step_target_prev);
get_direction_of_speed_step(speed_step_target_prev);
// In case of a direction change, functions need to be updated because functions depend on direction
if (direction_changed) {
update_active_functions(0, 0, true);
@@ -488,12 +488,13 @@ void instruction_evaluation(const uint8_t number_of_bytes, const uint8_t *const
// Check for reset message - When reset message is found, stop the motor and disable all functions.
bool reset_message_check(const uint8_t number_of_bytes, const uint8_t *const byte_array) {
if (byte_array[number_of_bytes - 1] == 0b00000000 && byte_array[number_of_bytes - 2] == 0b00000000) {
const bool direction = get_direction_of_speed_step(speed_step_target);
const direction_t dir = get_direction_of_speed_step(speed_step_target);
// Set previous speed step target to current speed step target
speed_step_target_prev = speed_step_target;
// TODO: Improve comment below
// Set speed step target to emergency stop speed step ( 1 for Reverse direction / 129 for forward direction)
// for details see get_direction_of_speed_step() doxygen comment in "shared.h"
speed_step_target = 1 + 128*direction;
speed_step_target = 1 + 128*dir;
update_active_functions(0,0,0); // Disables all functions
return true;
}
+41 -3
View File
@@ -9,6 +9,43 @@
struct repeating_timer pid_control_timer, speed_helper_timer;
// Measures Back-EMF voltage (proportional to the rotational speed of the motor) on GPIO 28 and GPIO 29 respectively (depending on direction)
float measure(uint8_t total_iterations,
uint8_t measurement_delay_us,
uint8_t l_side_arr_cutoff,
uint8_t r_side_arr_cutoff,
bool direction){ //TODO: Change to direction_t
uint32_t sum = 0;
uint16_t adc_val[total_iterations];
int32_t j = 0;
uint16_t key;
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
adc_select_input((!direction)+2); // TODO: fix with #define macros!
busy_wait_us(measurement_delay_us);
adc_run(true);
for (int32_t i = 0; i < total_iterations; ++i) {
//Take adc value out of fifo when available, then do one step of insertion sort.
adc_val[i] = adc_fifo_get_blocking();
key = adc_val[i];
j = i - 1;
while (j >= 0 && adc_val[j] > key) {
adc_val[j + 1] = adc_val[j];
j--;
}
adc_val[j + 1] = key;
}
adc_run(false);
//discard x entries beginning from the lowest entry of the array (counting up); x = msr.l_side_arr_cutoff
//discard y entries beginning from the highest index of the array (counting down); y = msr.r_side_arr_cutoff
for (int32_t i = l_side_arr_cutoff; i < total_iterations - r_side_arr_cutoff ; ++i) {
sum += adc_val[i];
}
float res = (float)sum / (float)( total_iterations - (l_side_arr_cutoff + r_side_arr_cutoff) );
// return true to keep the repeating timer active
return res;
}
// Get speed_step_table_index depending on speed_step
uint8_t get_speed_step_table_index_of_speed_step(uint8_t speed_step) {
// Clear direction information in bit7
@@ -79,12 +116,13 @@ bool speed_helper(struct repeating_timer *const t) {
// Helper function to adjust pwm level/duty cycle.
void adjust_pwm_level(const uint16_t level) {
if (get_direction_of_speed_step(speed_step_target)) {
direction_t dir = get_direction_of_speed_step(speed_step_target);
if (dir == DIRECTION_FORWARD) {
// Forward
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
pwm_set_gpio_level(MOTOR_FWD_PIN, level);
}
else {
else if (dir == DIRECTION_REVERSE){
// Reverse
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
pwm_set_gpio_level(MOTOR_REV_PIN, level);
@@ -186,7 +224,7 @@ void controller_pid_mode(controller_parameter_t *const ctrl_par) {
}
// General controller function gets called every x milliseconds where x is CV_49 i.e. sampling time (pid->t)
// TODO: fixed point arithmetic to improve performance...
// TODO: Consider fixed point arithmetic to improve performance...
bool controller_general(struct repeating_timer *const t) {
// Cast (void *) to (controller_parameter_t *)
controller_parameter_t *const ctrl_par = (controller_parameter_t *) (t->user_data);
+21
View File
@@ -82,6 +82,27 @@ typedef struct controller_parameter_t{
} controller_parameter_t;
/**
* @brief Measures Back-EMF voltage (proportional to the rotational speed of the motor) on GPIO 28 and GPIO 29 respectively (depending on direction).
*
* This function performs multiple ADC measurements, sorts the results using insertion sort, and calculates the average of the middle values,
* discarding a specified number of the lowest and highest values.
*
* @param total_iterations The total number of ADC measurements to perform.
* @param measurement_delay_us The delay in microseconds between the switch-off of the motor PWM and the measurement.
* @param l_side_arr_cutoff The number of lowest ADC values to discard.
* @param r_side_arr_cutoff The number of highest ADC values to discard.
* @param direction The direction of the motor. //TODO: Introduce Motor direction typedef
* @return The average of the middle ADC values after discarding the specified number of lowest and highest values.
*/
float measure(uint8_t total_iterations,
uint8_t measurement_delay_us,
uint8_t l_side_arr_cutoff,
uint8_t r_side_arr_cutoff,
bool direction);
/**
* @brief Get the speed step table index based on the speed step value.
*
+2 -40
View File
@@ -8,47 +8,9 @@
uint8_t speed_step_target = 0;
uint8_t speed_step_target_prev = 0;
uint32_t error_state = 0;
// Functions in shared.c can be accessed by both cores
// Measures Back-EMF voltage (proportional to the rotational speed of the motor) on GPIO 28 and GPIO 29 respectively (depending on direction)
float measure(uint8_t total_iterations,
uint8_t measurement_delay_us,
uint8_t l_side_arr_cutoff,
uint8_t r_side_arr_cutoff,
bool direction){
uint32_t sum = 0;
uint16_t adc_val[total_iterations];
int32_t j = 0;
uint16_t key;
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
adc_select_input((!direction)+2);
busy_wait_us(measurement_delay_us);
adc_run(true);
for (int32_t i = 0; i < total_iterations; ++i) {
//Take adc value out of fifo when available, then do one step of insertion sort.
adc_val[i] = adc_fifo_get_blocking();
key = adc_val[i];
j = i - 1;
while (j >= 0 && adc_val[j] > key) {
adc_val[j + 1] = adc_val[j];
j--;
}
adc_val[j + 1] = key;
}
adc_run(false);
//discard x entries beginning from the lowest entry of the array (counting up); x = msr.l_side_arr_cutoff
//discard y entries beginning from the highest index of the array (counting down); y = msr.r_side_arr_cutoff
for (int32_t i = l_side_arr_cutoff; i < total_iterations - r_side_arr_cutoff ; ++i) {
sum += adc_val[i];
}
float res = (float)sum / (float)( total_iterations - (l_side_arr_cutoff + r_side_arr_cutoff) );
// return true to keep the repeating timer active
return res;
}
// Functions in shared.c are accessed by both cores
// retrieve 4Bytes from CV_ARRAY_FLASH at once and return 32-Bit value
uint32_t get_32bit_CV (uint16_t CV_start_index){
@@ -67,7 +29,7 @@ uint16_t get_16bit_CV (uint16_t CV_start_index){
return (byte_0) + (byte_1<<8);
}
bool get_direction_of_speed_step(uint8_t speed_step){
direction_t get_direction_of_speed_step(uint8_t speed_step){
// Shift by 7 Bytes to move bit7 into bit0 position and return
return speed_step >> 7;
}
+7 -7
View File
@@ -28,6 +28,11 @@ typedef enum {
REBOOT_BY_WATCHDOG = (1<<5),
} error_t;
typedef enum {
DIRECTION_FORWARD = 1,
DIRECTION_REVERSE = 0,
} direction_t;
#define LOG(level, ...) { \
if (level <= LOGLEVEL) { \
@@ -49,11 +54,6 @@ extern uint8_t speed_step_target_prev;
void core1_entry();
void write_cv_handler(uint16_t cv_index, uint8_t cv_data);
float measure(uint8_t total_iterations,
uint8_t measurement_delay_us,
uint8_t l_side_arr_cutoff,
uint8_t r_side_arr_cutoff,
bool direction);
uint32_t get_32bit_CV (uint16_t CV_start_index);
uint16_t get_16bit_CV (uint16_t CV_start_index);
/*!
@@ -85,9 +85,9 @@ uint16_t get_16bit_CV (uint16_t CV_start_index);
* | 255 | 1111 1111 | Forward | Speed step 126 |
*
* @param speed_step The speed step byte.
* @return `true` for forward direction, `false` for reverse direction.
* @return `DIRECTION_FORWARD` for forward direction, `DIRECTION_REVERSE` for reverse direction.
*/
bool get_direction_of_speed_step(uint8_t speed_step);
direction_t get_direction_of_speed_step(uint8_t speed_step);
void set_error(error_t err);