37#define DBGSERIAL Serial
54PID wpid(&
win, &
wout, &
wset, 1.0, 0.01, 0.01, DIRECT);
55PID apid(&
ain, &
aout, &
aset, 0.5, 0.05, 0.02, DIRECT);
70#define HANDOFFW ((TARGW * 5) / 10)
78 ServoPD(
long Kp,
long Kd,
long zero,
long range,
long scalebits = 8) :
79 itsKp(Kp), itsKd(Kd), itsPos(zero << scalebits),
80 itsPrevTarget(zero << scalebits), itsZero(zero << scalebits),
81 itsRange(range << scalebits), itsScaleBits(scalebits)
87 itsPos = (pos << itsScaleBits);
93 return (itsPos >> itsScaleBits);
98 targetpos <<= itsScaleBits;
99 long diff = itsKp * targetpos + itsKd * (targetpos - itsPrevTarget);
100 itsPos += (diff >> 16);
101 itsPos = constrain(itsPos, itsZero - itsRange, itsZero + itsRange);
102 itsServo.write(itsPos >> itsScaleBits);
103 itsPrevTarget = targetpos;
108 targetpos <<= itsScaleBits;
109 itsPos = constrain(itsPos, itsZero - itsRange, itsZero + itsRange);
110 itsPrevTarget = targetpos;
115 return itsPos >> itsScaleBits;
120 itsPos = rawval << itsScaleBits;
121 itsServo.write(rawval);
126 long itsKp, itsKd, itsPos, itsPrevTarget, itsZero, itsRange, itsScaleBits;
155 wpid.SetMode(AUTOMATIC);
156 wpid.SetSampleTime(10);
157 wpid.SetOutputLimits(-120, 120);
160 apid.SetMode(AUTOMATIC);
161 apid.SetSampleTime(10);
162 apid.SetOutputLimits(-50, 50);
171 char * tok = strtok(
instr,
" \r\n");
172 int state = 0;
int id, targx, targy, targw, targh;
187 case 0:
if (strcmp(tok,
"N2") == 0) state = 1;
else state = 1000;
break;
188 case 1:
id = atoi(&tok[1]); state = 2;
break;
189 case 2: targx = atoi(tok); state = 3;
break;
190 case 3: targy = atoi(tok); state = 4;
break;
191 case 4: targw = atoi(tok); state = 5;
break;
192 case 5: targh = atoi(tok); state = 6;
break;
195 tok = strtok(0,
" \r\n");
260 if (left > 100) left = 100;
else if (left < -100) left = -100;
261 if (right > 100) right = 100;
else if (right < -100) right = -100;
263 long motleft = (left *
leftgain) >> 8;
264 long motright = (right *
rightgain) >> 8;
270 }
else if (motleft < 0) {
273 analogWrite(
LPWMPIN, -motleft);
282 analogWrite(
RPWMPIN, motright);
283 }
else if (motright < 0) {
286 analogWrite(
RPWMPIN, -motright);
void motor(long left, long right)
ServoPD tiltservo(300, 100, TILTZERO, TILTRANGE)
ServoPD panservo(100, 50, PANZERO, PANRANGE)
void update(long targetpos)
void attach(int pin, int pos)
void reset(long targetpos)
ServoPD(long Kp, long Kd, long zero, long range, long scalebits=8)