Author Topic: C code for a servo pulse  (Read 2263 times)

0 Members and 1 Guest are viewing this topic.

Offline StrikeskidsTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
C code for a servo pulse
« on: August 23, 2009, 01:58:50 PM »
What is the C code for a servo pulse.
I tried using the AVR library but the code there didn't work very well.
The 1500 us pulse works well but i don't know the code for it.
Help Please!!!! ??? ??? ??? ???
Don't take life seriously
Nobody gets out alive anyway.

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: C code for a servo pulse
« Reply #1 on: August 24, 2009, 12:45:33 AM »
what code were you using?
this is using pwm right?
Howdy

Offline StrikeskidsTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: C code for a servo pulse
« Reply #2 on: August 24, 2009, 04:05:39 AM »
I used admin's hex to modify my servo and that code seemed to work better than the servo header in the avrlib.
I am using pwm
Don't take life seriously
Nobody gets out alive anyway.

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: C code for a servo pulse
« Reply #3 on: August 24, 2009, 04:21:40 AM »
can you provide the section of code you are using?
generally something like this will work for pwm:
Code: [Select]
while(1)
{
portb = 0xff; // turn all b ports high
delay_us(1500);
portb = 0x00; // turn all b ports low
delay_us(20000);
}
Howdy

Offline StrikeskidsTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: C code for a servo pulse
« Reply #4 on: August 24, 2009, 04:53:19 AM »
How do you specify a specific port? I have 3 servos, 2 modified and one not for my robot and i don't want them all moving at the same time.
Don't take life seriously
Nobody gets out alive anyway.

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: C code for a servo pulse
« Reply #5 on: August 24, 2009, 05:00:17 AM »
Code: [Select]
while(1)
{
portb.5 = 0xff; // turn pb5 high
delay_us(1500);
portb.5 = 0x00; // turn pb5 low
delay_us(20000);
}
Howdy

Offline StrikeskidsTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: C code for a servo pulse
« Reply #6 on: August 24, 2009, 05:08:12 AM »
Perfect so,
Code: [Select]
u08 i;
for (i=50; i-->0;)
     portd.0 = 0xFF
     portd.1 = 0xFF
     delay_us(1700)
     portd.0 = 0x00
     portd.1 = 0x00
     delay_us(20000)
}
should turn 2 unmodified servos in ports pd1 and pd2 forward for a second.
Don't take life seriously
Nobody gets out alive anyway.

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: C code for a servo pulse
« Reply #7 on: August 24, 2009, 05:14:27 AM »
i don't know :P
try it!
normally when i move servos i use:
Code: [Select]
void servo_left(signed long int speed)
{
PORT_ON(PORTD, 0);
delay_cycles(speed);
PORT_OFF(PORTD, 0);
delay_cycles(200);
}
void servo_right(signed long int speed)
{
PORT_ON(PORTD, 1);
delay_cycles(speed);
PORT_OFF(PORTD, 1);
delay_cycles(200);
}
but im not much of a programmer :P
Howdy

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 110
Re: C code for a servo pulse
« Reply #8 on: August 24, 2009, 05:16:26 AM »
The examples the others have given use software to generate a pulse. Your original post mentioned PWM. If you want to use hardware PWM then checkout my members tutorial at http://www.societyofrobots.com/member_tutorials/node/228 which discusses PWM and gives an example for driving a servo.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline StrikeskidsTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: C code for a servo pulse
« Reply #9 on: August 24, 2009, 06:51:03 AM »
Neither the PORT_ON code nor the PORTD = 0xFF code work
Don't take life seriously
Nobody gets out alive anyway.

Offline StrikeskidsTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: C code for a servo pulse
« Reply #10 on: August 24, 2009, 09:48:16 AM »
Webbot, your code works perfectly, but I had to modify my circuit board first. Also there is a much less complicated way of explaining it shown here: http://www.societyofrobots.com/robotforum/index.php?topic=1827.0. Thanks everybody. :) ;) :D ;D 8) ::) :P :-*
Don't take life seriously
Nobody gets out alive anyway.

 


Get Your Ad Here