次の方法で共有


Pi For Brains Robot - Part 1 Setup

This is a pull from my blog: www.indiedevspot.com The other parts will be published there prior to making it here.

Pi for Brains Robot – Part 1 – Setup

Hello World!

Welcome to my blog’s version of the #IDevThis project that will be coming out.  This full series will be made available on instructables and channel9 once it has been completed as a full unit.

Pi for Brains Robot (aka Danger-Bot)

These days it seems like I’m always dealing with a robot, they are on the phone, in my car, at the grocery store, just about everywhere.  It also seems like every single one of them had Pie for brains, because it just couldn’t do what I told it to.  This project is going to build a robot that ACTUALLY has PI for brains!  Get out your breadboards, your Raspberry Pis and follow along. We will use a high performance cross platform robotics framework, Cylon, Raspberry Pi, and an Arduino Robotics Kit to build this internet connected robot.

 

About Part 1

We start from a freshly unboxed Raspberry Pi v1 B+, set up the OS, connect Wifi, enable SSH, setup permissions, install the latest node + npm, enable node to have pin access, and of course it wouldn’t be complete if we didn’t blink an led.

PiwithLED

All of these steps should be done exactly in order.  Doing these steps out of order may have different results. If you are encountering an issue not addressed, please leave a comment.

What you need for Part 1

  1. Raspberry Pi B+ or v2 Ultimate Starter Kit (cheapest option)
  2. USB Mouse, USB Keyboard, Monitor (HDMI) + Associated Cables
  3. Female Jumper Wires
    1. You can hack something together with regular wires, wire strippers, and insulated connectors.
  4. Wifi Network for your Raspberry Pi
  5. Patience

Setup the Pi and Update

Connecting it all up

  1. Insert your Pi into the protective case
  2. Connect the USB of your mouse and keyboard into any USB slots on the Pi.
  3. Connect the USB Wifi (came with the Ultimate Starter Kit) into any USB slot
  4. Connect your Monitor to the HDMI slot on the Pi
  5. Insert the Mini SD Card (came with the Ultimate Starter Kit) into the mini SD Card Slot
  6. Plug the mini USB power supply into the mini USB slot
    1. Your Pi should power on at this point and take you to an OS selection page.

PiConnectedFAndB

Installing Raspbian

  1. Using your mouse, choose Raspbian from the list
    1. Ensure no other options are selected
  2. Ensure your keyboard layout is US (or your layout)
  3. Click Install
  4. Click yes on the dangerous looking message
  5. Wait patiently…
  6. When it is done, click OK
    1. a console will appear and text will fly, just wait some more…

InstallOS

Basic Configuration

You can always get back to this menu later by typing: “sudo raspi-config” at a command terminal.  From this menu, you can only use arrow input from your keyboard. Up and Down flow there the vertical Menu, while LEFT and RIGHT move between cancel and finish.  Enter selects the current highlighted item.  Any time you type a command, you must execute the command by pressing the enter key.

RaspiConfig

  1. Select Change User Password
    1. a prompt will appear indicating you are about to enter a password, press enter to dismiss it
    2. at the bottom you will be prompted in a cmd window for the password and to confirm it.
  2. Enter your new password (I use david1234 for non commercial applications)
    1. Press Enter
  3. Enter your password again for confirmation.
    1. Press Enter
    2. A prompt will appear indicating success, press enter.
  4. Select Advanced Options
  5. Select HostName (this is the name of your raspberry pi)
    1. a prompt will appear indicating you have to use normal letters and numbers for a name.  Press enter to dismiss
    2. a view will appear, use the delete key to delete the old name and enter your own. a-z and 0-9 only.
      1. My Pi’s name is dangerbot
    3. use the arrow keys to go to OK and press enter on OK.
      1. You are brought to the first menu again.
  6. Select Advanced Options
  7. Select SSH
    1. Select Enable SSH
    2. Select OK
  8. Use the right arrow key to navigate to Finish
  9. Select Yes, you would like to reboot now.
  10. Wait some more…
  11. A cmd promp will appear asking you to log in.
    1. dangerbot Login:
      1. type “pi” here – just pi, no quotes.
      2. pi is your user name
    2. Password:
      1. type the password you configured, I typed: david1234
      2. note that it will not show the letters or even *s
  12. SUCCESS, you are logged in at the Raspberry Pi’s cmd prompt!
  13. type the command: startx
    1. this will launch the Raspberry Pi’s Desktop Mode, which is very similar to old Windows.
    2. We will do some configuration in desktop mode

RaspiDesktop

Configure Wifi

We will be doing a lot with wifi in this project, for now we need it to download various software packages, but later we will be connecting it to Twitter, Azure, who knows?  Nothing is better than a tiny computer backed by a warehouse of servers.

  1. On the desktop, there is an Icon, “Wifi Config”, Click on that.
    1. You should see an adapter wlan <some number> show up
    2. Network should be empty
  2. configwifi1
  3. On the bottom right, click Scan
  4. A new mostly empty window will show up.
  5. On the bottom right here, click Scan as well.
  6. You should see a few networks pop up
  7. configwifi2
  8. Double Click on your network, Mine is Version-29…
    1. This should be the same network your laptop is on if you plan to connect to the pi from your laptop.
  9. A new window will appear
    1. If you know what these settings are, verify and ensure they are right
    2. If you do not, just keep the defaults for the first try.  If that doesn’t work, try various combinations.
    3. The default settings for me were correct.
  10. In the PSK field, type your network password.
    1. I’m not telling mine, as this is my real wifi I travel with.
  11. configwifi3
  12. Click Add
  13. Wait for a while…
    1. It may say complete, but it is not done until an IP address appears
    2. If it seems to take a while, try clicking connect again.
  14. configwifi4
  15. configwifi5
  16. Verify connection…
    1. On the desktop, on the left is an Icon: LXTerminal
    2. Double click this, you will be brought to a cmd prompt
    3. type: ifconfig
    4. look for your ip address
  17. configwifi6
  18. Close the wifi configuration panel.

Updating the Pi

Update your firmware

  1. If you still have the cmd prompt open from checking your ip address, you may use that, otherwise, open a new one.
  2. type the command: sudo rpi-update
    1. If this doesn’t do anything, your Raspberry Pi’s firmware is already up to date.
  3. Go get coffee, this might take a while…
  4. When it is done, type the command: sudo reboot

Update your OS

  1. if you had to reboot, login again.
    1. Login: pi
    2. Password: <yourpassword>
    3. You don’t need to go to the desktop.
  2. from the cmd prompt, type: sudo apt-get update
    1. Go get some more coffee, this might take a while.
  3. from the cmd prompt, type: sudo apt-get upgrade
    1. Accept the prompts by typing: y
      1. follow y with the enter key
    2. Go get some more coffee, this will take a while.
  4. from the cmd prompt, type: sudo reboot

SUCCESS!  Your Raspberry pi is running the latest firmware with the latest version of Raspbian!  If you just wanted to know how to set up a Pi, you can end here.  If you are interested in building robots using JavaScript and Raspberry Pis, I highly suggest you read on, as this is where things get complicated.

Install and Configure Node

Node is a JavaScript framework that CylonJS (a robotics framework) is installed from.  NPM is a Node Package Manager for getting the packages we need for dealing with our robot.

download/install node

  1. Login to the Pi
  2. Get to a command prompt if you are not already on one.
  3. type the command: sudo mkdir /opt/node
  4. Find the latest version of node from: https://nodejs.org/dist/ with the ending -Linux-arm-pi.tar.gz
    1. There is currently no easy way to do this.  You literally have to look at the dates each folder was updated and attempt to find the latest version for pi.
    2. the pi versions are kept behind the regular branches.
    3. Cylon and Node both recommend that for the Pi you use the pre-compiled versions.
    4. The latest version today is v0.10.28
      1. v0.10.28 is in fact newer than v0.10.4 (it should be v0.10.04)
      2. if you find a newer version, replace references to 0.10.28 with your version.
      3. double check cylon’s site for a version of node if you need to.
  5. type the command: sudo mkdir /opt/node
  6. type the command: wget https://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-arm-pi.tar.gz
  7. type the command: tar xvzf node-v0.10.28-linux-arm-pi.tar.gz
  8. type the command: sudo cp -r node-v0.10.28-linux-arm-pi/* /opt/node

add node and npm to your path

Adding node to your path will make executing node programs much easier.  But also once we ditch some of the fake security, we can do it easily with sudo as well.

  1. type the command: cd ~
  2. type the command: sudo nano .bash_profile ..bash_profile
  3. Add the code:
    1 2 PATH=$PATH:/opt/node/bin export PATH
  4. Hold ctrl, then push x.
  5. type y
  6. click enter
    1. if you are still in the file…
      1. hold ctrl, then push x
      2. type n if it asks you to save
  7. type the command: source ~/.bash_profile
  8. Test the pathing
    1. Type the command: node -v
      1. you should see a version number
    2. Type the command: npm -v
      1. you should see a version number

Ditch Security

The only danger posed to us here is the npm install process and 3rd party libraries we use, but we are going to need to give most of these modules low level access anyways.  Lets just go ahead and ditch security and make some robots.  This will make executing commands much easier.  If you are worried about the packages you are installing and the 3rd party libraries you are using doing something malicious, you should probably stop using that library and pick a different one, or write your own.

  1. type the command: sudo chown pi ~/ –recursive
  2. type the command: sudo nano /etc/sudoers
    1. At the end of the Defaults section, add the line: Defaults !secure_path
      1. This allows sudo to use the default path variables (we just set up node and npm)
  3. notsecurepath
  4. hold ctrl, then push x
  5. type: y
  6. hold ctrl, then push x
  7. type: n
  8. You should be back at the cmd prompt
  9. Test
    1. Type: sudo node -v
      1. you should see a version number
    2. Type: sudo npm -v
      1. you should see a version number.
  10. type the command: sudo usermod -G gpio pi
  11. type the command: sudo nano /etc/udev/rules.d/91-gpio.rules
    1. type the following code in

    2. 1 2 SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'" SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"
    3. It might be easier to ssh and copy/paste this in (do this if you are an advanced user)

    4. This must be EXACT

  12. hold ctrl, then push x.  Type y , then enter.  This saves the file
  13. type the command: sudo nano/etc/modules
  14. add the following lines
    1. i2c-bcm2708
    2. i2c-dev
  15. modules
  16. hold ctrl, then push x.  Type y , then enter.  This saves the file
  17. type the command: sudo nano /etc/modprobe.d/raspi-blacklist.conf
  18. comment out all lines by add # to the beginning
    1. blacklist
  19. type the command: sudo nano /boot/config.txt
  20. add the lines:
    1. dtparam=i2c1=on
    2. dtparam=i2c_arm=on
    3. bootconfig
  21. hold ctrl, then push x.  Type y , then enter.  This saves the file
  22. type the command: sudo addgroup i2c
  23. type the command: sudo usermod -G i2c pi
  24. type the command: sudo reboot

There really isn’t much testing we can do here yet.  We just have to have faith that it all worked and keep going.  But you have effectively removed all the extra security garbage that would ordinarily prevent pi and cylon from doing what it needs to such that we can build robots!

Write Some Code Already!

Create Folders and Install Packages for Coding

Alright, lets get set up for coding…we are going to start by building out the folder structure, installing necessary packages and testing out the cylon framework.

  1. Login using pi and your password
  2. Get to your cmd window if you are not there already.
  3. type the command: sudo mkdir Programs
  4. type the command: cd Programs
  5. type the command: sudo mkdir HelloPi
  6. type the command: cd HelloPi
  7. type the command: sudo chown pi ~/ –recursive
  8. type the command: sudo chown pi ~/Programs –recursive
    1. this is just for good measure
  9. sudo reboot
    1. this too is just for good measure
  10. login again
  11. type the command: npm install cylon cylon-raspi
    1. note that it does not say sudo.  If you use sudo, it will not work.
    2. This takes a while, wait and go get some coffee…
  12. type the command: npm install cylon-gpio cylon-i2c

Simple Console Output

  1. type nano app.js
    1. Type the code:

    2. 1 2 3 4 5 6 7 8 9 10 11 12 var Cylon = require("cylon");   Cylon.robot({     connections: {         raspi: { adaptor: 'raspi' }     },     work: function (my) {         every((1).second(), function () {             console.log("led toggle");         });     } }).start();
  2.  hold ctrl, then push x.  Push y then enter, this saves the file.
  3. type the command: sudo node app.js
  4. you should see the following output:
  5. cylonworking
  6. hold ctrl, then push c to stop execution.

SUCCESS!!!  You have not only node working, but also cylon executing.  Now we can wire up our LED and instead of writing led toggle to the output, lets actually toggle an led

Wiring the Pi

Ensure your pi does not have power attached to it before you begin wiring.

piWiring

The pi is a bit confusing to wire.  I think of the pins as physical vs logical.  Physical pins on the top are even, while physical pins on the bottom are odd.  The counting is based left to right with the pi oriented in the fashion I have shown in the diagram above.  In this instance, the wire going to the + side of the LED is physical pin 7.  This maps to a different logical pin, but because we use cylon, we don’t care :).  The ground is connected to physical pin 39.  The best site so far that explains the pins can be found here: https://pi.gadgetoid.com/pinout the numbers on this site correlate to physical pins.  You always can just count left to right acknowledging even/odd.

Blinking an LED with Code.

  1. login again

  2. type the command: cd Programs/HelloPi

  3. type the command: nano app.js

  4. replace the current code with the following code:

  5. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 //make sure we have cylon as a dependency var Cylon = require("cylon");   Cylon.robot({ name: 'HelloPi' })     //setup connection     .connection('pi', { adaptor: 'raspi' })     .device('led', { driver: 'led', pin: 7 })     .on('ready', function (dangerbot) {     every((1).second(), function () {         //toggle LED on or off         dangerbot.led.toggle();         console.log("led toggle");     }); }).start();
  6.  hold ctrl, push x, type y followed by enter to save the file.

  7. type the command: sudo node app.js

    1. Watch your pi light up!
    2. PiLEDOff
    3. PiLEDOn

AND THATS IT!  You have a Raspberry Pi, with Cylon and GPIO access.

Common Linux Commands

  • sudo: preceding a command with sudo gives root access
  • mkdir: makes a directory
  • man <cmd>: tells you how to use a specific command
  • chown: changes the owner for a specified directory
  • cd: changes a directory
  • ls: lists the contents of the current directory
  • wget: downloads a files to the current location
  • tar: unzips a file
  • nano: is a very crude text editor
  • usermod: modifies properties of a user.
  • node: (not linux specific)executes a node runtime, you can write code directly from here, or  execute javascript files.
  • npm: (not linux specific) installs or uninstalls node packages.

Why use Raspberry Pi and CylonJS

Raspberry Pi can be acquired very cheaply and is a great general purpose computer as well as tinkering platform.  It really has a lot of nice components to it for a beginner.  I would recommend starting with a Raspberry Pi and then upgrading to an Intel Edison.  Why CylonJS?  Cylon is JavaScript, has a really nice coding interface for it and is multiplatform.  With simple changes, I can move my code written for Raspberry Pi over to an Edison.  Beyond just the robotics, Node and JavaScript are skills that will serve you in web as well as database programming.  This makes CylonJS an ideal language for learning to program.  Raspberry Pi between being affordable and the fact that we were able to do all of this without using a traditional computer at all makes it an ideal educational platform.  Not to mention the Raspberry Pi v2 can run Windows 10, which will make programming for it easier.

Summary

So there you have it, we have covered a TON of material in this article.  We have covered, Getting started with a brand new Raspberry Pi, how to update the firmware, how to update the Operating System, how to configure wifi.  We have covered how to install and configure NodeJS as well as NPM allowing them to access the low level hardware of the device by removing many of the security components that prevent this.  We have covered basics of wiring to the pins of a Raspberry Pi and executing node scripts that leverage CylonJS as their robotics platform.  We also covered why you would even want to use Cylon and Raspberry Pi specifically.  I hope you enjoyed this article.  Please leave comments, feedback and questions in the comments section below.