Monday, June 23, 2014

Lets Control a Servo

HELLO WORLD


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.

Assumption


  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.

Wiring


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':
        PWM.stop(servo_pin)
        PWM.stop(servo_pin2)
        PWM.cleanup()
        break
    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)

THE END.

Friday, June 6, 2014

A little Coffee for the morning

Opening:

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 http://goo.gl/lfwbVV. 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:

Pro:
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.

Monday, May 26, 2014

TP-Link TL-WN722N USB wireless adapter Beagle Bone Black

So you have a Beagle Bone Black (BBB) and bought the TP-Link TL-WN722N wireless adapter. WOOT me too. Then you went off and installed Debian onto your BBB. Wow you and I are like twins. But now you can't find anything that points you in the directions on how to get your newly found wireless adapter working darn internet. GOOD news this will help you with that.
Note: I'm using Debian distro Wheezy-bbb

Working assumptions you have installed Debian on to your BBB and you do have a TP-Link TL-WN722N. If any of these assumption are wrong then I can't say these steps will work.

Step 1: Make sure your source.list looks like this
cat /etc/apt/sources.list
deb http://ftp.us.debian.org/debian/ wheezy main contrib non-free
#deb-src http://ftp.us.debian.org/debian/ wheezy main contrib non-free
deb http://ftp.us.debian.org/debian/ wheezy-updates main contrib non-free
#deb-src http://ftp.us.debian.org/debian/ wheezy-updates main contrib non-free
deb http://security.debian.org/ wheezy/updates main contrib non-free
#deb-src http://security.debian.org/ wheezy/updates main contrib non-free
deb http://ftp.debian.org/debian wheezy-backports main contrib non-free
#deb-src http://ftp.debian.org/debian wheezy-backports main contrib non-free
deb [arch=armhf] http://beagle.s3.amazonaws.com/debian wheezy-bbb main
#deb-src [arch=armhf] http://beagle.s3.amazonaws.com/debian wheezy-bbb main

Step 2: If you had to change or add things to the source.list you will need to update your packages
sudo apt-get update

Step 3: Install the wifi libraries.
sudo apt-get install wireless-tools usbutils

Step 4: Download and install the firmware for the wifi card.
sudo apt-get install firmware-atheros
sudo wget http://linuxwireless.org/download/htc_fw/1.3/htc_9271.fw
sudo cp htc_9271.fw /lib/firmware

Step 5: Adding the adaptor info to the interface file.
sudo vi /etc/network/interface
Add to the bottom of the file.
# The wireless interface
auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa.conf
Step 6: Plug the Wifi card in.
Step 7: Make sure system sees the wifi card.
sudo iwconfig
root ~ # sudo iwconfig
wlan0     IEEE 802.11bgn  ESSID:"crash_wireless"  
          Mode:Managed  Frequency:2.437 GHz  Access Point: 00:14:BF:05:4A:05   
          Bit Rate=24 Mb/s   Tx-Power=20 dBm   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:off
          Link Quality=70/70  Signal level=-30 dBm  
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:264   Missed beacon:0
Step 8: Make the wpa.conf file.
sudo vi /etc/wpa.conf
network={
    ssid="TheSSID"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="ThePassword"
}
Step 9: Lets start this thing.
sudo ifup wlan0
Step 10: Lets make sure we are connected
root ~ # ifconfig
eth0      Link encap:Ethernet  HWaddr 90:59:af:95:ca:42  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:40 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

usb0      Link encap:Ethernet  HWaddr f6:76:6d:4e:92:12  
          inet addr:192.168.7.2  Bcast:192.168.7.3  Mask:255.255.255.252
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr c0:4a:00:1e:51:c4  
          inet addr:192.168.3.139  Bcast:192.168.3.255  Mask:255.255.255.0
          inet6 addr: fe80::c24a:ff:fe1e:51c4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:183939 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1264 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:27428428 (26.1 MiB)  TX bytes:207095 (202.2 KiB)
Step 11: Do a dance cause it is connected. If not try turning off the BBB and turning it on again.

Hello World

Hello to all that have stumbled across this blog. What we do in this blog is pretty simple I write things and you read (or don't read) what I write.

Disclaimer: I am no English expert so if you see something that is not grammatically correct then correct it mentally and move on. Miss spellings are going to happen it's life in my world I will do my best not to make them but it will happen.

Some things you will find me writing about. I am getting in to programing ARM processors and I have a beagle bone black Rev B. I am also a consultant that writes C# code  so maybe things on that. Some things with Node.js and some other things. Some of the blogs be how I did things and some of them will be my thoughts on the language or code or process. All in all it will be just be a bunch of geek/nerd/programmer things. This is my first "technical" blog I have ever written so don't be surprised as I post more that more "cool" things show up in it.
Well we will call this the end to my first blog about what my blogs will be about. Enjoy life and just remember don't look back to much you may trip over something in front of you.
:f
-Mat