} else {
// we are outside, now roll to get inside
motorRightSpeedRpmSet = -motorSpeedMaxRpm / 1.5;
motorLeftSpeedRpmSet = motorSpeedMaxRpm / 1.5;
}
t 13 l2868 v0 PFND spd 16 -16 0 sen 0 0 0 bum 0 0 dro 0 0 son 0 yaw 0 pit 0 rol 0 per 3 bat 12.4 chg 0.0 0.0 imu 0 adc116 Ardumower
t 14 l2326 v0 PFND spd 16 -16 0 sen 0 0 0 bum 0 0 dro 0 0 son 0 yaw 0 pit 0 rol 0 per 3 bat 12.4 chg 0.0 0.0 imu 0 adc116 Ardumower
t 15 l2203 v0 PFND spd 16 -16 0 sen 0 0 0 bum 0 0 dro 0 0 son 0 yaw 0 pit 0 rol 0 per 3 bat 12.4 chg 0.0 0.0 imu 0 adc116 Ardumower
t 0 l2130 v0 PTRK spd 16 -16 0 sen 0 0 0 bum 0 0 dro 0 0 son 0 yaw 0 pit 0 rol 0 per 4 bat 12.4 chg 0.0 0.0 imu 0 adc 49 Ardumower
t 0 l2130 v0 PTRK spd 16 -16 0 sen 0 0 0 bum 0 0 dro 0 0 son 0 yaw 0 pit 0 rol 0 per 4 bat 12.4 chg 0.0 0.0 imu 0 adc 57 Ardumower
t 1 l2543 v0 PTRK spd 16 -16 0 sen 0 0 0 bum 0 0 dro 0 0 son 0 yaw 0 pit 0 rol 0 per 4 bat 12.4 chg 0.0 0.0 imu 0 adc117 Ardumower
as the code looks correct for me ...
// PID controller: track perimeter
void Robot::motorControlPerimeter(){
if (millis() < nextTimeMotorPerimeterControl) return;
nextTimeMotorPerimeterControl = millis() + 100;
if ((millis() > stateStartTime + 5000) && (millis() > perimeterLastTransitionTime + trackingPerimeterTransitionTimeOut)){
// robot is wheel-spinning while tracking => roll to get ground again
if (trackingBlockInnerWheelWhilePerimeterStruggling == 0){
if (perimeterMag < 0) setMotorPWM( -motorSpeedMaxPwm/1.5, motorSpeedMaxPwm/1.5, false);
else setMotorPWM( motorSpeedMaxPwm/1.5, -motorSpeedMaxPwm/1.5, false);}
else if (trackingBlockInnerWheelWhilePerimeterStruggling == 1){
if (perimeterMag < 0) setMotorPWM( 0, motorSpeedMaxPwm/1.5, false);
else setMotorPWM( motorSpeedMaxPwm/1.5, 0, false);
}
if (millis() > perimeterLastTransitionTime + trackingErrorTimeOut){
Console.println("Error: tracking error");
addErrorCounter(ERR_TRACKING);
//setNextState(STATE_ERROR,0);
setNextState(STATE_PERI_FIND,0);
}
return;
}
if (perimeterMag < 0) perimeterPID.x = -1;
else if (perimeterMag > 0) perimeterPID.x = 1;
else perimeterPID.x = 0;
perimeterPID.w = 0;
perimeterPID.y_min = -motorSpeedMaxPwm;
perimeterPID.y_max = motorSpeedMaxPwm;
perimeterPID.max_output = motorSpeedMaxPwm;
perimeterPID.compute();
//setMotorPWM( motorLeftPWMCurr +perimeterPID.y,
// motorRightPWMCurr -perimeterPID.y, false);
setMotorPWM( max(-motorSpeedMaxPwm, min(motorSpeedMaxPwm, motorSpeedMaxPwm/2 - perimeterPID.y)),
max(-motorSpeedMaxPwm, min(motorSpeedMaxPwm, motorSpeedMaxPwm/2 + perimeterPID.y)), false);
Console.print(perimeterPID.x);
Console.print("t");
Console.println(perimeterPID.y);
}
//JEMI TRIED HIS BEST
void Robot::setMotorPWM(int pwmLeft, int pwmRight, boolean useAccel){
unsigned long TaC = millis() - lastSetMotorSpeedTime; // sampling time in millis
lastSetMotorSpeedTime = millis();
if (TaC > 1000) TaC = 1;
if (useAccel){
// [URL]http://phrogz.net/js/framerate-independent-low-pass-filter.html[/URL]
// smoothed += elapsedTime * ( newValue - smoothed ) / smoothing;
motorLeftPWMCurr += TaC * (pwmLeft - motorLeftPWMCurr) / motorAccel;
motorRightPWMCurr += TaC * (pwmRight - motorRightPWMCurr) / motorAccel;
}
// ----- driver protection (avoids driver explosion) ----------
if ( ((pwmLeft < 0) && (motorLeftPWMCurr >= 0)) ||
((pwmLeft > 0) && (motorLeftPWMCurr <= 0)) ) { // changing direction should take place?
if (motorLeftZeroTimeout != 0)
pwmLeft = motorLeftPWMCurr - motorLeftPWMCurr * ((float)TaC)/200.0; // reduce speed
}
if ( ((pwmRight < 0) && (motorRightPWMCurr >= 0)) ||
((pwmRight > 0) && (motorRightPWMCurr <= 0)) ) { // changing direction should take place?
if (motorRightZeroTimeout != 0) // reduce motor rotation? (will reduce EMF)
pwmRight = motorRightPWMCurr - motorRightPWMCurr * ((float)TaC)/200.0; // reduce speed
}
if (odometryUse){
if (abs(motorLeftRpmCurr) <1) motorLeftZeroTimeout = max(0, ((int)(motorLeftZeroTimeout - TaC)) );
else motorLeftZeroTimeout = 500;
if (abs(motorRightRpmCurr) <1) motorRightZeroTimeout = max(0, ((int)(motorRightZeroTimeout - TaC)) );
else motorRightZeroTimeout = 500;
} else {
if (pwmLeft == 0) motorLeftZeroTimeout = max(0, ((int)(motorLeftZeroTimeout - TaC)) );
else motorLeftZeroTimeout = 700;
if (pwmRight == 0) motorRightZeroTimeout = max(0, ((int)(motorRightZeroTimeout - TaC)) );
else motorRightZeroTimeout = 700;
}
motorLeftPWMCurr = pwmLeft;
motorRightPWMCurr = pwmRight;
// ---------------------------------
if (motorLeftSwapDir) // swap pin polarity?
setActuator(ACT_MOTOR_LEFT, -motorLeftPWMCurr);
else
setActuator(ACT_MOTOR_LEFT, motorLeftPWMCurr);
if (motorRightSwapDir) // swap pin polarity?
setActuator(ACT_MOTOR_RIGHT, -motorRightPWMCurr);
else
setActuator(ACT_MOTOR_RIGHT, motorRightPWMCurr);
}
//DEVELOPER FOLDER
void Robot::setMotorPWM(int pwmLeft, int pwmRight, boolean useAccel){
unsigned long TaC = millis() - lastSetMotorSpeedTime; // sampling time in millis
lastSetMotorSpeedTime = millis();
if (TaC > 1000) TaC = 1;
if (useAccel){
// [URL]http://phrogz.net/js/framerate-independent-low-pass-filter.html[/URL]
// smoothed += elapsedTime * ( newValue - smoothed ) / smoothing;
motorLeftPWMCurr += TaC * (pwmLeft - motorLeftPWMCurr) / motorAccel;
motorRightPWMCurr += TaC * (pwmRight - motorRightPWMCurr) / motorAccel;
}
// ----- driver protection (avoids driver explosion) ----------
if ( ((pwmLeft < 0) && (motorLeftPWMCurr >= 0)) ||
((pwmLeft > 0) && (motorLeftPWMCurr <= 0)) ) { // changing direction should take place?
if (motorLeftZeroTimeout != 0)
pwmLeft = motorLeftPWMCurr - motorLeftPWMCurr * ((float)TaC)/200.0; // reduce speed
}
if ( ((pwmRight < 0) && (motorRightPWMCurr >= 0)) ||
((pwmRight > 0) && (motorRightPWMCurr <= 0)) ) { // changing direction should take place?
if (motorRightZeroTimeout != 0) // reduce motor rotation? (will reduce EMF)
pwmRight = motorRightPWMCurr - motorRightPWMCurr * ((float)TaC)/200.0; // reduce speed
}
motorLeftPWMCurr = pwmLeft;
motorRightPWMCurr = pwmRight;
if (odometryUse){
if (abs(motorLeftRpmCurr) <1) motorLeftZeroTimeout = max(0, ((int)(motorLeftZeroTimeout - TaC)) );
else motorLeftZeroTimeout = 500;
if (abs(motorRightRpmCurr) <1) motorRightZeroTimeout = max(0, ((int)(motorRightZeroTimeout - TaC)) );
else motorRightZeroTimeout = 500;
} else {
if (pwmLeft == 0) motorLeftZeroTimeout = max(0, ((int)(motorLeftZeroTimeout - TaC)) );
else motorLeftZeroTimeout = 700;
if (pwmRight == 0) motorRightZeroTimeout = max(0, ((int)(motorRightZeroTimeout - TaC)) );
else motorRightZeroTimeout = 700;
}
// ---------------------------------
if (motorLeftSwapDir) // swap pin polarity?
setActuator(ACT_MOTOR_LEFT, -motorLeftPWMCurr);
else
setActuator(ACT_MOTOR_LEFT, motorLeftPWMCurr);
if (motorRightSwapDir) // swap pin polarity?
setActuator(ACT_MOTOR_RIGHT, -motorRightPWMCurr);
else
setActuator(ACT_MOTOR_RIGHT, motorRightPWMCurr);
}