diff --git a/CV.h b/CV.h index adf6d24..59f05f1 100644 --- a/CV.h +++ b/CV.h @@ -1,23 +1,19 @@ -#include "inttypes.h" +#pragma once #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, ... //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 CV_1 = 0 is not allowed and used for initiating ADC offset adjustment - 0b00000000, //CV_2 - Minimum speed + 0b00001000, //CV_2 - Minimum speed ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 0 == Fastest dec/acc rate; 255 == slowest; - 0b00000000, //CV_3 - Acceleration rate - CV_3*CV_59 = Time for one discrete speed step change in ms - 0b00000000, //CV_4 - Deceleration rate - CV_4*CV_59 = Time for one discrete speed step change in ms + 0b00010000, //CV_3 - Acceleration rate - CV_3*CV_59 = Time for one discrete speed step change in ms + 0b00010000, //CV_4 - Deceleration rate - CV_4*CV_59 = Time for one discrete speed step change in ms ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - 0b11111111, //CV_5 - V_max - 0b01111111, //CV_6 - V_mid + 0b01111111, //CV_5 - V_max + 0b00111111, //CV_6 - V_mid 0b00000001, //CV_7 - Version no. (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 @@ -63,29 +59,25 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = { 0b00000000, //CV_44 - 0b00000000, //CV_45 - 0b00000000, //CV_46 - - 0b00000001, //CV_47 - Additional motor-PWM clock divider. - // PID - Configuration ////////////////////////////////////////////////////////////////////////////////////////////// - 0b00000101, //CV_48 - PID Control Sampling Time t_s in ms ≙ Duration of 1 PID-Cycle // - 0b01110011, //CV_49 - PID Control P_Factor ≙ CV_49/256 i.e. Default = 115 -> 0.4492 // - 0b01100110, //CV_50 - PID Control I_Factor ≙ CV_50/64 i.e. Default = 102 -> 1.594 (1/sec)// - 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 // + // PID - Configuration ////////////////////////////////////////////////////////////////////////////////////////////// + 0b00000010, //CV_47 - PID Control sampling time t_s in ms for speed step < 2 // + 0b00000101, //CV_48 - PID Control sampling time t_s in ms for speed step > 2 // + 0b01110011, //CV_49 - PID Control P_Factor ≙ CV_49/256 Default = 115 -> 0.4492 // + 0b01100110, //CV_50 - PID Control I_Factor ≙ CV_50/64 Default = 102 -> 1.594 (1/sec)// + 0b01111011, //CV_51 - PID Control D_Factor ≙ CV_51/4096 Default = 123 -> 0.03002 sec // + 0b00110000, //CV_52 - PID Integral Limit positive ≙ CV_52*10 Default = 48 -> +480 // + 0b00110000, //CV_53 - PID Integral Limit negative ≙ CV_53*(-10) 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, although you should // - //keep in mind, that the programming track might limit current. // - 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 // - 0b00001000, //CV_58 - Offset measurement cycles Default = 8 // + 0b00000001, //CV_54 - --------------------------------------------------------------- // + 0b00000000, //CV_55 - --------------------------------------------------------------- // + 0b00000000, //CV_56 - --------------------------------------------------------------- // + 0b00000000, //CV_57 - --------------------------------------------------------------- // + 0b00000101, //CV_58 - --------------------------------------------------------------- // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 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 // + 0b00010100, //CV_60 - ..................................................................... // // V_EMF Measurement Configuration ////////////////////////////////////////////////////////////////////////////////// - 0b01000110, //CV_61 - V_EMF Total amount of Measurement Iterations - Default = 70 iterations // + 0b01000110, //CV_61 - V_EMF Total amount of Measurement Iterations - Default = 100 iterations // 0b01100100, //CV_62 - V_EMF Delay before Measuring - Default = 100us // // CV_63 and CV_64 are used to determine how many elements of the sorted measurement array will be discarded // // For Example: CV_63 = x and CV_64 = y // @@ -213,12 +205,12 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = { 0b00000000, //CV_171 - Channel B Level Bits 8-15 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Offset adjustment for ADC // -//If CV_172&CV_173 = 0xFF = 255 there will be a measurement procedure on startup (Note: Bitwise &) // +//If CV_172 = 0xFF = 255 there will be a measurement procedure on startup // //alternatively, the measurement can be done by writing a 0 to CV_1 on the programming track // 0b11111111, //CV_172 - ADC offset Default: 255 // - 0b11111111, //CV_173 - ADC offset measurement cycles Default: 127 // + 0b01111111, //CV_173 - ADC offset measurement cycles Default: 127 // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - 0b01111111, //CV_174 - + 0b00000001, //CV_174 - Additional motor-PWM clock divider. 0b00000000, //CV_175 - 0b00000000, //CV_176 - 0b00000000, //CV_177 - diff --git a/core0.c b/core0.c index fd6ead4..9789d81 100644 --- a/core0.c +++ b/core0.c @@ -42,7 +42,6 @@ uint16_t two_sigma(const uint16_t arr[], uint8_t length){ return sum / counter; } void adc_offset_adjustment(uint8_t length){ - length = 100; uint16_t offsets[length]; //Set PWM level to 0 just for good measure pwm_set_gpio_level(MOTOR_FWD_PIN, 0); @@ -59,40 +58,6 @@ void adc_offset_adjustment(uint8_t length){ flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2); } -uint16_t m_pwm_find_offset(uint16_t level, uint8_t step, uint8_t delay, uint32_t threshold, bool direction){ - uint8_t gpio = MOTOR_FWD_PIN * direction + MOTOR_REV_PIN * !direction; - pwm_set_gpio_level(MOTOR_FWD_PIN,0); - pwm_set_gpio_level(MOTOR_REV_PIN,0); - while( measure(direction) < threshold ){ - pwm_set_gpio_level(gpio,level); - busy_wait_ms(delay); - level += step; - } - pwm_set_gpio_level(MOTOR_FWD_PIN,0); - pwm_set_gpio_level(MOTOR_REV_PIN,0); - return level; -} -void m_pwm_offset_adjustment(uint8_t length){ - uint16_t offsets_fwd[length]; - uint16_t offsets_rev[length]; - uint16_t max_lvl = _125M/(CV_ARRAY_FLASH[8]*100+10000); - for (uint8_t i = 0; i < length; ++i) { - offsets_fwd[i] = m_pwm_find_offset(0, max_lvl / 3000, 1, 30, true); - busy_wait_ms(500); - offsets_rev[i] = m_pwm_find_offset(0, max_lvl / 3000, 1, 30, false); - busy_wait_ms(500); - } - uint16_t offsets_fwd_avg = two_sigma(offsets_fwd, length); - uint16_t 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); -} void acknowledge() { uint16_t max_lvl = _125M/(CV_ARRAY_FLASH[8]*100+10000); pwm_set_gpio_level(MOTOR_FWD_PIN, max_lvl); @@ -119,14 +84,11 @@ void write_cv_byte(uint16_t cv_index, uint8_t cv_data){ flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2); } - //Motor PWM offset Adjustment (CV_7; Value = 7); + //ADC offset Adjustment (CV_7; Value = 7); else if(cv_index == 6 && cv_data == 7){ - m_pwm_offset_adjustment(CV_ARRAY_FLASH[57]); - } - //CV_1 = 0 is not permitted, therefore this is used to start the ADC offset adjustment routine - else if( !( cv_index&cv_data) ){ - adc_offset_adjustment(100/*CV_ARRAY_FLASH[173]*/); + adc_offset_adjustment(CV_ARRAY_FLASH[173]); } + else if( !cv_index && !cv_data ); else{ uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE]; memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP)); @@ -181,20 +143,22 @@ uint16_t get_16bit_CV (uint16_t CV_start_index){ uint8_t byte_1 = CV_ARRAY_FLASH[CV_start_index+1]; return (byte_0) + (byte_1<<8); } -void set_outputs(uint32_t active_functions) { +void set_outputs(uint32_t functions_to_set_bitmask) { uint32_t outputs_to_set = 0; - uint32_t outputs_with_PWM_enabled = get_32bit_CV(111); //get outputs with pwm enabled (bitmask) + uint32_t outputs_with_PWM_enabled = get_32bit_CV(111); //get outputs with pwm enabled (bitmask) uint32_t outputs_to_set_PWM = outputs_with_PWM_enabled; + uint32_t temp_mask = 1; for (uint8_t i = 0; i < 32; i++) { - if ( (active_functions>>i) & 1 ){ + if (temp_mask & functions_to_set_bitmask ){ outputs_to_set |= get_32bit_CV(i * 8 + 260 - 4 * target_direction); } + temp_mask<<=1; } outputs_to_set &= GPIO_ALLOWED_OUTPUTS; // Prevent illegal GPIO outputs_to_set_PWM &= outputs_to_set; // Outputs with PWM to be enabled outputs_to_set &= ~outputs_to_set_PWM; // Outputs without PWM to be enabled gpio_put_masked(GPIO_ALLOWED_OUTPUTS, outputs_to_set); - uint32_t temp_mask = 1; + temp_mask = 1; for (uint8_t i = 0; i < 32; ++i) { if(temp_mask & outputs_to_set_PWM){ pwm_set_gpio_level(i, level_table[i]); @@ -207,15 +171,20 @@ void set_outputs(uint32_t active_functions) { temp_mask<<=1; } } -void update_active_functions(uint32_t function_byte, uint8_t clr_bit_ind) { +void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind, bool direction_change) { static uint32_t active_functions; static uint32_t prev_active_func; - function_byte &= ~(clr_bit_arr[clr_bit_ind]); - active_functions &= clr_bit_arr[clr_bit_ind]; - active_functions |= function_byte; - uint32_t changes = active_functions ^ prev_active_func; - if(changes)set_outputs(active_functions); - prev_active_func = active_functions; + if (direction_change){ + set_outputs(prev_active_func); + } + else{ + 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 |= new_function_bitmask; //write bits corresponding to group of functions + uint32_t changes = active_functions ^ prev_active_func; //xor effectively checks for changes from previous state + if(changes)set_outputs(active_functions); + prev_active_func = active_functions; + } } bool error_detection(int8_t number_of_bytes, const uint8_t byte_array[]) { //Bitwise XOR for all Bytes -> Successful result is: "0000 0000" @@ -270,24 +239,26 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) //0011-1111 (128 Speed Step Control) - 2 Byte length if (command_byte_n == 0b00111111){ target_speed_step = byte_array[command_byte_start_index - 1]; - if(target_speed_step>127) { - target_direction = true; //Forward + if(target_speed_step>127 && !target_direction) { + target_direction = true; //Change direction variable to forward (true) + update_active_functions(0,0,1); //Some functions depend on direction } - else { - target_direction = false; //Reverse + else if (target_speed_step<128 && target_direction){ + target_direction = false; //Change direction variable to reverse (false) + update_active_functions(0,0,1); //Some functions depend on direction } } // 10XX-XXXX (Function Group Instruction) else if (command_byte_n >> 6 == 0b00000010){ switch (command_byte_n >> 4) { case 0b00001011: // F5-F8 - update_active_functions((command_byte_n << 5),1); + update_active_functions((command_byte_n << 5),1,false); break; case 0b00001010: // F9-F12 - update_active_functions((command_byte_n << 9),2); + update_active_functions((command_byte_n << 9),2,false); break; default: // F0-F4 - update_active_functions(((command_byte_n << 1) + (command_byte_n >> 4)),0); + update_active_functions(((command_byte_n << 1) + (command_byte_n >> 4)),0,false); break; } } @@ -295,13 +266,13 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) else if (command_byte_n >> 5 == 0b00000110) { switch (command_byte_n) { case 0b11011110: // F13-F20 - update_active_functions((byte_array[command_byte_start_index - 1] << 13),3); + update_active_functions((byte_array[command_byte_start_index - 1] << 13),3,false); break; case 0b11011111: // F21-F28 - update_active_functions((byte_array[command_byte_start_index - 1] << 21),4); + update_active_functions((byte_array[command_byte_start_index - 1] << 21),4,false); break; case 0b11011000: // F29-F31 - update_active_functions((byte_array[command_byte_start_index - 1] << 29),5); + update_active_functions((byte_array[command_byte_start_index - 1] << 29),5,false); break; default: break; @@ -392,7 +363,7 @@ void init_outputs(){ for (uint8_t i = 0; i < 32; ++i) { if (Outputs_with_PWM_enabled&temp_mask){ slice = pwm_gpio_to_slice_num(i); - channel = pwm_gpio_to_channel(i); //0 for A, 1 for B ? + channel = pwm_gpio_to_channel(i); //0 for Channel A, 1 for Channel B wrap = get_16bit_CV(115 + slice*7); level = get_16bit_CV(118 + 7*slice+ 2*channel); gpio_set_function(i,GPIO_FUNC_PWM); @@ -410,18 +381,12 @@ void cv_setup_check(){ multicore_fifo_pop_blocking(); //wait for core1 launch before proceeding if (CV_ARRAY_FLASH[64]){ uint8_t arr[4] = {125,8,7,124}; - program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (CV_8; Value = 8) + program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8) } //Check for adc offset setup if ( CV_ARRAY_FLASH[171] == 0xFF ){ - uint8_t arr[4] = {125,0,0,124}; - program_mode(4,arr); //ADC offset adjustment (CV_1; Value = 0) - } - //Check for motor duty cycle offset setup - uint sum = (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]) + (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]); - if(!sum) { uint8_t arr[4] = {125,7,6,124}; - program_mode(4,arr); //Motor PWM offset adjustment (CV_7; Value = 7); + program_mode(4,arr); //ADC offset adjustment (write CV_7 = 7) } } int main() { diff --git a/core0.h b/core0.h index 206ec43..b4de3f9 100644 --- a/core0.h +++ b/core0.h @@ -25,8 +25,6 @@ const uint32_t clr_bit_arr[6] = { }; uint16_t two_sigma(const uint16_t arr[], uint8_t length); void adc_offset_adjustment(uint8_t length); -uint16_t m_pwm_find_offset(uint16_t level, uint8_t step, uint8_t delay, uint32_t threshold, bool direction); -void m_pwm_offset_adjustment(uint8_t length); void acknowledge(); 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); @@ -34,8 +32,8 @@ void write_cv_byte(uint16_t cv_index, uint8_t cv_data); void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]); uint32_t get_32bit_CV (uint16_t CV_start_index); uint16_t get_16bit_CV (uint16_t CV_start_index); -void set_outputs(uint32_t active_functions); -void update_active_functions(uint32_t byte, uint8_t clr_bit_ind) ; +void set_outputs(uint32_t functions_to_set_bitmask); +void update_active_functions(uint32_t byte, uint8_t clr_bit_ind,bool direction_change) ; 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[]) ; diff --git a/core1.c b/core1.c index bd3c5a2..7d9fa46 100644 --- a/core1.c +++ b/core1.c @@ -7,6 +7,8 @@ #include "core1.h" uint32_t latest_target = 0; +uint8_t latest_target_index = 0; +uint16_t speed_table[127] = {0}; struct repeating_timer pid_control_timer,speed_helper_timer; typedef struct pid_params{ @@ -20,10 +22,8 @@ typedef struct pid_params{ int32_t e; int32_t e_prev; int32_t e_sum; - uint32_t m_fwd_scaled; - uint32_t m_rev_scaled; - uint32_t b_fwd; - uint32_t b_rev; + int32_t offset; + int32_t offset_base; uint16_t max_output; bool direction_prev; }pid_params; @@ -40,46 +40,47 @@ typedef struct measure_params{ measure_params msr; -uint8_t target_factor = 10; /*CV_ARRAY_FLASH[59];*/ // One Discrete Step - uint16_t calc_end_target(){ //Forward if(target_direction){ if (target_speed_step == 128) return 0; - else return (target_speed_step-129) * target_factor ; /* (float) CV_ARRAY_FLASH[59]*/ + else return (target_speed_step-129); } //Reverse else { if (target_speed_step == 0) return 0; - else return (target_speed_step-1) * target_factor; /*(float)CV_ARRAY_FLASH[59]*/ + else return (target_speed_step-1); } } bool speed_helper() { uint8_t accel_rate = CV_ARRAY_FLASH[2]; uint8_t decel_rate = CV_ARRAY_FLASH[3]; - uint16_t end_target = calc_end_target(); + uint16_t end_target_index = calc_end_target(); + static uint8_t speed_helper_counter; //latest_target only gets adjusted when the counter equals the acc/dec rate //-> Time for 1 Speed Step := (speed_helper timer delay)*(accel_rate) - static uint8_t speed_helper_counter; //Emergency Stop if (target_speed_step == 129 || target_speed_step==1) { - latest_target = 0; + latest_target_index = 0; speed_helper_counter = 0; + latest_target = speed_table[latest_target_index]; } //Acceleration - else if (end_target > latest_target && speed_helper_counter == accel_rate) { - if(!accel_rate) latest_target = end_target; // directly accelerate to end target speed - else latest_target += target_factor; + else if (end_target_index > latest_target_index && speed_helper_counter == accel_rate) { + if(!accel_rate) latest_target_index = end_target_index; // directly accelerate to end target speed + else latest_target_index++; + latest_target = speed_table[latest_target_index]; speed_helper_counter = 0; } //Deceleration - else if (end_target < latest_target && speed_helper_counter == decel_rate) { - if(!decel_rate) latest_target = end_target; // directly decelerate to end target speed - else latest_target -= target_factor; + else if (end_target_index < latest_target_index && speed_helper_counter == decel_rate) { + if(!decel_rate) latest_target_index = end_target_index; // directly decelerate to end target speed + else latest_target_index--; + latest_target = speed_table[latest_target_index]; speed_helper_counter = 0; } //Acceleration/Deceleration "scheduled" - else if (end_target != latest_target) speed_helper_counter++; + else if (end_target_index != latest_target_index) speed_helper_counter++; //Target reached -> Reset Counter else speed_helper_counter = 0; return true; @@ -87,14 +88,14 @@ bool speed_helper() { uint32_t measure(bool direction){ uint32_t sum = 0; uint16_t adc_val[100 /*msr.total_iterations*/]; - int16_t j = 0; + 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); busy_wait_us(msr.delay_in_us); adc_run(true); - for (int16_t i = 0; i < msr.total_iterations; ++i) { + for (int32_t 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(); key = adc_val[i]; @@ -108,7 +109,7 @@ uint32_t measure(bool direction){ adc_run(false); //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) { + for (int32_t i = msr.left_side_array_cutoff; i < msr.total_iterations - msr.right_side_array_cutoff ; ++i) { sum += adc_val[i]; } uint32_t res = sum / ( msr.total_iterations - (msr.left_side_array_cutoff+msr.right_side_array_cutoff) ); @@ -125,60 +126,76 @@ void adjust_pwm_level(uint16_t level) { pwm_set_gpio_level(MOTOR_REV_PIN, level); } } -void init_offsets(uint16_t lvl_max){ - uint16_t lvl_min_fwd = (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]<<8); - uint16_t lvl_min_rev = (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]<<8); - pid.m_fwd_scaled = ((lvl_max-lvl_min_fwd)*1000) / (125*target_factor); //scaled with 1000 due to integer division - pid.m_rev_scaled = ((lvl_max-lvl_min_rev)*1000) / (125*target_factor); //scaled with 1000 due to integer division - pid.b_fwd = lvl_min_fwd - ((pid.m_fwd_scaled*target_factor) / 1000) - 200; - pid.b_rev = lvl_min_rev - ((pid.m_rev_scaled*target_factor) / 1000) - 200; -} -uint32_t offset(){ - // f(x) = mx+b - with x = latest_target - if (target_direction) return (pid.m_fwd_scaled*latest_target)/1000 + pid.b_fwd; - else return (pid.m_rev_scaled*latest_target)/1000 + pid.b_rev; -} bool pid_control(struct repeating_timer *t){ - if(target_direction != pid.direction_prev || !latest_target) pid.e_sum = 0; + if(target_direction != pid.direction_prev || !latest_target) { + pid.e_sum = 0; + pid.offset = pid.offset_base; + } msr.val = measure(target_direction); pid.e = (int32_t)latest_target - (int32_t)msr.val + msr.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*pid.e + pid.k_i*pid.t_s*pid.e_sum + (pid.k_d/pid.t_s)*(pid.e-pid.e_prev) ) / 1000; - pid.output += (int32_t)offset(); + pid.output = ( pid.k_p*pid.e + pid.k_i*pid.t_s*pid.e_sum + ( pid.k_d*(pid.e-pid.e_prev) / pid.t_s ) ) / 1000; + pid.output += pid.offset; + if (latest_target_index < 5){ + pid.output += 20*pid.e; //"compensate" friction + pid.offset_base = pid.offset; //"baseline" pwm offset level + } if (pid.output < 0 || !latest_target) pid.output = 0; else if (pid.output > pid.max_output) pid.output = pid.max_output; adjust_pwm_level(pid.output); - printf("o: %d\n",pid.output); - printf("e: %d\n",pid.e); adc_fifo_drain(); pid.direction_prev = target_direction; + pid.e_sum += pid.e; pid.e_prev = pid.e; + // Adjust offset when the absolute value of e_sum is half of max + if (pid.e_sum > pid.sum_limit_max/2 || pid.e_sum < pid.sum_limit_min/2){ + pid.offset += 3*(pid.e_sum/pid.sum_limit_max); + } + if (pid.offset > 5000) pid.offset = 5000; + else if (pid.offset < 1) pid.offset = 1; + // Increase sampling time for higher speed steps - Decrease for lower speed steps + pid_control_timer.delay_us = (8000*latest_target_index)/126 + 2000; + pid.t_s = ((8000*latest_target_index)/126 + 2000)/1000; + printf("%d\n",pid.output); return true; + //printf("off:%d\n",pid.offset); +} +void init_speed_table(){ + double v_min = (double)CV_ARRAY_FLASH[1]; + double v_mid = (double)CV_ARRAY_FLASH[5]*16; + double v_max = (double)CV_ARRAY_FLASH[4]*16; + double m_1 = (v_mid-v_min)/62; + double m_2 = (v_max-v_mid)/63; + for (uint8_t i = 1; i < 63; ++i) { + speed_table[i] = (uint16_t)lround(m_1*(i-1)+v_min); + } + for (uint8_t i = 63; i < 127; ++i) { + speed_table[i] = (uint16_t)lround(m_2*(i-63)+v_mid); + } } void init_pid(){ + pid.k_p = 2000; + pid.k_i = 50; + pid.k_d = 1000; + pid.t_s = CV_ARRAY_FLASH[46]; // Default: (CV_ARRAY_FLASH[46]) = 2 + pid.offset = 2500; + pid.sum_limit_max = 1000; /*(float)(CV_ARRAY_FLASH[51] );*/ + pid.sum_limit_min = -1000; /*(float)(CV_ARRAY_FLASH[52] * (-1));*/ + pid.direction_prev = target_direction; + pid.max_output = _125M/(CV_ARRAY_FLASH[8]*100+10000); +} +void init_msr(){ 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];*/ - msr.adc_offset = CV_ARRAY_FLASH[171];; - pid.e_sum = 0; - pid.e_prev = 0; - pid.k_p = 2500; - pid.k_i = 100; - pid.k_d = 100; - pid.t_s = 5 /*(float) CV_ARRAY_FLASH[47]*/; - pid.sum_limit_max = 1000;/*(float)(CV_ARRAY_FLASH[51] );*/ - pid.sum_limit_min = -1000;/*(float)(CV_ARRAY_FLASH[52] * (-1));*/ - pid.direction_prev = target_direction; - pid.max_output = _125M/(CV_ARRAY_FLASH[8]*100+10000); + msr.adc_offset = CV_ARRAY_FLASH[171]; } void init_motor_pwm(uint8_t gpio) { - uint16_t wrap_counter = _125M/(CV_ARRAY_FLASH[8]*100+10000); // 125MHz / f_pwm - init_offsets(wrap_counter); + uint16_t wrap_counter = _125M/(CV_ARRAY_FLASH[8]*100+10000); // 125MHz / f_pwm uint32_t slice_num = pwm_gpio_to_slice_num(gpio); - pwm_set_clkdiv_int_frac(slice_num,CV_ARRAY_FLASH[46],0); + pwm_set_clkdiv_int_frac(slice_num,CV_ARRAY_FLASH[173],0); pwm_set_wrap(slice_num, wrap_counter); pwm_set_gpio_level(gpio,0); pwm_set_enabled(slice_num, true); @@ -195,6 +212,8 @@ void core1_entry() { init_motor_pwm(MOTOR_FWD_PIN); init_motor_pwm(MOTOR_REV_PIN); } + init_speed_table(); + init_msr(); init_pid(); alarm_pool_add_repeating_timer_ms(alarm_pool_create(0,1), CV_ARRAY_FLASH[47], @@ -209,4 +228,4 @@ void core1_entry() { &speed_helper_timer); multicore_fifo_push_blocking(20); //send some data to let core0 know that core1 is done while (1); -} +} \ No newline at end of file diff --git a/core1.h b/core1.h index 8a1ed8a..e1c1fc9 100644 --- a/core1.h +++ b/core1.h @@ -6,11 +6,12 @@ #pragma once #include "shared.h" +#include uint16_t calc_end_target(); bool speed_helper(); void adjust_pwm_level(uint16_t level); -void init_offsets(uint16_t lvl_max); -uint32_t offset(); bool pid_control(struct repeating_timer *t); +void init_speed_table(); void init_pid(); -void init_motor_pwm(uint8_t gpio); +void init_msr(); +void init_motor_pwm(uint8_t gpio); \ No newline at end of file diff --git a/shared.h b/shared.h index f8753d8..31ec167 100644 --- a/shared.h +++ b/shared.h @@ -9,14 +9,12 @@ #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" #define _125M 125000000 -#define ADC_OFFSET 13 -#define DCC_INPUT_PIN 4u +#define DCC_INPUT_PIN 9u #define MOTOR_FWD_PIN 22u #define MOTOR_REV_PIN 23u #define FWD_V_EMF_ADC_PIN 26u