- implemented linear approximation for pwm duty cycle offset (feed forward)

- changed pid params and variables in measure() from float to integers
- implemented Motor PWM Frequency CV
- improved acknowledge() function ( 3ms forward / 3ms reverse )
- moved automatic offset setup to speed step evaluation
- removed zener diode correction
- some minor changes regarding naming of constants
This commit is contained in:
1nct committed 2022-02-23 02:15:12 +01:00
1 parent cdb68b84da
commit 2cad01370c
6 files changed
+146 -151

No files matched your search

+9 -9
View File
@@ -13,15 +13,15 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_2 - Minimum Speed 0b00000000, //CV_2 - Minimum Speed
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 0 == Fastest dec/acc rate; 255 == slowest; // 0 == Fastest dec/acc rate; 255 == slowest;
0b00000000, //CV_3 - Acceleration Rate 0b00000000, //CV_3 - Acceleration Rate - CV_3*CV_59 = Time for one discrete speed step change in ms
0b00000000, //CV_4 - Deceleration Rate 0b00000000, //CV_4 - Deceleration Rate - CV_4*CV_59 = Time for one discrete speed step change in ms
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b11111111, //CV_5 - V_max 0b11111111, //CV_5 - V_max
0b01111111, //CV_6 - V_mid 0b01111111, //CV_6 - V_mid
0b00000001, //CV_7 - Version No. 0b00000001, //CV_7 - Version No.
0b00001101, //CV_8 - Manufacturer 0b00001101, //CV_8 - Manufacturer (13 = Public Domain & Do-It-Yourself Decoders)
0b00000000, //CV_9 - PWM-Period 0b10010110, //CV_9 - PWM Frequency in Hz = CV_9*100+10000 - Default = (150*100+10000)Hz = 25kHz
0b00000001, //CV_10 - PWM Clock Divider 0b00000001, //CV_10 -
0b11111111, //CV_11 - Packet Timeout in seconds 0b11111111, //CV_11 - Packet Timeout in seconds
0b00000100, //CV_12 - Permitted operating modes 0b00000100, //CV_12 - Permitted operating modes
0b00000000, //CV_13 - 0b00000000, //CV_13 -
@@ -65,20 +65,21 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_46 - 0b00000000, //CV_46 -
0b00000000, //CV_47 - 0b00000000, //CV_47 -
// PID - Configuration ////////////////////////////////////////////////////////////////////////////////////////////// // PID - Configuration //////////////////////////////////////////////////////////////////////////////////////////////
0b00001111, //CV_48 - PID Control Sampling Time t_s in ms ≙ Duration of 1 PID-Cycle // 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 // 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)// 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 // 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_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 // 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 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//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// //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. // //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_54 - Forward Direction Offset Bits 0-7 Default = 0 //
0b00000000, //CV_55 - Forward Direction Offset Bits 8-15 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_56 - Reverse Direction Offset Bits 0-7 Default = 0 //
0b00000000, //CV_57 - Reverse Direction Offset Bits 8-15 Default = 0 // 0b00000000, //CV_57 - Reverse Direction Offset Bits 8-15 Default = 0 //
0b00000110, //CV_58 - Offset Measurement Cycles Default = 6 // 0b00001000, //CV_58 - Offset Measurement Cycles Default = 8 //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b00000111, //CV_59 - speed_helper timer delay - can be used to adjust accel/decel rate // 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 - PWM scaling factor (target EMF Voltage) -> 126*CV_60 = end_target|max //
@@ -86,7 +87,6 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b01000110, //CV_61 - V_EMF Total amount of Measurement Iterations - Default = 70 iterations // 0b01000110, //CV_61 - V_EMF Total amount of Measurement Iterations - Default = 70 iterations //
0b01100100, //CV_62 - V_EMF Delay before Measuring - Default = 100us // 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 // // CV_63 and CV_64 are used to determine how many elements of the sorted measurement array will be discarded //
// This is used to filter out data. //
// For Example: CV_63 = x and CV_64 = y // // For Example: CV_63 = x and CV_64 = y //
// -> The x smallest values will be discarded. // // -> The x smallest values will be discarded. //
// -> The y largest values will be discarded. // // -> The y largest values will be discarded. //
+34 -29
View File
@@ -13,10 +13,10 @@ uint64_t last_bits = 0;
bool reset_package_flag = false; bool reset_package_flag = false;
absolute_time_t falling_edge_time,rising_edge_time; absolute_time_t falling_edge_time,rising_edge_time;
uint find_offset(uint level,uint step,uint delay,float threshold, bool direction){ uint16_t find_offset(uint16_t level, uint8_t step, uint8_t delay, uint32_t threshold, bool direction){
pwm_set_gpio_level(!direction,0); pwm_set_gpio_level(!direction,0);
uint gpio = MOTOR_PWM_FORWARD*direction + MOTOR_PWM_REVERSE*!direction; uint8_t gpio = MOTOR_FWD_PIN * direction + MOTOR_REV_PIN * !direction;
while((measure(direction)-ADC_OFFSET)<threshold){ while( measure(direction) < threshold ){
pwm_set_gpio_level(gpio,level); pwm_set_gpio_level(gpio,level);
busy_wait_ms(delay); busy_wait_ms(delay);
level += step; level += step;
@@ -24,45 +24,46 @@ uint find_offset(uint level,uint step,uint delay,float threshold, bool direction
pwm_set_gpio_level(direction,0); pwm_set_gpio_level(direction,0);
return level; return level;
} }
uint two_sigma(const uint arr[], uint length){ uint16_t two_sigma(const uint16_t arr[], uint8_t length){
//Calculate arithmetic average //Calculate arithmetic average
uint sum = 0; uint32_t sum = 0;
for (uint i = 0; i < length; ++i) { for (uint i = 0; i < length; ++i) {
sum += arr[i]; sum += arr[i];
} }
uint x_avg = sum / length; uint16_t x_avg = sum / length;
//Calculate variance //Calculate variance
sum = 0; sum = 0;
for (uint i = 0; i < length; ++i) { for (uint8_t i = 0; i < length; ++i) {
sum += (arr[i]-x_avg)*(arr[i]-x_avg); sum += (arr[i]-x_avg)*(arr[i]-x_avg);
} }
float variance = (float) sum/ (float)(length-1); float variance = (float) sum/ (float)(length-1);
//Calculate standard deviation //Calculate standard deviation
uint std_dev = (uint)sqrtf(variance); uint16_t std_dev = (uint16_t)sqrtf(variance);
//"Filter" every array element that deviates more than 2*(std_dev) //"Filter" every array element that deviates more than 2*(std_dev)
sum = 0; sum = 0;
uint counter = 0; uint8_t counter = 0;
for (uint i = 0; i < length; ++i) { for (uint8_t i = 0; i < length; ++i) {
uint val = arr[i]-x_avg; uint16_t val = arr[i]-x_avg;
if ( val < 2*std_dev || val > (0-2*std_dev) ){ if ( val < 2*std_dev || val > (0-2*std_dev) ){
sum += arr[i]; sum += arr[i];
counter++; counter++;
} }
} }
//Calculate and return "new" arithmetic average //Calculate and return "new" arithmetic average from "filtered" array
return sum / counter; return sum / counter;
} }
void setup_offsets(uint length){ void setup_offsets(uint8_t length){
uint offsets_fwd[length]; uint16_t offsets_fwd[length];
uint offsets_rev[length]; uint16_t offsets_rev[length];
for (int i = 0; i < length; ++i) { uint16_t max_lvl = _125M/(CV_ARRAY_FLASH[8]*100+10000);
offsets_fwd[i] = find_offset(0,1,1,0.05f,true); for (uint8_t i = 0; i < length; ++i) {
offsets_fwd[i] = find_offset(0,max_lvl/3000,1,30,true);
busy_wait_ms(500); busy_wait_ms(500);
offsets_rev[i] = find_offset(0,1,1,0.05f,false); offsets_rev[i] = find_offset(0,max_lvl/3000,1,30,false);
busy_wait_ms(500); busy_wait_ms(500);
} }
uint offsets_fwd_avg = two_sigma(offsets_fwd, length); uint16_t offsets_fwd_avg = two_sigma(offsets_fwd, length);
uint offsets_rev_avg = two_sigma(offsets_rev, length); uint16_t offsets_rev_avg = two_sigma(offsets_rev, length);
uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE]; uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE];
memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP)); memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP));
CV_ARRAY_TEMP[53] = offsets_fwd_avg; CV_ARRAY_TEMP[53] = offsets_fwd_avg;
@@ -74,9 +75,13 @@ void setup_offsets(uint length){
acknowledge(); acknowledge();
} }
void acknowledge() { void acknowledge() {
pwm_set_gpio_level(MOTOR_PWM_FORWARD,5000); uint16_t max_lvl = _125M/(CV_ARRAY_FLASH[8]*100+10000);
busy_wait_ms(6); pwm_set_gpio_level(MOTOR_FWD_PIN, max_lvl);
pwm_set_gpio_level(MOTOR_PWM_FORWARD,0); busy_wait_ms(3);
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
pwm_set_gpio_level(MOTOR_REV_PIN, max_lvl);
busy_wait_ms(3);
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
} }
void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos) { void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos) {
uint8_t mask = 0b00000001; uint8_t mask = 0b00000001;
@@ -219,6 +224,12 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[])
//0011-1111 (128 Speed Step Control) - 2 Byte length //0011-1111 (128 Speed Step Control) - 2 Byte length
if (command_byte_n == 0b00111111){ if (command_byte_n == 0b00111111){
target_speed_step = byte_array[command_byte_start_index - 1]; target_speed_step = byte_array[command_byte_start_index - 1];
//Check for offset setup
uint sum = (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]) + (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]);
if( !sum && target_speed_step != 128 && target_speed_step) {
uint8_t arr[4] = {125,7,6,124};
program_mode(4,arr);
}
if(target_speed_step>127) { if(target_speed_step>127) {
target_direction = true; //Forward target_direction = true; //Forward
} }
@@ -345,12 +356,6 @@ int main() {
multicore_launch_core1(core1_entry); multicore_launch_core1(core1_entry);
gpio_init(DCC_INPUT_PIN); gpio_init(DCC_INPUT_PIN);
gpio_set_dir(DCC_INPUT_PIN, GPIO_IN); gpio_set_dir(DCC_INPUT_PIN, GPIO_IN);
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); gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise);
printf("core0 done\n"); printf("core0 done\n");
while (1); while (1);
+3 -4
View File
@@ -23,7 +23,6 @@
#define PACKAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001 #define PACKAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
int8_t check_for_package(); int8_t check_for_package();
void writeLastBit(bool bit) ; void writeLastBit(bool bit) ;
int64_t readBit_alarm_callback(alarm_id_t id, void *user_data);
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) ; void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) ;
void acknowledge(); void acknowledge();
void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos); void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos);
@@ -40,6 +39,6 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]);
void track_signal_rise(unsigned int gpio, long unsigned int events); void track_signal_rise(unsigned int gpio, long unsigned int events);
void track_signal_fall(unsigned int gpio, long unsigned int events); void track_signal_fall(unsigned int gpio, long unsigned int events);
void evaluation(); void evaluation();
uint find_offset(uint level,uint step,uint delay,float threshold, bool direction); uint16_t find_offset(uint16_t level, uint8_t step, uint8_t delay, uint32_t threshold, bool direction);
uint two_sigma(const uint arr[], uint length); uint16_t two_sigma(const uint16_t arr[], uint8_t length);
void setup_offsets(uint length); void setup_offsets(uint8_t length);
+88 -89
View File
@@ -6,96 +6,101 @@
#include "core1.h" #include "core1.h"
uint16_t current_target = 0; uint32_t latest_target = 0;
float current_target_voltage = 0;
struct repeating_timer pid_control_timer,speed_helper_timer; struct repeating_timer pid_control_timer,speed_helper_timer;
typedef struct pid_params{ typedef struct pid_params{
float k_p; int32_t k_p;
float t_s; int32_t k_i;
float a_1; int32_t k_d;
float a_2; int32_t t_s;
float sum_limit_max; int32_t sum_limit_max;
float sum_limit_min; int32_t sum_limit_min;
float output; int32_t output;
float e; int32_t e;
float e_prev; int32_t e_prev;
float e_sum; int32_t e_sum;
uint startup_offset_fwd; uint32_t m_fwd_scaled;
uint startup_offset_rev; uint32_t m_rev_scaled;
uint32_t b_fwd;
uint32_t b_rev;
uint16_t max_output;
bool direction_prev; bool direction_prev;
}pid_params; }pid_params;
pid_params pid; pid_params pid;
typedef struct measure_params{ typedef struct measure_params{
uint32_t val;
uint8_t delay_in_us; uint8_t delay_in_us;
uint8_t total_iterations; uint8_t total_iterations;
uint8_t left_side_array_cutoff; uint8_t left_side_array_cutoff;
uint8_t right_side_array_cutoff; uint8_t right_side_array_cutoff;
}measure_params; }measure_params;
measure_params msr; measure_params msr;
uint8_t a_factor = 50; /*CV_ARRAY_FLASH[59];..................*/
uint8_t target_factor = 10; /*CV_ARRAY_FLASH[59];*/ // One Discrete Step
uint16_t calc_end_target(){ uint16_t calc_end_target(){
//Forward //Forward
if(target_direction){ if(target_direction){
if (target_speed_step == 128) return 0; if (target_speed_step == 128) return 0;
else return (target_speed_step-129) * a_factor ; /* (float) CV_ARRAY_FLASH[59]*/ else return (target_speed_step-129) * target_factor ; /* (float) CV_ARRAY_FLASH[59]*/
} }
//Reverse //Reverse
else { else {
if (target_speed_step == 0) return 0; if (target_speed_step == 0) return 0;
else return (target_speed_step-1) * a_factor; /*(float)CV_ARRAY_FLASH[59]*/ else return (target_speed_step-1) * target_factor; /*(float)CV_ARRAY_FLASH[59]*/
} }
} }
bool speed_helper() { bool speed_helper() {
uint8_t accel_rate = CV_ARRAY_FLASH[2]; uint8_t accel_rate = CV_ARRAY_FLASH[2];
uint8_t decel_rate = CV_ARRAY_FLASH[3]; uint8_t decel_rate = CV_ARRAY_FLASH[3];
uint16_t end_target = calc_end_target(); uint16_t end_target = calc_end_target();
//current_target only gets adjusted when the counter equals the acc/dec rate //latest_target only gets adjusted when the counter equals the acc/dec rate
//-> Time for 1 Speed Step := (speed_helper timer delay)*(accel_rate) //-> Time for 1 Speed Step := (speed_helper timer delay)*(accel_rate)
static uint8_t speed_helper_counter; static uint8_t speed_helper_counter;
//Emergency Stop //Emergency Stop
if (target_speed_step == 129 || target_speed_step==1) { if (target_speed_step == 129 || target_speed_step==1) {
current_target = 0; latest_target = 0;
speed_helper_counter = 0; speed_helper_counter = 0;
} }
//Acceleration //Acceleration
else if (end_target > current_target && speed_helper_counter == accel_rate) { else if (end_target > latest_target && speed_helper_counter == accel_rate) {
if(!accel_rate) current_target = end_target; // directly accelerate to end target speed if(!accel_rate) latest_target = end_target; // directly accelerate to end target speed
else current_target += a_factor; else latest_target += target_factor;
speed_helper_counter = 0; speed_helper_counter = 0;
} }
//Deceleration //Deceleration
else if (end_target < current_target && speed_helper_counter == decel_rate) { else if (end_target < latest_target && speed_helper_counter == decel_rate) {
if(!decel_rate) current_target = end_target; // directly decelerate to end target speed if(!decel_rate) latest_target = end_target; // directly decelerate to end target speed
else current_target -= a_factor; else latest_target -= target_factor;
speed_helper_counter = 0; speed_helper_counter = 0;
} }
//Acceleration/Deceleration "scheduled" //Acceleration/Deceleration "scheduled"
else if (end_target != current_target) speed_helper_counter++; else if (end_target != latest_target) speed_helper_counter++;
//Target reached -> Reset Counter //Target reached -> Reset Counter
else speed_helper_counter = 0; else speed_helper_counter = 0;
current_target_voltage = (float)current_target/1260;
return true; return true;
} }
float measure(bool direction){ uint32_t measure(bool direction){
uint32_t sum = 0; uint32_t sum = 0;
uint16_t adc_val[100]; uint16_t adc_val[100 /*msr.total_iterations*/];
pwm_set_gpio_level(MOTOR_PWM_FORWARD,0); int16_t j = 0;
pwm_set_gpio_level(MOTOR_PWM_REVERSE,0); uint16_t key;
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
adc_select_input(!direction); adc_select_input(!direction);
busy_wait_us(msr.delay_in_us); busy_wait_us(msr.delay_in_us);
adc_run(true); adc_run(true);
int key, j; for (int16_t i = 0; i < 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. //Take adc value out of fifo when available, then do one step of insertion sort.
adc_val[i] = adc_fifo_get_blocking(); adc_val[i] = adc_fifo_get_blocking();
key = adc_val[i]; key = adc_val[i];
j = i - 1; j = i - 1;
while (j >= 0 && adc_val[j] > key) { while (j >= 0 && adc_val[j] > key) {
adc_val[j + 1] = adc_val[j]; adc_val[j + 1] = adc_val[j];
j = j - 1; j--;
} }
adc_val[j + 1] = key; adc_val[j + 1] = key;
} }
@@ -105,50 +110,47 @@ float measure(bool direction){
for (uint8_t i = msr.left_side_array_cutoff; i < msr.total_iterations - msr.right_side_array_cutoff ; ++i) { for (uint8_t i = msr.left_side_array_cutoff; i < msr.total_iterations - msr.right_side_array_cutoff ; ++i) {
sum += adc_val[i]; sum += adc_val[i];
} }
//V_z is represented in 12bit uint32_t res = sum / ( msr.total_iterations - (msr.left_side_array_cutoff+msr.right_side_array_cutoff) );
float V_z = (float)sum / (float)(msr.total_iterations - (msr.left_side_array_cutoff+msr.right_side_array_cutoff)); return res;
//Zener correction (approximated) & Conversion factor for V_z <= 1951
//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);
} }
void adjust_pwm_level(uint16_t level) { void adjust_pwm_level(uint16_t level) {
if (target_direction) { if (target_direction) {
pwm_set_gpio_level(MOTOR_PWM_REVERSE,0); pwm_set_gpio_level(MOTOR_REV_PIN, 0);
pwm_set_gpio_level(MOTOR_PWM_FORWARD,level); pwm_set_gpio_level(MOTOR_FWD_PIN, level);
} }
//Reverse //Reverse
else { else {
pwm_set_gpio_level(MOTOR_PWM_FORWARD,0); pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
pwm_set_gpio_level(MOTOR_PWM_REVERSE,level); pwm_set_gpio_level(MOTOR_REV_PIN, level);
} }
} }
void set_pid_params(float v){ void init_offsets(uint16_t lvl_max){
float v_max = (float)a_factor/10; uint16_t lvl_min_fwd = (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]<<8);
pid.t_s = 5.0f / 1000; uint16_t lvl_min_rev = (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]<<8);
pid.k_p = 200.0f; pid.m_fwd_scaled = ((lvl_max-lvl_min_fwd)*1000) / (125*target_factor); //scaled with 1000 due to integer division
pid.k_p = (-200000*current_target_voltage)/v_max+200000.0f; pid.m_rev_scaled = ((lvl_max-lvl_min_rev)*1000) / (125*target_factor); //scaled with 1000 due to integer division
pid.a_1 = 200.0f * pid.t_s; // k_i*t_s pid.b_fwd = lvl_min_fwd - ((pid.m_fwd_scaled*target_factor) / 1000) - 200;
pid.a_2 = 0.0f / pid.t_s; // k_d/t_s 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){ bool pid_control(struct repeating_timer *t){
pid.output += (float)(pid.startup_offset_fwd*target_direction + pid.startup_offset_rev*(!target_direction)); if(target_direction != pid.direction_prev || !latest_target) pid.e_sum = 0;
float measured_voltage = measure(target_direction); msr.val = measure(target_direction);
if(target_direction != pid.direction_prev) pid.e_sum = 0.0f; pid.e = (int32_t)latest_target - (int32_t)msr.val + ADC_OFFSET;
pid.e = current_target_voltage - (measured_voltage - ADC_OFFSET);
pid.e_sum += pid.e; pid.e_sum += pid.e;
if (pid.e_sum > pid.sum_limit_max) pid.e_sum = pid.sum_limit_max; 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; 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*pid.e + pid.k_i*pid.t_s*pid.e_sum + (pid.k_d/pid.t_s)*(pid.e-pid.e_prev) ) / 1000;
(pid.a_1 * (float)pid.e_sum) + // k_i*t_s*e_sum pid.output += (int32_t)offset();
(pid.a_2 * (float)(pid.e - pid.e_prev)); // (k_d/t_s)*(e-e_prev) if (pid.output < 0 || !latest_target) pid.output = 0;
if (pid.output < 0 || current_target == 0) pid.output = 0; else if (pid.output > pid.max_output) pid.output = pid.max_output;
else if (pid.output > 5000) pid.output = 5000; adjust_pwm_level(pid.output);
adjust_pwm_level((uint16_t )pid.output); printf("o: %d\n",pid.output);
printf("e: %d\n",pid.e);
adc_fifo_drain(); adc_fifo_drain();
pid.direction_prev = target_direction; pid.direction_prev = target_direction;
pid.e_prev = pid.e; pid.e_prev = pid.e;
@@ -159,41 +161,38 @@ void init_pid(){
msr.delay_in_us = 100; /*CV_ARRAY_FLASH[61];*/ msr.delay_in_us = 100; /*CV_ARRAY_FLASH[61];*/
msr.left_side_array_cutoff = 15;/*CV_ARRAY_FLASH[62];*/ msr.left_side_array_cutoff = 15;/*CV_ARRAY_FLASH[62];*/
msr.right_side_array_cutoff = 15;/*CV_ARRAY_FLASH[63];*/ msr.right_side_array_cutoff = 15;/*CV_ARRAY_FLASH[63];*/
// pid.k_p = ((float)CV_ARRAY_FLASH[48] / 256); pid.e_sum = 0;
// pid.t_s = ((float)CV_ARRAY_FLASH[47] / 1000); pid.e_prev = 0;
// pid.a_1 = ((float)CV_ARRAY_FLASH[49] / 64) * pid.t_s ; // k_i*t_s pid.k_p = 2500;
// pid.a_2 = ((float)CV_ARRAY_FLASH[50] / 4096) / pid.t_s; // k_d/t_s pid.k_i = 100;
//pid.k_p = (-200000*current_target_voltage)/v_max+200000.0f; pid.k_d = 100;
pid.k_p = 200.0f; pid.t_s = 5 /*(float) CV_ARRAY_FLASH[47]*/;
pid.t_s = (float) CV_ARRAY_FLASH[47] / 1000; pid.sum_limit_max = 1000;/*(float)(CV_ARRAY_FLASH[51] );*/
pid.a_1 = 2000.0f * pid.t_s; // k_i*t_s pid.sum_limit_min = -1000;/*(float)(CV_ARRAY_FLASH[52] * (-1));*/
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.direction_prev = target_direction;
pid.startup_offset_fwd = (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]<<8); pid.max_output = _125M/(CV_ARRAY_FLASH[8]*100+10000);
pid.startup_offset_rev = (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]<<8);
} }
void init_pwm_core1(uint gpio) { void init_pwm(uint8_t gpio) {
uint slice_num = pwm_gpio_to_slice_num(gpio); uint16_t wrap_counter = _125M/(CV_ARRAY_FLASH[8]*100+10000);
uint32_t wrap_counter = 5000; // 5000 Cycles @ 125MHz for 1 Period -> 25kHz init_offsets(wrap_counter);
uint32_t slice_num = pwm_gpio_to_slice_num(gpio);
pwm_set_wrap(slice_num, wrap_counter); pwm_set_wrap(slice_num, wrap_counter);
pwm_set_gpio_level(gpio,0); pwm_set_gpio_level(gpio,0);
pwm_set_enabled(slice_num, true); pwm_set_enabled(slice_num, true);
} }
void core1_entry() { void core1_entry() {
printf("core1 init...\n"); printf("core1 init...\n");
if(gpio_get_function(MOTOR_PWM_FORWARD)!=4){ if(gpio_get_function(MOTOR_FWD_PIN) != 4){
gpio_set_function(MOTOR_PWM_FORWARD, GPIO_FUNC_PWM); gpio_set_function(MOTOR_FWD_PIN, GPIO_FUNC_PWM);
gpio_set_function(MOTOR_PWM_REVERSE, GPIO_FUNC_PWM); gpio_set_function(MOTOR_REV_PIN, GPIO_FUNC_PWM);
adc_init(); adc_init();
adc_gpio_init(V_EMF_ADC_PIN_FORWARD); adc_gpio_init(FWD_V_EMF_ADC_PIN);
adc_gpio_init(V_EMF_ADC_PIN_REVERSE); adc_gpio_init(REV_V_EMF_ADC_PIN);
adc_fifo_setup(true,false,0,false,false); adc_fifo_setup(true,false,0,false,false);
init_pwm_core1(MOTOR_PWM_FORWARD); init_pwm(MOTOR_FWD_PIN);
init_pwm_core1(MOTOR_PWM_REVERSE); init_pwm(MOTOR_REV_PIN);
init_pid();
} }
init_pid();
alarm_pool_add_repeating_timer_ms(alarm_pool_create(0,1), alarm_pool_add_repeating_timer_ms(alarm_pool_create(0,1),
CV_ARRAY_FLASH[47], CV_ARRAY_FLASH[47],
pid_control, pid_control,
+7 -16
View File
@@ -6,22 +6,13 @@
#pragma once #pragma once
#include "shared.h" #include "shared.h"
#define V_EMF_ADC_PIN_FORWARD 26 #define FWD_V_EMF_ADC_PIN 26
#define V_EMF_ADC_PIN_REVERSE 27 #define REV_V_EMF_ADC_PIN 27
#define MEASURE_CORRECTION_THRESHOLD 1695
#define L1 0.001611328125f
#define C0 26.18270299751651f
#define C1 -0.04661614287233649f
#define C2 3.29589342261872e-05f
#define C3 -1.000062393830345e-08f
#define C4 1.15408454807169e-12f
void adjust_pwm_level(uint16_t level);
uint16_t calc_end_target(); uint16_t calc_end_target();
bool speed_helper(); bool speed_helper();
void init_pid(); void adjust_pwm_level(uint16_t level);
void init_pwm_core1(uint gpio); void init_offsets(uint16_t lvl_max);
uint32_t offset();
bool pid_control(struct repeating_timer *t); bool pid_control(struct repeating_timer *t);
void set_pid_params(float v); void init_pid();
void print_cv_array(const uint8_t *buf, size_t len); void init_pwm(uint8_t gpio);
+5 -4
View File
@@ -14,9 +14,10 @@
#include "hardware/adc.h" #include "hardware/adc.h"
#include "hardware/flash.h" #include "hardware/flash.h"
#include "hardware/irq.h" #include "hardware/irq.h"
#define ADC_OFFSET 0.025f #define _125M 125000000
#define MOTOR_PWM_FORWARD 22 #define ADC_OFFSET 13
#define MOTOR_PWM_REVERSE 23 #define MOTOR_FWD_PIN 22
#define MOTOR_REV_PIN 23
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE) //This Offset should be far enough away from program data #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 pid_control_timer;
extern repeating_timer_t speed_helper_timer; extern repeating_timer_t speed_helper_timer;
@@ -24,4 +25,4 @@ extern bool target_direction;
extern uint target_speed_step; extern uint target_speed_step;
extern const uint8_t *CV_ARRAY_FLASH; extern const uint8_t *CV_ARRAY_FLASH;
void core1_entry(); void core1_entry();
float measure(bool direction); uint32_t measure(bool direction);