Skip to content

Commit

Permalink
Add arg checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredliw committed Sep 16, 2021
1 parent 954c6f8 commit d12731f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
52 changes: 29 additions & 23 deletions src/PikaBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,24 @@ PikaBot::PikaBot(void)

void PikaBot::_delayMotors(int speed, double distance)
{
if (distance > 0)
{
delay((int) round((double) distance / this->_distanceRef * speed / this->_speedRef * this->_delayRef));
this->stop();
}
if (not this->_isBetween(speed, 0, 255)) return;
if (distance <= 0) return;

delay((int) round((double) distance / this->_distanceRef * speed / this->_speedRef * this->_delayRef));
this->stop();
}

bool PikaBot::_isBetween(int value, int lowerBound, int upperBound)
{
return lowerBound <= value and value <= upperBound;
}

void PikaBot::calibrateMotors(int speed, int delay, int distance)
{
if (not this->_isBetween(speed, 0, 255)) return;
if (delay <= 0) return;
if (distance <= 0) return;

this->_speedRef = speed;
this->_delayRef = delay;
this->_distanceRef = distance;
Expand Down Expand Up @@ -119,6 +128,8 @@ bool PikaBot::isPressed()

void PikaBot::lineFollow(int speed)
{
if (not this->_isBetween(speed, 0, 255)) return;

if (this->detectLine(LEFTIR) && this->detectLine(RIGHTIR))
{
this->stop();
Expand All @@ -140,22 +151,8 @@ void PikaBot::lineFollow(int speed)
void PikaBot::move(int leftSpeed, int rightSpeed)
{
while (this->lastMotorInstructionTime != 0 and millis() < lastMotorInstructionTime + DELAY) {}
if (leftSpeed > 255)
{
leftSpeed = 255;
}
else if (leftSpeed < -255)
{
leftSpeed = -255;
}
if (rightSpeed > 255)
{
rightSpeed = 255;
}
else if (rightSpeed < -255)
{
rightSpeed = -255;
}

if (not this->_isBetween(leftSpeed, -255, 255) or not this->_isBetween(rightSpeed, -255, 255)) return;

if (leftSpeed == 0)
{
Expand Down Expand Up @@ -189,6 +186,8 @@ void PikaBot::move(int leftSpeed, int rightSpeed)

void PikaBot::moveBackward(int speed)
{
if (not this->_isBetween(speed, 0, 255)) return;

this->move(-speed, -speed);
}

Expand All @@ -200,6 +199,8 @@ void PikaBot::moveBackward(int speed, int distance)

void PikaBot::moveForward(int speed)
{
if (not this->_isBetween(speed, 0, 255)) return;

this->move(speed, speed);
}

Expand Down Expand Up @@ -258,11 +259,14 @@ void PikaBot::play(String musicSheet)

void PikaBot::playTone(String pitch, int note)
{
if (not this->_isBetween(note, 1, 256)) return;

this->playToneMs(pitch, round(240000. / this->tempo / note));
}

void PikaBot::playToneMs(String pitch, long duration)
{
// Negative duration = forever
int freq = pitchToFreq(pitch);
tone(BUZZER, freq, duration);
delay(duration * 1.3);
Expand All @@ -281,6 +285,7 @@ void PikaBot::stop()

void PikaBot::turnLeft(int speed)
{
if (not this->_isBetween(speed, 0, 255)) return;
this->move(speed, 0);
}

Expand All @@ -292,6 +297,7 @@ void PikaBot::turnLeft(int speed, int angle)

void PikaBot::turnRight(int speed)
{
if (not this->_isBetween(speed, 0, 255)) return;
this->move(0, speed);
}

Expand All @@ -301,14 +307,14 @@ void PikaBot::turnRight(int speed, int angle)
this->_delayMotors(speed, 2 * PI * WIDTH * angle / 360);
}

int PikaBot::ultrasonicDistance()
unsigned int PikaBot::ultrasonicDistance()
{
digitalWrite(ULTRASONIC_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG, LOW);
int distance = pulseIn(ULTRASONIC_ECHO, HIGH) * 0.017;
unsigned int distance = pulseIn(ULTRASONIC_ECHO, HIGH) * 0.017;
if (distance > 2000)
{
return 0;
Expand Down
3 changes: 2 additions & 1 deletion src/PikaBot.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ class PikaBot
void turnLeft(int speed, int angle);
void turnRight(int speed);
void turnRight(int speed, int angle);
int ultrasonicDistance();
unsigned int ultrasonicDistance();

private:
int _delayRef = 500;
int _distanceRef = 25;
int _speedRef = 255;
unsigned long lastMotorInstructionTime = 0;
void _delayMotors(int speed, double distance);
bool _isBetween(int value, int lowerBound, int upperBound);
};

#endif

0 comments on commit d12731f

Please sign in to comment.