From 594f693b9f0e6ed2df3c787a1665d1483779941b Mon Sep 17 00:00:00 2001 From: 1nct Date: Thu, 27 Jan 2022 01:09:06 +0100 Subject: [PATCH] - Correction for Zener Diode Voltage - Changes to Header Files --- CMakeLists.txt | 5 ++++- core0.c | 4 ---- core0.h | 12 ++---------- core1.c | 30 ++++++++++++++++++++---------- core1.h | 19 ++++--------------- shared.h | 14 ++++++++++---- 6 files changed, 40 insertions(+), 44 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 820b682..bc3a9d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,10 @@ add_compile_options(-Wall add_executable( RP2040-Decoder core0.c - core1.c core1.h shared.h) + core0.h + core1.c + core1.h + shared.h) # Add pico_multicore which is required for multicore functionality diff --git a/core0.c b/core0.c index 847c428..68e9d64 100644 --- a/core0.c +++ b/core0.c @@ -4,10 +4,7 @@ // core0.c // ////////////////////////// -#include "CV.h" #include "core0.h" -#include "shared.h" - bool target_direction = true; uint target_speed_step = 128; const uint8_t *CV_ARRAY_FLASH = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET); @@ -71,7 +68,6 @@ void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos) { if (res) { acknowledge(); } - } void verify_cv_byte(uint16_t cv_address, uint8_t cv_data){ if (CV_ARRAY_FLASH[cv_address] == cv_data) acknowledge(); diff --git a/core0.h b/core0.h index 99c3e40..693a6a5 100644 --- a/core0.h +++ b/core0.h @@ -4,17 +4,9 @@ // core0.h // ////////////////////////// - #pragma once -#include -#include "string.h" -#include "pico/stdlib.h" -#include "pico/multicore.h" -#include "hardware/pwm.h" -#include "hardware/adc.h" -#include "hardware/flash.h" -#include "hardware/irq.h" -#include "stdint.h" +#include "shared.h" +#include "CV.h" #define SIZE_BYTE_ARRAY 5 #define SIZE_ACTIVE_FUNCTIONS 32 #define DCC_INPUT_PIN 11 //Custom: 11 Default: 14 diff --git a/core1.c b/core1.c index 516a0c1..f748680 100644 --- a/core1.c +++ b/core1.c @@ -4,9 +4,9 @@ // core1.c // ////////////////////////// -#include "inttypes.h" #include "core1.h" -#include "shared.h" +int msr_arr[10000] = {0}; + uint current_target = 0; struct repeating_timer pid_control_timer,speed_helper_timer; @@ -23,6 +23,7 @@ typedef struct pid_params{ int e_sum; }pid_params; pid_params pid; + typedef struct measure_params{ uint8_t delay_in_us; uint8_t total_iterations; @@ -106,22 +107,31 @@ int measure(){ pwm_set_gpio_level(MOTOR_PWM_REVERSE,0); adc_select_input(!target_direction); busy_wait_us(msr.delay_in_us); - gpio_put(9,true); - int sum = 0; + //gpio_put(9,true); + uint32_t sum = 0; uint16_t adc_values[msr.total_iterations]; for (int i = 0; i < msr.total_iterations; ++i) { adc_values[i] = adc_read(); } - gpio_put(9,false); + //gpio_put(9,false); quicksort(adc_values, 0, msr.total_iterations - 1); for (uint8_t i = msr.left_side_array_cutoff; i < msr.total_iterations - msr.right_side_array_cutoff ; ++i) { sum = sum+adc_values[i]; } - return sum/(msr.total_iterations - 20); + float result_f = (float)sum/(float)(msr.total_iterations - 20); + if(result_f>MEASURE_CORRECTION_THRESHOLD){ + result_f = 11*result_f*result_f/(1241*1241) - 43*result_f/1241 + 45; + } + int result = (int)result_f; + return result; } bool pid_control(struct repeating_timer *t){ - pid.e = (int)current_target - measure(); + static uint counter; + int msr_val = measure(); + pid.e = (int)current_target - msr_val; + msr_arr[counter] = msr_val; + counter++; pid.e_sum = 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; @@ -141,10 +151,10 @@ void init_pid(){ // 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 = 0.45f; + pid.k_p = 1.0f; pid.t_s = 10.0f / 1000; - pid.a_1 = 1.6f * pid.t_s ; // k_i*t_s - pid.a_2 = 0.03f / pid.t_s; // k_d/t_s + pid.a_1 = 0.0f * pid.t_s ; // k_i*t_s + pid.a_2 = 0.0f / pid.t_s; // k_d/t_s pid.sum_limit_max = CV_ARRAY_FLASH[51] * 10; pid.sum_limit_min = CV_ARRAY_FLASH[52] * (-10); msr.total_iterations = 70;/*CV_ARRAY_FLASH[60];*/ diff --git a/core1.h b/core1.h index 5d39383..bef2f44 100644 --- a/core1.h +++ b/core1.h @@ -4,26 +4,15 @@ // core1.h // ////////////////////////// - -#ifndef RP2040_DECODER_CORE1_H -#define RP2040_DECODER_CORE1_H - -#endif //RP2040_DECODER_CORE1_H +#pragma once +#include "shared.h" #define V_EMF_ADC_PIN_FORWARD 26 #define V_EMF_ADC_PIN_REVERSE 27 -#include -#include "string.h" -#include "pico/stdlib.h" -#include "pico/multicore.h" -#include "hardware/pwm.h" -#include "hardware/adc.h" -#include "hardware/flash.h" -#include "hardware/irq.h" -#include "stdint.h" +#define MEASURE_CORRECTION_THRESHOLD 2271 void quicksort(uint16_t number[], int first, int last); uint calc_end_target(); bool speed_helper(); int measure(); void init_pid(); void init_pwm(uint gpio); -void print_cv_array(const uint8_t *buf, size_t len); +void print_cv_array(const uint8_t *buf, size_t len); \ No newline at end of file diff --git a/shared.h b/shared.h index c0a00f2..fdd2016 100644 --- a/shared.h +++ b/shared.h @@ -4,10 +4,16 @@ // shared.h // ////////////////////////// - -#ifndef RP2040_DECODER_SHARED_VARS_H -#define RP2040_DECODER_SHARED_VARS_H -#endif //RP2040_DECODER_SHARED_VARS_H +#pragma once +#include +#include "string.h" +#include "pico/stdlib.h" +#include "pico/multicore.h" +#include "hardware/pwm.h" +#include "hardware/adc.h" +#include "hardware/flash.h" +#include "hardware/irq.h" +#include "stdint.h" #define MOTOR_PWM_FORWARD 10 //Custom: 10 Default: 12 #define MOTOR_PWM_REVERSE 13 extern repeating_timer_t pid_control_timer;