Merge pull request #10 from questor/main
- Printf debugging with different logging levels, disabled by default - Disabled UART, USB by default
This commit is contained in:
6 files changed
+120
-38
No files matched your search
+6
-4
@@ -43,9 +43,11 @@ target_link_libraries(
|
|||||||
pico_multicore
|
pico_multicore
|
||||||
)
|
)
|
||||||
|
|
||||||
# enable usb output, disable uart output
|
# disable usb stack
|
||||||
pico_enable_stdio_usb(RP2040-Decoder 1)
|
pico_enable_stdio_usb(RP2040-Decoder 0)
|
||||||
pico_enable_stdio_uart(RP2040-Decoder 1)
|
|
||||||
|
# disable uart output (to enable put a 1 instead of the 0)
|
||||||
|
pico_enable_stdio_uart(RP2040-Decoder 0)
|
||||||
|
|
||||||
# create map/bin/hex file etc.
|
# create map/bin/hex file etc.
|
||||||
pico_add_extra_outputs(RP2040-Decoder)
|
#pico_add_extra_outputs(RP2040-Decoder)
|
||||||
@@ -203,8 +203,8 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
|
|||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//Offset adjustment for ADC //
|
//Offset adjustment for ADC //
|
||||||
//If CV_172 = 0xFF = 255 there will be a measurement procedure on startup //
|
//If CV_172 = 0xFF = 255 there will be a measurement procedure on startup //
|
||||||
0b11111111, //CV_172 - ADC offset Default: 255 //
|
0b11111111, //CV_172 - ADC offset Default: 255 (triggers new measurement!) //
|
||||||
0b01111111, //CV_173 - ADC offset measurement cycles Default: 127 //
|
0b01111111, //CV_173 - ADC offset measurement cycles Default: 127 //
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
0b00000001, //CV_174 - Additional motor-PWM clock divider.
|
0b00000001, //CV_174 - Additional motor-PWM clock divider.
|
||||||
0b00000111, //CV_175 - speed_helper timer delay - can be used to adjust accel/decel rate
|
0b00000111, //CV_175 - speed_helper timer delay - can be used to adjust accel/decel rate
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ float two_std_dev(const float arr[], uint32_t length){
|
|||||||
|
|
||||||
// Measures constant adc offset and programs the offset into flash
|
// Measures constant adc offset and programs the offset into flash
|
||||||
void adc_offset_adjustment(uint32_t n){
|
void adc_offset_adjustment(uint32_t n){
|
||||||
|
LOG(1, "adc_offset_adjustment\n");
|
||||||
|
|
||||||
float offsets_fwd[n];
|
float offsets_fwd[n];
|
||||||
float offsets_rev[n];
|
float offsets_rev[n];
|
||||||
// Set PWM level to 0 just to be sure
|
// Set PWM level to 0 just to be sure
|
||||||
@@ -77,6 +79,8 @@ void adc_offset_adjustment(uint32_t n){
|
|||||||
float overall_avg = (offset_avg_fwd+offset_avg_rev)/2;
|
float overall_avg = (offset_avg_fwd+offset_avg_rev)/2;
|
||||||
uint8_t offset = (uint8_t)roundf(overall_avg);
|
uint8_t offset = (uint8_t)roundf(overall_avg);
|
||||||
|
|
||||||
|
LOG(1, "new adc offset CV[171] (%f): (uint8_t)%d\n", overall_avg, offset);
|
||||||
|
|
||||||
// Create temporary array -> change CV 172 in temp array -> erase flash -> write temp array to flash
|
// Create temporary array -> change CV 172 in temp array -> erase flash -> write temp array to flash
|
||||||
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));
|
||||||
@@ -131,18 +135,24 @@ void write_cv_handler(uint16_t cv_index, uint8_t cv_data){
|
|||||||
switch (cv_index) {
|
switch (cv_index) {
|
||||||
case 0: //CV_1
|
case 0: //CV_1
|
||||||
// CV_1 => Value = 0 is not allowed
|
// CV_1 => Value = 0 is not allowed
|
||||||
if (cv_data == 0 || cv_data > 127) break;
|
if (cv_data == 0 || cv_data > 127)
|
||||||
else regular_cv_write(cv_index,cv_data);
|
break;
|
||||||
|
else
|
||||||
|
regular_cv_write(cv_index,cv_data);
|
||||||
break;
|
break;
|
||||||
case 6: //CV_7
|
case 6: //CV_7
|
||||||
// Read only (CV_7 - Version no.)
|
// Read only (CV_7 - Version no.)
|
||||||
// ADC offset Adjustment (CV_7; Value = 7);
|
// ADC offset Adjustment (CV_7; Value = 7);
|
||||||
if (cv_data == 7) adc_offset_adjustment(8192);
|
if (cv_data == 7) {
|
||||||
|
LOG(1, "trigger adc offset adjustment via cv7 => 7\n");
|
||||||
|
adc_offset_adjustment(8192);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 7: //CV_8
|
case 7: //CV_8
|
||||||
// Read only (CV_8 - Manufacturer ID)
|
// Read only (CV_8 - Manufacturer ID)
|
||||||
// Reset all CVs to Default (CV_8; Value = 8)
|
// Reset all CVs to Default (CV_8; Value = 8)
|
||||||
if (cv_data == 8){
|
if (cv_data == 8){
|
||||||
|
LOG(1, "reset of flash triggered via cv8 => 8\n");
|
||||||
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
|
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
|
||||||
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2);
|
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2);
|
||||||
}
|
}
|
||||||
@@ -150,12 +160,16 @@ void write_cv_handler(uint16_t cv_index, uint8_t cv_data){
|
|||||||
// CV_17 must have a value between 11000000->(192dec) and 11100111->(231dec) inclusive
|
// CV_17 must have a value between 11000000->(192dec) and 11100111->(231dec) inclusive
|
||||||
// CV_17 == 192 && CV_18 == 0 -> Address = 0 is also not valid and therefore shall not be set
|
// CV_17 == 192 && CV_18 == 0 -> Address = 0 is also not valid and therefore shall not be set
|
||||||
case 16: //CV_17
|
case 16: //CV_17
|
||||||
if ( (cv_data < 192 || cv_data > 231) || (CV_ARRAY_FLASH[17] == 0 && cv_data == 192) ) break;
|
if ( (cv_data < 192 || cv_data > 231) || (CV_ARRAY_FLASH[17] == 0 && cv_data == 192) )
|
||||||
else regular_cv_write(cv_index,cv_data);
|
break;
|
||||||
|
else
|
||||||
|
regular_cv_write(cv_index,cv_data);
|
||||||
break;
|
break;
|
||||||
case 17: //CV_18
|
case 17: //CV_18
|
||||||
if(CV_ARRAY_FLASH[16] == 192 && cv_data == 0) break;
|
if(CV_ARRAY_FLASH[16] == 192 && cv_data == 0)
|
||||||
else regular_cv_write(cv_index,cv_data);
|
break;
|
||||||
|
else
|
||||||
|
regular_cv_write(cv_index,cv_data);
|
||||||
break;
|
break;
|
||||||
// CV_31 & CV_32 Index high byte isn't implemented and shall not be set
|
// CV_31 & CV_32 Index high byte isn't implemented and shall not be set
|
||||||
case 30: //CV_31
|
case 30: //CV_31
|
||||||
@@ -169,9 +183,9 @@ void write_cv_handler(uint16_t cv_index, uint8_t cv_data){
|
|||||||
|
|
||||||
|
|
||||||
// CV Programming function resets core1 and evaluates the type of programming instruction (e.g. write byte)
|
// CV Programming function resets core1 and evaluates the type of programming instruction (e.g. write byte)
|
||||||
void program_mode(uint8_t number_of_bytes, const uint8_t * const byte_array){
|
void program_mode(uint8_t number_of_bytes, const uint8_t * const byte_array) {
|
||||||
//First check for valid programming command ("address" 112-127)
|
//First check for valid programming command ("address" 112-127)
|
||||||
if (byte_array[number_of_bytes - 1]<128 && byte_array[number_of_bytes - 1]>111){
|
if (byte_array[number_of_bytes - 1]<128 && byte_array[number_of_bytes - 1]>111) {
|
||||||
uint8_t instruction_type_mask = 0b00001100;
|
uint8_t instruction_type_mask = 0b00001100;
|
||||||
uint8_t instruction_type = instruction_type_mask & byte_array[number_of_bytes - 1];
|
uint8_t instruction_type = instruction_type_mask & byte_array[number_of_bytes - 1];
|
||||||
uint8_t cv_address_ms_bits_mask = 0b00000011;
|
uint8_t cv_address_ms_bits_mask = 0b00000011;
|
||||||
@@ -179,9 +193,13 @@ void program_mode(uint8_t number_of_bytes, const uint8_t * const byte_array){
|
|||||||
uint16_t cv_address = byte_array[number_of_bytes - 2] + (cv_address_ms_bits << 8);
|
uint16_t cv_address = byte_array[number_of_bytes - 2] + (cv_address_ms_bits << 8);
|
||||||
|
|
||||||
// Before accessing flash, timers and interrupts need to be disabled and core1 needs to be shut down.
|
// Before accessing flash, timers and interrupts need to be disabled and core1 needs to be shut down.
|
||||||
if(pid_control_timer.pool)alarm_pool_destroy(pid_control_timer.pool);
|
if(pid_control_timer.pool)
|
||||||
if(speed_helper_timer.pool)alarm_pool_destroy(speed_helper_timer.pool);
|
alarm_pool_destroy(pid_control_timer.pool);
|
||||||
|
if(speed_helper_timer.pool)
|
||||||
|
alarm_pool_destroy(speed_helper_timer.pool);
|
||||||
|
|
||||||
multicore_reset_core1();
|
multicore_reset_core1();
|
||||||
|
|
||||||
uint32_t saved_interrupts = save_and_disable_interrupts();
|
uint32_t saved_interrupts = save_and_disable_interrupts();
|
||||||
|
|
||||||
// Verify CV bit instruction
|
// Verify CV bit instruction
|
||||||
@@ -346,7 +364,9 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_a
|
|||||||
if (command_byte_n == 0b00111111){
|
if (command_byte_n == 0b00111111){
|
||||||
static bool prev_dir;
|
static bool prev_dir;
|
||||||
uint32_t speed_step = byte_array[command_byte_start_index - 1];
|
uint32_t speed_step = byte_array[command_byte_start_index - 1];
|
||||||
|
// LOG(1, "new speed %d", speed_step);
|
||||||
multicore_fifo_push_blocking(speed_step);
|
multicore_fifo_push_blocking(speed_step);
|
||||||
|
// LOG(1, " submitted to list\n");
|
||||||
// In case of a direction change, functions need to be updated because functions might depend on direction state
|
// In case of a direction change, functions need to be updated because functions might depend on direction state
|
||||||
if (get_direction(0) != prev_dir) {
|
if (get_direction(0) != prev_dir) {
|
||||||
update_active_functions(0,0,true);
|
update_active_functions(0,0,true);
|
||||||
@@ -523,6 +543,7 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){
|
|||||||
for (int i = 0; i < iterations; ++i) {
|
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;
|
uint16_t level = max_level/20;
|
||||||
|
LOG(3, "iteration %d: maxlevel %d level %d\n", i, max_level, level);
|
||||||
float measurement;
|
float measurement;
|
||||||
do {
|
do {
|
||||||
pwm_set_gpio_level(gpio, level);
|
pwm_set_gpio_level(gpio, level);
|
||||||
@@ -533,15 +554,21 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){
|
|||||||
CV_ARRAY_FLASH[62],
|
CV_ARRAY_FLASH[62],
|
||||||
CV_ARRAY_FLASH[63],
|
CV_ARRAY_FLASH[63],
|
||||||
direction);
|
direction);
|
||||||
// Abort measurement and write default value of 0 to flash
|
LOG(3, "level %d measurement %f loop-cond: %f\n", level, measurement, measurement-(float)CV_ARRAY_FLASH[171]);
|
||||||
if (level > max_level) return 0;
|
if (level > max_level) {
|
||||||
}
|
// Abort measurement and write default value of 0 to flash
|
||||||
while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f);
|
LOG(1, "measure_base_pwm: abort measurement and return 0\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f);
|
||||||
|
LOG(3, "level_arr[%d] = %f\n", i, level);
|
||||||
level_arr[i] = (float)level;
|
level_arr[i] = (float)level;
|
||||||
busy_wait_ms(100);
|
busy_wait_ms(100);
|
||||||
}
|
}
|
||||||
// Find and return overall average discarding outliers in measurement - multiply with 0.9
|
// Find and return overall average discarding outliers in measurement - multiply with 0.9
|
||||||
return (uint16_t)(0.9f*two_std_dev(level_arr,5));
|
float retVal = 0.9f*two_std_dev(level_arr,5);
|
||||||
|
LOG(1, "measure_base_pwm: %d(%f)\n", (uint16_t)retVal, retVal);
|
||||||
|
return (uint16_t)(retVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -549,15 +576,16 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){
|
|||||||
// When factory condition is found the CV_ARRAY_DEFAULT will be written into flash
|
// When factory condition is found the CV_ARRAY_DEFAULT will be written into flash
|
||||||
// When ADC Setup is not configured run adc offset adjustment function and write adc offset into flash
|
// When ADC Setup is not configured run adc offset adjustment function and write adc offset into flash
|
||||||
void cv_setup_check(){
|
void cv_setup_check(){
|
||||||
|
|
||||||
// Check for flash factory setting and set CV_FLASH_ARRAY to default values when factory condition ("0xFF") is found.
|
// 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 ){
|
if ( CV_ARRAY_FLASH[64] == 0xFF ){
|
||||||
|
LOG(1, "found cv[64] equals to 0xff, reseting CVs (and CV[8] = 8)\n");
|
||||||
uint8_t arr[4] = {125,8,7,124};
|
uint8_t arr[4] = {125,8,7,124};
|
||||||
program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8)
|
program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for adc offset setup
|
// Check for adc offset setup
|
||||||
if ( CV_ARRAY_FLASH[171] == 0xFF ){
|
if ( CV_ARRAY_FLASH[171] == 0xFF ){
|
||||||
|
LOG(1, "found cv[171] equals to 0xff, adc offset adjstments (and cr[7] = 7)\n");
|
||||||
uint8_t arr[4] = {125,7,6,124};
|
uint8_t arr[4] = {125,7,6,124};
|
||||||
program_mode(4,arr); //ADC offset adjustment (write CV_7 = 7)
|
program_mode(4,arr); //ADC offset adjustment (write CV_7 = 7)
|
||||||
}
|
}
|
||||||
@@ -565,6 +593,7 @@ void cv_setup_check(){
|
|||||||
// Check for base PWM configuration - used for feed-forward
|
// Check for base PWM configuration - used for feed-forward
|
||||||
// Forward Direction
|
// Forward Direction
|
||||||
if (get_16bit_CV(175) == 0){
|
if (get_16bit_CV(175) == 0){
|
||||||
|
LOG(1, "found cv[175] equals to 0, forward direction\n");
|
||||||
uint16_t base_pwm_fwd = measure_base_pwm(true,10);
|
uint16_t base_pwm_fwd = measure_base_pwm(true,10);
|
||||||
uint8_t base_pwm_fwd_high_byte = base_pwm_fwd>>8;
|
uint8_t base_pwm_fwd_high_byte = base_pwm_fwd>>8;
|
||||||
uint8_t base_pwm_fwd_low_byte = base_pwm_fwd&0x00FF;
|
uint8_t base_pwm_fwd_low_byte = base_pwm_fwd&0x00FF;
|
||||||
@@ -575,6 +604,7 @@ void cv_setup_check(){
|
|||||||
}
|
}
|
||||||
// Reverse Direction
|
// Reverse Direction
|
||||||
if (get_16bit_CV(177) == 0){
|
if (get_16bit_CV(177) == 0){
|
||||||
|
LOG(1, "found cv[177] equals to 0, reverse direction\n");
|
||||||
uint16_t base_pwm_rev = measure_base_pwm(false,10);
|
uint16_t base_pwm_rev = measure_base_pwm(false,10);
|
||||||
uint8_t base_pwm_rev_high_byte = base_pwm_rev>>8;
|
uint8_t base_pwm_rev_high_byte = base_pwm_rev>>8;
|
||||||
uint8_t base_pwm_rev_low_byte = base_pwm_rev&0x00FF;
|
uint8_t base_pwm_rev_low_byte = base_pwm_rev&0x00FF;
|
||||||
@@ -583,6 +613,8 @@ void cv_setup_check(){
|
|||||||
uint8_t arr1[4] = {125,base_pwm_rev_low_byte,178,124};
|
uint8_t arr1[4] = {125,base_pwm_rev_low_byte,178,124};
|
||||||
program_mode(4,arr1);
|
program_mode(4,arr1);
|
||||||
}
|
}
|
||||||
|
LOG(1, "int_lim_max %d\n", CV_ARRAY_FLASH[51]);
|
||||||
|
LOG(1, "int_lim_min %d\n", CV_ARRAY_FLASH[52]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -607,11 +639,13 @@ void init_motor_pwm(uint8_t 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);
|
||||||
|
|
||||||
|
LOG(2, "init motor(%d): wrapCounter %d clkdiv %d\n", gpio, wrap_counter, CV_ARRAY_FLASH[173]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main initialization - GPIO Config / ADC Config
|
// Main initialization - GPIO Config / ADC Config
|
||||||
void init_main(){
|
void init_gpio_adc(){
|
||||||
if(gpio_get_function(MOTOR_FWD_PIN) != 4){
|
if(gpio_get_function(MOTOR_FWD_PIN) != 4){
|
||||||
gpio_set_function(MOTOR_FWD_PIN, GPIO_FUNC_PWM);
|
gpio_set_function(MOTOR_FWD_PIN, GPIO_FUNC_PWM);
|
||||||
gpio_set_function(MOTOR_REV_PIN, GPIO_FUNC_PWM);
|
gpio_set_function(MOTOR_REV_PIN, GPIO_FUNC_PWM);
|
||||||
@@ -625,16 +659,20 @@ void init_main(){
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
printf("core0 init...\n");
|
LOG(1, "\n\n======\ncore0 init\n");
|
||||||
init_main();
|
init_gpio_adc();
|
||||||
|
LOG(1, "init motor pwms\n");
|
||||||
init_motor_pwm(MOTOR_FWD_PIN);
|
init_motor_pwm(MOTOR_FWD_PIN);
|
||||||
init_motor_pwm(MOTOR_REV_PIN);
|
init_motor_pwm(MOTOR_REV_PIN);
|
||||||
|
LOG(1, "check cvs\n");
|
||||||
cv_setup_check();
|
cv_setup_check();
|
||||||
|
LOG(1, "init outputs\n");
|
||||||
init_outputs();
|
init_outputs();
|
||||||
|
LOG(1, "init gpios\n");
|
||||||
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);
|
||||||
gpio_pull_up(DCC_INPUT_PIN);
|
gpio_pull_up(DCC_INPUT_PIN);
|
||||||
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");
|
LOG(1, "core0 done\n");
|
||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
@@ -30,6 +30,13 @@ 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);
|
void verify_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
||||||
void regular_cv_write(uint16_t cv_index, uint8_t cv_data);
|
void regular_cv_write(uint16_t cv_index, uint8_t cv_data);
|
||||||
void write_cv_handler(uint16_t cv_index, uint8_t cv_data);
|
void write_cv_handler(uint16_t cv_index, uint8_t cv_data);
|
||||||
|
|
||||||
|
|
||||||
|
#define ProgramInstrVerifyBit 0b10
|
||||||
|
#define ProgramInstrVerifyByte 0b01
|
||||||
|
#define ProgramInstrWriteByte 0b11
|
||||||
|
#define ProgramCmd(instr, adrmsbits) ((instr<<2) | (adrmsbits))
|
||||||
|
|
||||||
void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||||
void set_outputs(uint32_t functions_to_set_bitmask);
|
void set_outputs(uint32_t functions_to_set_bitmask);
|
||||||
void update_active_functions(uint32_t byte, uint8_t clr_bit_ind,bool direction_change);
|
void update_active_functions(uint32_t byte, uint8_t clr_bit_ind,bool direction_change);
|
||||||
@@ -48,4 +55,4 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations);
|
|||||||
void cv_setup_check();
|
void cv_setup_check();
|
||||||
bool get_direction(bool *direction_ptr);
|
bool get_direction(bool *direction_ptr);
|
||||||
void init_motor_pwm(uint8_t gpio);
|
void init_motor_pwm(uint8_t gpio);
|
||||||
void init_main();
|
void init_gpio_adc();
|
||||||
@@ -16,14 +16,18 @@ void update_speed_dir(pid_params *pid, uint32_t speed_step_target){
|
|||||||
// Forward
|
// Forward
|
||||||
if(speed_step_target > 127) {
|
if(speed_step_target > 127) {
|
||||||
pid->direction = true^reverse_dir;
|
pid->direction = true^reverse_dir;
|
||||||
if (pid->speed_step_target == 128) pid->end_target_index = 0;
|
if (pid->speed_step_target == 128)
|
||||||
else pid->end_target_index = (pid->speed_step_target-129);
|
pid->end_target_index = 0;
|
||||||
|
else
|
||||||
|
pid->end_target_index = (pid->speed_step_target-129);
|
||||||
}
|
}
|
||||||
// Reverse
|
// Reverse
|
||||||
else {
|
else {
|
||||||
pid->direction = false^reverse_dir;
|
pid->direction = false^reverse_dir;
|
||||||
if (pid->speed_step_target == 0) pid->end_target_index = 0;
|
if (pid->speed_step_target == 0)
|
||||||
else pid->end_target_index = (pid->speed_step_target-1);
|
pid->end_target_index = 0;
|
||||||
|
else
|
||||||
|
pid->end_target_index = (pid->speed_step_target-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,6 +196,15 @@ bool pid_control(struct repeating_timer *t){
|
|||||||
if (output < 0 || !pid->setpoint) output = 0.0f;
|
if (output < 0 || !pid->setpoint) output = 0.0f;
|
||||||
else if (output > pid->max_output) output = pid->max_output;
|
else if (output > pid->max_output) output = pid->max_output;
|
||||||
|
|
||||||
|
if(LOGLEVEL >= 1) {
|
||||||
|
static int counter = 0;
|
||||||
|
counter++;
|
||||||
|
if(counter > 100) {
|
||||||
|
LOG(1, "pid error(%f) output(%f)\n", e, output);
|
||||||
|
counter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set PWM Level and discard results in adc fifo
|
// Set PWM Level and discard results in adc fifo
|
||||||
adjust_pwm_level((uint16_t)roundf(output),pid);
|
adjust_pwm_level((uint16_t)roundf(output),pid);
|
||||||
adc_fifo_drain();
|
adc_fifo_drain();
|
||||||
@@ -226,6 +239,9 @@ void init_pid(pid_params *pid){
|
|||||||
double v_min = (double)CV_ARRAY_FLASH[1];
|
double v_min = (double)CV_ARRAY_FLASH[1];
|
||||||
double v_mid = (double)CV_ARRAY_FLASH[5]*16;
|
double v_mid = (double)CV_ARRAY_FLASH[5]*16;
|
||||||
double v_max = (double)CV_ARRAY_FLASH[4]*16;
|
double v_max = (double)CV_ARRAY_FLASH[4]*16;
|
||||||
|
|
||||||
|
LOG(1, "speedtable min(%f) mid(%f) max(%f)\n", v_min, v_mid, v_max);
|
||||||
|
|
||||||
double delta_x = 63;
|
double delta_x = 63;
|
||||||
double m_1 = (v_mid-v_min)/delta_x;
|
double m_1 = (v_mid-v_min)/delta_x;
|
||||||
double m_2 = (v_max-v_mid)/delta_x;
|
double m_2 = (v_max-v_mid)/delta_x;
|
||||||
@@ -284,7 +300,7 @@ void init_pid(pid_params *pid){
|
|||||||
|
|
||||||
|
|
||||||
void core1_entry() {
|
void core1_entry() {
|
||||||
printf("core1 init...\n");
|
LOG(1, "core1 init...\n");
|
||||||
pid_params pid_parameters;
|
pid_params pid_parameters;
|
||||||
pid_params *pid = &pid_parameters;
|
pid_params *pid = &pid_parameters;
|
||||||
init_pid(pid);
|
init_pid(pid);
|
||||||
@@ -305,7 +321,7 @@ void core1_entry() {
|
|||||||
multicore_fifo_push_blocking((uint32_t) &(pid->direction));
|
multicore_fifo_push_blocking((uint32_t) &(pid->direction));
|
||||||
uint64_t packet_timeout_threshold_in_us = CV_ARRAY_FLASH[10]*1000000;
|
uint64_t packet_timeout_threshold_in_us = CV_ARRAY_FLASH[10]*1000000;
|
||||||
absolute_time_t time_last_update = get_absolute_time();
|
absolute_time_t time_last_update = get_absolute_time();
|
||||||
printf("core1 done\n");
|
// printf("core1 done\n");
|
||||||
while (1){
|
while (1){
|
||||||
// Check for time difference between last packet and current time (Packet Timeout)
|
// Check for time difference between last packet and current time (Packet Timeout)
|
||||||
absolute_time_t current_time = get_absolute_time();
|
absolute_time_t current_time = get_absolute_time();
|
||||||
|
|||||||
@@ -15,6 +15,18 @@
|
|||||||
#include "hardware/flash.h"
|
#include "hardware/flash.h"
|
||||||
#include "hardware/irq.h"
|
#include "hardware/irq.h"
|
||||||
|
|
||||||
|
|
||||||
|
// here you can enable printf debugging (needs extra soldering and you have to enable uart in the cmake configuration)
|
||||||
|
// please note
|
||||||
|
// - if it's disabled it will be compiled completely out
|
||||||
|
// - be careful that printf debugging can change your timing behavior, for example in the pid controller limit the outputs
|
||||||
|
// like already done (only print every 100th iteration for example!)
|
||||||
|
// - also note that with logging enabled GPIO0/1 are DISABLED from decoder functions because they are used for uart to the
|
||||||
|
// outside world (this is configure automatically for you below!)
|
||||||
|
// - IMPORTANT: you have to enable uart in the cmakelists.txt to enable logging!
|
||||||
|
#define LOGLEVEL 0
|
||||||
|
#define LOG(level, ...) { if(level <= LOGLEVEL) {printf(__VA_ARGS__);}}
|
||||||
|
|
||||||
// Constant Value of 125 x 10⁶
|
// Constant Value of 125 x 10⁶
|
||||||
#define _125M 125000000
|
#define _125M 125000000
|
||||||
|
|
||||||
@@ -25,8 +37,15 @@
|
|||||||
#define FWD_V_EMF_ADC_PIN 28u
|
#define FWD_V_EMF_ADC_PIN 28u
|
||||||
#define REV_V_EMF_ADC_PIN 29u
|
#define REV_V_EMF_ADC_PIN 29u
|
||||||
|
|
||||||
// GPIO used directly (GPIO 0-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
// if logging is enabled do NOT use gpio0/1 for functions in the decoder
|
||||||
#define GPIO_OUTPUT_PIN_MASK (1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2) | (1u<<1) | (1u<<0)
|
#if LOGLEVEL != 0
|
||||||
|
// GPIO used directly (GPIO 2-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
||||||
|
#define GPIO_OUTPUT_PIN_MASK (1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2)
|
||||||
|
#else
|
||||||
|
// GPIO used directly (GPIO 0-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
||||||
|
#define GPIO_OUTPUT_PIN_MASK (1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2) | (1u<<1) | (1u<<0)
|
||||||
|
#endif
|
||||||
|
|
||||||
// GPIO pin mask to prevent setting illegal GPIOs (ADC, Motor, DCC Input Pin)
|
// GPIO pin mask to prevent setting illegal GPIOs (ADC, Motor, DCC Input Pin)
|
||||||
#define GPIO_ILLEGAL_MASK (1u<<DCC_INPUT_PIN) | (1u<<MOTOR_FWD_PIN) | (1u<<MOTOR_REV_PIN) | (1u<<FWD_V_EMF_ADC_PIN) | (1u<<REV_V_EMF_ADC_PIN)
|
#define GPIO_ILLEGAL_MASK (1u<<DCC_INPUT_PIN) | (1u<<MOTOR_FWD_PIN) | (1u<<MOTOR_REV_PIN) | (1u<<FWD_V_EMF_ADC_PIN) | (1u<<REV_V_EMF_ADC_PIN)
|
||||||
|
|
||||||
@@ -46,4 +65,4 @@ float measure(uint8_t total_iterations,
|
|||||||
uint8_t r_side_arr_cutoff,
|
uint8_t r_side_arr_cutoff,
|
||||||
bool direction);
|
bool direction);
|
||||||
uint32_t get_32bit_CV (uint16_t CV_start_index);
|
uint32_t get_32bit_CV (uint16_t CV_start_index);
|
||||||
uint16_t get_16bit_CV (uint16_t CV_start_index);
|
uint16_t get_16bit_CV (uint16_t CV_start_index);
|
||||||
Reference in new issue
Block a user