diff --git a/shared/comps/d.c b/shared/comps/d.c new file mode 100644 index 00000000..4f984389 --- /dev/null +++ b/shared/comps/d.c @@ -0,0 +1,38 @@ +#include "commands.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" + +HAL_COMP(d); + +HAL_PIN(pwm_volt);//max volt in +HAL_PIN(uq);//volt in +HAL_PIN(limit);//voltage limit +HAL_PIN(id);//d current out +HAL_PIN(p);//gain + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct rev_ctx_t * ctx = (struct rev_ctx_t *)ctx_ptr; + struct d_pin_ctx_t *pins = (struct d_pin_ctx_t *)pin_ptr; + float reserve = PIN(pwm_volt) - ABS(PIN(uq)) - PIN(limit); + if(reserve <= 0){ + PIN(id) = reserve * PIN(p); + }else{ + PIN(id) = 0; + } +} + +hal_comp_t d_comp_struct = { + .name = "d", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct d_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/shared/hal.h b/shared/hal.h index ef42d29a..472caf3d 100644 --- a/shared/hal.h +++ b/shared/hal.h @@ -22,8 +22,8 @@ #include #include -#define HAL_CALC_TIME -#define HAL_COMP_CALC_TIME +//#define HAL_CALC_TIME +//#define HAL_COMP_CALC_TIME //#define HAL_WATCHDOG #ifndef HAL_MAX_PINS diff --git a/stm32f303/Makefile b/stm32f303/Makefile index 751a4e15..c6b06856 100644 --- a/stm32f303/Makefile +++ b/stm32f303/Makefile @@ -31,11 +31,11 @@ SOURCES += stm32f303/src/hal_tbl.c CFLAGS += -DHAL_MAX_CTX=1024 COMPS += stm32f303/src/comps/hv.c -COMPS += stm32f303/src/comps/hvdc.c +# COMPS += stm32f303/src/comps/hvdc.c COMPS += stm32f303/src/comps/io.c -COMPS += stm32f303/src/comps/ls.c -COMPS += stm32f303/src/comps/enc.c -COMPS += stm32f303/src/comps/uart.c +# COMPS += stm32f303/src/comps/ls.c +COMPS += stm32f303/src/comps/ppm.c +# COMPS += stm32f303/src/comps/uart.c COMPS += shared/comps/sim.c COMPS += shared/comps/term.c @@ -242,7 +242,7 @@ showsize: build btburn: build showsize $(TARGET).dfu @tools/bootloader.py @sleep 1 - @dfu-util -d 0483:df11 -a 0 -s 0x8004000:leave -D $(TARGET).dfu + @dfu-util -d 0483:df11 -a 0 -s 0x8000000:leave -D $(TARGET).dfu flash: $(TARGET).bin st-flash --reset write $(TARGET).bin 0x8004000 diff --git a/stm32f303/src/comps/hv.c b/stm32f303/src/comps/hv.c index efe660a2..c7637e72 100644 --- a/stm32f303/src/comps/hv.c +++ b/stm32f303/src/comps/hv.c @@ -86,13 +86,14 @@ static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_ temp_error = 0; } - if(PIN(mot_temp) > 110.0) { - mot_error = 1; - } - - if(PIN(mot_temp) < 85.0) { - mot_error = 0; - } + // if(PIN(mot_temp) > 110.0) { + // mot_error = 1; + // } + // + // if(PIN(mot_temp) < 85.0) { + // mot_error = 0; + // } + mot_error = 0; if(PIN(en) > 0.0 && !temp_error && !mot_error){ TIM8->BDTR |= TIM_BDTR_MOE; // on diff --git a/stm32f303/src/comps/io.c b/stm32f303/src/comps/io.c index 53e6af46..edce0d7e 100644 --- a/stm32f303/src/comps/io.c +++ b/stm32f303/src/comps/io.c @@ -144,21 +144,21 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst PIN(iw) = -AMP((float)(a12 & 0xFFFF) / 5.0, SHUNT_GAIN) + ctx->w_offset; // 1u PIN(iu) = -AMP((float)(a12 >> 16) / 5.0, SHUNT_GAIN) + ctx->u_offset; PIN(iv) = -AMP((float)(a34 & 0xFFFF) / 5.0, SHUNT_GAIN) + ctx->v_offset; - PIN(w) = VOLT(adc_12_buf[5] & 0xFFFF) * 0.05 + PIN(w) * 0.95; // 0.6u - PIN(v) = VOLT(adc_12_buf[5] >> 16) * 0.05 + PIN(v) * 0.95; - PIN(u) = VOLT(adc_34_buf[5] & 0xFFFF) * 0.05 + PIN(u) * 0.95; + //PIN(w) = VOLT(adc_12_buf[5] & 0xFFFF) * 0.05 + PIN(w) * 0.95; // 0.6u + //PIN(v) = VOLT(adc_12_buf[5] >> 16) * 0.05 + PIN(v) * 0.95; + //PIN(u) = VOLT(adc_34_buf[5] & 0xFFFF) * 0.05 + PIN(u) * 0.95; PIN(udc) = VOLT(adc_34_buf[5] >> 16) * 0.05 + PIN(udc) * 0.95; PIN(udc_pwm) = PIN(udc) / 2.0; - PIN(temp_v) = ADC(adc_34_buf[4] >> 16); - toggle = !toggle; - if (toggle) { + // PIN(temp_v) = ADC(adc_34_buf[4] >> 16); + // toggle = !toggle; + // if (toggle) { PIN(hv_temp) = r2temp(HV_R(ADC(adc_34_buf[4] >> 16))) * 0.01 + PIN(hv_temp) * 0.99; // 5.5u - } - else { - PIN(mot_temp) = r2temp(HV_R(ADC(adc_34_buf[1] >> 16))) * 0.01 + PIN(mot_temp) * 0.99; // 5.5u//MOT_R(MOT_REF(ADC(adc_34_buf[0] >> 16))); // 1.4u - } - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, PIN(led) > 0 ? GPIO_PIN_SET : GPIO_PIN_RESET); // 0.1u + // } + // else { + // PIN(mot_temp) = r2temp(HV_R(ADC(adc_34_buf[1] >> 16))) * 0.01 + PIN(mot_temp) * 0.99; // 5.5u//MOT_R(MOT_REF(ADC(adc_34_buf[0] >> 16))); // 1.4u + // } + //HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, PIN(led) > 0 ? GPIO_PIN_SET : GPIO_PIN_RESET); // 0.1u PIN(in0) = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_8); PIN(in1) = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9); diff --git a/stm32f303/src/comps/ppm.c b/stm32f303/src/comps/ppm.c new file mode 100644 index 00000000..e9c633dd --- /dev/null +++ b/stm32f303/src/comps/ppm.c @@ -0,0 +1,105 @@ +#include "commands.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" +#include "stm32f3xx_hal.h" + +HAL_COMP(ppm); + +HAL_PIN(pos); +HAL_PIN(a); +HAL_PIN(b); +HAL_PIN(cnt); +HAL_PIN(c1); +HAL_PIN(c2); +HAL_PIN(diff); +HAL_PIN(out); +HAL_PIN(zero); +HAL_PIN(max); +HAL_PIN(en); +HAL_PIN(gain); + +float timeout = 100; + +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct ppm_ctx_t * ctx = (struct ppm_ctx_t *)ctx_ptr; + struct ppm_pin_ctx_t * pins = (struct ppm_pin_ctx_t *)pin_ptr; + + __HAL_RCC_TIM2_CLK_ENABLE(); + + /**TIM1 GPIO Configuration + PB10,tx ------> TIM2_CH3 + PA11,rx ------> TIM2_CH4 + */ + GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + // TIM_encoder_InitTypeDef sConfig; + // TIM_MasterConfigTypeDef sMasterConfig; + TIM_HandleTypeDef htim2; + htim2.Instance = TIM2; + htim2.Init.Prescaler = 10; + htim2.Init.CounterMode = TIM_COUNTERMODE_UP; + htim2.Init.Period = 65535; + htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; + htim2.Init.RepetitionCounter = 0; + HAL_TIM_Base_Init(&htim2); + + TIM2->CCMR2 |= TIM_CCMR2_CC3S_0 | TIM_CCMR2_CC4S_1; + TIM2->CCER |= TIM_CCER_CC3E | TIM_CCER_CC4E | TIM_CCER_CC4P; + //TIM2->SMCR |= TIM_SMCR_TS_2 | TIM_SMCR_TS_0 | TIM_SMCR_SMS_2; + TIM2->CR1 |= TIM_CR1_CEN; + + PIN(zero) = 32125; + PIN(max) = 3350; + PIN(gain) = 20; +} +//diff: + +// 28956.000000 +// 32045.000000 +// 35317.000000 +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct ppm_ctx_t * ctx = (struct ppm_ctx_t *)ctx_ptr; + struct ppm_pin_ctx_t *pins = (struct ppm_pin_ctx_t *)pin_ptr; + PIN(cnt) = TIM2->CNT; + if((TIM2->SR & TIM_SR_CC3IF) && (TIM2->SR & TIM_SR_CC4IF)){ + PIN(c1) = TIM2->CCR3; + PIN(c2) = TIM2->CCR4; + float diff = PIN(c2) - PIN(c1); + if(diff < 0){ + diff += 65535; + } + if(diff > 28000 && diff < 36000){ + PIN(diff) = diff; + PIN(out) = ((diff-PIN(zero))/PIN(max))*PIN(gain); + timeout = 0; + PIN(en) = 1; + } + } + if(timeout > 0.5){ + PIN(en) = 0; + PIN(out) = 0; + } + timeout += 0.0001; +} + +hal_comp_t ppm_comp_struct = { + .name = "ppm", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct ppm_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/stm32f303/src/main.c b/stm32f303/src/main.c index c364b909..2f400739 100644 --- a/stm32f303/src/main.c +++ b/stm32f303/src/main.c @@ -227,10 +227,10 @@ int main(void) { load_comp(comp_by_name("hv")); load_comp(comp_by_name("uvw")); load_comp(comp_by_name("curpid")); - load_comp(comp_by_name("uart")); + load_comp(comp_by_name("ppm")); hal_parse("term0.rt_prio = 0.1"); - hal_parse("uart0.rt_prio = 0.2"); + hal_parse("ppm0.rt_prio = 0.2"); hal_parse("ls0.rt_prio = 0.6"); hal_parse("io0.rt_prio = 1.0"); @@ -251,7 +251,8 @@ int main(void) { hal_parse("term0.gain6 = 10.0"); hal_parse("term0.gain7 = 10.0"); - hal_parse("curpid0.max_cur = 100.0"); + hal_parse("curpid0.max_cur = 80.0"); + hal_parse("ppm0.gain = 80.0"); //link LS hal_parse("ls0.mot_temp = io0.mot_temp"); @@ -324,9 +325,9 @@ int main(void) { hal_parse("idq0.mode = 2.0"); hal_parse("dq0.mode = 2.0"); - hal_parse("curpid0.iq_cmd = uart0.current"); - hal_parse("hv0.en = uart0.en"); - hal_parse("curpid0.en = uart0.en"); + hal_parse("curpid0.iq_cmd = ppm0.out"); + hal_parse("hv0.en = ppm0.en"); + hal_parse("curpid0.en = ppm0.en"); hal_parse("uart0.rpm = uvw0.rpm"); hal_parse("uart0.iabs = curpid0.iq_fb"); diff --git a/toolchain.mak b/toolchain.mak index ba0f8f1d..e0c4504e 100644 --- a/toolchain.mak +++ b/toolchain.mak @@ -1,5 +1,5 @@ # Define programs and commands for the toolchain -TOOLCHAIN = /home/niklas/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi- +TOOLCHAIN = arm-none-eabi- CC = $(TOOLCHAIN)gcc OBJCOPY = $(TOOLCHAIN)objcopy OBJDUMP = $(TOOLCHAIN)objdump