Servo Control Stuff

In the past few months I have been playing around with some cheap servos.  I started by making a four-leg walking “thingie” out of oversized Popsicle sticks.  The hardest part was keeping it balanced when one of the legs was lifted for movement.  That exercise prompted me to build a 3-DOF (degrees of freedom) tester so that I could test servo movement in general and also verify pulse width settings for the desired positions of each 3-DOF leg.  More recently I decided to buy a cheap 4-DOF robot arm kit that did not include control electronics.  This article is sort of a mishmash of what I’ve learned so far about servo control and includes schematics and PIC software for the 3-DOF tester and the 4-DOF robot arm controller.

Cheap Servos

The cheap servos available from online suppliers have a rotation range of 180 degrees.  They consist of a small motor, some gears, and a control circuit that requires a PWM (pulse width modulation) input.  In general, the PWM frequency is set to 50-Hz for servos and the servo position is varied by sending a pulse width of about 500 microseconds to about 2.5 milliseconds (1.5 milliseconds centers the servo).  The first information I found online indicated a range of 1ms to 2ms but that only moved the servos 45 degrees either side of center.

Servos1

PWM

One of my earlier projects (posted on my website) talked in detail about PWM so I won’t repeat that information here.  Check out the Model Train Controller for those details.  In my previous projects I used the PWM capability built into one of the PIC microcontrollers but that is insufficient when you have more than one servo to control.  There are a variety of ways to handle multiple servos and that includes external modules that do the PWM for you.  When I built the four-leg walker I used three servos per leg so I needed to control 12 altogether.  Fortunately, there is a module that accepts I2C serial command inputs to generate up to 16 separate PWM outputs.    Just search for “PCA9685” on ebay.  There are also some simple PWM modules that have either an LED or LCD and can output 2 or 3 separate PWM signals.  Mostly they are for manual control via the onboard buttons but they can also be controlled using a 9600 baud RS-232 type of interface.  I don’t recommend these for servo control because multiple PWM outputs can be generated in software for a PIC.  I used the PIC 16F688 chip for both the 3-DOF servo tester and the 4-DOF robot arm controller.

Software Generated PWM

As I indicated earlier, a typical PWM frequency for servos is 50-Hz and the servos are positioned by sending a pulse during each frequency period.  The 50-Hz interval is set in the PIC software using Timer0.  When it times out, an interrupt is generated and a flag is set to let the main routine know that it is time to send pulses to the servos.  Given that 50-Hz is a 20ms period it is easy to fit control pulses for several servos within each time frame.  For simplicity, the pulses for each servo are sent out sequentially.  That means that the PIC only has to use a single timer to generate the desired pulse widths.  For instance, Timer1 is loaded with the desired pulse time for servo1, the control output for servo1 is set high, the software waits for the timeout, and then the control output for servo1 is set low.  The sequence is repeated for each of the remaining servos.  The maximum pulse width for each servo is 2.5ms so four servos only use about half of each 20ms period.  The servos don’t care where the pulse occurs within the time frame.

3-DOF Servo Tester

20180531_163208

The servo tester uses potentiometers to allow variance of the servo pulse widths.  There is also a switch for forcing all servos to the center position.  An LCD is included so that the actual pulse width values can be observed without having to connect to an oscilloscope.  Each potentiometer forms a simple voltage divider between +5 volts and ground and is connected to a PIC pin that is set for use as an ADC (analog-to-digital conversion) input.  Based on the voltage read, a calculation is performed to convert the voltage to a servo pulse width.  Basically, the formula is: (V * 7.5) + 600.  I found that 600 was the “real” minimum pulse width for my servos as opposed to the stated value of 500.  Remember that “V” in the formula is actually the truncated 8-bit ADC value (0-255) of the input voltage from the potentiometer.  On its own, the PIC doesn’t do multiplies or divides so the simple method for getting 7.5 was to left shift the ADC value three times (multiply by 8) and then subtract the right shift of the ADC value (0.5).

The LCD interface uses just 3 PIC pins but requires the addition of a 74HC164 shift register.  The details of the hardware are discussed in the “3-wire, 8-bit LCD Interface” project on my website.  The LCD shown in the picture is an oversized, single line one that I picked up as surplus but the software will work with the standard 1602 LCD.

Servo Tester 3DOF

Servo tester 3DOF

4-DOF Robot Arm Controller

20180531_163721

20180531_163700-e1527886885312.jpg

The kit for the robot arm shown in the picture can be bought for $25-$30 online.  I opted for one with servos that have metal gears so it was at the high end of that range.  I attached the flat robot arm base to a large project box which contains the control circuitry.  The hardware/software was base lined off of the 3-DOF servo tester.  A fourth potentiometer was added to the hardware, limit checks were added to the software, and the routines for the LCD were stripped out.  I used the 3-DOF servo tester to get a feel for the maximum ranges I wanted for each servo.  The range checks are not essential but I thought they might be a good idea given that I let my young grandkids play with it.  I’ve debated adding simple Bluetooth control for the robot arm so I specifically left the PIC serial ports pins available.  Otherwise, the hardware/software could be modified for 5-DOF.  You might think that you could go to 6-DOF with 12 I/O lines but RA3 is an input only and digital only pin.

Robot Arm1

RobotArmCtrl1

Check out my other projects at: http://www.boomerrules.com

 

Turkeys

Since we’re so close to Thanksgiving, today I want to talk to you about turkeys.  Wild turkeys, that is, not the kind of turkeys we eat at Thanksgiving.  Did you know that the Pilgrims actually brought some turkeys with them on the boat?  Anyway, my interest in turkeys is just part of my interest in wildlife.  I’ve always been a city boy and I still get excited when I see things like deer and turkeys roaming around in the wild.  Snakes excite me too but not in a good way.

 

Unlike domesticated turkeys, wild turkeys are smart.  They are also very sociable.  They hang around in groups.  They play together.  And the older ones teach the younger ones everything they need to know about being a turkey.

 

When I lived in Iowa, I would drive by a small corn field on the way to church and every Fall there would be at least one bunch of turkeys grazing on corn that had fallen to the ground during the harvest.  In West Texas, though, the turkeys don’t have the luxury of corn fields.  I guess I don’t really know what they normally eat, but I do know that they like acorns.  I’ve seen a group of turkeys gathered around a tree eating acorns.  And when the supply of acorns runs low, one of the turkeys flies up into the tree and starts thrashing around to knock down more acorns.  See, I said they were pretty smart.  After awhile, the turkey will jump down from the tree to eat some acorns and another turkey will take its place in the tree.  It’s all part of living in community as turkeys.

 

So what do turkeys have to teach us about being a Christian?  Well, when Jesus founded the church, he basically told us to be like turkeys.  He didn’t say it exactly that way but he did expect us to be in community with one another.  He expected us to worship together.  He expected us to play together.  He expected us to teach each other how to be a Christian.  He expected us to take turns doing the work of “shaking the tree” so that everyone can get a share of the acorns.  And he expected us to be sociable enough that others would want to join our group.

 

Benjamin Franklin once wrote a letter to his daughter in which he compared the Bald Eagle and the turkey.  Basically, he said that the Bald Eagle looked good on the outside, but that it only cared about itself and was both a coward and a thief.  On the other hand, Ben praised the turkey for all of the things that I mentioned.  It’s like that for us as Christians too.  It doesn’t matter if you look more like a turkey than an eagle, it’s what’s inside that counts.  That’s what God sees in us and that’s what other people will experience through our actions.

The Goldfish Experience

I was sitting and enjoying some quiet reading time when my peripheral vision detected the outline of what appeared to be a translucent version of the bouncing ball from the old ‘Mitch Miller Show’.

“Look what I won, Daddy!”

As I looked up from my book I came face to face with a small plastic bag, filled with water.  The face on the far side of the bag belonged to my young daughter.  The face inside of the bag looked like a very small version of the one that Jonah must have seen just before being ingested.  From my perspective the view was almost as unnerving.

The bag and its occupant began bouncing as my daughter hopped from foot to foot.  “It’s a goldfish, Daddy, and I won him at the Fourth of July pool party!”

Recovering some of my composure I made a mental note to not renew my membership at the community pool.

At this point my daughter stopped her bouncing which, I’m sure, made the fish happy.  Unfortunately, however, the bouncing stopped because my daughter was now looking unhappy.

“I know I can’t keep him, can I?”

In similar situations, some people come all unglued.  After all, they think, how can you snatch away joy from the heart of your precious offspring?  I, on the other hand, have very little problem saying no.  I first weigh the facts at hand and then make a rational decision.  In this particular case, as every adult knows, the odds are about even that the fish will die soon after you have invested 50 or 60 dollars in the necessary accessories for it.  If the fish doesn’t die of natural causes in a short time, then it usually dies of neglect at the hands of the very kid who swore that they would carefully feed it and clean its bowl.  A third likely prospect, in our house, is that one of our cats will eventually crack the combination to the fish vault and sample the delights of domestic sushi.  In any case, the odds were that the fish would soon be riding the old toilet bowl express to fishy heaven.  Even so, I hated to break her heart so I decided to try the logical approach.

“But sweetheart, I don’t think we even have anything to put him in.  Besides, we don’t have any fish food, or a net, or any of that other stuff we need.”  I sat back like F. Lee Bailey after a closing argument and waited for her to come to the right decision.

She screwed up her face for a moment as she pondered the self-evident truth of my words, then broke out with a smile.  “Mommy has a big bowl in the garage that we can use, and we can get fish stuff at K-mart, and…”

I took a deep breath.

“…Kelly can teach me how to take care of him, and I can take him to school for show and tell, and….”

I started feeling a brain cramp coming on.

“…and, and, well, I just really want to keep him.”

Realizing that adult logic was useless, I decided to change to my ‘Father Knows Best’ mode.  As I tried to set my face into a classic Robert Young pose, I could see a small puddle forming on the lower lids of my daughter’s eyes.

“…I, ah, well, I guess we could try it.”  Jeez, I thought, who said that?

“Oh thank you Daddy!  Thank you, thank you, thank you!” Then off to the garage she ran, cats and furniture scattering in all directions.

Together we carefully cleaned up the old terrarium bowl, and then poured the contents of the plastic fish bag into it.  Next we added enough water to provide a semi-spacious home for the fish, and then set the bowl on top of a dresser in her room.  She looked at it for about thirty seconds, then headed out the door to find more entertaining activity.  I shuffled back to my recliner a defeated man.

About an hour later I took a snack break and decided to check on ‘Goldie’.  As I opened the door to my daughter’s bedroom, I could sense an unsettling stillness.  Sure enough, as I slowly approached the bowl, I found ‘Goldie’ in a still life pose of the classic Australian crawl position.  As I toted the bowl and its deceased occupant to the bathroom, I tried to distance myself by pretending that I was the person in charge of cleaning up road kill.  I also wondered, just for a moment, what size toilet it would take to handle a flattened raccoon.

Once again, I cleaned out the bowl and set it back on the shelf in the garage.  I hadn’t had much time to ponder the words I would use to break the news to my daughter when she came flying back into the house.  Fortunately, I thought, at least she hadn’t brought home some of her little friends to show off her new pet.

She whirled to a halt when she saw my worried parent look.

“Honey,” I began, “I’m afraid that I have some bad news.”

She looked up at me for a moment, then said “my fish died, didn’t he?”

I tried, in vain, to think of some comforting words.  “Yes, I’m afraid he did.  And I guess I don’t know why.”

She placed a reassuring hand on my arm.  “That’s o.k., Daddy.  Other kids’ fish have died too.  Maybe the fish store gave us a bunch of sick fish.

She tossed her pigtails back and shrugged her shoulders.  “Or maybe the fish just didn’t like being in such a small bowl after getting to swim in the big pool.”

I was in a bit of a fog, and thankful that she was taking it all so well.  Just then, the last of her words filtered through, and an incredulous look forced its way onto my face.  “The fish were swimming in the big pool?”

“Yeah,” she said.  “The lifeguards put them in the pool, and we all swam around catching them.”

As I slumped back into my recliner I felt just a small amount of relief that the mystery of Goldie’s demise was solved and that the facts showed my daughter and I to be blameless.  I also realized that none of the lifeguards was a biology major.

Why I Write

I have a need to express myself creatively.  Mostly that takes the form of building stuff or fixing stuff because, well, I’m a guy.  That doesn’t mean that my only supplies are 2×4’s and tenpenny nails.  Actually, I have a particular fondness for fine woods and like to make things like crosses, decorator boxes, and even furniture.  But just about any material or craft area is fair game if I get an idea.  The problem, though, is that while I may have the heart of an artist I don’t have the hands to match.  That doesn’t keep me from trying but I’m realistic enough to understand and appreciate what real craftsmanship looks like.  I figure that if I pass along my how-to information then maybe someone with artist’s hands will get inspired.  So I write.

I also like to solve problems.  Ok, so they have to be problems I choose to solve.  Otherwise they are just irritants that eat into my time.  A big part of that fun problem solving has taken the form of designing and building little microcontroller projects.  Getting stuff to work is fun but being able to pass along my knowledge to a younger generation really puts a fine finish on it.  So I write.

As you may have guessed, I’m a retired engineer with backgrounds in both electronics and software.  But there’s more because originally I wanted to be a Psychologist.  Wait, what?  Yup, turns out that the other half of my personality and education involves the “soft sciences”.  When I finally got serious about college (after a stint in the military) I actually got multiple degrees in Psychology and Sociology.  And then I promptly landed a job as an electronics test technician.  Thankfully my military experience paid off and kept me from the dreaded “Do you want fries with that?” scenario.

As I moved into different positions in the tech world (writing, instructing) it became more evident to me that I wouldn’t really feel fulfilled unless I was designing stuff.  So I took night classes and got a degree in Computer Science.  Not only did I start having more fun, but I also got much more use out of my Social Science education when dealing with traditional engineers.  Unfortunately, word got out that I was a little bit better than the average engineer at writing and presentations so those tasks began to infringe on my fun time.  I mean, just how creative can you get while writing tech manuals for big companies and the military?

Through it all I did dabble in some creative writing.  I joined a small writing group and even took a Creative Writing class at the local Community College – four times.  I did technical articles for a couple of hobby magazines and wrote unpublished personal essays, short stories, and even poetry.  I thought about a novel but didn’t think I could harness my attention deficit long enough to see it through.  Now that I’m retired I have more time but also a wider variety of interests.  So writing gets set on the back burner unless it’s documenting one of my craft or electronics projects.  But I still have a need to express my thoughts on other topics and doing so by posting comments on Yahoo articles just doesn’t seem to cut it.  So why do I write?  Because sometimes I need to be heard – even if I’m the only one listening.