AiStarter User Guide 6AI-Starter Demo
IssueV1.0.3(2018-09-01) AiStarter User Guide Copyright © Yuejiang Technology Co., Ltd
20
//calculate the car position offset
for (int i = 0; i < IR_NUM; i++) {
if (irstate & (1 << i)) {
total += irPos[i];
irOffCnt++;
}
}
if (irOffCnt) {
readPos = total / irOffCnt;
}
else {
readPos = lastPos;
}
//calculate the current position
curPos = (1 - coeff) * lastPos + coeff * readPos;
lastPos = curPos;
return curPos;
}
3) Set AI-Starter speed.
Program 6.3 Set AI-Starter speed
void setCarSpeed(const float curPos)
{
const int baseSpeed = 50; //rpm
const float kp = 1;
const float ki = 0.06;
const float kd = 0.0;
const float errorsumLimit = 50;
float error = curPos;
static float lastError;
static float errorsum;
float errorChange;
int speedLeftWheel;
int speedRightWheel;