View previous topic :: View next topic |
Author |
Message |
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
Serial Servo controller problem |
Posted: Tue Nov 04, 2003 12:34 am |
|
|
I have been trying to get a serial servo controller to work for a couple of days, but I get very strange results.
CCS 3.155
16F628 @10MHz
With the program listing as it is, the program runs for a while but the output to the servo just disappears after a couple of seconds. I also tried to put the servo code in the ISR but thenthe program does not see the serial comms after a couple of seconds. I did look at the ASM listing and it seems that kbhit() may not be switched into the correct page.
In essence I need to do the following:
1. Run an output at a 20ms frame.
2. At the start of each 20ms frame, the servo output will be high for between 1ms and 2ms.
3. Recieve input from the PC in three bytes:
1. 255 Sync
2. Servo No
3. Servo Pos where 0=1ms 127=1.5ms and 255=2ms.
4. Reposition the appropriate servo in relation to the Servo Position.
5. I do not want to bit bang the timing because I may need to have up to 8 servos.
Any help will be appreciated:
SerialServo.h
----------------
#use delay(clock=10000000)
#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,MCLR,NOLVP,NOCPD
#use rs232(baud=9600,xmit=PIN_B2,rcv=PIN_B1)
SerialServo.c
----------------
#include <SerialServo.h>
#define ServoPin PIN_B4
int ServoPos;
boolean IntSet;
#int_TIMER1
TIMER1_isr()
{
set_timer1(15536);
IntSet = TRUE;
}
void SetServo()
{
int i = 0;
if (IntSet)
{
output_high(ServoPin);
delay_ms(1);
for (i=0;i<=ServoPos;++i)
delay_us(4);
output_low(ServoPin);
IntSet = FALSE;
}
}
void main() {
byte Sync = 0; // 0x55
byte ServoNo = 0; // 1 - 255
output_low(ServoPin);
set_timer1(15536);
setup_timer_1(T1_INTERNAL);
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
ServoPos = 127;
IntSet = FALSE;
while (TRUE)
{
SetServo();
if (kbhit())
{
Sync = getc();
ServoNo = getc();
ServoPos = getc(); // 0-255
}
// printf("D");
}
// printf("C");
} |
|
![](templates/subSilver/images/spacer.gif) |
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
Some more info |
Posted: Tue Nov 04, 2003 2:53 am |
|
|
I have run the prg for 15 minutes without sending and serial commands to the device. The device is still running fine.
The problem must be related to the combination of serial input and interrupt. |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Nov 04, 2003 7:05 am |
|
|
I didn't look at the code that closely, but you should add the ERRORS parameter to your #use rs232 statement. I know this can cause you to stop receiving but I am not sure if it would cause the getc() to hangup forever. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 04, 2003 1:13 pm |
|
|
Can you post a link to the manufacturer's web site for these servos ?
I can see better ways to do it, but I would like to read the data sheet
first. |
|
![](templates/subSilver/images/spacer.gif) |
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
I found the answer - Strange but true |
Posted: Tue Nov 04, 2003 7:18 pm |
|
|
When I switched from using Timer1 to Timer0 the problem went away. I changed the program a little so that the 20ms loop does not hang up if the getc() gets stuck. Yes I did add ERRORS and RESTART_WDT to #use rs232 but it made no difference on Timer1
So here goes with a working program - ready to be modified for more servos.
Serialservo.h
---------------
#include <16F628.h>
#use delay(clock=10000000)
#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,MCLR,NOLVP,NOCPD
#use rs232(baud=9600,bits=8,parity=N,xmit=PIN_B2,rcv=PIN_B1,ERRORS,RESTART_WDT)
SerialServo.c
----------------
#include <SerialServo.h>
#define PinServo0 PIN_B4
int PosServo0;
int i = 0;
#int_TIMER0
TIMER0_isr()
{
output_high(PinServo0);
delay_ms(1);
for (i=0;i<=PosServo0;i++)
delay_us(3);
output_low(PinServo0);
}
void main()
{
byte Sync = 0; // 0xFF
byte ServoNo = 0; // 1 - 255
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);
PosServo0 = 127; // default servo pos
output_low(PinServo0);
while (TRUE)
{
if (kbhit())
{
Sync = getc();
if (Sync==0xFF)
{
ServoNo = getc();
if (ServoNo==0)
PosServo0 = getc(); // 0-255
}
}
}
} |
|
![](templates/subSilver/images/spacer.gif) |
johnh
Joined: 03 Jan 2004 Posts: 19 Location: UK - Brighton
|
Servo control |
Posted: Sat Jan 03, 2004 9:23 am |
|
|
Hello,
I am a newbie to all this C programming and PIC stuff. As it happens i am doing a project that controls a servo (Hi-Tec MG81) through a serial port. I am going to use VB6 as the control.
My question is to Hillcraft - can you/somebody put comments in the program below so that i can follow it through and understand what is going on.
I would appreciate any help on this that ANYONE can give me.
Also PCM programmer, you mentioned that you could see better ways to do this, what do you mean? I'll see if i can dig out a data sheet
Also (on a roll now!) I was thinking of using a 16F84 but would a 16F628 be better??
Cheers
John
Quote: | Serialservo.h
---------------
#include <16F628.h>
#use delay(clock=10000000)
#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,MCLR,NOLVP,NOCPD
#use rs232(baud=9600,bits=8,parity=N,xmit=PIN_B2,rcv=PIN_B1,ERRORS,RESTART_WDT)
SerialServo.c
----------------
#include <SerialServo.h>
#define PinServo0 PIN_B4
int PosServo0;
int i = 0;
#int_TIMER0
TIMER0_isr()
{
output_high(PinServo0);
delay_ms(1);
for (i=0;i<=PosServo0;i++)
delay_us(3);
output_low(PinServo0);
}
void main()
{
byte Sync = 0; // 0xFF
byte ServoNo = 0; // 1 - 255
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);
PosServo0 = 127; // default servo pos
output_low(PinServo0);
while (TRUE)
{
if (kbhit())
{
Sync = getc();
if (Sync==0xFF)
{
ServoNo = getc();
if (ServoNo==0)
PosServo0 = getc(); // 0-255
}
}
}
} |
_________________ There are 10 kinds of people who understand binary, those who do and those who don't |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
Servo Control |
Posted: Sat Jan 03, 2004 10:44 am |
|
|
Johnh,
I love your tagline:
"There are 10 kinds of people who understand binary, those who do and those who don't"
It took about 5 seconds for it to slap me in the face....!
Dave |
|
![](templates/subSilver/images/spacer.gif) |
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
Servo Controller |
Posted: Sun Jan 04, 2004 2:28 am |
|
|
Hi John,
I will document the program for you - It is 10am here in Cape Town, South Africa. We are off to the beach so I'll do it early this evening.
If you mail me on adrianh@hillcraft.co.za then I will send you the entire project.
Regards
Adrian |
|
![](templates/subSilver/images/spacer.gif) |
johnh
Joined: 03 Jan 2004 Posts: 19 Location: UK - Brighton
|
|
Posted: Sun Jan 04, 2004 6:33 am |
|
|
Cheers Adrian, I have emailed you.
"It took about 5 seconds for it to slap me in the face....! "
Dave surely you mean 101 seconds !!
Regards
John _________________ There are 10 kinds of people who understand binary, those who do and those who don't |
|
![](templates/subSilver/images/spacer.gif) |
|