Society of Robots - Robot Forum

Software => Software => Topic started by: TheDarkLord on September 03, 2011, 10:12:55 AM

Title: Programming a servo - what could the problem be? (using $50 robot board)
Post by: TheDarkLord on September 03, 2011, 10:12:55 AM
Hello, I've successfully (to a certain extent, save for some problems with the servos) built the $50 robot, and now I'm trying to explore different possibilities using the same board. I wanted to make a servo which responds to sound, and so I built an amplifier circuit with a microphone and connected it to the Atmega8, following the instructions here: http://tinkerlog.com/2007/05/20/cheap-sound-sensor-for-avr/ (http://tinkerlog.com/2007/05/20/cheap-sound-sensor-for-avr/)

I then modified the code posted on that same website, and successfully got an LED to turn on when I clap, and then off when I clap a second time. Now I want to introduce into this code a servo, so that when I clap the first time, the servo spins in one direction, and the second time, in the other direction.

I copy-pasted some of the code from the $50 robot in order to try and create this. However, it seems that whatever value I put into the servo_left(); function (I'm only using one servo) the SPEED of the servo does not change. Instead, it seems the servo is 'on' for whatever value I put in. Even this does not seem consistent. When I first clap, the servo is on for, let's say x seconds. The second time, it's on for y seconds. Then the third time, instead of being on for x seconds (because it is on a loop) the servo seems to be on for an arbitrary amount of time. The speed of the servo in all cases seems to be fixed.

What could the problem be? If you want me to upload the code, please say so and I will.
Thanks a lot!

Vignesh R.
Title: Re: Programming a servo - what could the problem be? (using $50 robot board)
Post by: richiereynolds on September 03, 2011, 03:04:32 PM
Yes, please show the code.
Title: Re: Programming a servo - what could the problem be? (using $50 robot board)
Post by: TheDarkLord on September 03, 2011, 04:21:55 PM
Okay. Sorry that I could not do so earlier.
The project file (zip) is attached.

I'm using AVR Studio 4.
Also, if it will help in making the behavior of the servo more clear, I could upload a video.

Thanks,
Vignesh R.

EDIT: I have large sections of the code commented out; it refers to stuff I tried but didn't work. You can ignore these please. Also, I'm sorry for the lack of comments on the program itself, but it seemed relatively straightforward..

EDIT #2: As I continued testing, I found that the servo doesn't change direction even while using the original $50 code. So is this a hardware problem?
Title: Re: Programming a servo - what could the problem be? (using $50 robot board)
Post by: joe61 on September 04, 2011, 06:35:42 AM
I'm not following what happens in your servo_left() function. Servos are normally controlled by PWM, and I'm not clear on how a single pulse would do anything to them. They want a signal with a 20ms frequency, and duty cycle of between 1ms and 2ms.

Take a look at http://www.societyofrobots.com/member_tutorials/node/228 (http://www.societyofrobots.com/member_tutorials/node/228) that has examples of driving a servo with PWM. I think that's what you want to do.

Joe
Title: Re: Programming a servo - what could the problem be? (using $50 robot board)
Post by: TheDarkLord on September 04, 2011, 09:32:21 AM
Thanks for the quick reply.
I simply copied the servo_left() function from the Photovore_v1 source code, which was created by Admin. Could I not simply modify it to make the servos work properly?

That tutorial says to use PB0 or PB1.. However I cannot connect the servos here because my programmer connects to these pins.
And apparently, the timer only works for those two pins, but I want my servo to be connected on either PD0 or PD1..

Any ideas on how to proceed?

Thanks,
Vignesh R.
Title: Re: Programming a servo - what could the problem be? (using $50 robot board)
Post by: joe61 on September 04, 2011, 11:16:18 AM
I looked at the source, but I honestly don't know what it's doing. I'm guessing that it sends one pulse of the appropriate length rather than using PWM. Apparently that works, but I'm not familiar with the technique.

I've always used PWM to control servos, which means using a timer. In that case you'd have to either use the predefined pins, or setup an interrupt handler to pulse the pin of your choice. For what you're doing, admin's way is probably better. Perhaps he'll explain.

Joe
Title: Re: Programming a servo - what could the problem be? (using $50 robot board)
Post by: TheDarkLord on September 04, 2011, 11:26:14 AM
Well, thank you anyway :)

As a side note.. you mention "setup an interrupt handler to pulse the pin of your choice".
Would you be able to briefly explain how I would go about doing this? Or would you happen to know of any tutorials that explain this clearly? (for a beginner)

Thanks,
Vignesh R.
Title: Re: Programming a servo - what could the problem be? (using $50 robot board)
Post by: greywanderer012345 on July 27, 2012, 01:59:30 PM
I'm asking the same question in a newer post. If your looking for this answer go here:

http://www.societyofrobots.com/robotforum/index.php?topic=15926.0 (http://www.societyofrobots.com/robotforum/index.php?topic=15926.0)
Title: Re: Programming a servo - what could the problem be? (using $50 robot board)
Post by: greywanderer012345 on August 16, 2012, 01:12:09 AM
I got my $50 robot setup working with my servo code if you want to look at it in the above post. The make the delay 1000 for one direction and 2000 for the other. I couldn't get the delay functions to work when passed a variable, but you should be able to alter my code for your purpose.