Change C18 to be 0603 - 25V and rotate on PCB for easier soldering; Small logging changes; Speed step enums are actually used now for better clarity; Introduced cv_setup_check_done and flash_safe_execute_core_init_done flags for coordinating the two cores better; Incremented version no. CV_7 to 2;

This commit is contained in:
gab-k committed 2024-12-31 07:55:14 +01:00
1 parent 0462d3f6b7
commit f1961fa5bd
7 files changed
+868 -896

No files matched your search

+3 -2
View File
@@ -16,7 +16,7 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b01100100, //CV_5 - V_max - Default = 100*16
0b00111111, //CV_6 - V_mid - Default = 63*16
0b00000001, //CV_7 - Version no. (read-only)
0b00000010, //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)
0b10010110, //CV_9 - PWM frequency in Hz = CV_9*100+10000 - Default = (150*100+10000)Hz = 25kHz
@@ -62,7 +62,8 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_44 -
0b00000000, //CV_45 -
0b00000000, //CV_46 -
// Speed controller - Configuration ////////////////////////////////////////////////////////////////////////////////
// TODO: Update controller CVs
// Speed controller - Configuration /////////////////////////////////////////////////////////////////////////////////
0b00110010, //CV_47 - Feed-forward gain k_ff = CV_47/10000 Default = 50 -> 0.005 //
0b00001010, //CV_48 - PID Control low pass filter time constant (tau) in ms //
0b00000101, //CV_49 - PID Control sampling time t in ms //
+18 -9
View File
@@ -149,7 +149,7 @@ static uint16_t two_std_dev(const uint16_t arr[], const uint32_t length) {
static void adc_offset_adjustment(uint32_t const n) {
// Measures constant adc offset and programs the offset into flash
LOG(1, "ADC offset adjustment...\n");
LOG(1, "ADC offset adjustment with %u iterations...\n", n);
uint16_t offsets_fwd[n];
uint16_t offsets_rev[n];
@@ -537,7 +537,8 @@ static bool reset_message_check(size_t const number_of_bytes, const uint8_t *con
speed_step_target_prev = speed_step_target;
// 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() comment in "shared.h"
speed_step_target = 1 + 128*dir;
if(dir == DIRECTION_FORWARD) speed_step_target = SPEED_STEP_FORWARD_EMERGENCY_STOP;
else if(dir == DIRECTION_REVERSE) speed_step_target = SPEED_STEP_REVERSE_EMERGENCY_STOP;
update_active_functions(0,0,0); // Disables all functions
return true;
}
@@ -665,11 +666,12 @@ static void cv_setup_check() {
}
// Check for existing ADC offset setup
if (CV_ARRAY_FLASH[171] == 0xFF) {
if (true || CV_ARRAY_FLASH[171] == 0xFF) {
LOG(1, "Detected ADC offset factory condidition (CV_172 == %u), running offset adjustment measurement function...\n", CV_ARRAY_FLASH[171]);
adc_offset_adjustment(ADC_CALIBRATION_ITERATIONS);
}
LOG(1, "CV check done!\n");
LOG(1, "CV check done! Setting wait_for_cv_setup_check flag to false!\n");
cv_setup_check_done = true;
}
static void init_motor_pwm(uint8_t const gpio) {
@@ -753,10 +755,6 @@ int main() {
LOG(1, "core0 Initialization...\n");
// Enable the watchdog, requiring the watchdog to be updated every WATCHDOG_TIMER_IN_MS milliseconds or the chip will reboot
// second arg is pause on debug which means the watchdog will pause when stepping through code
watchdog_enable(WATCHDOG_TIMER_IN_MS, 1);
// Check for reboot by watchdog and set error when true
if (watchdog_caused_reboot()) {
LOG(1, "Reboot by watchdog detected!\n");
@@ -780,7 +778,13 @@ int main() {
// Launch core1
multicore_launch_core1(core1_entry);
// Wait 100ms to give core1 some time to call flash_safe_execute_core_init()
// Wait for core1 to call flash_safe_execute_core_init() and set the flash_safe_execute_core_init_done flag
LOG(1, "Waiting for flash_safe_execute_core_init_done flag...\n");
while (!flash_safe_execute_core_init_done) {
watchdog_update();
}
busy_wait_ms(100);
@@ -798,6 +802,11 @@ int main() {
gpio_set_irq_enabled(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true);
irq_set_enabled(IO_IRQ_BANK0, true);
// Enable the watchdog, requiring the watchdog to be updated every WATCHDOG_TIMER_IN_MS milliseconds or the chip will reboot
// second arg is pause on debug which means the watchdog will pause when stepping through code
// Watchdog is updated by core0 and core1 whenever there is time and hopefully soon enough to prevent a reboot.
watchdog_enable(WATCHDOG_TIMER_IN_MS, 1);
// Endless loop
while (true) {
// Check for new messages in ring buffer
+8 -20
View File
@@ -82,7 +82,7 @@ void speed_helper(controller_parameter_t * ctrl_par) {
const bool direction_changed = get_direction_of_speed_step(speed_step_target) !=
get_direction_of_speed_step(speed_step_target_prev);
if (speed_step_target == 129 || speed_step_target == 1) {
if (speed_step_target == SPEED_STEP_FORWARD_EMERGENCY_STOP || speed_step_target == SPEED_STEP_REVERSE_EMERGENCY_STOP) {
// Emergency Stop
speed_table_index = 0;
speed_helper_counter = 0;
@@ -353,34 +353,22 @@ bool speed_helper_timer_callback(__unused struct repeating_timer *t) {
void core1_entry() {
LOG(1, "core1 Initialization...\n");
if (flash_safe_execute_core_init() != true){
flash_safe_execute_core_init_done = flash_safe_execute_core_init();
if (flash_safe_execute_core_init_done != true){
set_error(FLASH_SAFE_EXECUTE_CORE_INIT_FAILURE);
return;
}
// Wait for core0 to finish CV check (see cv_setup_check)
LOG(1, "Waiting for CV check flag...\n");
while(!cv_setup_check_done){
watchdog_update(); // Update watchdog while waiting on core0
}
controller_parameter_t control_parameter;
controller_parameter_t *ctrl_par = &control_parameter;
init_controller(ctrl_par);
/*
alarm_pool_add_repeating_timer_ms(alarm_pool_create(0, 1),
CV_ARRAY_FLASH[48],
controller_general,
ctrl_par,
&pid_control_timer);
// 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),
CV_ARRAY_FLASH[174],
speed_helper,
ctrl_par,
&speed_helper_timer);
*/
struct repeating_timer timer_controller, timer_speed_helper;
add_repeating_timer_ms(-CV_ARRAY_FLASH[48], controller_timer_callback, NULL, &timer_controller);
add_repeating_timer_ms(-CV_ARRAY_FLASH[174], speed_helper_timer_callback, NULL, &timer_speed_helper);
+7 -4
View File
@@ -7,8 +7,10 @@
#include "shared.h"
// TODO: Implement enum typedef for speed steps, also getter and setter functions checking validity of speed step values.
uint8_t speed_step_target = 0;
uint8_t speed_step_target_prev = 0;
speed_step_t speed_step_target = SPEED_STEP_REVERSE_STOP;
speed_step_t speed_step_target_prev = SPEED_STEP_REVERSE_STOP;
bool cv_setup_check_done = false;
bool flash_safe_execute_core_init_done = false;
static error_t error_state = 0;
// Functions in shared.c are accessed by both cores
@@ -30,8 +32,9 @@ uint16_t get_16bit_CV (uint16_t CV_start_index){
return (byte_0) + (byte_1<<8);
}
direction_t get_direction_of_speed_step(uint8_t speed_step){
// Shift by 7 Bytes to move bit7 into bit0 position and return
direction_t get_direction_of_speed_step(speed_step_t speed_step){
// Bit 7 is the direction bit.
// Shift by 7 bits to move bit7 into bit0 position and return direction.
return speed_step >> 7;
}
+5 -3
View File
@@ -309,8 +309,10 @@ typedef enum {
extern repeating_timer_t pid_control_timer;
extern repeating_timer_t speed_helper_timer;
extern const uint8_t *CV_ARRAY_FLASH;
extern uint8_t speed_step_target;
extern uint8_t speed_step_target_prev;
extern speed_step_t speed_step_target;
extern speed_step_t speed_step_target_prev;
extern bool cv_setup_check_done;
extern bool flash_safe_execute_core_init_done;
void core1_entry();
@@ -347,7 +349,7 @@ uint16_t get_16bit_CV (uint16_t CV_start_index);
* @param speed_step The speed step byte.
* @return `DIRECTION_FORWARD` for forward direction, `DIRECTION_REVERSE` for reverse direction.
*/
direction_t get_direction_of_speed_step(uint8_t speed_step);
direction_t get_direction_of_speed_step(speed_step_t speed_step);
void set_error(error_t err);