From cdb68b84da828226d33a2e896a4a2bd84be35ea8 Mon Sep 17 00:00:00 2001 From: 1nct Date: Sun, 20 Feb 2022 11:32:03 +0100 Subject: [PATCH] added startup offset to establish a baseline duty cycle at which the motor starts to spin --- CV.h | 41 +++++----- core0.c | 229 ++++++++++++++++++++++++++++++++++--------------------- core0.h | 10 ++- core1.c | 151 +++++++++++++++++------------------- core1.h | 8 +- shared.h | 8 +- 6 files changed, 251 insertions(+), 196 deletions(-) diff --git a/CV.h b/CV.h index 5cd8943..52dc782 100644 --- a/CV.h +++ b/CV.h @@ -1,16 +1,20 @@ #include "inttypes.h" +#define CV_ARRAY_SIZE 512 //All CVs are 8-bit numbers (range 0 - 255) //Note : CV_1 ≙ index 0, CV_2 ≙ index 1, CV_3 ≙ index 2, ... -uint8_t CV_ARRAY_DEFAULT [512] = { +//TODO: +// - pwm CV for aux & gpio (duty cycle/freq) +// - Packet Timeout +// - Motor PWM Clock & Divider +// - V_max/V_min & V_emf +// - +uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = { 0b00000011, //CV_1 - Basic Address 0b00000000, //CV_2 - Minimum Speed ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // 255 == Fastest dec/acc rate; 0 == slowest; - // Note that there is an integer division in play here which will get rid of the fractional part - // this means that a value of 254 for example, will result in the same behaviour as an 248. - // This does not correspond to the norm and might be changed eventually. - 0b00000101, //CV_3 - Acceleration Rate - 0b00000101, //CV_4 - Deceleration Rate + // 0 == Fastest dec/acc rate; 255 == slowest; + 0b00000000, //CV_3 - Acceleration Rate + 0b00000000, //CV_4 - Deceleration Rate ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 0b11111111, //CV_5 - V_max 0b01111111, //CV_6 - V_mid @@ -18,7 +22,7 @@ uint8_t CV_ARRAY_DEFAULT [512] = { 0b00001101, //CV_8 - Manufacturer 0b00000000, //CV_9 - PWM-Period 0b00000001, //CV_10 - PWM Clock Divider - 0b11111111, //CV_11 - Packet Timeout in seconds TODO: not implemented yet + 0b11111111, //CV_11 - Packet Timeout in seconds 0b00000100, //CV_12 - Permitted operating modes 0b00000000, //CV_13 - 0b00000000, //CV_14 - @@ -67,14 +71,17 @@ uint8_t CV_ARRAY_DEFAULT [512] = { 0b01111011, //CV_51 - PID Control D_Factor ≙ CV_51/4096 i.e. Default = 123 -> 0.03002 sec // 0b00110000, //CV_52 - PID Integral Limit positive ≙ CV_52*10 i.e. Default = 48 -> +480 // 0b00110000, //CV_53 - PID Integral Limit negative ≙ CV_53*(-10) i.e. Default = 48 -> -480 // + //This Offset is used to establish a "baseline" motor PWM duty cycle at which the motor starts to move + //If CV_54+CV_55+CV_56+CV_57 = 0 (after reset or manual overwrite) there will be a measurement procedure on startup// + //alternatively, the measurement can be done by writing a 7 to CV_7 on the programming track. // + 0b00000000, //CV_54 - Forward Direction Offset Bits 0-7 Default = 0 // + 0b00000000, //CV_55 - Forward Direction Offset Bits 8-15 Default = 0 // + 0b00000000, //CV_56 - Reverse Direction Offset Bits 0-7 Default = 0 // + 0b00000000, //CV_57 - Reverse Direction Offset Bits 8-15 Default = 0 // + 0b00000110, //CV_58 - Offset Measurement Cycles Default = 6 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - 0b00000000, //CV_54 - - 0b00000000, //CV_55 - - 0b00000000, //CV_56 - - 0b00000000, //CV_57 - - 0b00000000, //CV_58 - - 0b00000000, //CV_59 - - 0b00010100, //CV_60 - PWM scaling factor (target EMF Voltage) -> 126*CV_60 = end_target|max + 0b00000111, //CV_59 - speed_helper timer delay - can be used to adjust accel/decel rate // + 0b00010100, //CV_60 - PWM scaling factor (target EMF Voltage) -> 126*CV_60 = end_target|max // // V_EMF Measurement Configuration ////////////////////////////////////////////////////////////////////////////////// 0b01000110, //CV_61 - V_EMF Total amount of Measurement Iterations - Default = 70 iterations // 0b01100100, //CV_62 - V_EMF Delay before Measuring - Default = 100us // @@ -83,8 +90,8 @@ uint8_t CV_ARRAY_DEFAULT [512] = { // For Example: CV_63 = x and CV_64 = y // // -> The x smallest values will be discarded. // // -> The y largest values will be discarded. // - 0b00001111, //CV_63 - left_side_array_cutoff - Default = 15 // - 0b00001111, //CV_64 - right_side_array_cutoff - Default = 15 // // + 0b00001111, //CV_63 - left_side_array_cutoff - Default = 15 // + 0b00001111, //CV_64 - right_side_array_cutoff - Default = 15 // // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 0b00000000, //CV_65 - 0b00000000, //CV_66 - diff --git a/core0.c b/core0.c index 04d860e..75b3902 100644 --- a/core0.c +++ b/core0.c @@ -5,63 +5,79 @@ ////////////////////////// #include "core0.h" -bool target_direction = true; -uint target_speed_step = 128; +bool target_direction = true; //Forward = true - Reverse = false +uint target_speed_step = 128; //128 = Stop (Forward Direction) const uint8_t *CV_ARRAY_FLASH = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET); bool active_functions[SIZE_ACTIVE_FUNCTIONS] = {false}; uint64_t last_bits = 0; bool reset_package_flag = false; +absolute_time_t falling_edge_time,rising_edge_time; - -int8_t check_for_package() { //returns number of bytes if valid bit-pattern is found. Otherwise -1 is returned. - uint64_t package3Masked = last_bits & PACKAGE_MASK_3_BYTES; - if (package3Masked == PACKAGE_3_BYTES) { - return 3; +uint find_offset(uint level,uint step,uint delay,float threshold, bool direction){ + pwm_set_gpio_level(!direction,0); + uint gpio = MOTOR_PWM_FORWARD*direction + MOTOR_PWM_REVERSE*!direction; + while((measure(direction)-ADC_OFFSET) byte_n(address byte) -> ... -> byte_0(error detection byte) -> end of transmission -void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) { - for (uint8_t i = 0; i < number_of_bytes; i++) { - byte_array[i] = last_bits >> (i * 9 + 1); +uint two_sigma(const uint arr[], uint length){ + //Calculate arithmetic average + uint sum = 0; + for (uint i = 0; i < length; ++i) { + sum += arr[i]; } + uint x_avg = sum / length; + //Calculate variance + sum = 0; + for (uint i = 0; i < length; ++i) { + sum += (arr[i]-x_avg)*(arr[i]-x_avg); + } + float variance = (float) sum/ (float)(length-1); + //Calculate standard deviation + uint std_dev = (uint)sqrtf(variance); + //"Filter" every array element that deviates more than 2*(std_dev) + sum = 0; + uint counter = 0; + for (uint i = 0; i < length; ++i) { + uint val = arr[i]-x_avg; + if ( val < 2*std_dev || val > (0-2*std_dev) ){ + sum += arr[i]; + counter++; + } + } + //Calculate and return "new" arithmetic average + return sum / counter; } -void adjust_pwm_level(uint16_t level) { - if (target_direction) { - pwm_set_gpio_level(MOTOR_PWM_REVERSE,0); - pwm_set_gpio_level(MOTOR_PWM_FORWARD,level); - } - //Reverse - else { - pwm_set_gpio_level(MOTOR_PWM_FORWARD,0); - pwm_set_gpio_level(MOTOR_PWM_REVERSE,level); +void setup_offsets(uint length){ + uint offsets_fwd[length]; + uint offsets_rev[length]; + for (int i = 0; i < length; ++i) { + offsets_fwd[i] = find_offset(0,1,1,0.05f,true); + busy_wait_ms(500); + offsets_rev[i] = find_offset(0,1,1,0.05f,false); + busy_wait_ms(500); } + uint offsets_fwd_avg = two_sigma(offsets_fwd, length); + uint offsets_rev_avg = two_sigma(offsets_rev, length); + uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE]; + memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP)); + CV_ARRAY_TEMP[53] = offsets_fwd_avg; + CV_ARRAY_TEMP[54] = offsets_fwd_avg>>8; + CV_ARRAY_TEMP[55] = offsets_rev_avg; + CV_ARRAY_TEMP[56] = offsets_rev_avg>>8; + flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); + flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2); + acknowledge(); } void acknowledge() { - target_direction = false; - adjust_pwm_level(5000); + pwm_set_gpio_level(MOTOR_PWM_FORWARD,5000); busy_wait_ms(6); - adjust_pwm_level(0); + pwm_set_gpio_level(MOTOR_PWM_FORWARD,0); } - void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos) { uint8_t mask = 0b00000001; bool res = ( (CV_ARRAY_FLASH[cv_address] >> bit_pos) &mask ) == bit_val; @@ -74,36 +90,30 @@ void verify_cv_byte(uint16_t cv_address, uint8_t cv_data){ } void write_cv_byte(uint16_t cv_index, uint8_t cv_data){ //CV_7 & CV_8 are read-only - if (cv_index == 6 || cv_index == 7){ - //Reset all CVs to Default (CV_8; Value = 8) - if (cv_data == 8){ - flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); - flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2); - acknowledge(); - } + //Reset all CVs to Default (CV_8; Value = 8) + if(cv_index == 7 && cv_data == 8){ + flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); + flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2); + acknowledge(); } - else if(cv_index == 0 && cv_data == 0); //CV_1 = 0 is not permitted + //Re-setup offset Adjustment (CV_7; Value = 7); + else if(cv_index == 6 && cv_data == 7){ + setup_offsets(CV_ARRAY_FLASH[57]); + } + //CV_1 = 0 is not permitted + else if(!cv_index && !cv_data); else{ - uint8_t CV_ARRAY_TEMP[512] = {0}; - memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP)); - CV_ARRAY_TEMP[cv_index] = cv_data; - flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); - flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2); - acknowledge(); + uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE]; + memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP)); + CV_ARRAY_TEMP[cv_index] = cv_data; + flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); + flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2); + acknowledge(); } } - -bool reset_package_check(uint8_t number_of_bytes,const uint8_t byte_array[]){ - if (byte_array[number_of_bytes-1] == 0b00000000 && byte_array[number_of_bytes-2] == 0b00000000){ - target_speed_step = 1; - //update_active_functions(0,0,37); - return true; - }else { - return false; - } -} - void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]){ + if( !((CV_ARRAY_FLASH[53])+(CV_ARRAY_FLASH[54]<<8)) || !((CV_ARRAY_FLASH[55])+(CV_ARRAY_FLASH[56]<<8)) ){ + } //Check for valid programming command ("address" 112-127) if (byte_array[number_of_bytes - 1]<128 && byte_array[number_of_bytes - 1]>111){ uint8_t instruction_type_mask = 0b00001100; @@ -135,7 +145,6 @@ void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]){ multicore_launch_core1(core1_entry); } } - void set_outputs() { uint32_t GPIO_to_be_set = 0; for (uint8_t i = 0; i < 32; i++) { @@ -151,7 +160,6 @@ void set_outputs() { GPIO_to_be_set &= ALLOWED_GPIO_MASK; gpio_put_masked(ALLOWED_GPIO_MASK,GPIO_to_be_set); } - void update_active_functions(uint8_t function_number, uint8_t input_byte, uint8_t count) { uint8_t mask = 0b00000001; for (uint8_t i = 0; i < count; i++) { @@ -159,7 +167,6 @@ void update_active_functions(uint8_t function_number, uint8_t input_byte, uint8_ mask = mask << 1; } } - bool error_detection(int8_t number_of_bytes, const uint8_t byte_array[]) { //Bitwise XOR for all Bytes -> Successful result is: "0000 0000" uint8_t xor_byte = 0; @@ -168,13 +175,11 @@ bool error_detection(int8_t number_of_bytes, const uint8_t byte_array[]) { } return (0 == xor_byte); } - // Returns true for long address bool is_long_address(uint8_t number_of_bytes, const uint8_t byte_array[]) { if ((byte_array[number_of_bytes - 1]>>6) == 0b00000011) return true; return false; } - bool address_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) { //Check for Idle Package if (byte_array[number_of_bytes - 1] == 255) @@ -201,7 +206,6 @@ bool address_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) { return (CV_ARRAY_FLASH[0] == read_address); } } - void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) { uint8_t command_byte_n; uint8_t command_byte_start_index; @@ -216,10 +220,10 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) if (command_byte_n == 0b00111111){ target_speed_step = byte_array[command_byte_start_index - 1]; if(target_speed_step>127) { - target_direction = true; + target_direction = true; //Forward } else { - target_direction = false; + target_direction = false; //Reverse } } // 10XX-XXXX (Function Group Instruction) @@ -260,11 +264,40 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) } set_outputs(); } -//Interrupt handler for DCC Logic Signal -void gpio_callback_rise(unsigned int gpio, long unsigned int events) { - add_alarm_in_us(87, &readBit_alarm_callback, NULL, false); +bool reset_package_check(uint8_t number_of_bytes,const uint8_t byte_array[]){ + if (byte_array[number_of_bytes-1] == 0b00000000 && byte_array[number_of_bytes-2] == 0b00000000){ + target_speed_step = 1; + //update_active_functions(0,0,37); + return true; + }else { + return false; + } +} +int8_t check_for_package() { //returns number of bytes if valid bit-pattern is found. Otherwise -1 is returned. + uint64_t package3Masked = last_bits & PACKAGE_MASK_3_BYTES; + if (package3Masked == PACKAGE_3_BYTES) { + return 3; + } + uint64_t package4Masked = last_bits & PACKAGE_MASK_4_BYTES; + if (package4Masked == PACKAGE_4_BYTES) { + return 4; + } + uint64_t package5Masked = last_bits & PACKAGE_MASK_5_BYTES; + if (package5Masked == PACKAGE_5_BYTES) { + return 5; + } + return -1; +} +void writeLastBit(bool bit) { + last_bits <<= 1; + last_bits |= bit; +} +//start of transmission -> byte_n(address byte) -> ... -> byte_0(error detection byte) -> end of transmission +void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) { + for (uint8_t i = 0; i < number_of_bytes; i++) { + byte_array[i] = last_bits >> (i * 9 + 1); + } } - void evaluation(){ int8_t number_of_bytes = check_for_package(); if (number_of_bytes != -1) { @@ -274,9 +307,9 @@ void evaluation(){ } if (error_detection(number_of_bytes,byte_array)) { if (address_evaluation(number_of_bytes,byte_array)) { - reset_package_flag = false; - instruction_evaluation(number_of_bytes,byte_array); - } + reset_package_flag = false; + instruction_evaluation(number_of_bytes,byte_array); + } else if (reset_package_flag){ program_mode(number_of_bytes,byte_array); } @@ -286,7 +319,24 @@ void evaluation(){ } } } - +void track_signal_rise(unsigned int gpio, long unsigned int events) { + rising_edge_time = get_absolute_time(); + gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, false, &track_signal_rise); + gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_FALL, true, &track_signal_fall); +} +void track_signal_fall(unsigned int gpio, long unsigned int events) { + falling_edge_time = get_absolute_time(); + int64_t time_logical_high = absolute_time_diff_us(rising_edge_time,falling_edge_time); + if(time_logical_high > 87){ + writeLastBit(0); + } + else{ + writeLastBit(1); + } + evaluation(); + 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); +} int main() { stdio_init_all(); printf("core0 init...\n"); @@ -295,8 +345,13 @@ int main() { multicore_launch_core1(core1_entry); gpio_init(DCC_INPUT_PIN); gpio_set_dir(DCC_INPUT_PIN, GPIO_IN); - gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &gpio_callback_rise); - printf("core0 done\n"); - while (1){ + busy_wait_ms(100); + //Check for offset setup + if( !( (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]<<8) + (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]<<8) ) ){ + uint8_t arr[4] = {125,7,6,124}; + program_mode(4,arr); } + gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise); + printf("core0 done\n"); + while (1); } \ No newline at end of file diff --git a/core0.h b/core0.h index ab1316a..d4cb6b2 100644 --- a/core0.h +++ b/core0.h @@ -7,6 +7,7 @@ #pragma once #include "shared.h" #include "CV.h" +#include "pico/float.h" #define SIZE_BYTE_ARRAY 5 #define SIZE_ACTIVE_FUNCTIONS 32 #define DCC_INPUT_PIN 9 @@ -20,7 +21,6 @@ #define PACKAGE_MASK_4_BYTES 0b11111111111000000001000000001000000001000000001 #define PACKAGE_5_BYTES 0b11111111110000000000000000000000000000000000000000000001 #define PACKAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001 -#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE) //This Offset should be far enough away from program data int8_t check_for_package(); void writeLastBit(bool bit) ; int64_t readBit_alarm_callback(alarm_id_t id, void *user_data); @@ -37,5 +37,9 @@ 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 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 gpio_callback_rise(unsigned int gpio, long unsigned int events) ; -void evaluation(); \ No newline at end of file +void track_signal_rise(unsigned int gpio, long unsigned int events); +void track_signal_fall(unsigned int gpio, long unsigned int events); +void evaluation(); +uint find_offset(uint level,uint step,uint delay,float threshold, bool direction); +uint two_sigma(const uint arr[], uint length); +void setup_offsets(uint length); \ No newline at end of file diff --git a/core1.c b/core1.c index 0ed02ae..820ea2a 100644 --- a/core1.c +++ b/core1.c @@ -21,6 +21,9 @@ typedef struct pid_params{ float e; float e_prev; float e_sum; + uint startup_offset_fwd; + uint startup_offset_rev; + bool direction_prev; }pid_params; pid_params pid; @@ -31,7 +34,7 @@ typedef struct measure_params{ uint8_t right_side_array_cutoff; }measure_params; measure_params msr; -uint8_t a_factor = 30; /*CV_ARRAY_FLASH[59];..................*/ +uint8_t a_factor = 50; /*CV_ARRAY_FLASH[59];..................*/ uint16_t calc_end_target(){ //Forward @@ -45,11 +48,9 @@ uint16_t calc_end_target(){ else return (target_speed_step-1) * a_factor; /*(float)CV_ARRAY_FLASH[59]*/ } } - - bool speed_helper() { - uint8_t accel_rate = 1; /*CV_ARRAY_FLASH[2];*/ - uint8_t decel_rate = 1; /*CV_ARRAY_FLASH[3];*/ + uint8_t accel_rate = CV_ARRAY_FLASH[2]; + uint8_t decel_rate = CV_ARRAY_FLASH[3]; uint16_t end_target = calc_end_target(); //current_target only gets adjusted when the counter equals the acc/dec rate //-> Time for 1 Speed Step := (speed_helper timer delay)*(accel_rate) @@ -61,38 +62,35 @@ bool speed_helper() { } //Acceleration else if (end_target > current_target && speed_helper_counter == accel_rate) { - current_target += a_factor; + if(!accel_rate) current_target = end_target; // directly accelerate to end target speed + else current_target += a_factor; speed_helper_counter = 0; } //Deceleration else if (end_target < current_target && speed_helper_counter == decel_rate) { - current_target -= a_factor; + if(!decel_rate) current_target = end_target; // directly decelerate to end target speed + else current_target -= a_factor; speed_helper_counter = 0; } //Acceleration/Deceleration "scheduled" - else if (end_target != current_target){ - speed_helper_counter++; - } + else if (end_target != current_target) speed_helper_counter++; //Target reached -> Reset Counter - else{ - speed_helper_counter = 0; - } + else speed_helper_counter = 0; current_target_voltage = (float)current_target/1260; return true; } - -float measure(){ +float measure(bool direction){ + uint32_t sum = 0; + uint16_t adc_val[100]; pwm_set_gpio_level(MOTOR_PWM_FORWARD,0); pwm_set_gpio_level(MOTOR_PWM_REVERSE,0); - adc_select_input(!target_direction); - busy_wait_us(100/*msr.delay_in_us*/); - uint32_t sum = 0; - uint16_t adc_val[100/*msr.total_iterations*/]; + adc_select_input(!direction); + busy_wait_us(msr.delay_in_us); adc_run(true); int key, j; - for (int i = 0; i < 100/*msr.total_iterations*/; ++i) { + for (int i = 0; i < msr.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(); - //Insertion Sort (1 Step) key = adc_val[i]; j = i - 1; while (j >= 0 && adc_val[j] > key) { @@ -102,100 +100,87 @@ float measure(){ adc_val[j + 1] = key; } adc_run(false); - for (uint8_t i = msr.left_side_array_cutoff; i < 100/*msr.total_iterations*/ - msr.right_side_array_cutoff ; ++i) { + //discard x entries beginning from the lowest entry of the array (counting up); x = msr.left_side_array_cutoff + //discard y entries beginning from the highest index of the array (counting down); y = msr.right_side_array_cutoff + for (uint8_t i = msr.left_side_array_cutoff; i < msr.total_iterations - msr.right_side_array_cutoff ; ++i) { sum += adc_val[i]; } //V_z is represented in 12bit - float V_z = (float)sum / (float)(100/*msr.total_iterations*/ - 30); + float V_z = (float)sum / (float)(msr.total_iterations - (msr.left_side_array_cutoff+msr.right_side_array_cutoff)); //Zener correction (approximated) & Conversion factor for V_z <= 1951 - //Returns Value in "Volts" + //Function Returns Value in "Volts" if (V_z > 1951){ float V_z_sq = V_z * V_z; return ( C0 + C1 * V_z + C2 * V_z_sq + C3 * V_z * V_z_sq + C4 * V_z_sq * V_z_sq); } - else{ - return (L1*V_z); + else return (L1*V_z); +} +void adjust_pwm_level(uint16_t level) { + if (target_direction) { + pwm_set_gpio_level(MOTOR_PWM_REVERSE,0); + pwm_set_gpio_level(MOTOR_PWM_FORWARD,level); + } + //Reverse + else { + pwm_set_gpio_level(MOTOR_PWM_FORWARD,0); + pwm_set_gpio_level(MOTOR_PWM_REVERSE,level); } } void set_pid_params(float v){ float v_max = (float)a_factor/10; pid.t_s = 5.0f / 1000; - pid.k_p = (-90*v)/v_max+100.0f; - pid.a_1 = 0.0f * pid.t_s; // k_i*t_s - pid.a_2 = 2.0f / pid.t_s; // k_d/t_s - if (pid.k_p<10)pid.k_p=10; - if (pid.a_2<0)pid.k_p=0; - /*if (v > 0.6f * max_voltage){ - pid.k_p = 5.0f; - pid.t_s = 5.0f / 1000; - pid.a_1 = 0.5f * pid.t_s; // k_i*t_s - pid.a_2 = 0.0f / pid.t_s; // k_d/t_s - } - else if(v > 0.2f * max_voltage){ - pid.k_p = 30.0f; - pid.t_s = 5.0f / 1000; - pid.a_1 = 0.0f * pid.t_s; // k_i*t_s - pid.a_2 = 5.0f / pid.t_s; // k_d/t_s - }*/ - //else{ - - //} + pid.k_p = 200.0f; + pid.k_p = (-200000*current_target_voltage)/v_max+200000.0f; + pid.a_1 = 200.0f * pid.t_s; // k_i*t_s + pid.a_2 = 0.0f / pid.t_s; // k_d/t_s } bool pid_control(struct repeating_timer *t){ - float measured_voltage = measure(); - set_pid_params(measured_voltage); - pid.e = current_target_voltage - measured_voltage; - pid.e_sum = pid.e_sum + pid.e; + pid.output += (float)(pid.startup_offset_fwd*target_direction + pid.startup_offset_rev*(!target_direction)); + float measured_voltage = measure(target_direction); + if(target_direction != pid.direction_prev) pid.e_sum = 0.0f; + pid.e = current_target_voltage - (measured_voltage - ADC_OFFSET); + pid.e_sum += pid.e; if (pid.e_sum > pid.sum_limit_max) pid.e_sum = pid.sum_limit_max; else if (pid.e_sum < pid.sum_limit_min) pid.e_sum = pid.sum_limit_min; - pid.output += (pid.k_p * (float)pid.e) + + pid.output = (pid.k_p * (float)pid.e) + (pid.a_1 * (float)pid.e_sum) + // k_i*t_s*e_sum (pid.a_2 * (float)(pid.e - pid.e_prev)); // (k_d/t_s)*(e-e_prev) - pid.e_prev = pid.e; - if (current_target == 0) pid.output = 0; + if (pid.output < 0 || current_target == 0) pid.output = 0; else if (pid.output > 5000) pid.output = 5000; - else if (pid.output < 1000) pid.output = 1000; adjust_pwm_level((uint16_t )pid.output); adc_fifo_drain(); + pid.direction_prev = target_direction; + pid.e_prev = pid.e; return true; } - void init_pid(){ + msr.total_iterations = 100;/*CV_ARRAY_FLASH[60];*/ + msr.delay_in_us = 100; /*CV_ARRAY_FLASH[61];*/ + msr.left_side_array_cutoff = 15;/*CV_ARRAY_FLASH[62];*/ + msr.right_side_array_cutoff = 15;/*CV_ARRAY_FLASH[63];*/ // pid.k_p = ((float)CV_ARRAY_FLASH[48] / 256); // pid.t_s = ((float)CV_ARRAY_FLASH[47] / 1000); // pid.a_1 = ((float)CV_ARRAY_FLASH[49] / 64) * pid.t_s ; // k_i*t_s // pid.a_2 = ((float)CV_ARRAY_FLASH[50] / 4096) / pid.t_s; // k_d/t_s - //pid.k_p = 5.0f; - //pid.t_s = 5.0f / 1000; - //pid.a_1 = 0.5f * pid.t_s; // k_i*t_s - //pid.a_2 = 0.0f / pid.t_s; // k_d/t_s - pid.sum_limit_max = 200.0f;/*(float)(CV_ARRAY_FLASH[51] );*/ - pid.sum_limit_min = -200.0f;/*(float)(CV_ARRAY_FLASH[52] * (-1));*/ - msr.total_iterations = 70;/*CV_ARRAY_FLASH[60];*/ - msr.delay_in_us = CV_ARRAY_FLASH[61]; - msr.left_side_array_cutoff = 15;/*CV_ARRAY_FLASH[62];*/ - msr.right_side_array_cutoff = 15;/*CV_ARRAY_FLASH[63];*/ + //pid.k_p = (-200000*current_target_voltage)/v_max+200000.0f; + pid.k_p = 200.0f; + pid.t_s = (float) CV_ARRAY_FLASH[47] / 1000; + pid.a_1 = 2000.0f * pid.t_s; // k_i*t_s + pid.a_2 = 0.0f / pid.t_s; // k_d/t_s + pid.sum_limit_max = 100.0f;/*(float)(CV_ARRAY_FLASH[51] );*/ + pid.sum_limit_min = -100.0f;/*(float)(CV_ARRAY_FLASH[52] * (-1));*/ + pid.direction_prev = target_direction; + pid.startup_offset_fwd = (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]<<8); + pid.startup_offset_rev = (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]<<8); } - -void init_pwm(uint gpio) { +void init_pwm_core1(uint gpio) { uint slice_num = pwm_gpio_to_slice_num(gpio); uint32_t wrap_counter = 5000; // 5000 Cycles @ 125MHz for 1 Period -> 25kHz pwm_set_wrap(slice_num, wrap_counter); pwm_set_gpio_level(gpio,0); - pwm_set_clkdiv(slice_num,1); pwm_set_enabled(slice_num, true); } - -void print_cv_array(const uint8_t *buf, size_t len) { - for (size_t i = 0; i < len; ++i) { - printf("CV_%u: %02x",i+1, buf[i]); - if (i % 4 == 3) - printf("\n"); - else - printf(" "); - } -} void core1_entry() { printf("core1 init...\n"); if(gpio_get_function(MOTOR_PWM_FORWARD)!=4){ @@ -205,21 +190,21 @@ void core1_entry() { adc_gpio_init(V_EMF_ADC_PIN_FORWARD); adc_gpio_init(V_EMF_ADC_PIN_REVERSE); adc_fifo_setup(true,false,0,false,false); - init_pwm(MOTOR_PWM_FORWARD); - init_pwm(MOTOR_PWM_REVERSE); + init_pwm_core1(MOTOR_PWM_FORWARD); + init_pwm_core1(MOTOR_PWM_REVERSE); init_pid(); } alarm_pool_add_repeating_timer_ms(alarm_pool_create(0,1), - 5/*CV_ARRAY_FLASH[47]*/, + CV_ARRAY_FLASH[47], pid_control, NULL, &pid_control_timer); irq_set_priority(0,0x00); // 0x00 = highest priority alarm_pool_add_repeating_timer_ms(alarm_pool_create(1,1), - 7, + CV_ARRAY_FLASH[58], speed_helper, NULL, &speed_helper_timer); printf("core1 done\n"); while (1); -} \ No newline at end of file +} diff --git a/core1.h b/core1.h index 00b692b..2958c80 100644 --- a/core1.h +++ b/core1.h @@ -17,9 +17,11 @@ #define C3 -1.000062393830345e-08f #define C4 1.15408454807169e-12f +void adjust_pwm_level(uint16_t level); uint16_t calc_end_target(); bool speed_helper(); -float measure(); void init_pid(); -void init_pwm(uint gpio); -void print_cv_array(const uint8_t *buf, size_t len); \ No newline at end of file +void init_pwm_core1(uint gpio); +bool pid_control(struct repeating_timer *t); +void set_pid_params(float v); +void print_cv_array(const uint8_t *buf, size_t len); diff --git a/shared.h b/shared.h index cef2d7b..b07e6a5 100644 --- a/shared.h +++ b/shared.h @@ -9,17 +9,19 @@ #include "string.h" #include "pico/stdlib.h" #include "pico/multicore.h" +#include "pico/util/queue.h" #include "hardware/pwm.h" #include "hardware/adc.h" #include "hardware/flash.h" #include "hardware/irq.h" -#include "stdint.h" +#define ADC_OFFSET 0.025f #define MOTOR_PWM_FORWARD 22 #define MOTOR_PWM_REVERSE 23 +#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE) //This Offset should be far enough away from program data extern repeating_timer_t pid_control_timer; extern repeating_timer_t speed_helper_timer; extern bool target_direction; extern uint target_speed_step; extern const uint8_t *CV_ARRAY_FLASH; -void adjust_pwm_level(uint16_t level); -void core1_entry(); \ No newline at end of file +void core1_entry(); +float measure(bool direction); \ No newline at end of file