diff --git a/CV.h b/CV.h index 59f05f1..4f8c0d8 100644 --- a/CV.h +++ b/CV.h @@ -9,8 +9,8 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = { 0b00001000, //CV_2 - Minimum speed ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 0 == Fastest dec/acc rate; 255 == slowest; - 0b00010000, //CV_3 - Acceleration rate - CV_3*CV_59 = Time for one discrete speed step change in ms - 0b00010000, //CV_4 - Deceleration rate - CV_4*CV_59 = Time for one discrete speed step change in ms + 0b00000010, //CV_3 - Acceleration rate - CV_3*CV_59 = Time for one discrete speed step change in ms + 0b00000010, //CV_4 - Deceleration rate - CV_4*CV_59 = Time for one discrete speed step change in ms ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 0b01111111, //CV_5 - V_max 0b00111111, //CV_6 - V_mid diff --git a/core1.c b/core1.c index 7d9fa46..6de7a54 100644 --- a/core1.c +++ b/core1.c @@ -92,7 +92,7 @@ uint32_t measure(bool direction){ 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)+2); busy_wait_us(msr.delay_in_us); adc_run(true); for (int32_t i = 0; i < msr.total_iterations; ++i) { @@ -148,6 +148,8 @@ bool pid_control(struct repeating_timer *t){ pid.direction_prev = target_direction; pid.e_sum += pid.e; pid.e_prev = pid.e; + if (pid.e_sum < pid.sum_limit_min) pid.e_sum = pid.sum_limit_min; + else if (pid.e_sum > pid.sum_limit_max) pid.e_sum = pid.sum_limit_max; // Adjust offset when the absolute value of e_sum is half of max if (pid.e_sum > pid.sum_limit_max/2 || pid.e_sum < pid.sum_limit_min/2){ pid.offset += 3*(pid.e_sum/pid.sum_limit_max); @@ -157,7 +159,7 @@ bool pid_control(struct repeating_timer *t){ // Increase sampling time for higher speed steps - Decrease for lower speed steps pid_control_timer.delay_us = (8000*latest_target_index)/126 + 2000; pid.t_s = ((8000*latest_target_index)/126 + 2000)/1000; - printf("%d\n",pid.output); + printf("%d\n",pid.e_sum); return true; //printf("off:%d\n",pid.offset); } diff --git a/shared.h b/shared.h index 31ec167..d6984c6 100644 --- a/shared.h +++ b/shared.h @@ -14,17 +14,17 @@ #include "hardware/flash.h" #include "hardware/irq.h" #define _125M 125000000 -#define DCC_INPUT_PIN 9u -#define MOTOR_FWD_PIN 22u -#define MOTOR_REV_PIN 23u -#define FWD_V_EMF_ADC_PIN 26u -#define REV_V_EMF_ADC_PIN 27u -//Aux output pins -#define AUX_PIN_MASK 0b00000000000000000011110000000000 +#define DCC_INPUT_PIN 21u +#define MOTOR_FWD_PIN 23u +#define MOTOR_REV_PIN 22u +#define FWD_V_EMF_ADC_PIN 28u +#define REV_V_EMF_ADC_PIN 29u +//Aux output pins (GPIO 24,25,26,27) +#define AUX_PIN_MASK (1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) //GPIO pins configured as inputs (Input functionality not implemented yet) #define GPIO_INPUT_MASK 0b00000000000000000000000000000000 //GPIO pins configured as outputs -#define GPIO_OUTPUT_MASK 0b00000000000000000000000000111111 +#define GPIO_OUTPUT_MASK 0b00000000000000000000000000000000 //GPIO pin mask to prevent setting illegal GPIOs (ADC, Motor, DCC Input Pin) #define GPIO_ILLEGAL_MASK (1u<