Reformatting and renaming some functions; replaced get_direction() with get_direction_of_speed_step() in order to simplify handling of direction; additional explanation comment in CV.h; add doxygen config file;
This commit is contained in:
1 parent
d7f4621b96
commit
df9b0ba633
10 files changed
+3089
-169
No files matched your search
@@ -2,6 +2,9 @@
|
|||||||
.vscode
|
.vscode
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
|
# Doxygen generated files
|
||||||
|
doxygen
|
||||||
|
|
||||||
# Build folders
|
# Build folders
|
||||||
build
|
build
|
||||||
cmake-build-debug
|
cmake-build-debug
|
||||||
@@ -13,3 +16,4 @@ KiCad/*/*.bak
|
|||||||
KiCad/*/fp-info-cache
|
KiCad/*/fp-info-cache
|
||||||
KiCad/*/_autosave-*
|
KiCad/*/_autosave-*
|
||||||
KiCad/*/*.kicad_prl
|
KiCad/*/*.kicad_prl
|
||||||
|
|
||||||
@@ -44,6 +44,7 @@ target_link_libraries(
|
|||||||
hardware_adc
|
hardware_adc
|
||||||
hardware_flash
|
hardware_flash
|
||||||
pico_multicore
|
pico_multicore
|
||||||
|
pico_platform
|
||||||
)
|
)
|
||||||
|
|
||||||
# disable usb stack
|
# disable usb stack
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
|
|||||||
0b01100100, //CV_5 - V_max - Default = 100*16
|
0b01100100, //CV_5 - V_max - Default = 100*16
|
||||||
0b00111111, //CV_6 - V_mid - Default = 63*16
|
0b00111111, //CV_6 - V_mid - Default = 63*16
|
||||||
0b00000001, //CV_7 - Version no. (read-only)
|
0b00000001, //CV_7 - Version no. (read-only)
|
||||||
|
// Writing a value of 8 to CV_8 resets all CV values to the default values in CV.h
|
||||||
0b00001101, //CV_8 - Manufacturer (13 = Public Domain & Do-It-Yourself Decoders) (read-only)
|
0b00001101, //CV_8 - Manufacturer (13 = Public Domain & Do-It-Yourself Decoders) (read-only)
|
||||||
0b10010110, //CV_9 - PWM frequency in Hz = CV_9*100+10000 - Default = (150*100+10000)Hz = 25kHz
|
0b10010110, //CV_9 - PWM frequency in Hz = CV_9*100+10000 - Default = (150*100+10000)Hz = 25kHz
|
||||||
0b00000001, //CV_10 -
|
0b00000001, //CV_10 -
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
||||||
#include "core0.h"
|
#include "core0.h"
|
||||||
|
|
||||||
// XIP_BASE + FLASH_TARGET_OFFSET = "0x101FF000" <- This address need to be smaller than __flash_binary_end stored in RP2040-Decoder.elf.map
|
// XIP_BASE + FLASH_TARGET_OFFSET = "0x101FF000" <- This address need to be smaller than __flash_binary_end stored in RP2040-Decoder.elf.map
|
||||||
const uint8_t *CV_ARRAY_FLASH = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
|
const uint8_t *CV_ARRAY_FLASH = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
|
||||||
uint64_t input_bit_buffer = 0;
|
uint64_t input_bit_buffer = 0;
|
||||||
@@ -193,17 +194,19 @@ void program_mode(uint8_t number_of_bytes, const uint8_t * const byte_array) {
|
|||||||
uint16_t cv_address = byte_array[number_of_bytes - 2] + (cv_address_ms_bits << 8);
|
uint16_t cv_address = byte_array[number_of_bytes - 2] + (cv_address_ms_bits << 8);
|
||||||
|
|
||||||
// Before accessing flash, timers and interrupts need to be disabled and core1 needs to be shut down.
|
// Before accessing flash, timers and interrupts need to be disabled and core1 needs to be shut down.
|
||||||
if(pid_control_timer.pool)
|
if (pid_control_timer.pool) {
|
||||||
alarm_pool_destroy(pid_control_timer.pool);
|
alarm_pool_destroy(pid_control_timer.pool);
|
||||||
if(speed_helper_timer.pool)
|
}
|
||||||
|
if (speed_helper_timer.pool) {
|
||||||
alarm_pool_destroy(speed_helper_timer.pool);
|
alarm_pool_destroy(speed_helper_timer.pool);
|
||||||
|
}
|
||||||
|
|
||||||
multicore_reset_core1();
|
multicore_reset_core1();
|
||||||
|
|
||||||
uint32_t saved_interrupts = save_and_disable_interrupts();
|
uint32_t saved_interrupts = save_and_disable_interrupts();
|
||||||
|
|
||||||
// Verify CV bit instruction
|
|
||||||
if (instruction_type == 0b000001000) {
|
if (instruction_type == 0b000001000) {
|
||||||
|
// Verify CV bit instruction
|
||||||
uint8_t bit_pos_mask = 0b00000111;
|
uint8_t bit_pos_mask = 0b00000111;
|
||||||
uint8_t bit_pos = bit_pos_mask & byte_array[number_of_bytes - 3];
|
uint8_t bit_pos = bit_pos_mask & byte_array[number_of_bytes - 3];
|
||||||
uint8_t bit_val_mask = 0b00000001;
|
uint8_t bit_val_mask = 0b00000001;
|
||||||
@@ -212,20 +215,23 @@ void program_mode(uint8_t number_of_bytes, const uint8_t * const byte_array) {
|
|||||||
verify_cv_bit(cv_address, bit_val, bit_pos);
|
verify_cv_bit(cv_address, bit_val, bit_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify CV byte instruction
|
|
||||||
else if (instruction_type == 0b000000100) {
|
else if (instruction_type == 0b000000100) {
|
||||||
|
// Verify CV byte instruction
|
||||||
uint8_t cv_data = byte_array[number_of_bytes - 3];
|
uint8_t cv_data = byte_array[number_of_bytes - 3];
|
||||||
verify_cv_byte(cv_address, cv_data);
|
verify_cv_byte(cv_address, cv_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write CV byte instruction
|
|
||||||
else if (instruction_type == 0b000001100) {
|
else if (instruction_type == 0b000001100) {
|
||||||
|
// Write CV byte instruction
|
||||||
uint8_t cv_data = byte_array[number_of_bytes - 3];
|
uint8_t cv_data = byte_array[number_of_bytes - 3];
|
||||||
write_cv_handler(cv_address, cv_data);
|
write_cv_handler(cv_address, cv_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore interrupts
|
// Restore interrupts
|
||||||
restore_interrupts(saved_interrupts);
|
restore_interrupts(saved_interrupts);
|
||||||
|
// TODO: verify core1 launch...
|
||||||
|
multicore_launch_core1(core1_entry);
|
||||||
|
// TODO: Check busy wait necessity
|
||||||
busy_wait_ms(5);
|
busy_wait_ms(5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -242,8 +248,8 @@ void set_outputs(uint32_t functions_to_set_bitmask) {
|
|||||||
uint32_t temp_mask = 1;
|
uint32_t temp_mask = 1;
|
||||||
for (uint8_t i = 0; i < 32; i++) {
|
for (uint8_t i = 0; i < 32; i++) {
|
||||||
if (temp_mask & functions_to_set_bitmask) {
|
if (temp_mask & functions_to_set_bitmask) {
|
||||||
bool dir = get_direction(0);
|
bool direction = get_direction_of_speed_step(speed_step_target);
|
||||||
outputs_to_set |= get_32bit_CV(i * 8 + 260 - 4 * dir);
|
outputs_to_set |= get_32bit_CV(i * 8 + 260 - 4 * direction);
|
||||||
}
|
}
|
||||||
temp_mask <<= 1;
|
temp_mask <<= 1;
|
||||||
}
|
}
|
||||||
@@ -274,13 +280,15 @@ void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind,
|
|||||||
static uint32_t active_functions;
|
static uint32_t active_functions;
|
||||||
static uint32_t prev_active_func;
|
static uint32_t prev_active_func;
|
||||||
|
|
||||||
// Outputs depend on direction
|
// Outputs depend on direction, therefore on direction change outputs need to be set as configured for the other direction.
|
||||||
|
if (direction_change) {
|
||||||
// In case of a direction change every output needs to be set according to configuration for the other direction
|
// In case of a direction change every output needs to be set according to configuration for the other direction
|
||||||
if (direction_change) set_outputs(active_functions);
|
set_outputs(active_functions);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// TODO: improve explanation here...
|
||||||
// In case of a change in active functions only the function group corresponding to the instruction is considered
|
// In case of a change in active functions only the function group corresponding to the instruction is considered
|
||||||
// Check for changes and set outputs accordingly
|
// Check for changes and set outputs accordingly
|
||||||
else{
|
|
||||||
new_function_bitmask &= ~(clr_bit_arr[clr_bit_ind]); // Clear every bit except bits corresponding to group of functions
|
new_function_bitmask &= ~(clr_bit_arr[clr_bit_ind]); // Clear every bit except bits corresponding to group of functions
|
||||||
active_functions &= clr_bit_arr[clr_bit_ind]; // Clear bits corresponding to group of functions
|
active_functions &= clr_bit_arr[clr_bit_ind]; // Clear bits corresponding to group of functions
|
||||||
active_functions |= new_function_bitmask; // Write bits corresponding to group of functions
|
active_functions |= new_function_bitmask; // Write bits corresponding to group of functions
|
||||||
@@ -291,7 +299,7 @@ void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Bitwise XOR for all Bytes -> Successful result is: "0000 0000"
|
// Bitwise XOR for all Bytes -> when result is: "0000 0000" error check is passed.
|
||||||
bool error_detection(int8_t number_of_bytes, const uint8_t *const byte_array) {
|
bool error_detection(int8_t number_of_bytes, const uint8_t *const byte_array) {
|
||||||
uint8_t xor_byte = 0;
|
uint8_t xor_byte = 0;
|
||||||
for (int i = 0; i < number_of_bytes; i++) {
|
for (int i = 0; i < number_of_bytes; i++) {
|
||||||
@@ -303,27 +311,28 @@ bool error_detection(int8_t number_of_bytes, const uint8_t * const byte_array) {
|
|||||||
|
|
||||||
// Check for long address. Function returns true for long address.
|
// Check for long address. Function returns true for long address.
|
||||||
bool is_long_address(uint8_t number_of_bytes, const uint8_t *const byte_array) {
|
bool is_long_address(uint8_t number_of_bytes, const uint8_t *const byte_array) {
|
||||||
if ((byte_array[number_of_bytes - 1]>>6) == 0b00000011) return true;
|
if ((byte_array[number_of_bytes - 1] >> 6) == 0b00000011) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// First checks for idle package and then evaluates address included in byte_array[]
|
// First checks for idle message and then evaluates address included in byte_array[] which contains the received DCC message
|
||||||
bool address_evaluation(uint8_t number_of_bytes, const uint8_t *const byte_array) {
|
bool address_evaluation(uint8_t number_of_bytes, const uint8_t *const byte_array) {
|
||||||
// Check for Idle Package
|
// Check for idle message
|
||||||
if (byte_array[number_of_bytes - 1] == 255)
|
if (byte_array[number_of_bytes - 1] == 255) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Long/extended address decoder configuration (CV_29 Bit5)
|
// Long/extended address decoder configuration (CV_29 Bit5)
|
||||||
bool CV_29_bit5 = (CV_ARRAY_FLASH[28] & 0b00100000) >> 5;
|
bool CV_29_bit5 = (CV_ARRAY_FLASH[28] & 0b00100000) >> 5;
|
||||||
uint16_t read_address;
|
uint16_t read_address;
|
||||||
// Decoder configured to receive long/extended address
|
// Check if decoder is configured to receive long/extended address (CV_29 Bit5 set)
|
||||||
if (CV_29_bit5) {
|
if (CV_29_bit5) {
|
||||||
// Package received does contain a long address -> Check for accordance
|
// Check DCC message for long address format
|
||||||
if (is_long_address(number_of_bytes, byte_array))
|
if (is_long_address(number_of_bytes, byte_array)) {
|
||||||
{
|
// DCC message received has long address format
|
||||||
// start of transmission -> address_byte_1 -> address_byte_0 -> ... -> end of transmission
|
// start of transmission -> address_byte_1 -> address_byte_0 -> ... -> end of transmission
|
||||||
uint16_t address_byte_1 = (byte_array[number_of_bytes - 1]) - 192; // Remove long address identifier bits
|
uint16_t address_byte_1 = (byte_array[number_of_bytes - 1]) - 192; // Remove long address identifier bits
|
||||||
uint16_t address_byte_0 = (byte_array[number_of_bytes - 2]);
|
uint16_t address_byte_0 = (byte_array[number_of_bytes - 2]);
|
||||||
@@ -331,15 +340,16 @@ bool address_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_array
|
|||||||
uint16_t long_address = get_16bit_CV(16) & 0x3FFF; // &Mask to remove long address identifier bits
|
uint16_t long_address = get_16bit_CV(16) & 0x3FFF; // &Mask to remove long address identifier bits
|
||||||
return (long_address == read_address);
|
return (long_address == read_address);
|
||||||
}
|
}
|
||||||
// Package received does not contain a long address
|
else {
|
||||||
else return false;
|
// DCC message received does not have long address format
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Short address decoder configuration (CV_29 Bit5)
|
else {
|
||||||
else
|
// Else decoder is configured to receive short address (CV_29 Bit5 not set)
|
||||||
{
|
// Short address message:
|
||||||
// Short Address Package
|
|
||||||
// start of transmission -> address_byte_0 -> ... -> end of transmission
|
// start of transmission -> address_byte_0 -> ... -> end of transmission
|
||||||
read_address = (byte_array[number_of_bytes - 1]);
|
read_address = (byte_array[number_of_bytes - 1]);
|
||||||
return (CV_ARRAY_FLASH[0] == read_address);
|
return (CV_ARRAY_FLASH[0] == read_address);
|
||||||
@@ -348,7 +358,7 @@ bool address_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_array
|
|||||||
|
|
||||||
|
|
||||||
// Evaluate the type of instruction
|
// Evaluate the type of instruction
|
||||||
void instruction_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_array) {
|
void instruction_evaluation(const uint8_t number_of_bytes, const uint8_t *const byte_array) {
|
||||||
// start of transmission -> ... -> command_byte_n -> ... -> command_byte_0 -> ... -> end of transmission
|
// start of transmission -> ... -> command_byte_n -> ... -> command_byte_0 -> ... -> end of transmission
|
||||||
// The position of command bytes depend on whether the address is long or not
|
// The position of command bytes depend on whether the address is long or not
|
||||||
uint8_t command_byte_n;
|
uint8_t command_byte_n;
|
||||||
@@ -363,15 +373,14 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_a
|
|||||||
|
|
||||||
// 0011-1111 (128 Speed Step Control) - 2 Byte length
|
// 0011-1111 (128 Speed Step Control) - 2 Byte length
|
||||||
if (command_byte_n == 0b00111111) {
|
if (command_byte_n == 0b00111111) {
|
||||||
static bool prev_dir;
|
speed_step_target_prev = speed_step_target;
|
||||||
uint32_t speed_step = byte_array[command_byte_start_index - 1];
|
speed_step_target = byte_array[command_byte_start_index - 1];
|
||||||
// LOG(1, "new speed %d", speed_step);
|
// Check for direction change
|
||||||
multicore_fifo_push_blocking(speed_step);
|
bool direction_changed = get_direction_of_speed_step(speed_step_target) !=
|
||||||
// LOG(1, " submitted to list");
|
get_direction_of_speed_step(speed_step_target_prev);
|
||||||
// In case of a direction change, functions need to be updated because functions might depend on direction state
|
// In case of a direction change, functions need to be updated because functions depend on direction
|
||||||
if (get_direction(0) != prev_dir) {
|
if (direction_changed) {
|
||||||
update_active_functions(0, 0, true);
|
update_active_functions(0, 0, true);
|
||||||
prev_dir = get_direction(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +394,8 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_a
|
|||||||
update_active_functions((command_byte_n << 9), 2, false);
|
update_active_functions((command_byte_n << 9), 2, false);
|
||||||
break;
|
break;
|
||||||
default: // F0-F4
|
default: // F0-F4
|
||||||
update_active_functions((((command_byte_n & 0b00001111) << 1) + ((command_byte_n & 0b00010000) >> 4)),0,false);
|
update_active_functions((((command_byte_n & 0b00001111) << 1) + ((command_byte_n & 0b00010000) >> 4)),
|
||||||
|
0, false);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -409,8 +419,8 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_a
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Check for reset package - When reset package is found stop the motor and disable all functions
|
// Check for reset message - When reset message is found, stop the motor and disable all functions.
|
||||||
bool reset_package_check(uint8_t number_of_bytes,const uint8_t * const byte_array){
|
bool reset_message_check(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) {
|
if (byte_array[number_of_bytes - 1] == 0b00000000 && byte_array[number_of_bytes - 2] == 0b00000000) {
|
||||||
uint32_t speed_step = 1;
|
uint32_t speed_step = 1;
|
||||||
multicore_fifo_push_blocking(speed_step); // Emergency Stop
|
multicore_fifo_push_blocking(speed_step); // Emergency Stop
|
||||||
@@ -421,15 +431,17 @@ bool reset_package_check(uint8_t number_of_bytes,const uint8_t * const byte_arra
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Checks for valid package - looking for correct preamble
|
// Checks for valid message - looking for correct preamble using bitmasks
|
||||||
int8_t check_for_package() { //returns number of bytes if valid bit-pattern is found. Otherwise -1 is returned.
|
// returns number of bytes if valid bit-pattern is found. Otherwise -1 is returned.
|
||||||
uint64_t package3Masked = input_bit_buffer & PACKAGE_MASK_3_BYTES;
|
int8_t verify_dcc_message() {
|
||||||
if (package3Masked == PACKAGE_3_BYTES) return 3;
|
int8_t number_of_bytes = -1;
|
||||||
uint64_t package4Masked = input_bit_buffer & PACKAGE_MASK_4_BYTES;
|
uint64_t masked_message_3_bytes = input_bit_buffer & MESSAGE_MASK_3_BYTES;
|
||||||
if (package4Masked == PACKAGE_4_BYTES) return 4;
|
if (masked_message_3_bytes == MESSAGE_3_BYTES) number_of_bytes = 3;
|
||||||
uint64_t package5Masked = input_bit_buffer & PACKAGE_MASK_5_BYTES;
|
uint64_t masked_message_4_bytes = input_bit_buffer & MESSAGE_MASK_4_BYTES;
|
||||||
if (package5Masked == PACKAGE_5_BYTES) return 5;
|
if (masked_message_4_bytes == MESSAGE_4_BYTES) number_of_bytes = 4;
|
||||||
return -1;
|
uint64_t masked_message_5_bytes = input_bit_buffer & MESSAGE_MASK_5_BYTES;
|
||||||
|
if (masked_message_5_bytes == MESSAGE_5_BYTES) number_of_bytes = 5;
|
||||||
|
return number_of_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -442,11 +454,11 @@ void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) {
|
|||||||
|
|
||||||
|
|
||||||
// Sequence of bits gets evaluated
|
// Sequence of bits gets evaluated
|
||||||
void evaluation(){
|
void evaluate_message() {
|
||||||
static bool core1_launched = false;
|
static bool reset_message_flag;
|
||||||
static bool reset_package_flag;
|
// Check if input buffer contains a valid dcc message
|
||||||
int8_t number_of_bytes = check_for_package();
|
int8_t number_of_bytes = verify_dcc_message();
|
||||||
// number_of_bytes contains the amount of bytes when the package is valid, otherwise -1
|
// number_of_bytes contains the amount of bytes when the message is valid, otherwise -1
|
||||||
if (number_of_bytes != -1) {
|
if (number_of_bytes != -1) {
|
||||||
// Split data into 8Bit array
|
// Split data into 8Bit array
|
||||||
uint8_t byte_array[SIZE_BYTE_ARRAY] = {0};
|
uint8_t byte_array[SIZE_BYTE_ARRAY] = {0};
|
||||||
@@ -455,22 +467,16 @@ void evaluation(){
|
|||||||
if (error_detection(number_of_bytes, byte_array)) {
|
if (error_detection(number_of_bytes, byte_array)) {
|
||||||
// Check for matching address
|
// Check for matching address
|
||||||
if (address_evaluation(number_of_bytes, byte_array)) {
|
if (address_evaluation(number_of_bytes, byte_array)) {
|
||||||
reset_package_flag = false;
|
reset_message_flag = false;
|
||||||
if (!core1_launched){
|
|
||||||
multicore_launch_core1(core1_entry);
|
|
||||||
// Wait for pid.direction pointer via multicore fifo also signalizing that core1 initialization is finished.
|
|
||||||
get_direction((bool *) multicore_fifo_pop_blocking());
|
|
||||||
core1_launched = true;
|
|
||||||
}
|
|
||||||
instruction_evaluation(number_of_bytes, byte_array);
|
instruction_evaluation(number_of_bytes, byte_array);
|
||||||
}
|
}
|
||||||
// When previous packet was a reset package -> enter program mode
|
else if (reset_message_flag) {
|
||||||
else if (reset_package_flag) {
|
// When previous packet was a reset message -> enter program mode
|
||||||
program_mode(number_of_bytes, byte_array);
|
program_mode(number_of_bytes, byte_array);
|
||||||
}
|
}
|
||||||
// Check for reset package and set flag when reset package is received
|
|
||||||
else {
|
else {
|
||||||
reset_package_flag = reset_package_check(number_of_bytes,byte_array);
|
// Check for reset message and set flag when reset message is received
|
||||||
|
reset_message_flag = reset_message_check(number_of_bytes, byte_array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -500,7 +506,7 @@ void track_signal_fall(unsigned int gpio, long unsigned int events) {
|
|||||||
input_bit_buffer <<= 1;
|
input_bit_buffer <<= 1;
|
||||||
input_bit_buffer |= bit;
|
input_bit_buffer |= bit;
|
||||||
// evaluate sequence of bits saved in buffer
|
// evaluate sequence of bits saved in buffer
|
||||||
evaluation();
|
evaluate_message();
|
||||||
// Disable falling edge interrupt and enable rising edge interrupt
|
// Disable falling edge interrupt and enable rising edge interrupt
|
||||||
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_FALL, false, &track_signal_fall);
|
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_FALL, false, &track_signal_fall);
|
||||||
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise);
|
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise);
|
||||||
@@ -534,7 +540,6 @@ void init_outputs(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Measures "base" pwm duty cycle needed to overcome friction
|
// Measures "base" pwm duty cycle needed to overcome friction
|
||||||
uint16_t measure_base_pwm(bool direction, uint8_t iterations) {
|
uint16_t measure_base_pwm(bool direction, uint8_t iterations) {
|
||||||
uint gpio;
|
uint gpio;
|
||||||
@@ -555,7 +560,8 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){
|
|||||||
CV_ARRAY_FLASH[62],
|
CV_ARRAY_FLASH[62],
|
||||||
CV_ARRAY_FLASH[63],
|
CV_ARRAY_FLASH[63],
|
||||||
direction);
|
direction);
|
||||||
LOG(3, "level %d measurement %f loop-cond: %f\n", level, measurement, measurement-(float)CV_ARRAY_FLASH[171]);
|
LOG(3, "level %d measurement %f loop-cond: %f\n", level, measurement,
|
||||||
|
measurement - (float) CV_ARRAY_FLASH[171]);
|
||||||
if (level > max_level) {
|
if (level > max_level) {
|
||||||
// Abort measurement and write default value of 0 to flash
|
// Abort measurement and write default value of 0 to flash
|
||||||
LOG(1, "measure_base_pwm: abort measurement and return 0\n");
|
LOG(1, "measure_base_pwm: abort measurement and return 0\n");
|
||||||
@@ -619,24 +625,6 @@ void cv_setup_check(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// This function ensures that the direction pointer can only be written once.
|
|
||||||
// After first initialization this function can be called with arbitrary input to get the current direction
|
|
||||||
bool get_direction(bool *direction_ptr){
|
|
||||||
static bool set_flag;
|
|
||||||
static bool *dir_ptr;
|
|
||||||
if(!set_flag) {
|
|
||||||
if (direction_ptr == NULL){
|
|
||||||
LOG(0, "Error! Direction pointer argument is NULL-pointer!\n")
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
set_flag = true;
|
|
||||||
dir_ptr = direction_ptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return *dir_ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Motor PWM initialization
|
// Motor PWM initialization
|
||||||
void init_motor_pwm(uint8_t gpio) {
|
void init_motor_pwm(uint8_t gpio) {
|
||||||
uint16_t wrap_counter = (_125M / (CV_ARRAY_FLASH[8] * 100 + 10000)) - 1;
|
uint16_t wrap_counter = (_125M / (CV_ARRAY_FLASH[8] * 100 + 10000)) - 1;
|
||||||
@@ -680,5 +668,7 @@ int main() {
|
|||||||
gpio_pull_up(DCC_INPUT_PIN);
|
gpio_pull_up(DCC_INPUT_PIN);
|
||||||
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise);
|
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise);
|
||||||
LOG(1, "core0 done\n");
|
LOG(1, "core0 done\n");
|
||||||
while (1);
|
busy_wait_ms(100);
|
||||||
|
multicore_launch_core1(core1_entry);
|
||||||
|
while (true);
|
||||||
}
|
}
|
||||||
@@ -8,12 +8,12 @@
|
|||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
#include "CV.h"
|
#include "CV.h"
|
||||||
#define SIZE_BYTE_ARRAY 5
|
#define SIZE_BYTE_ARRAY 5
|
||||||
#define PACKAGE_3_BYTES 0b11111111110000000000000000000000000001
|
#define MESSAGE_3_BYTES 0b11111111110000000000000000000000000001
|
||||||
#define PACKAGE_MASK_3_BYTES 0b11111111111000000001000000001000000001
|
#define MESSAGE_MASK_3_BYTES 0b11111111111000000001000000001000000001
|
||||||
#define PACKAGE_4_BYTES 0b11111111110000000000000000000000000000000000001
|
#define MESSAGE_4_BYTES 0b11111111110000000000000000000000000000000000001
|
||||||
#define PACKAGE_MASK_4_BYTES 0b11111111111000000001000000001000000001000000001
|
#define MESSAGE_MASK_4_BYTES 0b11111111111000000001000000001000000001000000001
|
||||||
#define PACKAGE_5_BYTES 0b11111111110000000000000000000000000000000000000000000001
|
#define MESSAGE_5_BYTES 0b11111111110000000000000000000000000000000000000000000001
|
||||||
#define PACKAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
|
#define MESSAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
|
||||||
const uint32_t clr_bit_arr[6] = {
|
const uint32_t clr_bit_arr[6] = {
|
||||||
0b11111111111111111111111111100000, // Clear Bits 0-4 (F0-F4)
|
0b11111111111111111111111111100000, // Clear Bits 0-4 (F0-F4)
|
||||||
0b11111111111111111111111000011111, // Clear Bits 5-8 (F5-F8)
|
0b11111111111111111111111000011111, // Clear Bits 5-8 (F5-F8)
|
||||||
@@ -29,7 +29,7 @@ void acknowledge();
|
|||||||
void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos);
|
void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos);
|
||||||
void verify_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
void verify_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
||||||
void regular_cv_write(uint16_t cv_index, uint8_t cv_data);
|
void regular_cv_write(uint16_t cv_index, uint8_t cv_data);
|
||||||
void write_cv_handler(uint16_t cv_index, uint8_t cv_data);
|
//void write_cv_handler(uint16_t cv_index, uint8_t cv_data);
|
||||||
|
|
||||||
|
|
||||||
#define ProgramInstrVerifyBit 0b10
|
#define ProgramInstrVerifyBit 0b10
|
||||||
@@ -44,15 +44,14 @@ bool error_detection(int8_t number_of_bytes, const uint8_t byte_array[]);
|
|||||||
bool is_long_address(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
bool is_long_address(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||||
bool address_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]);
|
bool address_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]);
|
||||||
void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]);
|
void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]);
|
||||||
bool reset_package_check(uint8_t number_of_bytes,const uint8_t byte_array[]);
|
bool reset_message_check(uint8_t number_of_bytes, const uint8_t *const byte_array);
|
||||||
int8_t check_for_package();
|
int8_t verify_dcc_message();
|
||||||
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]);
|
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]);
|
||||||
void evaluation();
|
void evaluate_message();
|
||||||
void track_signal_rise(unsigned int gpio, long unsigned int events);
|
void track_signal_rise(unsigned int gpio, long unsigned int events);
|
||||||
void track_signal_fall(unsigned int gpio, long unsigned int events);
|
void track_signal_fall(unsigned int gpio, long unsigned int events);
|
||||||
void init_outputs();
|
void init_outputs();
|
||||||
uint16_t measure_base_pwm(bool direction, uint8_t iterations);
|
uint16_t measure_base_pwm(bool direction, uint8_t iterations);
|
||||||
void cv_setup_check();
|
void cv_setup_check();
|
||||||
bool get_direction(bool *direction_ptr);
|
|
||||||
void init_motor_pwm(uint8_t gpio);
|
void init_motor_pwm(uint8_t gpio);
|
||||||
void init_gpio_adc();
|
void init_gpio_adc();
|
||||||
@@ -5,29 +5,29 @@
|
|||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
||||||
#include "core1.h"
|
#include "core1.h"
|
||||||
|
|
||||||
struct repeating_timer pid_control_timer, speed_helper_timer;
|
struct repeating_timer pid_control_timer, speed_helper_timer;
|
||||||
|
|
||||||
|
|
||||||
// Update speed_step_target, end_target_index and direction on receiving data from core0
|
// Get speed_step_table_index depending on speed_step
|
||||||
void update_speed_dir(pid_params *pid, uint32_t speed_step_target){
|
uint8_t get_speed_step_table_index_of_speed_step(uint8_t speed_step) {
|
||||||
pid->speed_step_target = speed_step_target;
|
if (speed_step > 127) {
|
||||||
// When CV_29 Bit 0 is set direction is reversed
|
|
||||||
bool reverse_dir = CV_ARRAY_FLASH[28] & 0b00000001;
|
|
||||||
// Forward
|
// Forward
|
||||||
if(speed_step_target > 127) {
|
if (speed_step == 128) {
|
||||||
pid->direction = true^reverse_dir;
|
return 0;
|
||||||
if (pid->speed_step_target == 128)
|
|
||||||
pid->end_target_index = 0;
|
|
||||||
else
|
|
||||||
pid->end_target_index = (pid->speed_step_target-129);
|
|
||||||
}
|
}
|
||||||
// Reverse
|
|
||||||
else {
|
else {
|
||||||
pid->direction = false^reverse_dir;
|
return (speed_step - 129);
|
||||||
if (pid->speed_step_target == 0)
|
}
|
||||||
pid->end_target_index = 0;
|
}
|
||||||
else
|
else {
|
||||||
pid->end_target_index = (pid->speed_step_target-1);
|
// Reverse
|
||||||
|
if (speed_step == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return (speed_step - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,48 +40,52 @@ bool speed_helper(struct repeating_timer *t) {
|
|||||||
pid_params *pid = (pid_params *) (t->user_data);
|
pid_params *pid = (pid_params *) (t->user_data);
|
||||||
uint8_t accel_rate = CV_ARRAY_FLASH[2];
|
uint8_t accel_rate = CV_ARRAY_FLASH[2];
|
||||||
uint8_t decel_rate = CV_ARRAY_FLASH[3];
|
uint8_t decel_rate = CV_ARRAY_FLASH[3];
|
||||||
static bool prev_dir;
|
|
||||||
static uint8_t speed_helper_counter;
|
static uint8_t speed_helper_counter;
|
||||||
static uint8_t speed_table_index;
|
static uint8_t speed_table_index;
|
||||||
|
|
||||||
// Emergency Stop
|
// Get speed_step_table_end_index corresponding to current speed_step_target
|
||||||
|
uint8_t speed_step_table_end_index = get_speed_step_table_index_of_speed_step(speed_step_target);
|
||||||
|
bool direction_changed = get_direction_of_speed_step(speed_step_target) !=
|
||||||
|
get_direction_of_speed_step(speed_step_target_prev);
|
||||||
|
|
||||||
if (pid->speed_step_target == 129 || pid->speed_step_target == 1) {
|
if (pid->speed_step_target == 129 || pid->speed_step_target == 1) {
|
||||||
|
// Emergency Stop
|
||||||
speed_table_index = 0;
|
speed_table_index = 0;
|
||||||
speed_helper_counter = 0;
|
speed_helper_counter = 0;
|
||||||
pid->setpoint = pid->speed_table[speed_table_index];
|
pid->setpoint = pid->speed_table[speed_table_index];
|
||||||
}
|
}
|
||||||
|
else if (speed_step_table_end_index > speed_table_index && speed_helper_counter == accel_rate) {
|
||||||
// Acceleration
|
// Acceleration
|
||||||
else if (pid->end_target_index > speed_table_index && speed_helper_counter == accel_rate) {
|
if (!accel_rate) speed_table_index = speed_step_table_end_index; // directly accelerate to end target speed
|
||||||
if(!accel_rate) speed_table_index = pid->end_target_index; // directly accelerate to end target speed
|
|
||||||
else speed_table_index++;
|
else speed_table_index++;
|
||||||
pid->setpoint = pid->speed_table[speed_table_index];
|
pid->setpoint = pid->speed_table[speed_table_index];
|
||||||
speed_helper_counter = 0;
|
speed_helper_counter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (speed_step_table_end_index < speed_table_index && speed_helper_counter == decel_rate) {
|
||||||
// Deceleration
|
// Deceleration
|
||||||
else if (pid->end_target_index < speed_table_index && speed_helper_counter == decel_rate) {
|
if (!decel_rate) speed_table_index = speed_step_table_end_index; // directly decelerate to end target speed
|
||||||
if(!decel_rate) speed_table_index = pid->end_target_index; // directly decelerate to end target speed
|
|
||||||
else speed_table_index--;
|
else speed_table_index--;
|
||||||
pid->setpoint = pid->speed_table[speed_table_index];
|
pid->setpoint = pid->speed_table[speed_table_index];
|
||||||
speed_helper_counter = 0;
|
speed_helper_counter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (direction_changed) {
|
||||||
// Change of direction while decelerating -> jump to most recent speed_step without delay (other direction)
|
// Change of direction while decelerating -> jump to most recent speed_step without delay (other direction)
|
||||||
else if (pid->direction != prev_dir){
|
speed_table_index = speed_step_table_end_index;
|
||||||
speed_table_index = pid->end_target_index;
|
|
||||||
pid->setpoint = pid->speed_table[speed_table_index];
|
pid->setpoint = pid->speed_table[speed_table_index];
|
||||||
speed_helper_counter = 0;
|
speed_helper_counter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (speed_step_table_end_index != speed_table_index) {
|
||||||
// Acceleration/Deceleration "scheduled"
|
// Acceleration/Deceleration "scheduled"
|
||||||
else if (pid->end_target_index != speed_table_index) speed_helper_counter++;
|
speed_helper_counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
// Target reached -> Reset Counter
|
// Target reached -> Reset Counter
|
||||||
else speed_helper_counter = 0;
|
speed_helper_counter = 0;
|
||||||
|
}
|
||||||
// Save prev speed_helper direction - not to be confused with pid->prev_direction
|
|
||||||
prev_dir = pid->direction;
|
|
||||||
|
|
||||||
// return true to schedule the repeating timer again
|
// return true to schedule the repeating timer again
|
||||||
return true;
|
return true;
|
||||||
@@ -90,13 +94,13 @@ bool speed_helper(struct repeating_timer *t) {
|
|||||||
|
|
||||||
// Helper function to adjust pwm level/duty cycle.
|
// Helper function to adjust pwm level/duty cycle.
|
||||||
void adjust_pwm_level(uint16_t level, pid_params *pid) {
|
void adjust_pwm_level(uint16_t level, pid_params *pid) {
|
||||||
|
if (get_direction_of_speed_step(speed_step_target) == true) {
|
||||||
// Forward
|
// Forward
|
||||||
if (pid->direction) {
|
|
||||||
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
|
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
|
||||||
pwm_set_gpio_level(MOTOR_FWD_PIN, level);
|
pwm_set_gpio_level(MOTOR_FWD_PIN, level);
|
||||||
}
|
}
|
||||||
// Reverse
|
|
||||||
else {
|
else {
|
||||||
|
// Reverse
|
||||||
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
|
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
|
||||||
pwm_set_gpio_level(MOTOR_REV_PIN, level);
|
pwm_set_gpio_level(MOTOR_REV_PIN, level);
|
||||||
}
|
}
|
||||||
@@ -112,21 +116,20 @@ void update_m(pid_params *pid){
|
|||||||
|
|
||||||
// Update feedforward function value y_2 in both directions
|
// Update feedforward function value y_2 in both directions
|
||||||
void update_y(pid_params *pid, float i) {
|
void update_y(pid_params *pid, float i) {
|
||||||
|
if (get_direction_of_speed_step(speed_step_target) == true) {
|
||||||
// Forward
|
// Forward
|
||||||
if(pid->direction) {
|
|
||||||
pid->y_2_fwd += i * pid->k_ff;
|
pid->y_2_fwd += i * pid->k_ff;
|
||||||
// Limit y_2_fwd
|
// Limit y_2_fwd
|
||||||
if (pid->y_2_fwd > pid->max_output) pid->y_2_fwd = pid->max_output;
|
if (pid->y_2_fwd > pid->max_output) pid->y_2_fwd = pid->max_output;
|
||||||
else if (pid->y_2_fwd < pid->y_1_fwd) pid->y_2_fwd = pid->y_1_fwd;
|
else if (pid->y_2_fwd < pid->y_1_fwd) pid->y_2_fwd = pid->y_1_fwd;
|
||||||
}
|
}
|
||||||
// Reverse
|
|
||||||
else {
|
else {
|
||||||
|
// Reverse
|
||||||
pid->y_2_rev += i * pid->k_ff;
|
pid->y_2_rev += i * pid->k_ff;
|
||||||
// Limit y_2_rev
|
// Limit y_2_rev
|
||||||
if (pid->y_2_rev > pid->max_output) pid->y_2_rev = pid->max_output;
|
if (pid->y_2_rev > pid->max_output) pid->y_2_rev = pid->max_output;
|
||||||
else if (pid->y_2_rev < pid->y_1_rev) pid->y_2_rev = pid->y_1_rev;
|
else if (pid->y_2_rev < pid->y_1_rev) pid->y_2_rev = pid->y_1_rev;
|
||||||
}
|
}
|
||||||
|
|
||||||
update_m(pid);
|
update_m(pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,12 +137,12 @@ void update_y(pid_params *pid,float i){
|
|||||||
// Returns feedforward value corresponding to current setpoint
|
// Returns feedforward value corresponding to current setpoint
|
||||||
float get_ff_val(pid_params *pid) {
|
float get_ff_val(pid_params *pid) {
|
||||||
float val;
|
float val;
|
||||||
|
if (get_direction_of_speed_step(speed_step_target) == true) {
|
||||||
// Forward
|
// Forward
|
||||||
if(pid->direction){
|
|
||||||
val = pid->m_fwd * ((float) pid->setpoint - pid->x_1_fwd) + pid->y_1_fwd;
|
val = pid->m_fwd * ((float) pid->setpoint - pid->x_1_fwd) + pid->y_1_fwd;
|
||||||
}
|
}
|
||||||
// Reverse
|
|
||||||
else {
|
else {
|
||||||
|
// Reverse
|
||||||
val = pid->m_rev * ((float) pid->setpoint - pid->x_1_rev) + pid->y_1_rev;
|
val = pid->m_rev * ((float) pid->setpoint - pid->x_1_rev) + pid->y_1_rev;
|
||||||
}
|
}
|
||||||
return val;
|
return val;
|
||||||
@@ -164,7 +167,9 @@ bool pid_control(struct repeating_timer *t){
|
|||||||
pid_params *pid = (pid_params *) (t->user_data);
|
pid_params *pid = (pid_params *) (t->user_data);
|
||||||
|
|
||||||
// Acceleration from standstill or change in direction -> reset previous derivative, error and integral parts
|
// Acceleration from standstill or change in direction -> reset previous derivative, error and integral parts
|
||||||
if((!pid->setpoint_prev && !pid->setpoint) || (pid->direction != pid->direction_prev)){
|
bool direction_changed = get_direction_of_speed_step(speed_step_target) !=
|
||||||
|
get_direction_of_speed_step(speed_step_target_prev);
|
||||||
|
if ((!pid->setpoint_prev && !pid->setpoint) || direction_changed) {
|
||||||
pid->d_prev = 0.0f;
|
pid->d_prev = 0.0f;
|
||||||
pid->i_prev = 0.0f;
|
pid->i_prev = 0.0f;
|
||||||
pid->e_prev = 0.0f;
|
pid->e_prev = 0.0f;
|
||||||
@@ -179,8 +184,10 @@ bool pid_control(struct repeating_timer *t){
|
|||||||
pid->msr_delay_in_us,
|
pid->msr_delay_in_us,
|
||||||
pid->l_side_arr_cutoff,
|
pid->l_side_arr_cutoff,
|
||||||
pid->r_side_arr_cutoff,
|
pid->r_side_arr_cutoff,
|
||||||
pid->direction);
|
get_direction_of_speed_step(speed_step_target));
|
||||||
float e = (float)pid->setpoint - (pid->measurement - pid->adc_offset);
|
|
||||||
|
float measurement_corrected = pid->measurement - pid->adc_offset;
|
||||||
|
float e = (float) pid->setpoint - measurement_corrected;
|
||||||
|
|
||||||
// Proportional part, integral part and derivative part (including digital low-pass-filter with time constant tau)
|
// Proportional part, integral part and derivative part (including digital low-pass-filter with time constant tau)
|
||||||
float p = pid->k_p * e;
|
float p = pid->k_p * e;
|
||||||
@@ -210,13 +217,15 @@ bool pid_control(struct repeating_timer *t){
|
|||||||
adc_fifo_drain();
|
adc_fifo_drain();
|
||||||
|
|
||||||
// Check for High or Low Integrator values -> Update x,y values of feedforward function
|
// Check for High or Low Integrator values -> Update x,y values of feedforward function
|
||||||
if( (output < pid->max_output && output > pid->max_output/2) && ((i > pid->int_lim_max/2) || (i < pid->int_lim_min/2)) ) update_y(pid,i);
|
if ((output < pid->max_output && output > pid->max_output / 2) &&
|
||||||
|
((i > pid->int_lim_max / 2) || (i < pid->int_lim_min / 2))) {
|
||||||
|
update_y(pid, i);
|
||||||
|
}
|
||||||
|
|
||||||
// Save previous error, integrator, differentiator, direction, setpoint and measured value also sum error to e_sum
|
// Save previous error, integrator, differentiator, direction, setpoint and measured value also sum error to e_sum
|
||||||
pid->e_prev = e;
|
pid->e_prev = e;
|
||||||
pid->i_prev = i;
|
pid->i_prev = i;
|
||||||
pid->d_prev = d;
|
pid->d_prev = d;
|
||||||
pid->direction_prev = pid->direction;
|
|
||||||
pid->setpoint_prev = pid->setpoint;
|
pid->setpoint_prev = pid->setpoint;
|
||||||
pid->measurement_prev = pid->measurement;
|
pid->measurement_prev = pid->measurement;
|
||||||
|
|
||||||
@@ -280,9 +289,6 @@ void init_pid(pid_params *pid){
|
|||||||
pid->e_prev = 0.0f;
|
pid->e_prev = 0.0f;
|
||||||
pid->i_prev = 0.0f;
|
pid->i_prev = 0.0f;
|
||||||
pid->d_prev = 0.0f;
|
pid->d_prev = 0.0f;
|
||||||
pid->direction = false;
|
|
||||||
pid->direction_prev = false;
|
|
||||||
pid->speed_step_target = 0;
|
|
||||||
pid->setpoint = 0;
|
pid->setpoint = 0;
|
||||||
|
|
||||||
// Gain scheduling initialization
|
// Gain scheduling initialization
|
||||||
@@ -298,7 +304,6 @@ void init_pid(pid_params *pid){
|
|||||||
update_m(pid);
|
update_m(pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void core1_entry() {
|
void core1_entry() {
|
||||||
LOG(1, "core1 init...\n");
|
LOG(1, "core1 init...\n");
|
||||||
pid_params pid_parameters;
|
pid_params pid_parameters;
|
||||||
@@ -309,29 +314,13 @@ void core1_entry() {
|
|||||||
pid_control,
|
pid_control,
|
||||||
pid,
|
pid,
|
||||||
&pid_control_timer);
|
&pid_control_timer);
|
||||||
irq_set_priority(0,0x00); // 0x00 = highest priority
|
// Set TIMER_IRQ_0 to highest priority of 0x00
|
||||||
|
irq_set_priority(TIMER_IRQ_0, 0x00);
|
||||||
alarm_pool_add_repeating_timer_ms(alarm_pool_create(1, 1),
|
alarm_pool_add_repeating_timer_ms(alarm_pool_create(1, 1),
|
||||||
CV_ARRAY_FLASH[174],
|
CV_ARRAY_FLASH[174],
|
||||||
speed_helper,
|
speed_helper,
|
||||||
pid,
|
pid,
|
||||||
&speed_helper_timer);
|
&speed_helper_timer);
|
||||||
// Send direction pointer for access and to let core0 know that core1 is done with initialization,
|
LOG(1, "core1 done\n");
|
||||||
// Note: Pointer will be cast into uint32_t and then back into bool * on core0 (Both 32-Bit Datatype).
|
while (true);
|
||||||
// This is done only to accommodate the correct data type associated with multicore_fifo_push_blocking().
|
|
||||||
multicore_fifo_push_blocking((uint32_t) &(pid->direction));
|
|
||||||
uint64_t packet_timeout_threshold_in_us = CV_ARRAY_FLASH[10]*1000000;
|
|
||||||
absolute_time_t time_last_update = get_absolute_time();
|
|
||||||
// printf("core1 done\n");
|
|
||||||
while (1){
|
|
||||||
// Check for time difference between last packet and current time (Packet Timeout)
|
|
||||||
absolute_time_t current_time = get_absolute_time();
|
|
||||||
if (absolute_time_diff_us(time_last_update,current_time) > packet_timeout_threshold_in_us){
|
|
||||||
update_speed_dir(pid,1 + 128*pid->direction); // Emergency stop on timeout
|
|
||||||
}
|
|
||||||
// If core0 sends new speed data to core1 speed_step_target and direction variables get updated
|
|
||||||
if(multicore_fifo_rvalid()) {
|
|
||||||
update_speed_dir(pid, multicore_fifo_pop_blocking());
|
|
||||||
time_last_update = get_absolute_time();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -46,11 +46,8 @@ typedef struct pid_params{
|
|||||||
float m_rev; // Slope reverse direction
|
float m_rev; // Slope reverse direction
|
||||||
uint16_t speed_table[127]; // Array with setpoint values corresponding to every speed step
|
uint16_t speed_table[127]; // Array with setpoint values corresponding to every speed step
|
||||||
uint32_t speed_step_target; // End target speed step
|
uint32_t speed_step_target; // End target speed step
|
||||||
uint16_t end_target_index; // Index for speed table array corresponding to end target speed step
|
|
||||||
uint32_t setpoint; // Current setpoint
|
uint32_t setpoint; // Current setpoint
|
||||||
uint32_t setpoint_prev; // Previous setpoint
|
uint32_t setpoint_prev; // Previous setpoint
|
||||||
bool direction; // Current Direction
|
|
||||||
bool direction_prev; // Previous Direction
|
|
||||||
// Measurement Variables
|
// Measurement Variables
|
||||||
float measurement; // Newest Measurement value
|
float measurement; // Newest Measurement value
|
||||||
float adc_offset; // ADC offset value
|
float adc_offset; // ADC offset value
|
||||||
@@ -60,7 +57,7 @@ typedef struct pid_params{
|
|||||||
uint8_t r_side_arr_cutoff; // Discarded samples (right side)
|
uint8_t r_side_arr_cutoff; // Discarded samples (right side)
|
||||||
}pid_params;
|
}pid_params;
|
||||||
|
|
||||||
void update_speed_dir(pid_params *pid, uint32_t speed_step_target);
|
uint8_t get_speed_step_table_index_of_speed_step(uint8_t speed_step);
|
||||||
bool speed_helper(struct repeating_timer *t);
|
bool speed_helper(struct repeating_timer *t);
|
||||||
void adjust_pwm_level(uint16_t level, pid_params *pid);
|
void adjust_pwm_level(uint16_t level, pid_params *pid);
|
||||||
void update_m(pid_params *pid);
|
void update_m(pid_params *pid);
|
||||||
|
|||||||
@@ -6,8 +6,10 @@
|
|||||||
|
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
// Functions in shared.c can be accessed by both cores
|
uint8_t speed_step_target = 0;
|
||||||
|
uint8_t speed_step_target_prev = 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)
|
// 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,
|
float measure(uint8_t total_iterations,
|
||||||
uint8_t measurement_delay_us,
|
uint8_t measurement_delay_us,
|
||||||
@@ -61,3 +63,8 @@ uint16_t get_16bit_CV (uint16_t CV_start_index){
|
|||||||
uint8_t byte_0 = CV_ARRAY_FLASH[CV_start_index+1];
|
uint8_t byte_0 = CV_ARRAY_FLASH[CV_start_index+1];
|
||||||
return (byte_0) + (byte_1<<8);
|
return (byte_0) + (byte_1<<8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool 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;
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include "pico/multicore.h"
|
#include "pico/multicore.h"
|
||||||
#include "pico/printf.h"
|
#include "pico/printf.h"
|
||||||
|
#include "pico/platform.h"
|
||||||
#include "hardware/pwm.h"
|
#include "hardware/pwm.h"
|
||||||
#include "hardware/adc.h"
|
#include "hardware/adc.h"
|
||||||
#include "hardware/flash.h"
|
#include "hardware/flash.h"
|
||||||
@@ -58,7 +59,12 @@
|
|||||||
extern repeating_timer_t pid_control_timer;
|
extern repeating_timer_t pid_control_timer;
|
||||||
extern repeating_timer_t speed_helper_timer;
|
extern repeating_timer_t speed_helper_timer;
|
||||||
extern const uint8_t *CV_ARRAY_FLASH;
|
extern const uint8_t *CV_ARRAY_FLASH;
|
||||||
|
extern uint8_t speed_step_target;
|
||||||
|
extern uint8_t speed_step_target_prev;
|
||||||
|
|
||||||
|
|
||||||
void core1_entry();
|
void core1_entry();
|
||||||
|
void write_cv_handler(uint16_t cv_index, uint8_t cv_data);
|
||||||
float measure(uint8_t total_iterations,
|
float measure(uint8_t total_iterations,
|
||||||
uint8_t measurement_delay_us,
|
uint8_t measurement_delay_us,
|
||||||
uint8_t l_side_arr_cutoff,
|
uint8_t l_side_arr_cutoff,
|
||||||
@@ -66,3 +72,35 @@ float measure(uint8_t total_iterations,
|
|||||||
bool direction);
|
bool direction);
|
||||||
uint32_t get_32bit_CV (uint16_t CV_start_index);
|
uint32_t get_32bit_CV (uint16_t CV_start_index);
|
||||||
uint16_t get_16bit_CV (uint16_t CV_start_index);
|
uint16_t get_16bit_CV (uint16_t CV_start_index);
|
||||||
|
/*!
|
||||||
|
* @brief Get direction
|
||||||
|
* Function calculates direction.Direction is based on speed_step byte. <br>
|
||||||
|
* Refers to "128 Speed Control" - NMRA Standard 9.2.1 section 2.3.2.1 or RCN-212 section 2.2.2. <br>
|
||||||
|
*
|
||||||
|
* The following speed steps exist:
|
||||||
|
* * DEC | BIN | DIRECTION | DEFINITION
|
||||||
|
* * 000 | 0000 0000 | Reverse | STOP
|
||||||
|
* * 001 | 0000 0001 | Reverse | EMERGENCY STOP
|
||||||
|
* * 002 | 0000 0010 | Reverse | Speed step 001 (Lowest speed)
|
||||||
|
* * 003 | 0000 0011 | Reverse | Speed step 002
|
||||||
|
* * 004 | 0000 0100 | Reverse | Speed step 003
|
||||||
|
* * ... | .... .... | Reverse | .....
|
||||||
|
* * ... | .... .... | Reverse | .....
|
||||||
|
* * ... | .... .... | Reverse | .....
|
||||||
|
* * 126 | 0111 1110 | Reverse | Speed step 125
|
||||||
|
* * 127 | 0111 1111 | Reverse | Speed step 126 (Full speed)
|
||||||
|
* * 128 | 1000 0000 | Forward | STOP
|
||||||
|
* * 129 | 1000 0001 | Forward | EMERGENCY STOP
|
||||||
|
* * 130 | 1000 0010 | Forward | Speed step 001 (Lowest speed)
|
||||||
|
* * 131 | 1000 0011 | Forward | Speed step 002
|
||||||
|
* * 132 | 1000 0100 | Forward | Speed step 003
|
||||||
|
* * ... | .... .... | Forward | .....
|
||||||
|
* * ... | .... .... | Forward | .....
|
||||||
|
* * ... | .... .... | Forward | .....
|
||||||
|
* * 254 | 1111 1110 | Forward | Speed step 125
|
||||||
|
* * 255 | 1111 1111 | Forward | Speed step 126 (Full speed)
|
||||||
|
*
|
||||||
|
* @param speed_step speed step byte
|
||||||
|
* @return direction; false for reverse; true for forward;
|
||||||
|
*/
|
||||||
|
bool get_direction_of_speed_step(uint8_t speed_step);
|
||||||
Reference in new issue
Block a user