View previous topic :: View next topic |
Author |
Message |
pekka1234
Joined: 28 May 2017 Posts: 89
|
Servo accuracy |
Posted: Sun Mar 09, 2025 3:09 pm |
|
|
Hey. I am doing motor generators as a voltage adjustments.
It should adjust the 230VAC.
They are used as heaters, so the adjustment is not very accuracy.
I did the other (23kW) with step motor.
It worked well, but it needed reset switch, so that it knows where it starts.
The second I did with servo motor.
It do not need reset switch.
I used normally 8MHz internal clock CPU PIC16F886.
It needs the interface with 1ms-2ms and then total 20ms off.
I used setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );
I used CCS version V5.118
1 ms needs 9 pulses and 2 ms needs 18 pulses.
So the total adjustment is only 9 pulses.
When I tested it, it turned to be coarse.
I put the CPU in 20MHz working 2.5 times faster.
Now it works 22 pulses to 45 pulses.
It is much softer.
I wonder is there better case to adjust more than to increase the crystal?
Pekka Ritamaki |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19765
|
|
Posted: Mon Mar 10, 2025 2:11 am |
|
|
You don't tell us how you are actually doing this?.
This obviously affects how fine adjustment can be.
There are more sophisticated chips that have extra features to speed up
things like this, but at the end of the day, speed is your friend, when doing
any sort of timing operation.. |
|
 |
pekka1234
Joined: 28 May 2017 Posts: 89
|
|
Posted: Mon Mar 10, 2025 4:00 am |
|
|
Do you mean how often I do the motor controllers?
This is the first time, when I have done this kind of the controller.
I will go to my friends friend, who is keeping car repair shop. I have not seen any photograph of this device. This was step motor controller as my friend asked.
The second controller will go to my friend. He has a 6kW motor and generator. It will also generate heat at 230VAC.
I have done 30MW and 50MW SCR controllers to high power systems, but they are quite different.
Here are some information about these devices.
http://capacitor.wikidot.com/
I can send the PIC code, if you want to see it.
If I got 40MHz PIC like PIC18F2680, it will adjust the voltage two times more accurate, but I need the control voltage go upwards, so the the servo works.
Pekka |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19765
|
|
Posted: Mon Mar 10, 2025 4:58 am |
|
|
You talk about servo pulses, but then talk about counting on the timer.
It sounds as if you are possibly using overflow counts of the timer to time your
output pulse?. However timer1 is a 16bit timer, so this would not give the
sort of values you are referring to. There are much higher resolution ways
of working like using the CCP to do the count for you, but we need to have
some idea what you are actually doing. |
|
 |
pekka1234
Joined: 28 May 2017 Posts: 89
|
|
Posted: Mon Mar 10, 2025 9:31 am |
|
|
OK Ttelmah.
I wanted to do it some other way.
The basic control is here https://probyte.fi/servoControl.jpg
Hopefully you can give me a new control system.
Pekka Ritamaki |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19765
|
|
Posted: Mon Mar 10, 2025 11:20 am |
|
|
So, why not generate a proper servo pulse, not a chain of narrower pulses?.
A servo does not really expect a chain of high frequency pulses as you show.
You must be being lucky and have a servo that is integrating these.
This gives really poor accuracy.
What is the 'other way' you want to use?.
There have been many posts here before explaining how to drive a servo
using the CCP, rather than a pulse train. The resolution of this then becomes
the instruction cycle of the processor.
Have a look at this, the comments here, and the links from it:
[url]
https://www.ccsinfo.com/forum/viewtopic.php?t=58522&highlight=ccp+servo
[/url] |
|
 |
pekka1234
Joined: 28 May 2017 Posts: 89
|
|
Posted: Mon Mar 10, 2025 3:15 pm |
|
|
Thank you Ttelmah
I will look it carefully.
I knew that here I find the answer
Pekka Ritamaki |
|
 |
pekka1234
Joined: 28 May 2017 Posts: 89
|
|
Posted: Sun Mar 16, 2025 10:05 am |
|
|
I try to increase servo aaccurcy with PWM setting.
I looked pwm settings.
It doensn't work with normal settings.
I needs 0.833MHz oscilator.
I try with RC oscillator (100pF and 10k pot), but it was quite unstable.
I didn't even tried with the servo.
Then I tried with 10MHz crystal, when I diveded 10MHz by 12, I got 833333Hz
I build the pwm device and output C2 to servo.
I adjusted pwm to 20, which gives 1mS and 40 which gives 2ms
Then I put interrupt to count 390 , it will reser the count
It gives 20ms for the pulse.
So the accuracy was 20 postion to servo.
It was not enough and then I put 20MHz crystal to PIC16F886.
Now the lowest position was 40 and the high position was 80.
The accuracy was 40, which is enough for me.
I will get more accurascy, I use PIC18F26K80 with 40MHz crystal.
Or maybe 3.2 more accuracy with 64MHz crystal.
I do not know it it will work.
Here is the code:
#include <16F886.h>
#device ADC=10
#fuses NOWDT, NOPROTECT, NOLVP, HS
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK)
int16 pulse_width = 0;
// interrupt
#INT_TIMER2
void pwm_isr() {
static int16 counter = 0;
counter++;
if (counter <= pulse_width) {
output_high(PIN_C2);
} else {
output_low(PIN_C2);
}
if (counter >= 390*2) { // 20 ms = 390*2 * 0,02516 ms 20* 0.0.512= 19.968ms
counter = 0;
}
}
void setup_pwm() {
setup_timer_2(T2_DIV_BY_16, 7, 1); // PR2 = 7, 8 postions -> 0,0512 ms- 20Mhz/4= 5 Mhz. 5MHz/16= 312500Hz
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
}
void main() {
int16 adc_value;
set_tris_c(0b00000000);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(sAN1);
set_adc_channel(1);
setup_pwm();
while (TRUE) {
read_adc(ADC_START_ONLY);
delay_us(10);
adc_value = read_adc(ADC_READ_ONLY);
pulse_width = 40 + (adc_value * 40 / 1023); // 40–80 (1,0–2,0 ms)
printf("ADC = %ld, Pulse = %ld\r", adc_value, pulse_width);
delay_ms(50);
}
}
and here is the code operation:
https://probyte.fi/PWM2.jpg
Pekka Ritamaki |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9445 Location: Greensville,Ontario
|
|
Posted: Sun Mar 16, 2025 2:32 pm |
|
|
instead of all the fancy code...
do a simple test
something that does this.
create a loop...
compute ontime as 1000+n, where n=0 to 1000
offtime is 2000-ontime
set a bit high
delay_us (ontime)
clr the bit
delay_us(offtime)
maybe display the ontime count here ?
delay_ms(18)
...end of loop
it 'should' move the servo from full left (1ms) to full right (2ms)
not near the PIC PC to cut /test actual code but it is simple.... |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19765
|
|
Posted: Mon Mar 17, 2025 1:52 am |
|
|
As a comment though, the PWM, is not a good way to generate a reasonably
accurate servo pulse. The PWM is designed to generate high frequency
pulses, the servo requires quite a low frequency signal (on modern designs
400Hz, but older designs 50Hz). It is the repeatability and resolution of
this that is important.
I suggested using the CCP.
This was code I posted a while ago, to give servo pulses from 0 to 100
percent using the CCP:
[url]
https://www.ccsinfo.com/forum/viewtopic.php?t=50914&highlight=servo
[/url]
Also, ADC_CLOCK_INTERNAL is not recommended for accuracy on the
PIC. You should be using the master oscillator divided to give the required
rate. |
|
 |
pekka1234
Joined: 28 May 2017 Posts: 89
|
|
Posted: Mon Mar 17, 2025 4:30 am |
|
|
Ttelmah,
I have to measure 230VAC to do the servo working.
How I do without ADC converter?
I do not use RC converter, but 20MHz crystal, divided by 16 =8333333Hz.
Now I got 40 position for my servo accuracy, which is for me enough.
I can use also PIC18F26K80 with 40 MHz crystal and I will get 80 position.
I can not know if I can use 64MHz crystal.
You say that is not accurate.
How I can measure its accurate?
Pekka Ritamaki |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19765
|
|
Posted: Mon Mar 17, 2025 6:17 am |
|
|
The point is the code you post, has the ADC using it's internal FRC oscillator.
That is what tADC_CLOCK_INTERNAL means. You don't want to do this. Instead
you should select FOSC/32 (for your 20MHz clock). ADC_CLOCK_DIV_32
This is note 4 in the data sheet for the ADC_CLOCK_PERIOD table:
Quote: |
When the device frequency is greater than 1 MHz, the FRC clock source
is only recommended if the conversion will be performed during Sleep.
|
|
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9445 Location: Greensville,Ontario
|
|
Posted: Mon Mar 17, 2025 11:14 am |
|
|
also.. unless you're using a precision VR for the Vref, no need for ADC=10 ! You need GREAT PCB layout, proper parts selection and EMI suppression to get reasonable 10 bit accuracy.
There are several ways to make the servo control better,either in code or components. adding a 2nd, small PIC as a 'servo controller' might cost $1, solve a LOT of code 'issues', gives far better response too !
hmm '40' is good ? 180* /40 = 4.5* per bit, my way 180*/1000= 0.18* per bit |
|
 |
|