1
Fork 0

Compare commits

...

2 commits

Author SHA1 Message Date
cbf38e4fd6
slow down on turns 2024-10-25 13:29:23 -04:00
ba1dee7913
stop on line loss 2024-10-25 13:19:19 -04:00
4 changed files with 54 additions and 6 deletions

View file

@ -2,6 +2,9 @@
#define DARK_THRESHOLD 500; #define DARK_THRESHOLD 500;
#define LINE_THRESHOLD 200;
#define INTERSECTION_THRESHOLD 500;
void LineSensor::Initialize(void) void LineSensor::Initialize(void)
{ {
for (int i = 0; i<sensorCount; i++) { for (int i = 0; i<sensorCount; i++) {
@ -9,6 +12,20 @@ void LineSensor::Initialize(void)
} }
} }
float LineSensor::AverageReflectance(void)
{
float sum = 0;
for (int i = 0; i<sensorCount; i++) {
int ret = analogRead(sensors[i]);
sum += ret;
}
return sum/sensorCount;
}
bool LineSensor::LineDetected() {
return AverageReflectance() > LINE_THRESHOLD;
}
float LineSensor::CalcError(void) float LineSensor::CalcError(void)
{ {
float sum_pos = 0; float sum_pos = 0;

View file

@ -12,9 +12,12 @@ protected:
bool prevOnIntersection = false; bool prevOnIntersection = false;
float AverageReflectance();
public: public:
LineSensor(void) {} LineSensor(void) {}
void Initialize(void); void Initialize(void);
float CalcError(void); // varies between 1 and 6 float CalcError(void); // varies between 1 and 6
bool CheckIntersection(void); bool CheckIntersection(void);
bool LineDetected(void);
}; };

View file

@ -97,16 +97,40 @@ void Robot::EnterLineFollowing(float speed)
robotState = ROBOT_LINING; robotState = ROBOT_LINING;
} }
int lineLostFrames = 7;
void Robot::LineFollowingUpdate(void) void Robot::LineFollowingUpdate(void)
{ {
if(robotState == ROBOT_LINING) if(robotState == ROBOT_LINING)
{ {
// TODO: calculate the error in CalcError(), calc the effort, and update the motion float setpoint = 3.5;
float lineError = 3.5 - lineSensor.CalcError();
Serial.println(lineError);
float turnEffort = lineError * lining_kP;
chassis.SetTwist(baseSpeed, turnEffort); float lineError = setpoint - lineSensor.CalcError();
float powErr = (lineError*lineError);
powErr = (lineError < 0 ? -powErr : powErr);
float turnEffort = powErr * lining_kP;
rollingTurnRate = rollingTurnRate * turnRateDamp + turnEffort * (1-turnRateDamp);
float speed = baseSpeed;
speed *= 1 - (abs(rollingTurnRate) * KTurnRate);
if (!lineSensor.LineDetected()) {
lineLostFrames -= 1;
if (lineLostFrames < 0) {
rollingTurnRate = 0;
turnEffort = 0;
speed = 0;
}
}
else {
lineLostFrames = 7;
}
chassis.SetTwist(speed, turnEffort);
} }
} }

View file

@ -3,7 +3,7 @@
#include <LineSensor.h> #include <LineSensor.h>
#include <LSM6.h> #include <LSM6.h>
static float lining_kP = 1.0; static float lining_kP = 1.24;
class Robot class Robot
{ {
@ -57,6 +57,10 @@ protected:
/* baseSpeed is used to drive at a given speed while, say, line following.*/ /* baseSpeed is used to drive at a given speed while, say, line following.*/
float baseSpeed = 0; float baseSpeed = 0;
float rollingTurnRate = 0;
float turnRateDamp = 0.96; // the proportion of the turn rate retained each loop
float KTurnRate = 0.25; // slowdown coefficient
/** /**
* For tracking the motion of the Romi. We keep track of the intersection we came * For tracking the motion of the Romi. We keep track of the intersection we came
* from and the one we're headed to. You'll program in the map in handleIntersection() * from and the one we're headed to. You'll program in the map in handleIntersection()