// the control loop changes the dac:
static void control_loop(void){
int16_t tmp;
- int8_t ptmp=0;
tmp=target_val[0] - analog_result[0]; // current diff
if (tmp <0){
+ // ** current control:
+ //
// stay in currnet control if we are
// close to the target. We never regulate
// the difference down to zero otherweise
// and then back to current control. Permanent
// hopping would lead to oscillation and current
// spikes.
- if (tmp>-4) tmp=0;
- currentcontrol=40; // I control
+ if (tmp>-2) tmp=0;
+ currentcontrol=10; // I control
if (analog_result[1]>target_val[1]){
+ // oh, voltage too high, get out of current control:
tmp=-20;
currentcontrol=0; // U control
}
}else{
+ // ** voltage control:
+ //
// if we are in current control then we can only go
// down (tmp is negative). To increase the current
// we come here to voltage control. We must slowly
// count up.
- tmp=1 + target_val[1] - analog_result[1]; // voltage diff
- //
+ tmp=1+ target_val[1] - analog_result[1]; // voltage diff
if (currentcontrol){
currentcontrol--;
- if (currentcontrol%8==0){
- // slowly up, 20 will become 1 further down
- if (tmp>0) tmp=20;
- }else{
- tmp=0;
- }
+ // do not go up immediately after we came out of current control:
+ if (tmp>0) tmp=0;
}
}
- if (tmp==0){
- return; // nothing to change
- }
- if (tmp> -5 && tmp<5){ // avoid LSB bouncing if we are close
- if (tmp>0){
- ptmp++;
- tmp=0;
- if (ptmp>1){
- tmp=1;
- ptmp=0;
- }
- }
- if (tmp<0){
- ptmp--;
- tmp=0;
- if (ptmp<-1){
- tmp=-1;
- ptmp=0;
- }
- }
+ if (tmp> -3 && tmp<4){ // avoid LSB bouncing if we are close
+ tmp=0;
}
+ if (tmp==0) return; // nothing to change
// put a cap on increase
if (tmp>1){
tmp=1;
}
// put a cap on decrease
- if (tmp<-1){
+ if (tmp<-200){
+ tmp=-20;
+ }else if (tmp<-1){
tmp=-1;
}
dac_val+=tmp;