Page 2 of 2

PWM engine for a servo

Posted: Thu Mar 06, 2008 11:19 am
by Sean
I have compile a PIC16F877a program with a large array. The compiler generates a message indicating that the largest block of data it can allocate on the heap is 110 bytes. Arrays larger than this will probably be accepted by the compiler if there are no errors in the C code.

You are probably experiencing run-time errors if your program is trying to use memory that is not available. The effects of this will be unpredictable without an operating system to trap the error.

If the data in your array is not to be changed during execution of the program, you should consider storing it in program memory. This will require a block of C code.

The 256 bytes of on-chip EEPROM are another storage option. There is a Flowcode component that provides all the necessary functions.

Re: pwm examples needed

Posted: Thu Mar 06, 2008 12:59 pm
by chevy6600
HI sean, thanks again for the input, I confer with your point on a max of 110 that i can use, after some time experimenting i found that a newly made up prog would accept a greater amount than the 110 but in my current prog my max was brought down to 111, it took me awhile finding this out as i had to add in a few at a time and compile to the development board over and over again until i reached the max!...strange thing is i could not use the original variable! even after deleting it first!
You mention "You are probably experiencing run-time errors " do i need to do something here?. should i be concerned? i am a beginner! i`m afraid.
Although i have settled on a lower resolution..110 from the 255...and have got a program with 2 servo`s working off 2 pot`s, i think i ought to have a look at placing the table elsewhere as you suggest, if not for this table i think later on i shall need to do it. Can you link me to a prog example for me to look at?, as i am not sure how to do this. I am not too keen on getting involved with C as it`s something else i have to learn!...more problems for me...
I see the eeprom component but do not now what to do with it?
Also, would using the eeprom cause a slower program than the way i have set mine up?
I have attached my version of your prog 2 for you to pass judgement. I have put in an extra servo potentiometer,added servo end stops, moves servo`s in a log way from my `table data` then displays the servo positions onto the LCD.
Perhaps you could suggest whether i have arranged it in a sufficiently efficient manner. Is it better to place the servo routines one after the other as i have done?, or do sampling of the adc`s together, sending the info together, moving servo`s together? etc..etc.

change of subject: the forum `logging on` has a time limit, this is a real pain, the amount of times i have to redo my message because i forgot that i have only a short time before every thing i wrote is lost....well!... i have used a few juicy words!! . Can something be done to keep logged on?

Re: pwm examples needed

Posted: Thu Mar 06, 2008 2:01 pm
by Benj
Hello Chevvy

By Run time errors Sean means bugs in the running code. It will not damage your ardware but it may give you incorrect results.

If everything is running as it should then you do not have to worry about run time errors. Its only when things start acting perculiar that this comes into play.

With the EEPROM component you can write a program to store the values byte by byte. Use the hardware macro and refer to the component help if you need to.

Then when you reprogram the chip with your actual program code the data will still be available in the EEPROM. This means you can read it out as need be.

This will probably be slightly slower then reading data from an array but not by much.

Essentially your programming structure is up to you. However it is good practise to break up each function into a set of tasks or Macros. Eg sample the ADCs, move the servos etc.

I've also edited the timeout setting for the forums so you should no longer have any problems. I had to lower it before in an attempt to get our old server running correctly.

Re: pwm examples needed

Posted: Thu Mar 06, 2008 2:46 pm
by Sean
I have taken a copy of your program. I will post some modified versions, in the next few days, illustrating the alternative data storage methods I have suggested.
The C based option will not contain any 'real' C. Just an instruction to the compiler to include a set of data values in the program memory.

Re: pwm examples needed

Posted: Thu Mar 06, 2008 2:54 pm
by chevy6600
Thanks for that benj
That would be great sean, looking forward to it. Thanks.
Your all doing a grand job, ...ask for a pay rise :mrgreen:

Re: pwm examples needed

Posted: Sun Mar 09, 2008 11:40 pm
by chevy6600
Hi guys, well to start off i`m feeling rather chuffed as i have made my program that loops, counts, jumps, with macros, from tables, moves servos in response to potentiometers, then adjusts the servos to react against vehicle momentum and time, move servos in a non linear way, then print position of the potentiometer along with the servo position onto the Lcd screen. and tested on the v3 development board, pic 16F877a 8)

I must say that you chaps have made a cracking program (flowcode), i have looked at the compiled c code and hex file and can see the amount of data that was produced. If it were not for flowcode there is no way that i myself could of learnt or manage to follow my way around, in doing my program in C. :|
Well done chaps.

I need to take my project to the next stage, making the electronics, i have focused on using the PIC 18F2620, surface mount devices, as the elect. board has to be as small and light as possible as the project i`m doing is a self flying platform.

I would appreciate if you could answer a couple of questions concerning setting up the pic 18F2620.

Q. i`m trying to setup flowcode with a chip speed with the internal oscillator and the `pll` multiplier to give me 32mhz. when i select `chip`, `configuration ` in flowcode, the `internal` clock selection is greyed out. Looking through the expert setup (or PPP), i do not see anything that relates to what i read in the pic 2620 data sheet which requires various things to be setup to allow the 32 mhz. In PPP I have 12 choices in the `oscillator` section but which one is right for me :?:

Q. My program is based around your `multiservo 2` program, so if i just transfer the program to the PIC 2620, will running at 32mhz upset the pwm duty cycle/ length of pulse?. I`m guessing it will so can you please let me know what part i need to alter.
Thanks.

Re: pwm examples needed

Posted: Mon Mar 10, 2008 5:39 pm
by Sean
Congratulations on your success :D

The oscillator mode you require for the PIC18F2620 is achieved in 3 steps.

1/ Select one of the INT oscillator modes in the chip configuration panel. The INT RC-CLKOUT option makes the internal clock available on pin RA6. The INT RC-port option makes the internal clock unavailable externally.

2/ Select the 8MHz clock option. The chip powers up with the internal clock set to 1MHz. The program must set this to the 8MHz option by writing the hexidecimal value 0x72 to the special function register OSSCON.

3/ Enable the PLL to produce the 32MHz clock signal. This should be achieved by writing the hexadecimal value 0x40 to the special function register OSCTUNE.

Steps 2 and 3 should be achieved from Flowcode with a C code block with the following contents:

osscon = 0x72;
osctune = 0x40;

This seems to compile, unfortunately we don't currently have a suitable device to test the result on. Could you let us know if the system appears to be running at the correct speed.

The code for the MultiServo2 program will require some minor modification due to the change of clock speed. I don't think the changes will be too serious, so I should be able to post a test program soon.

One point to note is the fact that the frequency of the internal oscillator is not as stable as that of an external xtal. Servo positions will be sensitive to temperature drift of the internal oscillator. Check your application requirements against the oscillator characteristics in the device data sheet.

Re: pwm examples needed

Posted: Mon Mar 10, 2008 5:54 pm
by chevy6600
Hi sean, i have not yet absorbed the post you just sent so i`ll come back on that in due course, so thanks for looking at that problem. In the meantime i have another question that i hope you can answer. I`m trying to setup PIC16F877A to have it`s pin 5 to be the vref+ , so reading a few posts here, i did the following.

copied the `fcd` file 16F877a and called it 16F877a_vref
edited it to have an extra line in the [device] section to read `ChipName=16F877a` (note i did not add `vref` at the end!)

then edited the line

`ADCCapture="char ta, te, cnt;\nadcon1 = 0x00;\nta = trisa;\ntrisa = trisa`
to read
`ADCCapture="char ta, te, cnt;\nadcon1 = 0x01;\nta = trisa;\ntrisa = trisa`

the problem is: i can see the 16F877a_vref when choosing the chip in the pull down menu but when i try to look in the chip/ configuration settings i get a message that says `unrecognised chip on command line <16F877a_vref>.

If i then sellect `ok`, i then can see the configuration setup, but the file chip 16F877a_vref is not listed !
so i presume the rest of the settings in the configuration setup are also wrong!. :cry:

If i look in the expert setup the chip 16F877a_vref is not listed.

the post i read to get the info from was here
http://www.matrixmultimedia.com/mmforum ... hilit=vref
can you please help, thanks

Re: pwm examples needed

Posted: Mon Mar 10, 2008 6:22 pm
by Sean
Here is a copy of 16F877a_vref.
16f877a_vref.fcd
(6.41 KiB) Downloaded 1127 times
If there are any problems with the ChipName line, or it is in the wrong place, the option defaults to the fcd file name.

Re: pwm examples needed

Posted: Mon Mar 10, 2008 6:24 pm
by chevy6600
thanks sean. 8)

Re: pwm examples needed

Posted: Tue Mar 11, 2008 11:30 am
by chevy6600
Hi sean,
sean wrote:..................osscon = 0x72;
osctune = 0x40;

This seems to compile, unfortunately we don't currently have a suitable device to test the result on. Could you let us know if the system appears to be running at the correct speed.
If i am right, the lines of code above, which contain `osscon`, has a typing error, i believe it should read `osCcon`, i am not an expert so if i am wrong please let me know.
It will be awhile yet before i get the parts, so i`ll come back to you on the result in due course.
sean wrote:One point to note is the fact that the frequency of the internal oscillator is not as stable as that of an external xtal. Servo positions will be sensitive to temperature drift of the internal oscillator. Check your application requirements against the oscillator characteristics in the device data sheet..
I forgot about that but i shall still try this way as thinking about it, it is the sensors which are keeping things central and the servos are constantly adjusted until the sensor requierments are met....i`m trying to minimise on component count! :lol:

Re: pwm examples needed

Posted: Tue Mar 11, 2008 1:33 pm
by Sean
You spotted my deliberate mistake!

Yes, the register name should be osccon.

I will probably have access to a chip that can be used to confirm these settings in the next few days.

Re: pwm examples needed

Posted: Wed Mar 19, 2008 1:01 pm
by chevy6600
Hi sean I know you guys are buisy!..you all seem to be posting at all kinds of hours, but a while back you mentioned about a mod to the PWM prog ,
sean wrote:The code for the MultiServo2 program will require some minor modification due to the change of clock speed. I don't think the changes will be too serious, so I should be able to post a test program soon.
. Any chance of having a look some time?, I`m going to the states for 3 weeks, so it would be cool if it could be done while i was away. :mrgreen:
Though there does seem to be quit an interest with this post looking at the amount of hits it has, so if you posted sooner than later i`m sure others would get a benefit :lol:

Re: pwm examples needed

Posted: Tue Apr 01, 2008 10:16 am
by Sean
I have modified the servo program to run on a PIC18F2620, using the internal 8MHz oscillator, and configuring the PLL to run at 32MHz.
MultiServo2_Fx620.fcf
(10.49 KiB) Downloaded 894 times
The program was tested on a PIC18F4620 and has been successfully re-compiled for the 18F2620, but not tested.

I have attached the configuration screen as a jpeg. All the options below the ones shown are set to 'disabled'
Config.JPG
Config.JPG (83.78 KiB) Viewed 26038 times
I have also implemented two large data arrays using the rom char *var = {...}; command. The arrays are limited to 256 bytes each, but are placed in program memory.One is defined in a Flowcode block, the other is included as a separate header file - curve_data.h.
curve_data.h
(1.24 KiB) Downloaded 740 times
The file should be placed in the same folder as the main Flowcode program.Inclusion of the header file is controlled by the #include "curve_data.h" command in the Edit->Supplementary Code menu. This is only included as an example and can be removed by deleting the supplementary code command.
The array data is referenced by the ADC value to modify the servo position curve for Servo7. This example compresses the servo movement into the first and last quarter of the adjustment range, with a dwell around the centre position.

Flowcode for AVR users can use a similar technique with the const char var[] = {....}; command.

Any further postings regarding data storage should be made in a new topic.

Re: pwm examples needed

Posted: Sat Apr 12, 2008 2:30 pm
by chevy6600
HI sean, i see you have been buisy coding. i see you have also put together a way of using a large array, thanks for that. Now that i`m back from my holiday i`m keen on cracking on with my project so i shall be buying the bits and bobs soon, then i have to tackle the design and manufacture of the pcb :| so could be a while before i can try out the prog you gave using a 18F2620, i`ll let you know how i get on sooner or later.
Thanks again for your imput.

Re: pwm examples needed

Posted: Sat Oct 04, 2008 12:49 pm
by chevy6600
Hi all, if you guys are looking for an update to this thread i put together a program `PWM engine for servo`
which included explanations + instructions inside the program. I made a post which included the program attachment and an`pic 18F2620.fcd` attachment which was altered for a `+vref and -vref`.
It was decided that the post was better placed elsware in the forum rather than placing it here so if you fancy
having a look, here is the link.
http://www.matrixmultimedia.com/mmforum ... =26&t=4937

Re: pwm examples needed

Posted: Thu Dec 23, 2010 6:48 pm
by TomasS
Benj wrote:Hello Chevvy

We have a 8 channel servo example that Sean has put together. This uses the Timer1 module and two hardware capture compare interrupts.

The CCP1 Interrupt controls the individual Servo positions and the CCP2 interrupt controls the overall cycle of the timer1 counter (eg to fit in 8 different servos)

The outputs are driven from the CCP1_isr macro. This currently uses PortC as the output port. Eg servo0 would connect to RC0 and so on.

The servos are each controlled by an array of bytes. Eg to change the position of Servo0 you would change the variable servo_pos[0] = 0 using a calculation icon.

The example uses a 16F877A running at 19.6608MHz. A 16F88 will not work as it only has the CCP1 module. (eg PWM channel 1)

Hope this helps.

Let us know if you have any problems.
Hi

Is is possible to have en examble file made for an AVR controller, ex. the ATmega32

Re: pwm examples needed

Posted: Fri Dec 24, 2010 9:06 am
by Sean
The servo component in V4 versions of Flowcode (AVR,PIC and ARM) is an improved version of the original example program. It automatically adjusts to accommodate a wide range of target devices and clock speeds.

Re: pwm examples needed

Posted: Mon Jan 03, 2011 8:29 pm
by TomasS
Sean wrote:The servo component in V4 versions of Flowcode (AVR,PIC and ARM) is an improved version of the original example program. It automatically adjusts to accommodate a wide range of target devices and clock speeds.
Hi Sean

No question asked! But now you can add 1 - 8 servo from the Mechatronics menu, but you still need the PCM- signal to be generated to move them...

Have I looked in the wrong menues?

BR.

Tomas