This commit is contained in:
crinq
2014-09-21 00:35:02 +02:00
6 changed files with 84 additions and 34 deletions
+1
View File
@@ -4,6 +4,7 @@
*.elf
*.hex
*.bin
*.map
.DS_Store
term/build
term/win32/bin
+1 -1
View File
@@ -65,7 +65,7 @@ proj: $(PROJ_NAME).elf
$(PROJ_NAME).elf: $(SRCS)
$(CC) $(CFLAGS) $^ -o $@ -Llib -lstm32f4
$(CC) $(CFLAGS) $^ -o $@ -Llib -lstm32f4 -Wl,-Map -Wl,$(PROJ_NAME).map
$(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin
+61 -19
View File
@@ -45,6 +45,9 @@ volatile float res_pos2;//winkel vom resolver, -pi bsi +pi
volatile int amp1,amp2;//betrag
volatile int erreger = 0;//resolver erreger pin an/aus
volatile int erreger_enable = NO;//erreger aktiv
volatile float w = -1;
volatile int k = 0,l = 0;
volatile int data[10][2][2];
float minus(float a, float b){
if(ABS(a - b) < pi){
@@ -74,7 +77,8 @@ float get_enc_pos(){
}
float get_res_pos(){
return((res_pos2 + res_pos1) / 2 - res_offset);//TODO: avg funktion bauen und nutzen
//return((res_pos2 + res_pos1) / 2 - res_offset);//TODO: avg funktion bauen und nutzen
return (MIN(res_pos1, res_pos2) + MIN(ABS(minus(res_pos1,res_pos2)), ABS(minus(res_pos2,res_pos1))) / 2) - res_offset;
}
void output_ac_pwm(){
@@ -97,10 +101,8 @@ void output_dc_pwm(){
}
void TIM2_IRQHandler(void){ //20KHz
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
GPIO_SetBits(GPIOC,GPIO_Pin_4);//messpin
ADC_SoftwareStartConv(ADC1);
ADC_SoftwareStartConv(ADC2);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//GPIO_SetBits(GPIOC,GPIO_Pin_4);//messpin
}
void ADC_IRQHandler(void) // 20khz
@@ -118,10 +120,28 @@ void ADC_IRQHandler(void) // 20khz
GPIO_SetBits(GPIOC,GPIO_Pin_2);//erreger
res_pos1 = atan2f(t1-t1_mid, t2-t2_mid);
amp1 = (t1-t1_mid)*(t1-t1_mid)+(t2-t2_mid)*(t2-t2_mid);
if(w >= 0){
data[k][0][0] = t1 - t1_mid;
data[k][0][1] = t2 - t2_mid;
k++;
if(k == 10){
w = -1;
k = 0;
}
}
}else{//andere halbwelle
GPIO_ResetBits(GPIOC,GPIO_Pin_2);//erreger
res_pos2 = atan2f(t1_mid-t1, t2_mid-t2);
amp2 = (t1_mid-t1)*(t1_mid-t1)+(t2_mid-t2)*(t2_mid-t2);
if(w >= 0){
data[l][1][0] = t1_mid - t1;
data[l][1][1] = t2_mid - t2;
l++;
if(l == 10){
w = -1;
l = 0;
}
}
}
}else{//mittelpunkt messen
if(t1_mid == 0 && t2_mid == 0){//erster durchlauf
@@ -140,9 +160,16 @@ void ADC_IRQHandler(void) // 20khz
void TIM5_IRQHandler(void){ //1KHz
TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
float ist = get_enc_pos();
soll_pos = MIN(res_pos1, res_pos2) + MIN(ABS(minus(res_pos1,res_pos2)), ABS(minus(res_pos2,res_pos1))) / 2;
//soll_pos += DEG(0.36*1);// u/min
float ist = 0;//get_res_pos();
for(int i = 0;i<10;i++){
ist += atan2f(data[i][0][0], data[i][0][1]) * 0.05;
}
for(int i = 0;i<10;i++){
ist += atan2f(data[i][1][0], data[i][1][1]) * 0.05;
}
soll_pos = get_enc_pos();//MIN(res_pos1, res_pos2) + MIN(ABS(minus(res_pos1,res_pos2)), ABS(minus(res_pos2,res_pos1))) / 2;
//soll_pos += DEG(0.36*1);// u/sec
//soll_pos = mod(soll_pos);
pid.feedback = minus(ist,soll_pos);
@@ -152,11 +179,12 @@ void TIM5_IRQHandler(void){ //1KHz
calc_pid(&pid,1);
voltage_scale = pid.output;
//if(amp1 < 1000000 || amp2 < 1000000){
// voltage_scale = 0.0;
//}
if(amp1 < 1000000 || amp2 < 1000000){
voltage_scale = 0.0;
}
output_dc_pwm();
output_ac_pwm();
w=0;
}
int main(void)
@@ -166,13 +194,14 @@ int main(void)
register_float('p',&pid.pgain);
register_float('i',&pid.igain);
register_float('d',&pid.dgain);
register_float('w',&w);
GPIO_ResetBits(GPIOC,GPIO_Pin_2);//reset erreger
Wait(10);
TIM_Cmd(TIM2, ENABLE);//int
Wait(50);
erreger_enable = YES;
Wait(50);
soll_pos = get_enc_pos();
soll_pos = get_res_pos();
TIM_Cmd(TIM4, ENABLE);//PWM
TIM_Cmd(TIM5, ENABLE);//PID
@@ -195,21 +224,34 @@ int main(void)
#ifdef USBTERM
if(UB_USB_CDC_GetStatus()==USB_CDC_CONNECTED){
//UB_USB_CDC_SendString(buf, NONE);//schleppfehler senden
char name[APP_TX_BUF_SIZE];
UB_USB_CDC_SendString(buf, NONE);//schleppfehler senden
/*
if(w == -1){
w = -2;
printf_("pos:\n");
for(int i = 0;i<10;i++){
printf_("%i %i\n",data[i][0][0], data[i][0][1]);
}
printf_("neg:\n");
for(int i = 0;i<10;i++){
printf_("%i %i\n",data[i][1][0], data[i][1][1]);
}
}
*/
char name[APP_TX_BUF_SIZE];
float value = 0;
//int i = scanf_("%s = %f",name,&value);
int i = scanf_("%s = %f",name,&value);
//if(i != -1){
// printf_("scanf: %i value: %f name: %s\n",i,value,name);
//}
// printf_("scanf: %i value: %f name: %s\n",i,value,name);
//}
switch(i){
case 2:
if(is_param(name[0]))
printf_("%s=%f\n",name,get_float(name[0]));
else
else{
printf_("not found\n");
}
break;
case 5:
if(is_param(name[0])){
+4
View File
@@ -81,10 +81,14 @@ int vfsscanf_(const char *buf, const char *format, va_list arg){
switch(format[format_pos]){
case '%':
state = 1;
format_pos++;
continue;
break;
case '\'':
state = 2;
format_pos++;
continue;
break;
case ' ': // parse whithespaces
+14 -11
View File
@@ -21,7 +21,7 @@ void setup(){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//wird in UB_USB_CDC_Init() nochmal gesetzt!
//res erreger
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
@@ -57,15 +57,16 @@ void setup(){
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);//DAC?
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* int NVIC setup */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
setup_pwm();
setup_adc();
setup_pid_timer();
@@ -77,7 +78,8 @@ void setup(){
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
//systick prio
//NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
NVIC_SetPriority(SysTick_IRQn, 14);
pid_init(&pid);
@@ -154,8 +156,8 @@ void setup_pid_timer(){
/* int NVIC setup */
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
}
@@ -182,7 +184,8 @@ void setup_adc(){
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//no trigger for conversion
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;//trigger on rising edge of TIM2
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
ADC_InitStructure.ADC_NbrOfConversion = 1;//I think this one is clear :p
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel
ADC_Init(ADC1,&ADC_InitStructure);//Initialize ADC with the previous configuration
@@ -211,8 +214,8 @@ void setup_adc(){
// analog NVIC
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
+3 -3
View File
@@ -58,12 +58,12 @@ void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}