Skip to content

Instantly share code, notes, and snippets.

@ivanseidel
Created July 8, 2017 01:51
Show Gist options
  • Save ivanseidel/bcea534d5d7b2d07398699bdb50223ed to your computer and use it in GitHub Desktop.
Save ivanseidel/bcea534d5d7b2d07398699bdb50223ed to your computer and use it in GitHub Desktop.
Mark based Line Following algorithm
struct Mark {
float speed;
float acceleration;
uint16_t duration;
};
Mark marks[] = {
{100.0, 100, 300},
{60.0, 100, 100},
{60.0, 100, 100},
{60.0, 100, 100},
{60.0, 100, 100},
{60.0, 100, 100},
{0.0, 200, 10000},
// Aqui
};
// Accelerate Speed
// float targetSpeed = 0;
float currentSpeed = 0;
float speedAcceleration = 100;
int currentMark = 0;
uint16_t position = 0;
uint16_t lastMark = 0;
float lastT = 0
void loop() {
// Save dt
float now = Timer.read()
float dt = now - lastT;
lastT = now
if (dt > 0.005) {
flashLed();
return;
}
position = readEncoder();
// Get current mark
Mark mark = marks[currentMark];
// Check if changed mark
if (position - lastMark > mark.duration) {
lastMark += mark.duration;
currentMark++;
mark = marks[currentMark];
}
// Base speed to set
float targetSpeed = mark.speed;
float targetDelta = (targetSpeed - currentSpeed);
float speedDelta = speedAcceleration * dt;
// Revert target if lt 0
if (targetDelta < 0)
speedDelta = max(-speedDelta, targetDelta);
else
speedDelta = min( speedDelta, targetDelta);
currentSpeed += speedDelta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment