Merge pull request #19 from gab-k/refactorlogging

always append a new line to log
This commit is contained in:
Gabriel Koppenstein authored and GitHub committed 2023-12-23 18:13:34 +01:00
commit 27d68d3d24
3 files changed
+27 -27

No files matched your search

+23 -23
View File
@@ -45,7 +45,7 @@ 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"); LOG(1, "adc_offset_adjustment");
float offsets_fwd[n]; float offsets_fwd[n];
float offsets_rev[n]; float offsets_rev[n];
@@ -79,7 +79,7 @@ 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); LOG(1, "new adc offset CV[171] (%f): (uint8_t)%d", 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];
@@ -144,7 +144,7 @@ void write_cv_handler(uint16_t cv_index, uint8_t cv_data){
// 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) { if (cv_data == 7) {
LOG(1, "trigger adc offset adjustment via cv7 => 7\n"); LOG(1, "trigger adc offset adjustment via cv7 => 7");
adc_offset_adjustment(8192); adc_offset_adjustment(8192);
} }
break; break;
@@ -152,7 +152,7 @@ void write_cv_handler(uint16_t cv_index, uint8_t cv_data){
// 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"); LOG(1, "reset of flash triggered via cv8 => 8");
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);
} }
@@ -366,7 +366,7 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_a
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); // 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"); // LOG(1, " submitted to list");
// 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);
@@ -543,7 +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); LOG(3, "iteration %d: maxlevel %d level %d", i, max_level, level);
float measurement; float measurement;
do { do {
pwm_set_gpio_level(gpio, level); pwm_set_gpio_level(gpio, level);
@@ -554,20 +554,20 @@ 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);
LOG(3, "level %d measurement %f loop-cond: %f\n", level, measurement, measurement-(float)CV_ARRAY_FLASH[171]); LOG(3, "level %d measurement %f loop-cond: %f", level, measurement, measurement-(float)CV_ARRAY_FLASH[171]);
if (level > max_level) { if (level > max_level) {
// Abort measurement and write default value of 0 to flash // Abort measurement and write default value of 0 to flash
LOG(1, "measure_base_pwm: abort measurement and return 0\n"); LOG(1, "measure_base_pwm: abort measurement and return 0");
return 0; return 0;
} }
} while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f); } while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f);
LOG(3, "level_arr[%d] = %f\n", i, level); LOG(3, "level_arr[%d] = %f", 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
float retVal = 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); LOG(1, "measure_base_pwm: %d(%f)", (uint16_t)retVal, retVal);
return (uint16_t)(retVal); return (uint16_t)(retVal);
} }
@@ -578,14 +578,14 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){
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"); LOG(1, "found cv[64] equals to 0xff, reseting CVs (and CV[8] = 8)");
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"); LOG(1, "found cv[171] equals to 0xff, adc offset adjstments (and cr[7] = 7)");
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)
} }
@@ -593,7 +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"); LOG(1, "found cv[175] equals to 0, forward direction");
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;
@@ -604,7 +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"); LOG(1, "found cv[177] equals to 0, reverse direction");
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;
@@ -613,8 +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_max %d", CV_ARRAY_FLASH[51]);
LOG(1, "int_lim_min %d\n", CV_ARRAY_FLASH[52]); LOG(1, "int_lim_min %d", CV_ARRAY_FLASH[52]);
} }
@@ -640,7 +640,7 @@ void init_motor_pwm(uint8_t gpio) {
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]); LOG(2, "init motor(%d): wrapCounter %d clkdiv %d", gpio, wrap_counter, CV_ARRAY_FLASH[173]);
} }
@@ -659,20 +659,20 @@ void init_gpio_adc(){
int main() { int main() {
stdio_init_all(); stdio_init_all();
LOG(1, "\n\n======\ncore0 init\n"); LOG(1, "\n\n======\ncore0 init");
init_gpio_adc(); init_gpio_adc();
LOG(1, "init motor pwms\n"); LOG(1, "init motor pwms");
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"); LOG(1, "check cvs");
cv_setup_check(); cv_setup_check();
LOG(1, "init outputs\n"); LOG(1, "init outputs");
init_outputs(); init_outputs();
LOG(1, "init gpios\n"); LOG(1, "init gpios");
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);
LOG(1, "core0 done\n"); LOG(1, "core0 done");
while (1); while (1);
} }
+3 -3
View File
@@ -200,7 +200,7 @@ bool pid_control(struct repeating_timer *t){
static int counter = 0; static int counter = 0;
counter++; counter++;
if(counter > 100) { if(counter > 100) {
LOG(1, "pid error(%f) output(%f)\n", e, output); LOG(1, "pid error(%f) output(%f)", e, output);
counter = 0; counter = 0;
} }
} }
@@ -240,7 +240,7 @@ void init_pid(pid_params *pid){
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); LOG(1, "speedtable min(%f) mid(%f) max(%f)", 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;
@@ -300,7 +300,7 @@ void init_pid(pid_params *pid){
void core1_entry() { void core1_entry() {
LOG(1, "core1 init...\n"); LOG(1, "core1 init...");
pid_params pid_parameters; pid_params pid_parameters;
pid_params *pid = &pid_parameters; pid_params *pid = &pid_parameters;
init_pid(pid); init_pid(pid);
+1 -1
View File
@@ -25,7 +25,7 @@
// outside world (this is configure automatically for you below!) // outside world (this is configure automatically for you below!)
// - IMPORTANT: you have to enable uart in the cmakelists.txt to enable logging! // - IMPORTANT: you have to enable uart in the cmakelists.txt to enable logging!
#define LOGLEVEL 0 #define LOGLEVEL 0
#define LOG(level, ...) { if(level <= LOGLEVEL) {printf(__VA_ARGS__);}} #define LOG(level, ...) { if(level <= LOGLEVEL) {printf(__VA_ARGS__, "\n");}}
// Constant Value of 125 x 10⁶ // Constant Value of 125 x 10⁶
#define _125M 125000000 #define _125M 125000000