-
Notifications
You must be signed in to change notification settings - Fork 0
/
examining-pwm-with-piscope.html
210 lines (193 loc) · 15.4 KB
/
examining-pwm-with-piscope.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en">
<head>
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,400italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="http://projects.curiousllc.com/theme/css/style.min.css">
<link rel="stylesheet" type="text/css" href="http://projects.curiousllc.com/theme/css/pygments.min.css">
<link rel="stylesheet" type="text/css" href="http://projects.curiousllc.com/theme/css/font-awesome.min.css">
<link href="http://projects.curiousllc.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Working with hardware Atom">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="" />
<meta name="author" content="ryanday" />
<meta name="description" content="A closer look at PWM without an oscilloscope" />
<meta name="keywords" content="raspberry pi 2, pwm, piscope">
<meta property="og:site_name" content="Working with hardware"/>
<meta property="og:title" content="Examining PWM with Piscope"/>
<meta property="og:description" content="A closer look at PWM without an oscilloscope"/>
<meta property="og:locale" content="en_US"/>
<meta property="og:url" content="http://projects.curiousllc.com/examining-pwm-with-piscope.html"/>
<meta property="og:type" content="article"/>
<meta property="article:published_time" content="2015-08-15 05:32:00-04:00"/>
<meta property="article:modified_time" content=""/>
<meta property="article:author" content="http://projects.curiousllc.com/author/ryanday.html">
<meta property="article:section" content="electronics"/>
<meta property="article:tag" content="raspberry pi 2"/>
<meta property="article:tag" content="pwm"/>
<meta property="article:tag" content="piscope"/>
<meta property="og:image" content="https://avatars3.githubusercontent.com/u/6845642?v=3&s=200"> <title>Working with hardware – Examining PWM with Piscope</title>
</head>
<body>
<aside>
<div>
<a href="http://projects.curiousllc.com">
<img src="https://avatars3.githubusercontent.com/u/6845642?v=3&s=200" alt="" title="">
</a>
<h1><a href="http://projects.curiousllc.com"></a></h1>
<p></p>
<nav>
<div style="text-align: left; font-size: 0.9em">At Curious, we connect Things to the Cloud. <br/><br/>
If you would like to launch a product, or connect your product to the Cloud, drop us a line!</div>
<ul class="list">
<li><a href="mailto:[email protected]" target="_blank">[email protected]</a></li>
<li><a href="http://curiousllc.com/" target="_blank">Curious LLC</a></li>
<li><a href="http://www.meetup.com/NOVA-Python" target="_blank">Nova Python</a></li>
<li><a href="http://www.meetup.com/Golang-DC/" target="_blank">Golang DC</a></li>
</ul>
</nav>
<ul class="social">
<li><a class="sc-You can add links in your config file" href="#" target="_blank"><i class="fa fa-You can add links in your config file"></i></a></li>
<li><a class="sc-Another social link" href="#" target="_blank"><i class="fa fa-Another social link"></i></a></li>
</ul>
</div>
</aside>
<main>
<article>
<header>
<h1 id="examining-pwm-with-piscope">Examining PWM with Piscope</h1>
<p>
Posted on Sat 15 August 2015 in <a href="http://projects.curiousllc.com/category/electronics.html">electronics</a> by <a href="http://projects.curiousllc.com/author/ryanday.html">ryanday</a>
• Tagged with
<a href="http://projects.curiousllc.com/tag/raspberry-pi-2.html">raspberry pi 2</a>, <a href="http://projects.curiousllc.com/tag/pwm.html">pwm</a>, <a href="http://projects.curiousllc.com/tag/piscope.html">piscope</a>
</p>
</header>
<div>
<p>I've been experimenting with Piscope recently and simply had to write a little
bit about it. Need to look at some waves, but don't have an oscilloscope? Can't
get out to your local Makerspace?</p>
<p>Look no further than <a class="reference external" href="http://abyz.co.uk/rpi/pigpio/piscope.html">Piscope</a>.</p>
<p>Piscope is an easy way to view the state of your GPIOs. You can run it remotely
from your desktop. You can record sessions, play them back, its a nice program. You
can't get as involved as you could with an <a class="reference external" href="https://www.pololu.com/blog/17/servo-control-interface-in-detail">oscilloscope</a>, but this is a pretty
good way to visualize whats happening on the Pi.</p>
<p>But just to get started, how about we try to drive two servos using PWM from the
Raspberry Pi 2. First follow the <a class="reference external" href="https://www.youtube.com/watch?v=2apqOiLHEzs&feature=youtu.be">Piscope instructions</a> to get the scope up and
running on your desktop.</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">pigpio</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="k">def</span> <span class="nf">basic_loop</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="n">gpio2</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Demonstrate simple servo control by setting the pulsewidth</span>
<span class="sd"> """</span>
<span class="n">pi</span> <span class="o">=</span> <span class="n">pigpio</span><span class="o">.</span><span class="n">pi</span><span class="p">()</span>
<span class="c"># 1500us is a safe width for unknown servos</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_servo_pulsewidth</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="mi">1500</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_servo_pulsewidth</span><span class="p">(</span><span class="n">gpio2</span><span class="p">,</span> <span class="mi">1500</span><span class="p">)</span>
<span class="c"># We shouldn't hear any clicking or jittering</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="c"># We'll sweep from low to high inside the safe range</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">2000</span><span class="p">,</span> <span class="mi">15</span><span class="p">):</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_servo_pulsewidth</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_servo_pulsewidth</span><span class="p">(</span><span class="n">gpio2</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="o">.</span><span class="mi">15</span><span class="p">)</span>
<span class="c"># Shut off servo control, and close out</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_servo_pulsewidth</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_servo_pulsewidth</span><span class="p">(</span><span class="n">gpio2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
</pre></div>
<p>Just to be safe for anyone copy/pasting the code, I'm using known safe values for a servo.
I took a screen shot near the end of the sweep. You can see that the pulse is just about 2ms
long and that we are getting 1 pulse every 20ms. If you are using actual servos, you can watch
the pulse increase as your servos react.</p>
<img alt="" src="images/PiscopeBasic.png" />
<p>That is fairly basic though. We can also get more indepth into how PWM is actually working.
Let's create a more complex loop that will sweep our servo range. This loop will allow
us to specify our own frequency, and tweak the duty cycle exactly as we like.</p>
<div class="highlight"><pre><span class="k">def</span> <span class="nf">advanced_loop</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="n">gpio2</span><span class="p">,</span> <span class="n">freq1</span><span class="p">,</span> <span class="n">freq2</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Demonstrate advanced PWM control by setting the</span>
<span class="sd"> frequency and duty cycle manually</span>
<span class="sd"> """</span>
<span class="n">pi</span> <span class="o">=</span> <span class="n">pigpio</span><span class="o">.</span><span class="n">pi</span><span class="p">()</span>
<span class="c"># Set the range to be 1000 to give us plenty of</span>
<span class="c"># small steps to increment</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_range</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_range</span><span class="p">(</span><span class="n">gpio2</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span>
<span class="c"># To test we are using 100Hz and 50Hz. This should</span>
<span class="c"># give us a pulse every 10ms.</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_frequency</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="n">freq1</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_frequency</span><span class="p">(</span><span class="n">gpio2</span><span class="p">,</span> <span class="n">freq2</span><span class="p">)</span>
<span class="c"># Since we get a pulse every 10ms, and our servo</span>
<span class="c"># can only be roughly 2ms wide, we can only set</span>
<span class="c"># the duty cycle up to 10% of our range. This is</span>
<span class="c"># because our 50Hz signal will grow twice as fast</span>
<span class="c"># as the 100Hz signal, and we don't want to exceed</span>
<span class="c"># the 2ms pulse width.</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_dutycycle</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_dutycycle</span><span class="p">(</span><span class="n">gpio2</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="o">.</span><span class="mi">15</span><span class="p">)</span>
<span class="c"># Once we set both GPIOs to have the same frequency,</span>
<span class="c"># we can see their pulse widths match.</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_frequency</span><span class="p">(</span><span class="n">gpio2</span><span class="p">,</span> <span class="n">freq1</span><span class="p">)</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="c"># Shut everything down</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_dutycycle</span><span class="p">(</span><span class="n">gpio1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">set_PWM_dutycycle</span><span class="p">(</span><span class="n">gpio2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">pi</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
</pre></div>
<p>We will use a 100Hz frequency for the first GPIO, and a 50Hz frequency
for the second. Typically, a 50Hz frequency is used for controlling a servo.</p>
<img alt="" src="images/PiscopeAdv.png" />
<p>We can see by looking at the waves that our pulse width depends on the frequency
and the dutycycle. Even though we chose the same duty cycle for both GPIOs, the
width of GPIO12 is just half of GPIO18. We get the GPIO12 pulse twice as often,
but this doesn't matter.</p>
<p>This doesn't do much for us except help visualize what is happening with the pulse.
If your servo can make adjustments faster than 50Hz, then this may be helpful. Your
dutycycle would have to be adjusted, but you could update your servo faster than
every 20ms as well.</p>
<p><a class="reference external" href="https://www.pololu.com/blog/20/advanced-hobby-servo-control-pulse-generation-using-hardware-pwm">Pololu</a> has some great info on servos and multiplexing and how these numbers
all work together. Even though you don't have access to an oscilloscope, Piscope
can help you play along with their tutorials.</p>
</div>
<div class="tag-cloud">
<p>
<a href="http://projects.curiousllc.com/tag/raspberry-pi-2.html">raspberry pi 2</a>
<a href="http://projects.curiousllc.com/tag/pwm.html">pwm</a>
<a href="http://projects.curiousllc.com/tag/piscope.html">piscope</a>
</p>
</div>
</article>
<footer>
<p>© Ryan Day </p>
<p>Built using <a href="http://getpelican.com" target="_blank">Pelican</a> - <a href="https://github.com/alexandrevicenzi/flex" target="_blank">Flex</a> theme by <a href="http://alexandrevicenzi.com" target="_blank">Alexandre Vicenzi</a></p> </footer>
</main>
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-2393416-13', 'auto');
ga('send', 'pageview');
</script>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BlogPosting",
"name": "Examining PWM with Piscope",
"headline": "Examining PWM with Piscope",
"datePublished": "2015-08-15 05:32:00-04:00",
"dateModified": "",
"author": {
"@type": "Person",
"name": "ryanday",
"url": "http://projects.curiousllc.com/author/ryanday.html"
},
"image": "https://avatars3.githubusercontent.com/u/6845642?v=3&s=200",
"url": "http://projects.curiousllc.com/examining-pwm-with-piscope.html",
"description": "A closer look at PWM without an oscilloscope"
}
</script></body>
</html>