]> rtime.felk.cvut.cz Git - ert_linux_web.git/blob - rpi-motor-control/index.html
Some other minor corrections.
[ert_linux_web.git] / rpi-motor-control / index.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2    "http://www.w3.org/TR/html4/loose.dtd">
3 <html>
4 <head>
5   <title>[[!meta title="Raspberry Pi minimal components DC motor servo control"]]</title>
6   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7   <link><style type="text/css">
8     #main_content {max-width: 70em}
9   </style>
10 </head>
11 <body>
12
13 <div id="main_content" style="max-width: 60em;">
14
15 <p>
16 [[!img rpi_dc_motor_control-scn.png size="300x" align=right alt="Simulink connected to RPi running motor control model"]]
17 <a href="http://en.wikipedia.org/wiki/Raspberry_Pi">Raspberry Pi</a> is low cost
18 hardware which does not implement any usual motor control peripherals in hardware.
19 Yet fully preemptive variant of Linux kernel latencies are so low that fast signals
20 processing in software allows to implement precise DC motor feedback control
21 for incremental encoder inputs changing up to 15&nbsp;kHz.
22 </p>
23
24 <h2 id="background">Background</h2>
25 <p>
26 The goal was to prove that working control can be achieved with
27 <a href="http://lintarget.sourceforge.net/">Embedded Realtime Linux Target for Simulink</a>
28 (<em>ert_linux</em>) on this low cost hardware which provides option for broader
29 community to play with this control task taught during the
30 <a href="http://support.dce.felk.cvut.cz/psr/" target="_blank">real-time operating
31 systems programming course</a> at our university. The final task
32 <a href="http://support.dce.felk.cvut.cz/psr/cviceni/semestralka/" target="_blank">assignment</a>
33 targets industrial like PowerPC MPC5200-based board running VxWorks system.
34 But Raspberry Pi (RPi) is much cheaper and common in broad community.
35 The initial idea has been implemented in a frame of Radek Mečiar's
36 bachelor thesis with controller and monitor web server implemented
37 directly in C-language. The experiment has been improved and combined
38 with <em>ert_linux</em> project to allow teach controller design with Simulink.
39 </p>
40
41 [[!img rpi-mc-wwrap-schema.png size="300x" align=right alt="Diagram of Interconnection of RPi and DC Motor"]]
42 <h2 id="hardware">Hardware</h2>
43 <p>
44 Since the RPi (and its BCM2835 SoC) is not intended for motion control applications,
45 there is no hardware support for incremental encoder inputs (IRC)
46 and the RPi connector has only one pulse width modulation (PWM) output.
47 That is why most of signal and control processing is done in software
48 with minimal HW support. In order to be able to rotate the motor in both
49 directions, the only available PWM signal is demultiplex to two
50 sides of power H-bridge by using four NOR gates (SN74HCT02)
51 where direction is controlled by an additional GPIO output (DIR).
52 The incremental encoder signals are connected directly to the RPi and
53 processed only in software. The diagram of wire-wrapped interconnection
54 is available in <a href="rpi-mc-wwrap-schema.pdf">PDF</a> and
55 <a href="rpi-mc-wwrap-schema.svg">SVG</a> formats as well.
56 </p>
57
58 <h2 id="irc">IRC processing</h2>
59 <p>
60 The IRC signal processing is the most demanding part of our solution
61 since the frequency of the IRC signal can go up to 21&nbsp;kHz,
62 when maximal voltage is applied to the motor. To achieve reasonable
63 performance, a kernel driver has been implemented for this purpose.
64 It uses four GPIO pins &mdash; two for each IRC channel.
65 One of these two pins is configured to generate interrupts for
66 rising edges and the other for falling edges. Motor position is
67 derived from the order of interrupts and applications can read it via
68 <tt>/dev/irc0</tt>.
69 </p>
70 <p>
71 This solution has an interesting property that it works even when the
72 processing of one (or few more) interrupt(s) is delayed due to other
73 activities in the system. In preempt_rt, the interrupt handlers run
74 in dedicated threads, which can be preempted by threads with higher
75 priority. Thanks to the fact that the scheduler run queue is a FIFO
76 queue, the IRQ threads are activated in the same order as the original
77 interrupts and our position calculation works even in the case of
78 a delay. The alternative approach, where the position is calculated from
79 the actual IRC signal levels read in the interrupt handler would fail,
80 because the level read in a delayed handler might be different from
81 the level at the time when the corresponding IRQ was generated.
82 </p>
83 <p>
84 Even better performance could be probably achieved by processing the
85 IRC signals in ARM's fast interrupt requests (FIQ), but this solutions
86 would not be portable to other architectures. Other option is to
87 design more sophisticated hardware but minimal HW solution is
88 excellent for teaching.
89 </p>
90
91 <h2 id="model">Simulink model</h2>
92 <p>
93 Simulink model of the motor controller is depicted in Figure.
94 The
95 <a href="https://github.com/ppisa/rpi-rt-control/blob/master/simulink/sfIRCInput.c" target="_blank"><em>IRC0</em></a>
96 and
97 <a href="https://github.com/ppisa/rpi-rt-control/blob/master/simulink/sfPWMwDirOutput.c" target="_blank"><em>PWMwDir</em></a>
98 blocks are so called C MEX S-functions. The former reads the motor position
99 from <tt>/dev/irc0</tt>, the latter controls the PWM and DIR signals
100 by directly accessing the GPIO registers via <tt>mmap()</tt>ed <tt>/dev/mem</tt>.
101 The actual motor control is performed by a proportional-sum-derivative (PSD)
102 controller enhanced with an anti-windup technique. Setpoint <em>w</em>
103 is generated either manually or by a simple trajectory generator.
104 Sampling period of the whole model was set to 1&nbsp;ms. The source code
105 of the S-functions as well as the model are available from
106 <a href="https://github.com/ppisa/rpi-rt-control" target="_blank">Github</a> repository.
107 </p>
108 <p>
109 The <em>ert_linux</em> target was configured to use an ARM C cross-compiler.
110 The generated binary was copied to the target RPi board by the
111 <tt>scp</tt> command and ran there. Simulink <em>external mode</em> was
112 used to tune certain model parameters on-line as well as to see the
113 actual signal values in the scope windows.
114 </p>
115
116 <h2 id="evaluation">Performance evaluation</h2>
117 <p>
118 The Linux kernel used for our experiments was the official
119 Raspberry Pi rpi-3.12.y Linux kernel version 3.12.28 patched with Steven
120 Rostedt's stable preempt_rt patch (patch-3.12.26-rt40) and with Junjiro R.
121 Okajima's aufs3.12.x 20140512. The latencies measured by the <tt>cyclictest</tt>
122 tool when the system was loaded by TCP communication and SD card accesses are as
123 follows. The maximal latency was 170&nbsp;μs, average about 40&nbsp;μs. When
124 graphical desktop was run the maximal latencies increased to 280&nbsp;μs
125 (probably caused by contention on the system bus generated by the
126 VideoCore GPU). This observation is in same range as
127 more serious examination and long term performance monitoring
128 on better tuned RT-kernel kernel executed by
129 <a href="https://www.osadl.org/" target="_blank">Open Source Automation Development Lab</a>
130 and documented in their
131 <a href="https://www.osadl.org/Profile-of-system-in-rack-7-slot-3.qa-profile-r7s3.0.html" target="_blank">Quality assurance Farm</a>.
132 </p>
133 <p>
134 System load caused by running an unoptimized (soft-float only) model
135 was about 2%. The USB connected Ethernet controller (a part of RPi) generated
136 load of about 10%. The highest load was generated by the software IRC
137 signal processing &mdash; up to 95%, i.e. the limit for RT tasks. This
138 happened during fast motor rotation (e.g. when maximal input voltage
139 was applied) and the interrupt frequency was 8&nbsp;kHz per each channel
140 (32&nbsp;kHz in total). As can be seen, processing of the IRC signal at
141 full speed (21&nbsp;kHz) is above capabilities of the Linux kernel on this
142 hardware. As mentioned above, the use of FIQ (or raw interrupts) could
143 help here. For lower speeds, our simple solution works flawlessly and
144 can be used as a great tool for control education and experiments.
145 </p>
146
147 <h2 id="links">Links</h2>
148 <dl>
149   <dt>Bachelor thesis of Radek Mečiar: Motor control with Raspberry Pi board and Linux (Czech language only), 2014</dt>
150     <dd>Available <a href="https://support.dce.felk.cvut.cz/mediawiki/images/1/10/Bp_2014_meciar_radek.pdf" target="_blank">online in PDF format</a>
151     <br><dd>Corresponding source code on GitHub <a href="https://github.com/Ramese/servoPi" target="_blank">https://github.com/Ramese/servoPi</a>
152     </dd>
153   <dt>Michal Sojka, Pavel Píša: Usable Simulink Embedded Coder Target for Linux at 16th Real Time Linux Workshop, 2014</dt>
154     <dd><a href="https://www.osadl.org/?id=2018" target="_blank">Paper abstract</a> at <a href="https://www.osadl.org/RTLWS-2014.rtlws-2014.0.html" target="_blank">16th Real Time Linux Workshop</a> site.
155     </dd>
156   <dt>RPi RT Control Support</dt>
157     <dd><a href="https://github.com/ppisa/rpi-rt-control" target="_blank">https://github.com/ppisa/rpi-rt-control</a>
158     <br>GitHub Repository with RPi IRC kernel module and Simulink model and RPi blocks sources.
159     </dd>
160   <dt>Fully preemptive kernel sources for RPi (kernel-3.12.28-rt40+)</dt>
161     <dd><a href="https://github.com/ppisa/linux-rpi/tree/rpi-3.12.y-aufs-rt-ppisa" target="_blank">https://github.com/ppisa/linux-rpi</a>
162     <br>GitHub repository with branch rpi-3.12.y-aufs-rt-ppisa containing Linux kernel sources
163     with Steven Rostedt's stable preempt_rt (patch-3.12.26-rt40) and with Junjiro R. Okajima's aufs3.12.x 20140512
164     patches applied.
165     </dd>
166   <dt>Simulink Embedded Coder target for Linux</dt>
167     <dd><a href="http://rtime.felk.cvut.cz/gitweb/ert_linux.git" target="_blank">http://rtime.felk.cvut.cz/gitweb/ert_linux.git</a>
168     <br>Repository with core templates for Linux hosted, Linux cross architecture targetted
169     Simulink code generation templates and configuration.
170     </dd>
171   <dt>Auxiliary utilities for Raspberry Pi</dt>
172     <dd><a href="https://github.com/ppisa/rpi-utils" target="_blank">https://github.com/ppisa/rpi-utils</a>
173     <br>The repository includes RAM based root overlay support to protect SDcard against wearing and tearing
174     and keep root filesystem intact when experiments are download and run on system or even crash it.
175     </dd>
176 </dl>
177
178 </div>
179
180 </body>
181
182 </html>