Author Topic: Help to center servo  (Read 1103 times)

0 Members and 1 Guest are viewing this topic.

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Help to center servo
« on: July 26, 2011, 03:23:00 PM »
Hi,

My first post. Actually I have a question. I modify servo as per admin tutorial in http://www.societyofrobots.com/actuators_modifyservo.shtml and also use .hex file. Successfully the servo stopped.

But what I am looking for is the C code to stop the servo at center. I could not find that in 50$ robot codes. Can anyone help and give me C code to stop servo? Need it as I am curious to program myself and learn.

Thanks.

Edit: I am using Atmega8, and AVR Studio
« Last Edit: July 27, 2011, 01:08:26 AM by roboda »

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Help to center servo
« Reply #1 on: July 27, 2011, 01:59:31 AM »
Anybody help...

I have also tried the code below. After uploading the hex file, it slows down at a particular position, but just keeps vibrating. Admin's .hex file stops exactly without any movement and the very reason I need to use that code. I need to do this for a college project and hence need to document the code I use and so cannot just use the .hex file from admin.
Code: [Select]
#include <avr/io.h>
#include <util/delay.h>
void main(void)
{
DDRB = 0xFF;
while (1)
{
PORTB = 0xFF;
_delay_us(1500);  //Also tried _delay_ms(1.5);
PORTB = 0x00;
_delay_ms(20);  // Tried from 10 ms to 20 ms, but still results are the same
}
}
Can somebody reply and help plz.
Edit: added code formatting
« Last Edit: July 27, 2011, 01:17:19 PM by roboda »

Offline happy_hippo

  • Jr. Member
  • **
  • Posts: 45
  • Helpful? 0
Re: Help to center servo
« Reply #2 on: July 27, 2011, 04:51:08 AM »
I thought once you modify a servo for continuous rotation, you lose all control over angular position. But I'm no expert.
 ::)
??? -> :D

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Help to center servo
« Reply #3 on: July 27, 2011, 05:43:35 AM »
Anybody help...

I have also tried the code below. After uploading the hex file, it slows down at a particular position, but just keeps vibrating.


You might have to adjust the potentiometer. There should be a little screw showing somewhere on the servo. Adjust that until it stops.

Quote
Admin's .hex file stops exactly without any movement and the very reason I need to use that code. I need to do this for a college project and hence need to document the code I use and so cannot just use the .hex file from admin.


I suspect he's using a timer to produce the correct pulses. That's a better way to do it than a manual timing loop. Take a look at http://www.societyofrobots.com/member_tutorials/node/228 for how to do this kind of thing. I think it will have the answers you're looking for. It has an example of driving a servo.

Joe

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Help to center servo
« Reply #4 on: July 27, 2011, 06:06:18 AM »
Quote
You might have to adjust the potentiometer. There should be a little screw showing somewhere on the servo. Adjust that until it stops.

I have tried it. It somehow wouldn't stop using the code I have written. It comes to almost a stop, but keeps vibrating. In other words, it does not center.

Quote
I suspect he's using a timer to produce the correct pulses. That's a better way to do it than a manual timing loop. Take a look at http://www.societyofrobots.com/member_tutorials/node/228 for how to do this kind of thing. I think it will have the answers you're looking for. It has an example of driving a servo.

I am not sure if he uses timer. Admin has used PD0 and PD1 whereas the timer pins are PB0 and PB1. Not sure if we can use timer for other pins.

Is there anybody who can share the C code for the hex file admin has generated? "Admin", can you hear me?

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Help to center servo
« Reply #5 on: July 27, 2011, 06:43:24 AM »
I am not sure if he uses timer. Admin has used PD0 and PD1 whereas the timer pins are PB0 and PB1. Not sure if we can use timer for other pins.

According to the data sheet for the Atmega8, PD0 and PD1 are the TX and RX lines for the async serial port.  The data sheet also lists three timers. I'm not familiar with the Atmega8, but you might want to look at the data sheet and understand what the timers are and how they work. Take a look at the tutorial I mentioned last time.

You won't be able to program the part effectively unless you can understand the data sheet. Simply copying someone else's code isn't going to help you.

Joe

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Help to center servo
« Reply #6 on: July 27, 2011, 07:04:11 AM »
Thanks for your response. I did go through the datasheet. There are two 8 bit timers and one 16 bit timer. 8 bit timers are OC1A(PB1) and OC1B(PB2). I have also used a code available online for servo control using these two timer pins.

Quote
Take a look at the tutorial I mentioned last time
I did go through that tutorial. It uses timer and the pins used are PB1 and PB2 pins. That is the reason I do not want it :(
« Last Edit: July 27, 2011, 07:06:25 AM by roboda »

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Help to center servo
« Reply #7 on: July 27, 2011, 07:31:56 AM »
Quote
Take a look at the tutorial I mentioned last time
I did go through that tutorial. It uses timer and the pins used are PB1 and PB2 pins. That is the reason I do not want it :(

I missed that part. What do you have against using the timer? Or is it just those pins that you don't like? Maybe if you could explain more fully what you actually want to do it would be easier to help.

Joe

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Help to center servo
« Reply #8 on: July 27, 2011, 07:42:24 AM »
Quote
Maybe if you could explain more fully what you actually want to do it would be easier to help.
Many problems.

1. Those pins are connected to Programmer. So, I need to add multiple pins to utilize those pins
2. I am curious. I know that admin has used a splendid code to center the Servo. Need to know what that is, instead of just copying and using.
3. I wonder if it is just controlled with PWM, without using any timers. Need to learn and understand that.

Thanks for your patience. Also, to make it clear, I am not looking for other options as I am aware of other options. I just need to use the same specific or equivalent code which admin has used, and understand how it works. I may be stubborn, but I love learning like this.

Hence asking for people to help me.
« Last Edit: July 27, 2011, 07:58:01 AM by roboda »

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Help to center servo
« Reply #9 on: July 27, 2011, 08:04:49 AM »
Thanks for your patience. Also, to make it clear, I am not looking for other options as I am aware of other options. I just need to use the same specific or equivalent code which admin has used, and understand how it works. I may be stubborn, but I love learning like this.

Ok, well good luck.

Joe

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Help to center servo
« Reply #10 on: July 27, 2011, 08:14:25 AM »
well.. well.. I don't think anybody has any idea on that code.

Thanks Joe. You tried helping me.

Let me try searching online if there are any means to get a code to center servo without using those timer pins.

Offline adanvasco

  • Full Member
  • ***
  • Posts: 107
  • Helpful? 6
  • Necessity is the mother of invention.
Re: Help to center servo
« Reply #11 on: July 27, 2011, 11:27:03 AM »
Don't get discouraged so fast man 8)

I just modified a piece of code and I'm uploading it to my mcu to test. Let you know in few more minutes.
Knowledge does not weigh.

Offline adanvasco

  • Full Member
  • ***
  • Posts: 107
  • Helpful? 6
  • Necessity is the mother of invention.
Re: Help to center servo
« Reply #12 on: July 27, 2011, 11:37:09 AM »
I just tried this code in four different servos and they stop at once.

Code: [Select]
//AVR includes
#include <avr/io.h> // include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h> // include interrupt support

//AVRlib includes
#include "global.h" // include global settings

//define port functions; example: PORT_ON( PORTD, 6 );
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)

//************DELAY FUNCTIONS************
//wait for X amount of cycles (23 cycles is about .992 milliseconds)
//to calculate: 23/.992*(time in milliseconds) = number of cycles
void delay_cycles(unsigned long int cycles)
{
while(cycles > 0)
cycles--;
}
//***************************************

//*********hold servo********************
void hold_servo(signed long int speed)
{
PORT_ON(PORTD, 0);
delay_cycles(speed);
PORT_OFF(PORTD, 0);
delay_cycles(200);
}
//***************************************

int main(void)
{

DDRD = 0xFF;  //configure all D ports for output

while(1)
{
//hold servo
hold_servo(34.7782);
}
return 0;
}

BTW, I modified this from the $50 robot code.
Knowledge does not weigh.

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Help to center servo
« Reply #13 on: July 27, 2011, 12:36:55 PM »
Thanks a lot adanvasco.
I will burn it into my controller now and will let you know what happened.

In the meanwhile, I have few questions.
1. Why is interrupt used? Not sure it is used anywhere in the program.
2. Is the code working on internal 1Mhz Oscillator?
3. How have you derived with this number:
Code: [Select]
hold_servo(34.7782);
Whatever the answers, if this code works as expected, I would be extremely thankful to you.

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Help to center servo
« Reply #14 on: July 27, 2011, 01:07:46 PM »
Never mind my third question. Got the answer: 23/0.992*1.5
But it did not stop, or rather for some unknown reason, it did not even slow down.

Offline adanvasco

  • Full Member
  • ***
  • Posts: 107
  • Helpful? 6
  • Necessity is the mother of invention.
Re: Help to center servo
« Reply #15 on: July 27, 2011, 01:49:05 PM »
Ok, I assumed that you were running your ATmega8 at 8MHz since you were building the $50 robot. Is that assumption right or wrong?
Knowledge does not weigh.

Offline robodaTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Help to center servo
« Reply #16 on: July 27, 2011, 01:58:12 PM »
I tried it with both 1Mhz and 8Mhz. BTW, I am not building 50$ robot, but tried using the code to center the servo.
Also, are the files "inturrupt.h" and "global.h" really required?

I am sure I have done something wrong. Will recheck it.

 


Get Your Ad Here

data_list