go_away

Author Topic: Erratic behavior with robot bumper switches  (Read 579 times)

0 Members and 1 Guest are viewing this topic.

Offline lanamorTopic starter

  • Jr. Member
  • **
  • Posts: 45
  • Helpful? 0
Erratic behavior with robot bumper switches
« on: March 01, 2013, 04:08:31 PM »
I'm pretty new to AVRs and I'm designing a robot with differential steering and two bump switches. My problem is that I'm getting erratic behavior and I'm not sure if it's in my code or my hardware. I'm sure my code is far from nice so if anyone sees any problems I would appreciate the feedback. I also attached the schematic.

Code: [Select]
#define F_CPU 8000000    // AVR clock frequency in Hz, used by util/delay.h
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>

#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)

int speed = 255;

int main()
{
DDRD &= ~(1 << PD4);  // Left bump switch
PORTD |= _BV(PD4);   // Enable internal pull up

DDRD &= ~(1 << PD3); // Right bump switch
PORTD |= _BV(PD3);    // Enable internal pull up

//Motor 1
DDRB |= (1<<PB2);     // make PB2 an output
DDRB |= (1<<PB0);     // make PB0 an output

//Motor 2
DDRB |= (1<<PB3);     // make OC1A an output
DDRD |= (1<<PD6);     // make PD6 an output

// init timers as fast PWM
TCCR0A = (1 << WGM00) | (1 << WGM01);
TCCR1A = (1 << WGM10) | (1 << WGM12);

// set prescaler to 1
TCCR0B |= (1 << CS00);
TCCR1B |= (1 << CS00);

// set outputs to PWM
TCCR0A |= (1 << COM0A1);
TCCR1A |= (1 << COM1A1);
TCCR1A |= (1 << COM1B1);


int Left_Bump();
int Right_Bump();

Status(5); // Flash status LED

while(1)
{
Forward();

if (Left_Bump())
{
Reverse();
_delay_ms(1000);
Right();
_delay_ms(1000);
Forward();
}
if (Right_Bump())
{
Reverse();
_delay_ms(1000);
Left();
_delay_ms(1000);
Forward();
}
}

}


int Right_Bump()
{
if (bit_is_clear(PIND, PD4))
{
_delay_ms(25);
if (bit_is_clear(PIND, PD4)) return 1;
}

return 0;
}
int Left_Bump()
{
if (bit_is_clear(PIND, PD3))
{
_delay_ms(25);
if (bit_is_clear(PIND, PD3)) return 1;
}

return 0;
}

int Stop()
{
OCR0A = 0; //Lowest 190
OCR1A = 0;
}

int Left()
{
OCR0A = speed;
OCR1A = speed;

output_high(PORTB, PB0);
output_low(PORTD, PD6);
}

int Right()
{
OCR0A = speed;
OCR1A = speed;

output_high(PORTB, PB0);
output_low(PORTD, PD6);
}

int Forward()
{
OCR0A = speed;
OCR1A = speed;
output_low(PORTB, PB0);
output_low(PORTD, PD6);
}

int Reverse()
{
OCR0A = speed;
OCR1A = speed;

output_high(PORTB, PB0);
output_high(PORTD, PD6);
}

int Status(int flashes)
{
int i;

for (i = 0; i <
flashes; i++ )
{
output_high(PORTB, PB1);
_delay_ms(500);
output_low(PORTB, PB1);
_delay_ms(500);
}

}

« Last Edit: March 01, 2013, 05:21:40 PM by lanamor »

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Erratic behavior with robot bumper switches
« Reply #1 on: March 02, 2013, 12:13:16 AM »
it would help if you describe what you are expecting should happen, and what is actually happening, and what you've done to reduce the problem to its smallest possible part.

Offline lanamorTopic starter

  • Jr. Member
  • **
  • Posts: 45
  • Helpful? 0
Re: Erratic behavior with robot bumper switches
« Reply #2 on: March 02, 2013, 11:32:44 AM »
My apologies, I assumed the code was self explanatory. The erratic behavior is that sometimes the bump switch works as intended-- left bump switch is triggered so Left_Bump() returns true and the functions within that IF statement runs-- but sometimes when the switches are triggered the robot will start spinning in one direction indefinably, or reverse indefinably, or start to spin back and forth. It's as if the code is hanging, but only erratically. I think I can rule out a short since my PCB is etched and my connections are all solid.

My main intention for the post is to solicit feed back on the hardware and code design. This is my first robot project so I'm trying to teach myself from the deep end of the pool.

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Erratic behavior with robot bumper switches
« Reply #3 on: March 02, 2013, 01:53:25 PM »
I suggest breaking the problem down to the smallest possible components.

Start with a program that turns an LED on if the left bumper is touched, else turns it off. That's all it does.
Now, run the program, and verify that the LED matches the bumper.
Now, do the same thing with the right bumper, and try again.
Now, do something that just runs each motor when the bumper is not touched, and turns the motor off when the bumper is touched.
Put your robot on a pedestal (so it doesn't run around the floor) and verify that the behavior is as expected -- motors run, except when you push the bumpers.
Keep working forward in very small steps like this, until it doesn't do what you want it to do -- at that point, you did understand the system in the previous step, but not in the new step, so you can study the difference between the two steps to find out where you go wrong.

I know that this may sound like the slow way of doing things, but it's actually the fast way, and how professionals do it. Also, for each test case, don't just turn it on, poke the bumper, seems to work, done. Spend a minute with each test case, poking in various ways, hold it in for a long time, switch on/off several times quickly, etc. Try to break the actual behavior you expect, and see if you can break it, or if it's robust. This will let you become more sure of what you understand, and what you don't understand.

Offline MrWizard

  • Full Member
  • ***
  • Posts: 116
  • Helpful? 0
  • My cylon friend told me a killing joke......
Re: Erratic behavior with robot bumper switches
« Reply #4 on: March 02, 2013, 05:06:56 PM »
Do you use pull-up resistors ?

 


Get Your Ad Here

data_list