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

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Tue Jun 21, 2011 10:19 am

I will attach a modified version of your above flowchart in order to work with a keypad.
Can you take a look at and probably test in real life? :D
Attachments
Servo Controller 4 Channel V1 FC4 16F88 modified keypad.fcf
(17.34 KiB) Downloaded 1213 times

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Tue Jun 21, 2011 10:26 am

When the keypad value is 255 the the pulse width is 19...is this correct or it should have been 20?

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Tue Jun 21, 2011 10:38 am

OK, attached is the flowchart again but now I have assigned the keypad and servo board ( previous flowchart keypad was unassigned and the output was at port B now it is in portA)
I have deleted the ADC components.

I think that now IS WORKING!!!!!!

The servo responds to keystrokes but the only problem is that the servo arm does not utilize the whole workable radius, let me say. ==> It is a 180 degrees servo and at key value 1 it sets at say: 15 degrees and at keyvalue 250 sets at 160 degrees.
The values above for the degrees are just an eye measure estimate not true!

BUT even still many thanks to Martin. :D
Attachments
Servo Controller 4 Channel V1 FC4 16F88 modified keypad.fcf
(15.5 KiB) Downloaded 1255 times

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby Benj » Tue Jun 21, 2011 10:49 am

Hello,

For the entire range you will need to be able to set the control value to 0 and 255. You will probably not be able to move the motor over the entire 180 degree range between physical end stops but you can get close.

The tune property for the servo component can also be used to adjust to any offset say if you have several motors and you need the outputs aligned exactly. This will have the effect of moving more towards one of the end stops.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Tue Jun 21, 2011 11:32 am

Because using timer 0 which is triggered every 0.1ms
and pulse duration for servo has to be between 1.0 and 2.0ms
This means you can only have up to 20 steps.
That’s why I suggested 1 – 20.
You don’t have to stick with that of course, it was just an example.
1 – 254 is fine but with 20 steps , 254 would leave servo in same position as 253 ,252 251 etc.
You can test this by placing the formula in a spread sheet, and use values from 1 – 254 and observe the results. The servo will only move from when
I plan on making a higher resolution servo driver in the near future.
However this was originally set up as a tutorial, so it was more for learning than producing a higher resolution controller.

Re Jumper location:
Jumper setting.jpg
Jumper setting.jpg (75.23 KiB) Viewed 42232 times
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Tue Jun 21, 2011 12:04 pm

I have modified flowchart (by altering formula) to allow keypad entry for direct degrees
So 180 on entered on keypad should cause servo to rotate 180 deg, 090 on keypad = 90deg ect.
I’m unable to test myself yet, so can’t guarantee will work.

Martin
Attachments
Servo Controller 4 Channel V1 FC4 16F88 modified keypad.fcf
(15.5 KiB) Downloaded 1241 times
These users thanked the author medelec35 for the post:
achillis1 (Tue Jun 21, 2011 2:57 pm)
Rating: 5%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Tue Jun 21, 2011 3:06 pm

Thanks Martin,

Of course I take your tutorial as tutorial to learn and experiment and will not just hijack ideas!!!!

Concerning the EB006 board, the jumper is not present because I think that the board is from an old batch. Maybe Ben can confirm that.

About the flowchart, I would gladly test it myself and tell you the results!

Thanks a lot!!!

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Tue Jun 21, 2011 3:21 pm

Your welcome.

achillis1 wrote:Thanks Martin,

Of course I take your tutorial as tutorial to learn and experiment and will not just hijack ideas!!!!


I mentioned the fact it's a tutorial, not because of hacking ideas, but because the resolution or number of steps is lower than the flowcharts with servo components. I did not want anyone thinking this works as well.
Anything I post I don't mind people using, if they find posts useful.

Hope it does what you want it to do anyway!
I will test it myself later tonight.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Tue Jun 21, 2011 7:52 pm

Hello Martin,

I hope I have not offended you by mentioning the ''hijacking'' of ideas.
I was referring only to myself and that I am not trying to leech only from other person's posts, flowcharts or other, and try to contribute and ''grow'' WITH the knowledge and help from others!

Regards,
Andreas Achilleos

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Tue Jun 21, 2011 8:08 pm

The modified flowchart works Martin!

About the steps of a servo, you believe that through timer0 can a servo can achieve smaller number of steps?
I have never succeded in working with the flowcode servo component! Only now with your flowchart!!!!

Andreas Achilleos

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Tue Jun 21, 2011 9:46 pm

No offence was taken at all.

I believe the best I could to is possibly 50 steps which equate to a resolution of about 3.6 deg.

At least that would be x5 the current steps.
The only way there could be more steps is by using a target device with PLL.
Sadly 16F88 does not.
However the 18F4550 does have PLL so more steps, hence higher resolution can be acheived,


Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Wed Jun 22, 2011 9:03 am

Do you have any tips or hints about the 18F4550 and how to achieve the higher resolution steps. Maybe I could work a little bit on it!

I am attaching your flowchart modified, with a more easy control of the servo just for someone to be able to examine the servo movement more easily.
Key 1 pressed servo moves at one direction, key 2 servo moves at the opposite side, key * pressed the servo goes completely at the end and key # pressed the servo goes completely at thestart position.

I have noticed that my servo moves 2 steps more than 2 ms at 2.2 and 2 steps more than the 1 ms at 0.8 !

Andreas Achilleos
Attachments
Servo Controller 16F88 with keypad.fcf
(15.5 KiB) Downloaded 1204 times

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Thu Jun 23, 2011 5:14 pm

I have got a 18F4455. (can be very easily adapted for 18F4550)
So I will be using 19.6MHz xtal and osc configuration will be in PLL mode to run clock osc at 48MHz.
Unfortunantly unlike newer devices with PLL, I don't beleve 18F4xxx with work with internal osc and PLL. So to produce osc of 48MHz the xtal is required.
Resolution will be in 1deg steps.
There will be a keypad and LCD.
*key is to correct wrong entry and #key to Enter.
E.g 10# will cause servo to rotate to an angle of 10deg
1# will cause servo to rotate to an angle of 1deg
If you want 143 deg and you enter 146, then press*3 then # to enter.
Would this be of some interest?
I will post it later tonight.

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Thu Jun 23, 2011 10:44 pm

As promised he is the servomotor controler with a much higher resolution. Target device is 18F4455
At the moment the resolution is not quiet 1deg, because of the rounding of the pic maths.
The interrupt frequency is 100KHz.
So 1ms = count of 100, 2ms = count of 200 etc
As explaied you just enter the required degrees on the keypad # = Enter
* = cancel digit
if you press 235 then press ** (star twice) then just 2 with be displayed on display since both 3 and 5 have been cancelled.
Error trapping will prevent you from entering >180 on the keypad
If you want 0,1,2 deg etc. just enter the single digit then # e.g. 2#

I have tested on hardware before posting Flowchart.
achillis1 wrote:I have noticed that my servo moves 2 steps more than 2 ms at 2.2 and 2 steps more than the 1 ms at 0.8 !

Andreas Achilleos

Yes I also noticed that when I was 1st experimenting with servomotor, but I have kept to 1.0 - 2.0 as that's what is on the information sheet for the servo.
So 180deg is probably not a true 180deg?

Martin
Attachments
Servo Controller Kepad V1 FC4 18F4455.fcf
(19.81 KiB) Downloaded 1233 times
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Fri Jun 24, 2011 11:44 am

Hello Martin,

Would this be of some interest?


Are you kidding? This is wonderfull! You should be working for Matrix!!!!! :lol:

I have asked you before about the higher resolution servo in order to provide in any way any help to you but seems that you are "light years" ahead! :) You just did the flowchart!

Still I haven't tested yet tour flowchart but going to in a little bit.
Can it run in the 18F4550 or 16F877A PIC ?

So 180deg is probably not a true 180deg?

Not sure how you mean it but actually sticking to 1ms - 20 ms my servo was rotating 90 degrees and if I used 0.8ms - 1.2ms then the angle span was a little bit more(2 steps right-2 steps left)

Thanks again for your valuable contribution Martin!

Andreas Achilleos

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Fri Jun 24, 2011 12:04 pm

Hello Martin,

I have changed the device to 18F4550 and downloaded it succesfuly. The LCD shows the message but it respons in a very slow manner and when it finally sents the angle to the PORTA servo does not operate.
Note: the servo is not initiallised on PIC power up ( does stay still when try to move it by hand)
Any clues?

Thank you,

Andreas Achilleos

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Fri Jun 24, 2011 12:31 pm

I have set a break point on the flowchart after the portA is raised to 1 and the flowchart does not get there not even after I have set the angle!
I will dig through and see I I find something wrong.

ANdreas Achilleos

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby Benj » Fri Jun 24, 2011 1:02 pm

Hello,

From my experience the servo component functions really well. Is there a reason for all this effort trying to re-work it? You never really want to be pushing the motor right up to its end stops anyway as the control mechanism will struggle and may even burn itself out before too long.

A 180 degree servo motor will never actually move a full 180 degrees unless you are really pushing the control mechanism to its limits which is not advisable.

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Fri Jun 24, 2011 1:13 pm

Martin,

Regarding your macro for timer_0 tick, is there any special reason why you have included a decision icon which states when the count2 is over 200 then it steps over the calc. icon that increments count2?
At the latest version of flowchart that you uploaded you state that when count2 is over 2000 then count2 = 2000.
DO you have to do that since if pulse width is always > than 0 then the count2 never reaches 200 or at the other version over 2000.

Even still if the pulse width is zero and count2 continues to increment, then at some point it will overflow(I think) and start from zero again!

Andreas Achilleos

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Fri Jun 24, 2011 1:41 pm

@Ben, I’m only posting for education purposes and for people with target devices that do not have two PWM’s within chip.
In reality I would use the dedicated servo component within Flowcode.
@Andreas sounds like a timing issue. If you post your modified flowchart I can see if I can spot something.
If I was you I would
1) do the 1Hz test i.e. toggle a pin for 500ms on and 500ms off.
2) If you are able to measure the generated pulse before connecting to server then I would do that 1st.
I do agree with what Ben is saying about using the dedicated servo component within Flowcode.
Since you have a 18F4550, this should not be an issue at all.
As for count2. It’s vital that it does not rollover
So value of >2000 was just a random value it should never reach.
200 was chosen before was another random number.

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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

Re: Controlling a Servomotor with timer0 interrupt.

Postby achillis1 » Fri Jun 24, 2011 1:46 pm

Hello Ben,

I have never used the servo component with the analogue board as the Matrix examples. However I tried to replace the ADC with keypad but I have never succeded in moving the servo! I do not know why!!!!!!
Is it possible to send me a simple flowchart for the 16F877A or 16F88 which controls a servo via a keypad using the servo component?

Andreas Achilleos.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: MX027 - Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Tue Dec 20, 2011 9:56 pm

Here is a 1ms - 2ms 185 step servo control using a 12F675. I want to thank helesmes for the inspiration. I based the idea from interrupt of his flowchart.
Since 185 steps are used to get from 1ms to 2ms a keypad can be used for direct degrees input.
if the formula

Code: Select all

 Delay = Delay*73/100
is removed and delay variable is set from the keypad. 0 to 180. Formula is there for controlling servo with a pot.
This can be used with most target devices, so long as using a 19.660800MHz crystal is used.

Martin
Attachments
POT cont185 step Servo based on timer0 12F675 .fcf
(6.5 KiB) Downloaded 1169 times
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

shanck
Posts: 1
Joined: Sat May 09, 2015 10:46 am
Contact:

Re: MX027 - Controlling a Servomotor with timer0 interrupt.

Postby shanck » Sat May 09, 2015 10:48 am

how did you find the value you have to set for tmr0 in the datasheet.

I'm using the PIC16f1828 but I can't find it.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8604
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2476 times
Been thanked: 3558 times
Contact:

Re: MX027 - Controlling a Servomotor with timer0 interrupt.

Postby medelec35 » Sat May 09, 2015 4:11 pm

Hi shanck,
It boils down to getting a rough value, then trial and error as there will be an osc tolerance.
For the rough value, I developed an excel sheet that I have posted here
It would be best if you had a scope to measure the interrupt frequency.
This can be done by placing an output which is toggled every time interrupt service routine (ISR) macro is accessed.

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.