- Changes to sorting in measure() function

- Changes to Zener Voltage Correction /  measure now returns value in volts
- Changes to Motor control
- Resistor values changed
- Created BOM
This commit is contained in:
1nct committed 2022-02-16 10:45:30 +01:00
1 parent 57af1b67a4
commit 4d073dd7f7
8 files changed
+1376 -105

No files matched your search

+99 -78
View File
@@ -5,9 +5,9 @@
//////////////////////////
#include "core1.h"
int msr_arr[10000] = {0};
uint current_target = 0;
uint16_t current_target = 0;
float current_target_voltage = 0;
struct repeating_timer pid_control_timer,speed_helper_timer;
typedef struct pid_params{
@@ -15,12 +15,12 @@ typedef struct pid_params{
float t_s;
float a_1;
float a_2;
int sum_limit_max;
int sum_limit_min;
float sum_limit_max;
float sum_limit_min;
float output;
int e;
int e_prev;
int e_sum;
float e;
float e_prev;
float e_sum;
}pid_params;
pid_params pid;
@@ -31,50 +31,28 @@ typedef struct measure_params{
uint8_t right_side_array_cutoff;
}measure_params;
measure_params msr;
uint8_t a_factor = 30; /*CV_ARRAY_FLASH[59];..................*/
void quicksort(uint16_t number[], int first, int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
uint calc_end_target(){
uint16_t calc_end_target(){
//Forward
if(target_direction){
if (target_speed_step == 128) return 0;
else return (target_speed_step - 129) * CV_ARRAY_FLASH[59];
else return (target_speed_step-129) * a_factor ; /* (float) CV_ARRAY_FLASH[59]*/
}
//Reverse
else {
if (target_speed_step == 0) return 0;
else return (target_speed_step-1) * CV_ARRAY_FLASH[59];
else return (target_speed_step-1) * a_factor; /*(float)CV_ARRAY_FLASH[59]*/
}
}
bool speed_helper() {
uint8_t accel_rate = CV_ARRAY_FLASH[2];
uint8_t decel_rate = CV_ARRAY_FLASH[3];
uint end_target = calc_end_target();
uint8_t accel_rate = 1; /*CV_ARRAY_FLASH[2];*/
uint8_t decel_rate = 1; /*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)
static uint8_t speed_helper_counter;
//Emergency Stop
if (target_speed_step == 129 || target_speed_step==1) {
@@ -83,15 +61,15 @@ bool speed_helper() {
}
//Acceleration
else if (end_target > current_target && speed_helper_counter == accel_rate) {
current_target += CV_ARRAY_FLASH[59];
current_target += a_factor;
speed_helper_counter = 0;
}
//Deceleration
else if (end_target < current_target && speed_helper_counter == decel_rate) {
current_target -= CV_ARRAY_FLASH[59];
current_target -= a_factor;
speed_helper_counter = 0;
}
//Accel/Decel "scheduled"
//Acceleration/Deceleration "scheduled"
else if (end_target != current_target){
speed_helper_counter++;
}
@@ -99,51 +77,88 @@ bool speed_helper() {
else{
speed_helper_counter = 0;
}
current_target_voltage = (float)current_target/1260;
return true;
}
int measure(){
float measure(){
pwm_set_gpio_level(MOTOR_PWM_FORWARD,0);
pwm_set_gpio_level(MOTOR_PWM_REVERSE,0);
adc_select_input(!target_direction);
busy_wait_us(msr.delay_in_us);
//gpio_put(9,true);
busy_wait_us(100/*msr.delay_in_us*/);
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();
uint16_t adc_val[100/*msr.total_iterations*/];
adc_run(true);
int key, j;
for (int i = 0; i < 100/*msr.total_iterations*/; ++i) {
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) {
adc_val[j + 1] = adc_val[j];
j = j - 1;
}
adc_val[j + 1] = key;
}
//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];
adc_run(false);
for (uint8_t i = msr.left_side_array_cutoff; i < 100/*msr.total_iterations*/ - msr.right_side_array_cutoff ; ++i) {
sum += adc_val[i];
}
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;
//V_z is represented in 12bit
float V_z = (float)sum / (float)(100/*msr.total_iterations*/ - 30);
//Zener correction (approximated) & Conversion factor for V_z <= 1951
//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);
}
int result = (int)result_f;
return result;
else{
return (L1*V_z);
}
}
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{
//}
}
bool pid_control(struct repeating_timer *t){
static uint counter;
int msr_val = measure();
pid.e = (int)current_target - msr_val;
msr_arr[counter] = msr_val;
counter++;
float measured_voltage = measure();
set_pid_params(measured_voltage);
pid.e = current_target_voltage - measured_voltage;
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;
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;
else if (pid.output > 5000) pid.output = 5000;
else if (pid.output < 1000) pid.output = 1000;
adjust_pwm_level((uint16_t )pid.output);
return true;
(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;
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();
return true;
}
void init_pid(){
@@ -151,12 +166,12 @@ 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 = 1.0f;
pid.t_s = 10.0f / 1000;
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);
//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];*/
@@ -182,26 +197,32 @@ void print_cv_array(const uint8_t *buf, size_t len) {
}
}
void core1_entry() {
printf("core1 init...\n");
if(gpio_get_function(MOTOR_PWM_FORWARD)!=4){
gpio_set_function(MOTOR_PWM_FORWARD, GPIO_FUNC_PWM);
gpio_set_function(MOTOR_PWM_REVERSE, GPIO_FUNC_PWM);
adc_init();
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_pid();
}
alarm_pool_add_repeating_timer_ms(alarm_pool_create(0,1),
10/*CV_ARRAY_FLASH[47]*/,
5/*CV_ARRAY_FLASH[47]*/,
pid_control,
NULL,
&pid_control_timer);
irq_set_priority(0,0x00); // 0x00 is equivalent to the highest priority!
irq_set_priority(0,0x00); // 0x00 = highest priority
alarm_pool_add_repeating_timer_ms(alarm_pool_create(1,1),
7,
speed_helper,
NULL,
&speed_helper_timer);
while (1);
printf("core1 done\n");
while (1){
printf("1\n");
printf("-1\n");
}
}