Monday, June 23, 2014

Lets Control a Servo


Well hello there. In this tutorial I will be walking you through the process of controlling a continuous servo. How you control a continuous servo and a regular servo are the same.


  1. You have a BBB RevB (I don't know if the stuff I say or do will work on other version)
  2. You have already setup your OS on the BBB and have it configured the way you want. I have a tut for getting a TP-Link TL-WN722N USB wireless working if you want to do wireless. 
  3. You have a basic understanding of circuits. I probably won't do circuit diagrams but you never know I might get creative. 
  4. You know Python 2.X it is different than Python 3.X
  5. You have some type of bread board and jumper wires.
  6. You have installed the Adafruit Python Library

You will need

Your Bread Board, the Servos, 2 1 kΩ Resistor, 5v power supply, and Bread Board Jumper wires.


Connect the 5v power supply (upper right corner of picture) to the bread board's (bb) positive (red) and negative (blue/black).
Do once for each servo: 
  • Connect the servo's positive (red) to the bb's positive (red).
  • Connect the servo's negative (black) to bb's negative (black).
  • Connect the servo's control wire (Yellow) to one side of the bb
Put the 1kΩ Resistor in the middle of the 2 section of the bb to join them
Connect "P9_14" to one resistor and "P9_21" to the other.
Connect the Ground from the BBB to the negative (blue) of the bb and then run another wire across the bb so that you connect the ground of the BBB the the ground of the 5v power supply to complete the circuit.

The reason for the resistor is to make sure that the servo does not exceed the 250mA current that the BBB can handle.

Python Servo Code

import Adafruit_BBIO.PWM as PWM

servo_pin = "P9_14"
servo_pin2 = "P9_21"
Above we are importing the Adafruit library and setting up global variables the pins we are going to use.

PWM.start(servo_pin, (100), 60.0)
PWM.start(servo_pin2, (100), 60.0)
Now we setup the BBB so it knows which pins to us and that those pins are going to be used as PWMs. We are using the PWM.start method. This functions will take 4 parameters: channel, duty, frequency and polarity. The last 2 parameters frequency and polarity have default values thus are not required. The defaults are 2000 for frequency and polarity's is 0. Most servo's work with 50 and/or 60 Hz so I set the frequency to 60 Hz.
The first 2 parameters are required. The first one is the pin to be setup so 'P9_21' and 'P9_14'. The second one is the duty cycle of the pulse. The duty cycle is how long the pulse will be active. In this case we are setting it to 100 duty cycles which for my continuous servo means DON'T MOVE.

Note there are only 8 PWM pins P9_14, P9_16, P9_21, P9_22, P9_28, P9_29, P9_31, P9_42, P8_13, P8_19, P8_34, P8_36, P8_45, and P8_46 on a BBB RevB

PWM.set_duty_cycle(servo_pin, 0)
PWM.set_duty_cycle(servo_pin2, 0)
After we get it all setup we can change the direction and speed of the servo by using the method set_duty_cycle. It takes in the pin you want to change and the new duty cycle. By setting it to 0 the servo will now move clockwise at full speed. You can play around with your servo(s) to see what different duty_cycle values do on your servo.

Now the final code
import Adafruit_BBIO.PWM as PWM

servo_pin = "P9_14"
servo_pin2 = "P9_21"

PWM.start(servo_pin, (100), 60.0)
PWM.start(servo_pin2, (100), 60.0)

while True:
    speed1 = raw_input("Angle (0 to 100 x to exit):")
    speed2 = raw_input("Angle (0 to 100 x to exit):")
    if speed1 == 'x' or speed2 == 'x':
    speed_f1 = float(speed1)
    speed_f2 = float(speed2)
    duty1 = 100 - speed_f1
    duty2 = 100 - speed_f2
    PWM.set_duty_cycle(servo_pin, duty1)
    PWM.set_duty_cycle(servo_pin2, duty2)


Friday, June 6, 2014

A little Coffee for the morning


I was challenged by a co-worker to write a website in CoffeeScript (CS) on a Node.js server after telling him my opinion on CS. He said he had the same opinion too but once he started to use CS he really liked it and suggested I give CS a try. So I decided to make a simple webpage using Node.js and CS.

My initial thoughts:

·         Why would anyone want to learn something that only adds an extra step to the development process? You have to compile CS to Javascript (JS) so it will run on the browser.
·         Before you can write CS you have to know JS. As they say “It’s just JavaScript”. Why learn two things?
·         As you write CS you have to make sure you are not breaking anything in CS and/or JS. Yeah if you mess up in CS when debugging life can get very interesting.

What is CS:

Okay what is CS? “It’s just JavaScript”. It is a language that compiles into JS.

Its goal is to make writing JS easier. It does this by taking away some of the oddities that you have to remember when coding proper JS. Here are two examples of this:
·         In JS you have to make sure to use three equal signs instead of two so you compare type as well.
if( 1 === 1) is not the same as if( 1 == “1”)
·         In JS you have to making sure to declare variables with ‘var’ so they are put into the correct scope.
var chicken = “bird” vs chicken = “bird”
It also saves you lines of code since it is like Ruby/Python meaning it is white space delimited and not bracket delimited.
while true
                var chicken = “food”
is the equivalent as the following in JS
                                while( true ){
                                                var chicken = “food”

The meat:

As I said I was going to make a website using Node.js and CS. Well I did not have any ideas on what kind of website to make so I decided to follow a tutorial I had a while back with Node.js, Express, and Jade. It was from As I re-walked through the tutorial I converted the JS to CS. This process was not as bad as I thought it would be once I figured out the syntax for CS.

I started the converting process with the Node.js server file went from 34 lines of code to 28 lines of code. This is a pretty good save since it is just the base starter server file. The more you type the more you will save. The space saving is because CS is a white space delimited language. It was very nice to not have to remember to use === or var. That alone may be the saving grace of CS and why it has a strong following.

One thing I did find out, you need to convert any client side CS to JS so the browser can run the code. I had two options to do this. One was to use the IDE’s (PyCharm) built in ‘watcher’ functionality or to use CS’s built in ‘watcher’ functionality for this. I went with the IDE’s because I just had to click a button and not type a command (call me lazy). When you add a watcher to a CS file after every save it makes/updates two files. One file is the JS file that the browser/server will run. The other file is a map file this allows browsers like Chrome to be able to allow you to debug the CS code and not the JS code in them. That’s a point for Chrome ^5.

If you don’t want to add watchers to the server side CS file with node you need to add require("coffee-script").register(); to your Node.js server file. The debugger for the IDE I use has a hard time figuring out where the program is when running without the converted CS. So I ended up adding the watchers to the server side CS files anyway.

On the down side in my opinion is that it feels A LOT like Ruby to me and not so much like Python. What can I say Ruby on Rails left a bad taste in my mouth. The other down side is that you have to make sure your watchers are setup so you can debug your code. Those are the only two down sides I can think of if you are doing CS with Node.js. 

In the end of my little learning experience I came to the conclusion CS is great in the Node.js world but a pain in the non Node.js world.

In the end pro and con List:

1.       CS allows you to focus on the coding part and not the structuring part of JS.
2.       You are using a white space delimited language. Meaning no braces, brackets, and more. Thus saving you lines and lines of code.
3.       Chrome Dev Tools can debug CS on the client side assuming you set up watchers.
4.       IDE’s can debug CS on the server side. Some require that you add watchers.

Things I don’t care for:
1.       It feels too much like Ruby (not a Ruby fan). Give me Python.
2.       It does add an extra level of complexity for debugging

The END:

I will still use CS for all my Node.js projects moving forward. If you still are on the fence you should give CS a try. Just go ahead and do a simple project with it. Closing thought is MEH it’s nice and all but it was not life changing or life destroying.