diff --git a/CV.h b/CV.h index f7859d2..fe12167 100644 --- a/CV.h +++ b/CV.h @@ -66,8 +66,8 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = { 0b00110010, //CV_51 - PID Control D_Factor ≙ CV_51/10000 Default = 50 -> 0.005 // 0b01100100, //CV_52 - PID Integral Limit positive ≙ CV_52*10 Default = 100 -> +1000 // 0b01100100, //CV_53 - PID Integral Limit negative ≙ CV_53*(-10) Default = 100 -> -1000 // - 0b00000011, //CV_54 - k_p @ x0 - High Byte (Bits 8-15) divided by 100 // - 0b11101000, //CV_55 - k_p @ x0 - Low Byte (Bits 0-7) Default = 1000 -> 10 // + 0b00000111, //CV_54 - k_p @ x0 - High Byte (Bits 8-15) divided by 100 // + 0b11010000, //CV_55 - k_p @ x0 - Low Byte (Bits 0-7) Default = 2000 -> 20 // 0b00000000, //CV_56 - k_p @ x1 - High Byte (Bits 8-15) divided by 100 // 0b11111010, //CV_57 - k_p @ x1 - Low Byte (Bits 0-7) Default = 250 -> 2.5 // 0b00000000, //CV_58 - k_p @ x2 - High Byte (Bits 8-15) divided by 100 // diff --git a/README.md b/README.md index e81420c..cb927f7 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ Features Getting Started ------------ -A step-by-step explanation on how to make use of this is work in progress. +The [Wiki - Getting started](https://github.com/gab-k/RP2040-Decoder/wiki/Getting-started) page provides a brief overview. +A Description on how to compile and flash the decoder using Windows can be found [here](https://github.com/gab-k/RP2040-Decoder/wiki/Quick-start-guide-(Windows)). License diff --git a/core0.c b/core0.c index 7dcf1f8..d0ce556 100644 --- a/core0.c +++ b/core0.c @@ -519,18 +519,20 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ else gpio = MOTOR_REV_PIN; float level_arr[iterations]; for (int i = 0; i < iterations; ++i) { - uint16_t max_level = (_125M/(CV_ARRAY_FLASH[8]*100+10000)); + uint16_t max_level = _125M/(CV_ARRAY_FLASH[8]*100+10000); uint16_t level = max_level/20; float measurement; do { pwm_set_gpio_level(gpio, level); - busy_wait_ms(15); + busy_wait_ms(30); level += max_level/500; measurement = measure(CV_ARRAY_FLASH[60], CV_ARRAY_FLASH[61], CV_ARRAY_FLASH[62], CV_ARRAY_FLASH[63], direction); + // Abort measurement and write default value of 0 to flash + if (level > max_level) return 0; } while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f); level_arr[i] = (float)level; @@ -546,8 +548,8 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ // When ADC Setup is not configured run adc offset adjustment function and write adc offset into flash void cv_setup_check(){ - // Check for flash factory setting and set CV_FLASH_ARRAY to default values when factory condition is found. - if ( CV_ARRAY_FLASH[64] ){ + // Check for flash factory setting and set CV_FLASH_ARRAY to default values when factory condition ("0xFF") is found. + if ( CV_ARRAY_FLASH[64] == 0xFF ){ uint8_t arr[4] = {125,8,7,124}; program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8) } @@ -597,7 +599,7 @@ bool get_direction(bool *direction_ptr){ // Motor PWM initialization void init_motor_pwm(uint8_t gpio) { - uint16_t wrap_counter = _125M/(CV_ARRAY_FLASH[8]*100+10000); // 125MHz / f_pwm + uint16_t wrap_counter = (_125M/(CV_ARRAY_FLASH[8]*100+10000)) - 1; uint32_t slice_num = pwm_gpio_to_slice_num(gpio); pwm_set_clkdiv_int_frac(slice_num,CV_ARRAY_FLASH[173],0); pwm_set_wrap(slice_num, wrap_counter); diff --git a/core1.c b/core1.c index 598aeb3..c2072ab 100644 --- a/core1.c +++ b/core1.c @@ -36,6 +36,7 @@ bool speed_helper(struct repeating_timer *t) { pid_params *pid = (pid_params *)(t->user_data); uint8_t accel_rate = CV_ARRAY_FLASH[2]; uint8_t decel_rate = CV_ARRAY_FLASH[3]; + static bool prev_dir; static uint8_t speed_helper_counter; static uint8_t speed_table_index; @@ -62,12 +63,22 @@ bool speed_helper(struct repeating_timer *t) { speed_helper_counter = 0; } + // Change of direction while decelerating -> jump to most recent speed_step without delay (other direction) + else if (pid->direction != prev_dir){ + speed_table_index = pid->end_target_index; + pid->setpoint = pid->speed_table[speed_table_index]; + speed_helper_counter = 0; + } + // Acceleration/Deceleration "scheduled" else if (pid->end_target_index != speed_table_index) speed_helper_counter++; // Target reached -> Reset Counter else 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; } @@ -184,16 +195,10 @@ bool pid_control(struct repeating_timer *t){ // Set PWM Level and discard results in adc fifo adjust_pwm_level((uint16_t)roundf(output),pid); adc_fifo_drain(); - if(pid->setpoint > 2) { - printf("p:%f\n", p); - printf("i:%f\n", i); - printf("d:%f\n", d); - printf("o:%f\n", output); - } + // 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); - // Save previous error, integrator, differentiator, direction, setpoint and measured value also sum error to e_sum pid->e_prev = e; pid->i_prev = i; diff --git a/docs/png/Comparison.jpg b/docs/png/Comparison.jpg new file mode 100644 index 0000000..05309e4 Binary files /dev/null and b/docs/png/Comparison.jpg differ diff --git a/docs/svg/Debug_Connections.svg b/docs/svg/Debug_Connections.svg new file mode 100644 index 0000000..94cb1c6 --- /dev/null +++ b/docs/svg/Debug_Connections.svg @@ -0,0 +1,4 @@ + + + +
Raspberry Pi Pico
Raspberry Pi Pico
GND
GND
GPIO2
GPIO2
GPIO3
GPIO3
VBUS
VBUS
Text is not SVG - cannot display
\ No newline at end of file diff --git a/docs/svg/adaptive_kp.svg b/docs/svg/adaptive_kp.svg index e52fe65..c3356ca 100644 --- a/docs/svg/adaptive_kp.svg +++ b/docs/svg/adaptive_kp.svg @@ -1,135 +1,130 @@ - + gl2ps_renderer figure Creator: GL2PS 1.4.2, (C) 1999-2020 C. Geuzaine For: Octave -CreationDate: Mon Aug 1 18:33:56 2022 +CreationDate: Thu Aug 4 01:55:04 2022 - + - - - - - - - - - - - - - 0 + + + + + + + + + + + + + 0 - - 500 + + 500 - - 1000 + + 1000 - - 1500 + + 1500 - - 2000 + + 2000 - - - - - - - - - - - - - - 0 + + + + + + + + + + + + 0 - - 2 + + 5 - - 4 + + 10 - - 6 + + 15 - - 8 + + 20 - - 10 + + + + + + sp - - - - - - sp + + k + p + (sp) - - k - p - (sp) - - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - k - p + + + + + + + + k + p - - - - - - x0 | y0 + + + + + + x0 | y0 - - - - - - x1 | y1 + + + + + + x1 | y1 - - - - - - x2 | y2 + + + + + + x2 | y2