<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://miosix.org/wiki/index.php?action=history&amp;feed=atom&amp;title=Servo_tutorial</id>
	<title>Servo tutorial - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://miosix.org/wiki/index.php?action=history&amp;feed=atom&amp;title=Servo_tutorial"/>
	<link rel="alternate" type="text/html" href="https://miosix.org/wiki/index.php?title=Servo_tutorial&amp;action=history"/>
	<updated>2026-05-06T04:33:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>https://miosix.org/wiki/index.php?title=Servo_tutorial&amp;diff=97&amp;oldid=prev</id>
		<title>Andreabont at 20:47, 22 April 2014</title>
		<link rel="alternate" type="text/html" href="https://miosix.org/wiki/index.php?title=Servo_tutorial&amp;diff=97&amp;oldid=prev"/>
		<updated>2014-04-22T20:47:02Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 20:47, 22 April 2014&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l54&quot;&gt;Line 54:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 54:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The &amp;#039;&amp;#039;waitForCycleBegin()&amp;#039;&amp;#039; member function can be called by only one thread at a given time, so if you want to control more than one servo, be sure to put all the control code into only one loop. The other member functions are thread safe.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The &amp;#039;&amp;#039;waitForCycleBegin()&amp;#039;&amp;#039; member function can be called by only one thread at a given time, so if you want to control more than one servo, be sure to put all the control code into only one loop. The other member functions are thread safe.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:API]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Andreabont</name></author>
	</entry>
	<entry>
		<id>https://miosix.org/wiki/index.php?title=Servo_tutorial&amp;diff=95&amp;oldid=prev</id>
		<title>Fede.tft: Created page with &quot;Include statements and namespace required to use this library &lt;source lang=&quot;CPP&quot;&gt; #include &quot;drivers/servo_stm32.h&quot; using namespace miosix; &lt;/source&gt; This is a C++ library, it ...&quot;</title>
		<link rel="alternate" type="text/html" href="https://miosix.org/wiki/index.php?title=Servo_tutorial&amp;diff=95&amp;oldid=prev"/>
		<updated>2014-04-22T19:11:31Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Include statements and namespace required to use this library &amp;lt;source lang=&amp;quot;CPP&amp;quot;&amp;gt; #include &amp;quot;drivers/servo_stm32.h&amp;quot; using namespace miosix; &amp;lt;/source&amp;gt; This is a C++ library, it ...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Include statements and namespace required to use this library&lt;br /&gt;
&amp;lt;source lang=&amp;quot;CPP&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;drivers/servo_stm32.h&amp;quot;&lt;br /&gt;
using namespace miosix;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This is a C++ library, it cannot be used from a C source file.&lt;br /&gt;
&lt;br /&gt;
A servomotor is a device that allows to control the angular position of a shaft, like [https://www.sparkfun.com/products/10333 this one]. Servos work by reading a PWM signal with a fixed frequency, 50Hz by default, and a pulse period proportional to the desired angle.&lt;br /&gt;
&lt;br /&gt;
== Supported boards ==&lt;br /&gt;
&lt;br /&gt;
Miosix has a servo library that supports some boards using STM32 microcontrollers, namely the [[stm32f100rb_stm32vldiscovery]] and [[stm32f407vg_stm32f4discovery]].&lt;br /&gt;
&lt;br /&gt;
== GPIO connection ==&lt;br /&gt;
&lt;br /&gt;
The library supports up to 4 servo outputs, that must be connected to the following GPIOs (that the library configures as TIMER alternate function when the output is enabled).&lt;br /&gt;
&lt;br /&gt;
* PB6 is the output 0&lt;br /&gt;
* PB7 is the output 1&lt;br /&gt;
* PB8 is the output 2&lt;br /&gt;
* PB9 is the output 3&lt;br /&gt;
&lt;br /&gt;
== Library use ==&lt;br /&gt;
&lt;br /&gt;
The library is a C++ class named &amp;#039;&amp;#039;SynchronizedServo&amp;#039;&amp;#039;. It is a [https://en.wikipedia.org/wiki/Singleton_pattern singleton], so to instantiate it you have to call the &amp;#039;&amp;#039;instance()&amp;#039;&amp;#039; member function.&lt;br /&gt;
&lt;br /&gt;
You can find examples on how to use the library in the [https://gitorious.org/miosix-kernel/miosix-kernel/source/0fb80132cd27fbd48021cd5a9adb21c90a9cb94e:miosix/_examples/servo miosix/_examples/servo] directory of the kernel.&lt;br /&gt;
&lt;br /&gt;
=== Closed loop control support ===&lt;br /&gt;
&lt;br /&gt;
If there is the need to run a closed loop control code using the servo as an actuator, the library supports a way to synchronize the controller with the servo waveform generation, using the &amp;#039;&amp;#039;waitForCycleBegin()&amp;#039;&amp;#039; member function in a loop. The value set with &amp;#039;&amp;#039;setPosition()&amp;#039;&amp;#039; will be output at the &amp;#039;&amp;#039;&amp;#039;next&amp;#039;&amp;#039;&amp;#039; cycle, so take into account the existence of a z^-1 in your plant model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;CPP&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;quot;drivers/servo_stm32.h&amp;quot;&lt;br /&gt;
using namespace miosix;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    SynchronizedServo&amp;amp; servo=SynchronizedServo::instance();&lt;br /&gt;
    servo.enable(0); //Enable channel 0 (connect servo to PB6)&lt;br /&gt;
    servo.start();&lt;br /&gt;
    for(;;)&lt;br /&gt;
    {&lt;br /&gt;
        //This loop will be executed 50 times a second, and be synchronized with the waveform generation&lt;br /&gt;
        servo.waitForCycleBegin();&lt;br /&gt;
        float pos=computeServoPosition();&lt;br /&gt;
        servo.setPosition(0,pos);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Thread safety considerations ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;waitForCycleBegin()&amp;#039;&amp;#039; member function can be called by only one thread at a given time, so if you want to control more than one servo, be sure to put all the control code into only one loop. The other member functions are thread safe.&lt;/div&gt;</summary>
		<author><name>Fede.tft</name></author>
	</entry>
</feed>