Society of Robots - Robot Forum

Software => Software => Topic started by: Charizard on March 20, 2009, 07:56:50 AM

Title: Have anyone encountered this weird problem?
Post by: Charizard on March 20, 2009, 07:56:50 AM
I wonder has anyone encounter this problem. I wrote a code generating pulses for my servo, and i even have downloaded code provided by supplier to test the servo. In oscillator, the pulses are there, but whenever I connect to my servo, those pulses disappear and become 0v. I tested with two servos and they show the same problem. Do you guys have any idea on it?  ???thanks.
Title: Re: Have anyone encountered this weird problem?
Post by: pomprocker on March 20, 2009, 10:04:15 AM
how about a little more information?


microcontroller?

servo type?

code?
Title: Re: Have anyone encountered this weird problem?
Post by: Charizard on March 20, 2009, 10:32:31 AM
I am using PIC16f877 on C55S servo from this site. http://www.cytron.com.my/listProductCategory.asp?cid=90 (http://www.cytron.com.my/listProductCategory.asp?cid=90)

Code: [Select]
//==========================================================================
// Project : Sample code for control single servo motors using delays
//   This project is using 16F877A with 20MHz
//   Has been try succesfully using SK40B
//  
//   Please make sure the hardware is compatible with this code
//
// Project description : This source code is used to control a servo motor.
//   Servo motor will continuosly turn from one position to another position
//   Delay is use to calculate the pulse width timing.
//   The delay timing is depend on the crystal.
//   Difference timing for difference crystal.
//
// Date : Feb 2009
//==========================================================================
//
// include
//==========================================================================
#include <pic.h>    // this sample code is using 16F877A !!

// configuration
//==========================================================================
__CONFIG ( 0x3F32 ); //configuration for the  microcontroller

// define
//==========================================================================
#define servo RB1

#define MHZ *1000L /* number of kHz in a MHz */
#define KHZ *1 /* number of kHz in a kHz */

// Value x must not more that 255
#define DelayUs(x) { unsigned char _dcnt; \
  _dcnt = (((x)*(20MHZ))/(24MHZ))|1; \
  while(--_dcnt != 0) \
  continue; \
  _dcnt = (((x)*    (20MHZ))/(24MHZ))|1; \
  while(--_dcnt != 0) \
  continue; }


// function (every function must have a function prototype)
//==========================================================================

void DelayMs(unsigned char y); // Value y must not more that 255
// delay ms
// main function (main fucntion of the program)
//==========================================================================
void main(void)
{
unsigned int i,a;

//set IO port for servo pin
TRISB = 0b00000001; //servo motor is RB1
PORTB = 0b00000000; //clear port B

//servo will loop infinity
while(1) //from one position to another position
{
//*******************************************************
//
//Delay determine the servo motors position
//Try change the value for different position
//
//Value in DelayUs and DelayMs function must not more than 255
//Otherwise the timing will not accurate!!
//
//******************************************************

for(i=0;i<50;i++) //continues pulse for 50 times
{
servo=1; //set servo pin high
DelayUs(250);
DelayUs(250);
DelayUs(200); //delay 250+250+200= 700us = 0.7ms
//These delay only valid using 20MHz!

servo=0; //set servo pin low
DelayMs(19); //remain --> 20ms-0.7ms=19.3ms
DelayUs(250);
DelayUs(50); //19ms+0.250ms+0.050ms=19.3ms
//These delay only valid using 20MHz!
}

//  _                _                      _
// | |               | |                     | |
// | |               | |                     | |              ~~50 times             
// | |_____________| |____________| |____________________
// 0.7ms    19ms      0.7ms      19ms     0.7ms   19ms
// |    |
// |<-------20ms------>|

for(i=0;i<50;i++) //continues pulse for 50 times
{
servo=1; //set servo pin high
DelayMs(2); //delay 2ms
//These delay only valid using 20MHz!

servo=0; //set servo pin low
DelayMs(18); //delay 18ms
//These delay only valid using 20MHz!
}

//  ___        ___                 ___
// |   |               |   |               |   |
// |   |               |   |               |   |              ~~50 times       
// |   |________|   |_________|   |____________________
//  2ms       18ms      2ms      18ms       2ms 18ms
// |    |
// |<-------20ms------>|
}

}

//subroutine
//============================================================================
void DelayMs(unsigned char y)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
} while(--i);
} while(--y);
}


I program it with MPlab and PICkit2. The servo has 3wires there, red, black and white. I connected white to PIC, black to -ve / ground and red to +ve.
Title: Re: Have anyone encountered this weird problem?
Post by: airman00 on March 20, 2009, 12:03:38 PM
Can you provide a schematic? It might be an electronic issue. What are you powering the microcontroller with?
Title: Re: Have anyone encountered this weird problem?
Post by: galannthegreat on March 20, 2009, 12:54:23 PM
Check the datasheet of the servo for the tolerances and pulse range.
Title: Re: Have anyone encountered this weird problem?
Post by: Admin on April 07, 2009, 10:54:48 PM
Also, always use an oscilloscope to measure signals to make sure your code is working properly.
Title: Re: Have anyone encountered this weird problem?
Post by: slo on April 08, 2009, 04:40:59 PM
The ground for the servo and microcontroller should be connected.  Please verify the voltage level and frequency of the pulses are what they should be before connecting the servo.  If all you saw were pulses it could be they are not the right pulses.  I have been guilty of quickly hooking up a probe, seeing a waveform and assuming everything is good without checking the scale.  Turned out the wire I was on was disconnected and acting as an antenna for EMI.  You said the pulses are there in the oscillator.  I assumed this was a typo and you meant oscilloscope.  If you are actually connecting your servo to an oscillator it would probably, heck who knows.  But it certainly wouldn't run your code.
Dustin Maki