<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Mac, rev5, and Servo</title>
		<link>http://forums.leaflabs.com/topic.php?id=871</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:23:24 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=871" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5369</link>
			<pubDate>Tue, 21 Jun 2011 17:36:25 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">5369@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;iRobotGoddard - Assuming your most recently posted program is complete, then it still unlikely to work properly.&#60;/p&#62;
&#60;p&#62;The posted program does not set the PWM timer cycle time to 20 milliseconds, so it is luck that it might seem to work.&#60;/p&#62;
&#60;p&#62;My estimate to get a cycle time of roughly 20 milliseconds is to set a timer prescaler of 22, though 20 to 25 is likely to work okay.&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer-setprescalefactor&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer-setprescalefactor&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5356</link>
			<pubDate>Mon, 20 Jun 2011 12:08:02 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5356@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;thanks @mbolivar! yeah i'm not sure what approach I want to take, there are going to be multiple applications so iGuess I'm going to go with what works with each project. Some will be RC and some will be completely autonomous. I think by hand might work more with autonomous for timing reasons, just to be on the safe side.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5353</link>
			<pubDate>Mon, 20 Jun 2011 11:16:37 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">5353@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;iRobotGoddard:  why don't you try using the existing Servo library?  It's included with Maple IDE.  I linked to its documentation in an earlier post.  The Servo library has the correct timer configuration for using RC servos, and has easy methods for setting the pulse width, both in microseconds and degrees.&#60;/p&#62;
&#60;p&#62;If you're not interested in using the library, and want to do servo control &#34;by hand&#34;, maybe reading the source code for that library will help you fix the issues you've been having:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;https://github.com/leaflabs/libmaple/blob/master/libraries/Servo/Servo.h&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/libmaple/blob/master/libraries/Servo/Servo.h&#60;/a&#62;&#60;br /&#62;
&#60;a href=&#34;https://github.com/leaflabs/libmaple/blob/master/libraries/Servo/Servo.cpp&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/libmaple/blob/master/libraries/Servo/Servo.cpp&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;The file examples/test-servo.cpp (part of the libmaple repository) may also be useful to you as an example usage of the library:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;https://github.com/leaflabs/libmaple/blob/master/examples/test-servo.cpp&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/libmaple/blob/master/examples/test-servo.cpp&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5352</link>
			<pubDate>Mon, 20 Jun 2011 10:20:12 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5352@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;this is going crazy, it's working again... iTried new wires, and it's better now. Now I'm noticing it is running the sweep 6-10 times and then it pauses. I'm thinking this has something to do with the duty cycle.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5350</link>
			<pubDate>Mon, 20 Jun 2011 09:01:10 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5350@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Ok I must be missing some obvious answer. I had my servo orking over the weekend, and was trying to dampen the motion when transitioning in the sweep program. Now my Servo does not move at all. I've tried different servos, some work and some do not. I thought I blew my servo but I tried it on the old arduino I have and it worked fine. Can someone spell it out for me please, because I am starting to feel elementary.&#60;/p&#62;
&#60;p&#62;__________________________________________________________________________________________&#60;br /&#62;
const int servoPin = 11;   &#60;/p&#62;
&#60;p&#62;void setup()&#60;br /&#62;
{&#60;br /&#62;
  pinMode(servoPin, PWM);&#60;br /&#62;
} &#60;/p&#62;
&#60;p&#62;void loop()&#60;br /&#62;
{  &#60;/p&#62;
&#60;p&#62;  for(uint8 pos = 0; pos &#38;lt; 180; pos += 1)  // goes from 0 degrees to 180 degrees&#60;br /&#62;
  {&#60;br /&#62;
    uint16 dutyCycle = 4960 * (uint16)(1.25 * (double)pos);    // in steps of 1 degree&#60;br /&#62;
    pwmWrite(servoPin, dutyCycle);              // tell servo to go to position in variable 'pos'&#60;br /&#62;
    delay(15);&#60;br /&#62;
    SerialUSB.print(&#34;\n CW&#34;);&#60;br /&#62;
    delay(20);    // waits 15ms for the servo to reach the position&#60;br /&#62;
  }&#60;br /&#62;
  for(uint8 pos = 180; pos&#38;gt;=1; pos-=1)     // goes from 180 degrees to 0 degrees&#60;br /&#62;
  {&#60;br /&#62;
    uint16 dutyCycle = 4960 + (uint16)(1.25 * (double)pos);&#60;br /&#62;
    pwmWrite(servoPin, dutyCycle);              // tell servo to go to position in variable 'pos'&#60;br /&#62;
    delay(15);&#60;br /&#62;
    SerialUSB.print(&#34;\n CCW&#34;);&#60;br /&#62;
    delay(20);    // waits 15ms for the servo to reach the position&#60;br /&#62;
  }   &#60;/p&#62;
&#60;p&#62;}&#60;br /&#62;
__________________________________________________________________________________________&#60;br /&#62;
 It compiles, uploads, and seems fine on the serial monitor screen. As I said before it was just working saturday, and now it's stopped working.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5347</link>
			<pubDate>Mon, 20 Jun 2011 01:08:48 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">5347@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;iRobotGoddard:&#60;/p&#62;
&#60;p&#62;Is there some reason the existing Servo library isn't enough for your purposes?  We'd be glad to get some feedback on how to improve it.&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://leaflabs.com/docs/libs/servo.html&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/libs/servo.html&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5335</link>
			<pubDate>Fri, 17 Jun 2011 21:50:49 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5335@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;iBrought the servo home with me to work on over the weekend. It seems as if there are simple answers to many of the problems I am having, so I will be &#34;getting to know&#34; the Maple. If anyone has any suggestions please feel free to comment. I will be working on servo control and serial communication, it seems as if they will address many questions for Mac users.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5334</link>
			<pubDate>Fri, 17 Jun 2011 20:25:19 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5334@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;thanks alot, the theory makes complete sense. My issue is calling these functions or defining each perimeter the correct way, or remembering it's necessary if a specific library value/function isn't given. I simply decreased the value &#34;9.1&#34; to &#34;1.25&#34;. It's jumping back and forth, so I am working smoother motion.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5332</link>
			<pubDate>Fri, 17 Jun 2011 18:52:55 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">5332@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;The cycle time for a servo is 20msec, or 50Hz frequency.&#60;/p&#62;
&#60;p&#62;So this code:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;for(uint8 pos = 0; pos &#38;lt; 180; pos += 1) // goes from 0 degrees to 180 degrees
{
uint16 dutyCycle = 4096 + (uint16)(9.1 * (double)pos); // in steps of 1 degree
pwmWrite(servoPin, dutyCycle); // tell servo to go to position in variable &#38;#39;pos&#38;#39;
...
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Will set the duty cycle of the PWM to 4,096 (to 5,274) in 65536. Roughly 1/16th of a cycle.&#60;/p&#62;
&#60;p&#62;BUT what is the cycle time? It isn't set. &#60;/p&#62;
&#60;p&#62;I had a quick look at the library code in libmaple, and the timers are initialised to a prescaler (which sets the overall cycle time) to 1. The 16bit counts from 0 to 65536-1 each cycle,&#60;br /&#62;
So the timer would be running at 72MHz/65536 = 1.1KHz&#60;br /&#62;
I'd expect the servo to be confused, that is way too fast for it.&#60;/p&#62;
&#60;p&#62;The frequency at which the timer counts is set by the prescaler, and so the prescaler needs to cause the timer to count from 0 to 65536-1 at  50Hz.&#60;/p&#62;
&#60;p&#62;The equation is&#60;br /&#62;
72MHz/(prescaler*65536) = 50Hz&#60;br /&#62;
or&#60;br /&#62;
72MHz/(50*65536) = prescaler = 21.97&#60;br /&#62;
So it needs a prescaler of about 22 to have a frequency of about (close enough) 50Hz.&#60;/p&#62;
&#60;p&#62;Then the PWM signal would be the servo control signal.&#60;br /&#62;
Now 65536 = 20mseconds, so:&#60;br /&#62;
 1.25mseconds = 65536*1.25/20 = 4,096&#60;br /&#62;
 1.5mseconds = 65536*1.5/20 = 4,915&#60;br /&#62;
 1.75mseconds = 65536*1.75/20 = 5734&#60;/p&#62;
&#60;p&#62;As all the arithmetic can be 32bit the calculation might be:&#60;br /&#62;
&#60;code&#62;uint16 dutyCycle = 4096 + (91 * pos)/10; // in steps of 1 degree&#60;/code&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5328</link>
			<pubDate>Fri, 17 Jun 2011 16:09:36 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5328@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;iApologize that was completely wrong. I am trying to transfer test code over from old arduino to the new maple, and am not sending what was updated. In either case I did not place the calculation in, but I did enter new values but they were in the range of 16 bits. Here is my code with the updated information, but the servo will not do a sweep....it only completes it once and never again while the strings print back and forth between &#34;CW&#34; and &#34;CCW&#34;. &#60;/p&#62;
&#60;p&#62;________________________________________________________________________________&#60;br /&#62;
const int servoPin = 11;&#60;br /&#62;
void setup()&#60;br /&#62;
{&#60;br /&#62;
  pinMode(servoPin, PWM);&#60;br /&#62;
} &#60;/p&#62;
&#60;p&#62;void loop()&#60;br /&#62;
{  &#60;/p&#62;
&#60;p&#62;  for(uint8 pos = 0; pos &#38;lt; 180; pos += 1)  // goes from 0 degrees to 180 degrees&#60;br /&#62;
  {&#60;br /&#62;
    uint16 dutyCycle = 4096 + (uint16)(9.1 * (double)pos);    // in steps of 1 degree&#60;br /&#62;
    pwmWrite(servoPin, dutyCycle);              // tell servo to go to position in variable 'pos'&#60;br /&#62;
    delay(20);&#60;br /&#62;
    SerialUSB.print(&#34;\n CW&#34;);&#60;br /&#62;
    delay(20);    // waits 15ms for the servo to reach the position&#60;br /&#62;
  }&#60;br /&#62;
  for(uint8 pos = 180; pos&#38;gt;=1; pos-=1)     // goes from 180 degrees to 0 degrees&#60;br /&#62;
  {&#60;br /&#62;
    uint16 dutyCycle = 4096 + (uint16)(9.1 * (double)pos);&#60;br /&#62;
    pwmWrite(servoPin, dutyCycle);              // tell servo to go to position in variable 'pos'&#60;br /&#62;
    delay(20);&#60;br /&#62;
    SerialUSB.print(&#34;\n CCW&#34;);&#60;br /&#62;
    delay(20);    // waits 15ms for the servo to reach the position&#60;br /&#62;
  }   &#60;/p&#62;
&#60;p&#62;}&#60;br /&#62;
________________________________________________________________________________
&#60;/p&#62;</description>
		</item>
		<item>
			<title>robodude666 on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5327</link>
			<pubDate>Fri, 17 Jun 2011 15:52:39 +0000</pubDate>
			<dc:creator>robodude666</dc:creator>
			<guid isPermaLink="false">5327@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;The &#60;code&#62;pwmWrite&#60;/code&#62; function's second argument is a 16-bit value that represents a 0 - 100% duty cycle. You can't simply write an angle to it like that. You need to convert the angle to 16-bit duty cycle. For example:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;/*
About PWM:
	&#60;a href=&#34;http://leaflabs.com/docs/pwm.html#pwm&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/pwm.html#pwm&#60;/a&#62;

  0 Degrees - 1.25 ms - 4096
 90 Degrees - 1.50 ms - 4915
180 Degrees - 1.70 ms - 5734

*/

for(uint8 degrees = 0; degrees &#38;lt; 180; degrees++)
{
	uint16 dutyCycle = 4096 + (uint16)(9.1 * (double)degrees);
	pwmWrite(servoPin, dutyCycle);
	delay(20);
}

for(uint8 degrees = 180; degrees &#38;gt; 0; degrees--)
{
	uint16 dutyCycle = 4096 + (uint16)(9.1 * (double)degrees);
	pwmWrite(servoPin, dutyCycle);
	delay(20);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;-robodude666
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "Mac, rev5, and Servo"</title>
			<link>http://forums.leaflabs.com/topic.php?id=871#post-5326</link>
			<pubDate>Fri, 17 Jun 2011 15:12:58 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5326@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Trying to run a sweep program, but the servo will not repeat the sweep. The code switches back and forth, observed through the serial monitor, but the actual motor will not operate accordingly. I am running a sample program from the arduino but I have translated the minor things. Here's my code.&#60;br /&#62;
_________________________________________________________________________________________&#60;br /&#62;
const int servoPin = 11;&#60;br /&#62;
double pos = 0;&#60;br /&#62;
void setup()&#60;br /&#62;
{&#60;br /&#62;
  pinMode(servoPin, PWM);&#60;br /&#62;
} &#60;/p&#62;
&#60;p&#62;void loop()&#60;br /&#62;
{  &#60;/p&#62;
&#60;p&#62;  for(pos = 0; pos &#38;lt; 180; pos += 1)  // goes from 0 degrees to 180 degrees&#60;br /&#62;
  {                                  // in steps of 1 degree&#60;br /&#62;
    pwmWrite(servoPin, pos);              // tell servo to go to position in variable 'pos'&#60;br /&#62;
    delay(15);&#60;br /&#62;
    SerialUSB.print(&#34;\n CW&#34;);&#60;br /&#62;
    delay(20);    // waits 15ms for the servo to reach the position&#60;br /&#62;
  }&#60;br /&#62;
  for(pos = 180; pos&#38;gt;=1; pos-=1)     // goes from 180 degrees to 0 degrees&#60;br /&#62;
  {&#60;br /&#62;
    pwmWrite(servoPin, pos);              // tell servo to go to position in variable 'pos'&#60;br /&#62;
    delay(15);&#60;br /&#62;
    SerialUSB.print(&#34;\n CCW&#34;);&#60;br /&#62;
    delay(20);    // waits 15ms for the servo to reach the position&#60;br /&#62;
  }   &#60;/p&#62;
&#60;p&#62;} &#60;/p&#62;
&#60;p&#62;_______________________________________________________________________________
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
