Skip to content

Commit

Permalink
Cleaned up functions and added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoalcaraz committed Dec 13, 2017
1 parent c20e191 commit 9d68423
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 196 deletions.
63 changes: 9 additions & 54 deletions LineBot.ino
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ void setup() {
delay(500);
digitalWrite(LED, LOW);
delay(500);
// Serial.begin(9600);
// while(!Serial);

}

Expand All @@ -115,19 +113,8 @@ void loop() {
if( isIntersection() ) {
stop();
delay(1000);
// Serial.print("Current Dir: ");Serial.println(virtBot.dir);
// Serial.print("Current turn: ");Serial.println(virtBot.turn);
// Serial.print("Current row: ");Serial.println(virtBot.coord.row,HEX);
// Serial.print("Current col: ");Serial.println(virtBot.coord.col,HEX);
// Serial.print("Current Room: ");Serial.println(virtBot.room, HEX);
// Serial.println("Taking a step");
virtBot = enterRoom(virtBot);
// Serial.print("Row: ");Serial.println(virtBot.coord.row,HEX);
// Serial.print("Col: ");Serial.println(virtBot.coord.col,HEX);
// Serial.print("Room: ");Serial.println(virtBot.room, HEX);
char choice = whichWay(virtBot);
// Serial.print("Next choice: ");Serial.println(choice);
// Serial.println("");
switch (choice) {
case 'S':
while(IR_data[frontRightData] > 500 && IR_data[frontLeftData] > 500) lineFollow();
Expand All @@ -149,55 +136,21 @@ void loop() {
lineFollow();
}

/**Check if the bot is on an intersection or not
* Checks the IR data array which is available globally
* INPUTS: None
* OUTPUTS: None
*/
boolean isIntersection() {
if(IR_data[frontRightData] > 500 && IR_data[frontLeftData] > 500) {
return true;
}
return false;
}
/**Function to allow line following for the bot
*
*/
void lineFollow() {
static uint8_t prevState = 0;
uint8_t newState;
if( IR_data[frontLeftData] > 500 && IR_data[frontRightData] > 500 ) {
newState = prevState;
} else if( IR_data[frontLeftData] < 500 && IR_data[frontRightData] < 500 ) {
newState = 2;
} else if( IR_data[frontLeftData] > 500 && IR_data[frontRightData] < 500 ) {
newState = 3;
} else if( IR_data[frontLeftData] < 500 && IR_data[frontRightData] > 500 ) {
newState = 4;
}
if(prevState != newState) {
switch (newState) {
case 1: moveForward(maxSpeed,maxSpeed);
break;
case 2: if(prevState == 3) {
moveForward( maxSpeed+20, maxSpeed-10);
delay(95);
} else if(prevState == 4) {
moveForward( maxSpeed-10, maxSpeed+20);
delay(95);
}
stop();
moveForward(maxSpeed, maxSpeed);
go();
digitalWrite(LED, LOW);
break;
case 3: moveForward(maxSpeed, maxSpeed+10);
break;
case 4: moveForward(maxSpeed+10, maxSpeed);
break;
}
prevState = newState;
}
go();
}


/**Interrupt service routine to read analogIRData
*
* Data is then averaged out with the previous 3 values
*/
ISR(TIMER1_COMPA_vect) {
cli();
Expand All @@ -211,12 +164,14 @@ ISR(TIMER1_COMPA_vect) {
frontRight[counter] = analogRead(frontRightIR);
backLeft[counter] = analogRead(backLeftIR);
backRight[counter] = analogRead(backRightIR);
//Data is summed then the average is taken
for(int i = 0; i < 4; i++) {
sums[0] += frontLeft[i];
sums[1] += frontRight[i];
sums[2] += backLeft[i];
sums[3] += backRight[i];
}
//Global array is updated with new values
IR_data[frontLeftData] = (sums[0] >> 2);
IR_data[frontRightData] = (sums[1] >> 2);
IR_data[backLeftData] = (sums[2] >> 2);
Expand Down
19 changes: 17 additions & 2 deletions initializationFunctions.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@ void motor_init() {
PORTE &= ~( _BV(PB4) | _BV(PB5) | _BV(PB6) );
}

/**Initialize the motor pins 6 and 11 to use phase correct pwm at a much higher frequency
* INPUTS: None
* OUTPUTS: None
*/
void timer4_pwm_init() {
//Timer 4 control registers, I clear the registers before writing to them
TCCR4A |= ( _BV(COM4B1) | _BV(PWM4B) ); //Enable PWM output on this pin
TCCR4B &= ~( _BV(CS43) | _BV(CS42) | _BV(CS41) ); //clear prior settings
TCCR4B |= _BV(CS40) ; //Set the clock prescaler, we use no division since the clock is already halved by using phase correct PWM
TCCR4C |= ( _BV(COM4B1S) | _BV(COM4D1) | _BV(PWM4D) ); //Set these bits to overtake the pins, for some reason only setting the shadow bit works for OCR4B
TCCR4C |= ( _BV(COM4B1S) | _BV(COM4D1) | _BV(PWM4D) ); //Set these bits to overtake the pins
TCCR4D |= _BV(WGM40); //Setting phase correct PWM
//Programmable Counter Unit
OCR4B = maxSpeed; //Initial Duty Cycle
OCR4D = maxSpeed; //Initial Duty Cycle
}

/**Initialize timer 1 as an interrupt source
* Whenever timer 1 triggers, the IR data will be read
* INPUTS: None
* OUTPUTS: None
*/
void timer_isr_init() {
//Setting up timer 1 for periodic reading of the analog sensors
TCCR1A = 0; //Clearing Control registers since we don't want the timer attached to any pins
Expand All @@ -41,13 +50,19 @@ void timer_isr_init() {
TIMSK1 |= _BV(OCIE1A); //Enable interrupts on compare match A
}

/**Initialize IR sensor as inputs with no pullup resistor
* The sensors are located on pins A0-A3
* INPUTS: None
* OUTPUTS:None
*/
void sensor_init() {
//Setting the Analog Pins as inputs with no pullup resistor
DDRF &= ~( _BV(PF7) | _BV(PF6) | _BV(PF5) | _BV(PF4) );
PORTF &= ~( _BV(PF7) | _BV(PF6) | _BV(PF5) | _BV(PF4) );
}

/**Initializing outputs for the encoders
/**Initializing encoder pins as inputs with a pullup resistor
* Encoders should be placed onto the same port to increase speed
* Pins 2,3,14 and 15
* INPUTS: None
* OUTPUTS:None
Expand Down
Loading

0 comments on commit 9d68423

Please sign in to comment.