MX027 - Controlling a Servomotor with timer0 interrupt.

Forum to discuss articles which have been published in Matrix Multimedia's article zone. If you have any comments or questions regarding the articles please post them here.

Moderators: Benj, Mods

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

MX027 - Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

Article: http://www.matrixmultimedia.com/article.php?a=435

If you have any questions for the author or comments on the article, please post them below.
Last edited by medelec35 on Mon Jun 20, 2011 7:24 pm, edited 4 times in total.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

I could write a tutorial on controlling multiple servos with just a timer0 interrupt similar to above post

It should be straight forward to control multiple servos for instance:
Robotics, RC planes, cars boats etc. just using the 16F88 that arrived with programmer EBlock EB006 (without the LCD)

If anyone is interested in such an article please let me know.

Thanks

Martin
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by JohnCrow »

Hi Martin
That simulator program is impressive. Think I'll be registering that myself,especially looking at the cost. Did see you originaly posted about it a few days ago.

Did find using your program (above) with internal osc selected, the LCD didn't display anything, but changing to xtal and recompilling the hex code it all worked fine.

I'll try it on real hardware later.
Thanks for sharing that with forums
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

JohnCrow wrote: Did find using your program (above) with internal osc selected, the LCD didn't display anything, but changing to xtal and recompilling the hex code it all worked fine.
Hi John.
Not at all. I have only used the simulator to give a screen shot of the waveform.
I tried with my hardware before posting the tutorial and LCD worked as expected with the configuration file set to INTRC as Port I/O

What you said is interesting, as I have just altered configuration to HS sent to chip and still works 100%
How odd?

But if I do experience what you have stated, at least you gave work around.
Thanks for that.

Hope tutorial is wrote in a way in which it gives an understanding.
I have never used a servo before, so on Saturday, I set my HPACT board up for the 1st time, and learned how to drive servos from
TEFWS.pdf which is posted here:
http://www.matrixmultimedia.com/mmforum ... 177#p11066
So im not an expert with servos, but probably got a good enough of an understanding now to answer any questions on driving them

Martin
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by JohnCrow »

Hi Martin

I was refering to the Realpic simulator program not showing on the LCD. Ive not had chance to try it on real hardware.
I dont have the HPACT board, but I do have the servo eblock and a couple of servos
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

Ah sorry I must of miss read your post.
Same applies the simulator. LCD works fine, if configuration was internal OSC or not. I don't think the simulator uses the configuration settings within the hex file anyway.
However to find that it does not matter for real hardware if internal or HS is selected was interesting, so I'm glad ive misread your post :P

A tip I find works when your setting up LED's, LCDs etc for the connections, is use the next follows up/down function.

Any servo should work, and in the tutorial, I have included a bit on changing the formula to change the timing of the pulses.
Useful if pulse duration is different from the 1.0ms to 2.0ms limits.


Martin
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

Forgot to add....
Although I am also impressed with simulator, and I pointed out a bug which meant IOC did not work, they did produce an update.
So IOC works on V1.3 as intended.
Since then they don't appear to reply to any emails (although not about bugs, but suggestions).
Their support is NOWHERE near as good a Matrix Multimedia. Don't even come close!
They don't even have a forum you can respond in.
But overall is worth purchasing since it runs in real time and you load it with hex files Flowcode creates.
But as I said earlier I don't believe it takes configuration settings into account.
This means if you enable the WDT and on real hardware you get watchdog resetting your hardware.
I don't believe the simulator will reset also.
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

Hello Martin,

Have you yet test the routine in actual hardware with servo?
Did it worked?
Also, ( excuse me if I am wrong but I am just a hobbyist!) in order to achieve the 0.1 ms interrupt interval shouldn't the interrupt frequency be set at 10 000 hz instead at 7812.500 hz as it appears in the interrupt macro properties in your routine?

Thank you,

Andreas Achilleos

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

Hi Andreas ,
That's a good question.
Reason that's a good question is because I have added a comment in the timer0 routine within the flowchart how the 10KHz = 0.1 ms is produced, but left that out of tutorial:
Timer0 Comments.jpg
Timer0 Comments.jpg (65.23 KiB) Viewed 44940 times
I have place several comments within the flowchart.
Yes I have fully tested on real hardware First using a servo. But I also scoped the waveform before connected up to the servo motor, just to be on the safe side.
When the pulse width duration displayed on the LCD agreed with the readings on the oscilloscope
medelec35 wrote: I am using a target device with an internal osc set at 8MHz.
Timer0 is set up to generate an interrupt every 0.1ms. so 20ms is 20/0.1 = 200 lots of 0.1 = Count value of 200. At this point portA0 goes from low to high.
So this 0.1ms will be the maximum resolution for the duration of the pulses generated every 20ms
I will add a bit more to the tutorial
Last edited by medelec35 on Mon Jun 20, 2011 10:31 pm, edited 1 time in total.
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

Hello,

:mrgreen: actually I looked at that comment but my brain didn't quite get the meaning!!!

So, when you wrote : tmr0=+81; in the C code box it sets the timer0 to have an interrupt frequency of 10 000hz?

Is there a manual where you found out that or just advanced programming skills! :)

THank you Martin.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

achillis1 wrote: So, when you wrote : tmr0=+81; in the C code box it sets the timer0 to have an interrupt frequency of 10 000hz?

Is there a manual where you found out that or just advanced programming skills! :)
You are correct.
C code box it sets the timer0 to have an interrupt frequency of 10 000hz
No I don't class myself as an advanced programmer.
I have read about setting tmr0 with a value from someware after reading a bit about timer interrupts,
But is was Sean which showed the fomat of tmr0=+any_byte_value
From here:
http://www.matrixmultimedia.com/mmforum ... f=5&t=6833

I have updated the tutorial, so it expains this point in more detail.

Martin
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

Thank a lot Martin.

By the way what kind of servo's do you use? Can you provide me with the store's name you purchased them?

Thank you

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by Benj »

Hello,

I use servo motors like the ones shown here.

http://www.amazon.co.uk/s/ref=nb_sb_nos ... vo&x=0&y=0

You may be able to get them cheaper by going somewhere like eBay.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

Hi Andreas,
medelec35 wrote:HPACT Motors board
6V DC supply for powering HPACT Motors board (or any suitable servomotor)
Sorry, I should have said it's from Matrix multimedia:
http://www.matrixmultimedia.com/product ... PHPSESSID=
Board is very useful for learning to control different motors.
Although you can buy them separately as ben has shown
I believe all servos could be driven in a similar manor.
When I get home I will post the model number of the servo that’s used.

Martin.
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

Thanks a lot guys!!!!

I will try the routine and let you know( if my servo is still functional) !

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by JohnCrow »

I picked up a couple of Futaba servos from the local model shop, these are well made and come with a packet of different arms to swop on the axle.Think they were about £8 each. These work with both Martins program and the flowcode macros using the servo E-Block.
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

Here is a basic independent 4 channel servo controller again using 16F88 with 8MHz internal and no PWM or servo component.
There is nothing at all wrong with servo components. Since they allow fine trim adjustments, and the resolutions are probably greater. I'm looking in to the servo component of Flowcode next.
Any of the 4 channels can also be trimmed, by adjusting the formula (which is shown in the tutorial in the 1st post.)
The reason I have chose to use timer0 instead is so a smaller cheaper PIC Chip can be use which does not contain PWM component (which I believe two are required) for the servo component?
AN0 - AN3 control servos connected to B0 - B3 respectively.

I have kept all the clever stuff to a minimum (which is good as I'm not clever anyway :P )
So you can simply see how to include more servos (or less) if required.

Martin
Attachments
Servo Controller 4 Channel V1 FC3 16F88.fcf
(13.1 KiB) Downloaded 1169 times
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

Hello,

I am trying to open your routine and I have an error!
I have attached.
Does anyone has a clue?

Thank you

Andreas Achilleos
Attachments
flowcode error.png
(42.89 KiB) Downloaded 33928 times

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

Hello,

I am trying to compile the first fcf file Servo Controller1 FC3 16F88.fcf and I get an error!

Funny thing, I tried that yesterday on my other PC and first ( due to the fact that I had messed a little bit with it!) I had the same error:''if your flowchart contais C code'', but after I downloaded it again and it was OK!
Now I keep getting the error and I have not altered the file.
Do you thing that PC configuration or folder location can cause these errors?

Martin, in your flowchart you state internal clock of the 16F88, so I must put the switch on the EB006 to the '' RC '' intead of '' XTAL '' ?

I didn't had the chance yesterday to test your routine when it was succesfully loaded into PIC due to trhe fact I do not have an ADC board!
Can your routine work with keypad instead?

Thank you.
Attachments
error.jpg
error.jpg (64.31 KiB) Viewed 44891 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

Hi Andreas,
The following warnings about ADC will be ignored etc, are there because a Flowchart created with Flowcode V3 is opened with Flowcode V4.

With V3 you need to sample ADC 1st then use another component to assign the value to a variable.

With V4 that's not the case. Sample part is not used, so it's simply ignored. Hence the warnings.


As for the Errors.
Common cause would be if target device is changed, and there is a component that had not reassigned.

Have you changed to a different target device?

If you would like to post the flowchart that's causing the issues, I can make sure it works on my system 1st.

As for the RC switch. On my EB006 I just moved the jumper that's next to the xtal from OSC to A6&A7 position

Martin
Last edited by medelec35 on Tue Jun 21, 2011 7:47 am, edited 1 time in total.
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

Hello Martin,

Thanks for the respond. The flowchart that is causing the erros is your own that I downloaded at the beginning of your post above!
As I told, on my other PC compiled just fine but in this one not, hence the questions I made if a PC configuration might be causing this.
I have not changed the target device.

Can I use that flowchart on 16F877A or 18F4550?

Thank you.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

I was just wondering if you change target device.
But If you have not then that's odd?
Yes you can use any target device. I have successfully use a 18F4455 to drive servo, using flowchart posted.
Try this version that I have saved with with FC4
Attachments
Servo Controller 4 Channel V1 FC4 16F88.fcf
(15.43 KiB) Downloaded 1152 times
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

This one compiles just fine!
Of course I do not have an ADC board so I can test it!
I will try to modify it and work with a keypad, do you think that would be easy?

I have one issue though and that might be happening because the servo board and or flowcode routine does not actually work as it should have. When I connect the power to my EB006 then the servo moves righwards, lets say. If I unplug the power and replug it then the servo moves the other way and this happens every time.
Do you get this when connecting the power?

Also where do you have the swithes on the EB006 on RC and slow?

Thank you.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by medelec35 »

medelec35 wrote:As for the RC switch. On my EB006 I just moved the jumper that's next to the xtal from OSC to A6&A7 position
With the jumper in this position, it does not matter what positions the two switches are, as I believe they are effectively disconnected from the microcontroller.
No you don’t require Analogue on the target device.
The servo will work from byte variable 0 – 255
So this can be from a keypad ADC etc.
If using a keypad this would probably be the best way:
http://www.matrixmultimedia.com/mmforum ... 140#p18842
However the formula used will probably have to be changed to suit.
For example. Currently for servo to rotate to full CW byte = 0
for servo to rotate to full CCW byte = 255
With keypad, formula can be change so 1 to 20 can be used.
1 = full CW
20 = full CCW

2 – 19 = positions in between.
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Post by achillis1 »

As for the RC switch. On my EB006 I just moved the jumper that's next to the xtal from OSC to A6&A7 position
I think that I do not have such jumper!

Also, why change the formula for the keypad and have the 2-19 in between positions and not to have the 2-254?

Thank you!

Post Reply