Simple servo program

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Simple servo program

Post by TomasS »

Hi

I would like to make a simple servo program on a ATmega32 platform.

I would like it to have 2 push-button inputs so that if no button is pressed the servo is positioned in the middle position. Pressing the first button, the servo turns to the right position and pressing the second turns it to the left. If no button is pressed, the servo resets to the middle position again.

Is it possible, that one of you good people could help me making such a program? It would even be better if the program could run by interrupt :)

Thanks in advance

Tomas

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: Simple servo program

Post by Benj »

Hello Tomas,

Are you using Flowcode v3 or v4? v4 includes a servo component allowing easy and straightforward control of servo motors.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Simple servo program

Post by TomasS »

Benj wrote:Hello Tomas,

Are you using Flowcode v3 or v4? v4 includes a servo component allowing easy and straightforward control of servo motors.
I'm using V4. I have tried to play around with the macros, but are not that familiar using macros yet.

BR.
Tomas

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: Simple servo program

Post by Benj »

Ah great well that makes things easier.

You should be able to look at the servo component help file for an explaination of the different macros and their function.

A simplified version of the program might look like this.

while 1
{
Read Switches into a variable
If variable = 0 yes: move to position 128
If variable = 1 yes: move to position 0
If variable = 2 yes: move to position 255
delay 10ms
}

Or using a switch icon.

while 1
{
Read Switches into a variable
Switch Variable
{
0: move to position 128
1: move to position 0
2: move to position 255
}
delay 10ms
}

Doing the program like this means you can also press both switches and this will return 3 so your servo should stop where it is.

Changing the delay will have the effect of changing the speed the servo moves.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Simple servo program

Post by TomasS »

Benj wrote:Ah great well that makes things easier.

You should be able to look at the servo component help file for an explaination of the different macros and their function.

A simplified version of the program might look like this.

while 1
{
Read Switches into a variable
If variable = 0 yes: move to position 128
If variable = 1 yes: move to position 0
If variable = 2 yes: move to position 255
delay 10ms
}

Or using a switch icon.

while 1
{
Read Switches into a variable
Switch Variable
{
0: move to position 128
1: move to position 0
2: move to position 255
}
delay 10ms
}

Doing the program like this means you can also press both switches and this will return 3 so your servo should stop where it is.

Changing the delay will have the effect of changing the speed the servo moves.
Thanks for your help Ben. Now I can do a simulation with a working servo (See attachment). Now it actually works - in target as well (remembering the ENABLE-servo macro :lol: ).

Is it possible to run these macros as interrupt-routines?

BR.
Tomas

P.S.
Can I find a description on the settings of the different macros somewhere?
Attachments
servo.fcf_avr
(8.5 KiB) Downloaded 369 times

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: Simple servo program

Post by Benj »

Hello,

Program looks good :)

Component help can be found by clicking your component on the panel and then cclicking the "..." button next to the Help item in the properties side pane. Vista and Win7 users will have to follow this topic to enable Windows help support. http://www.matrixmultimedia.com/support ... f=31&t=606

The easiest way to get your timings correct is to run a simple while 1 loop that switches on a LED, waits a second, switches off the LED and again waits a second. This way you can confirm if the hardware is running at the expected speed.

I think AVRs have a funny configuration setting that is enabled by default that makes the core run 8 times slower then expected. Disabling this config setting should allow the device to run at full speed. Warning here that there is also a config setting that enables programming via SPI. if you disable this setting then you will not be able to reprogram your device.

Let me know how you get on.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Simple servo program

Post by TomasS »

Let me know how you get on.[/quote]

Hi again
Benj wrote: Program looks good :)
Thanks! 8)
Benj wrote:Component help can be found by clicking your component on the panel and then cclicking the "..." button next to the Help item in the properties side pane.
Nice - quite simple actually. But what is the trim of the servo? And what is the difference between SetPosition, MoveToPosition and AutoMoveToPosition?

Benj wrote:The easiest way to get your timings correct is to run a simple while 1 loop that switches on a LED, waits a second, switches off the LED and again waits a second. This way you can confirm if the hardware is running at the expected speed.
The KISS way is the way to do it 8) I Will try that out!
Benj wrote:I think AVRs have a funny configuration setting that is enabled by default that makes the core run 8 times slower then expected. Disabling this config setting should allow the device to run at full speed. Warning here that there is also a config setting that enables programming via SPI. if you disable this setting then you will not be able to reprogram your device.
That can really ****** up a lot of chips! I will keep that in mind!

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: Simple servo program

Post by Benj »

Hello,
But what is the trim of the servo? And what is the difference between SetPosition, MoveToPosition and AutoMoveToPosition?
Servo trim is a way to fine tune the servo position. eg if you have servos at say position 128 then they might not all be exactly in the same position. The trim allows you to fine tune to get them all into the same position.

SetPosition - Moves directly to the position specified

MoveToPosition - Moves one unit towards the specified position, must be called repeatedley to acheive your desired position but allows for smoother movements

AutoMoveToPosition - Moves one unit towards the specified position everytime the background servo control interrupt fires, allows for smooth movements without having to repeatedly call the macro.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Simple servo program

Post by TomasS »

Benj wrote:Hello,
But what is the trim of the servo? And what is the difference between SetPosition, MoveToPosition and AutoMoveToPosition?
Servo trim is a way to fine tune the servo position. eg if you have servos at say position 128 then they might not all be exactly in the same position. The trim allows you to fine tune to get them all into the same position.

SetPosition - Moves directly to the position specified

MoveToPosition - Moves one unit towards the specified position, must be called repeatedley to acheive your desired position but allows for smoother movements

AutoMoveToPosition - Moves one unit towards the specified position everytime the background servo control interrupt fires, allows for smooth movements without having to repeatedly call the macro.
Hi again
Thank you for clearing that up for me - I'll have to "play around" with the different macros to see the differences - But now I am a little wiser :)
Do you have an example on the use of the Servo trim macro?

BR.
Tomas

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Simple servo program

Post by TomasS »

Hi Ben

I have experienced that you have to lower the clock frequency when using multiple servos compared with driving 1 servo.

Why is it so?

BR.

Tomas

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: Simple servo program

Post by Benj »

Hello Tomas,

This should not be the case? How do you mean lower the clock speed? Do you mean changed the crystal value or changed the Flowcode project options clock speed or both?

I have used a 8 channel servo controller with an ECIO running at 48MHz without any issues. What device are using using? Is there maybe anything else in your program eg timer interrupts or other interrupts or components that may be effecting the interrupt timing?

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Simple servo program

Post by TomasS »

Benj wrote:Hello Tomas,

This should not be the case? How do you mean lower the clock speed? Do you mean changed the crystal value or changed the Flowcode project options clock speed or both?

I have used a 8 channel servo controller with an ECIO running at 48MHz without any issues. What device are using using? Is there maybe anything else in your program eg timer interrupts or other interrupts or components that may be effecting the interrupt timing?
Hi Ben

It's running on an ATmega32. The target is a STK500 platform. It is the Flowcode project options clock speed that has to be lowered when controlling multiple servos running in the STK-board. Running 2 servos the frequency is typical 5 MHz. When controlling 6 servos the frequency has to be lowered to 3.8MHz.

BR.

Tomas

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: Simple servo program

Post by Benj »

Hello Tomas,

Hm thats a very odd one.

First of all what crystal value are you using on the STK500 board? This should be the specific value you enter into the Flowcode clock speed text field.

The servo component is always controlling 8 motors whether they are enabled or not so adding extra channels should not be effecting the timings or latency. Have you made sure that the component connections etc are correct for your hardware.

Have you got an oscilloscope or other device that will allow you to monitor the timings of the signals to see if we can work out what's happening on your hardware.

Post Reply