Wednesday, February 19, 2014

47. Robotics with PiBot. II - Setting up the Software

11,000 page views!!
Here's what the hardware currently looks like:

It's powered up!  There are electrons flowing through its veins, but it's still asleep so it doesn't do anything yet.  It needs some software!  You may be able to see the blue LED lit on the Bot Board, the red PWR LED lit up on the Pi, and the flashing blue LED on the WiFi dongle.
It's on a raised platform just in case the wheels should start turning and it takes off!  The voltage regulator can be seen wrapped in green heat-shrink (I had to change the order of the regulated orange lead and the unregulated red lead before connecting to the Bot Board's power header). 

The wires to the neo-pixel header have been connected, and there are two flying leads - a purple and a green, which are attached to the two pins nearest to the GPIO pins on the Bot Board's 6-pin ISP header near the blue power LED.  These are for when the Bot Board's Arduino needs to be re-set - they just need to be momentarily touched together to do a micro-controller re-set.

The rainbow-coloured 5-wire strip is running from the Bot Board's Serial interface to a USB to CP2102 TTL UART Module which I purchased for £3.70 from ATelecs on ebay.  This module has a USB connection for inserting into the PC's USB socket.  

Be careful not to connect both the Pi's power supply (through the Pi's micro USB connector) and the UART module to the PC at the same time.  There is currently no battery in the PiBot, and in any case, its main switch is in the off position.  This is a third potential power conflict, so pay particular attention to what power sources are connected!

I thought I would start with a clean sheet - a new SD card, for the PiBot.  Actually, it's an 8 GB micro SD card slotted into a micro SD to SD adapter.  Here are the steps I took:
  1. Download SDFormatterv4 from https://www.sdcard.org/downloads/formatter_4/eula_windows/
  2. Unzip this and put the SD adapter card into the USB/SD adapter, and run SDFormatterv4
  3. Download NOOBS from www.raspberrypi.org/downloads, unzip and copy the files onto the SD card.  The NOOBS_v1_3_4.zip was only 1.27GB - much more compact than the images I used to download.
  4. Put the SD card into a Model B Raspberry Pi (because it has an Ethernet connector), connect to the WiFi Router via Ethernet, and fire up the Pi
  5. Insert the WiFi dongle (the Edimax appears to be the most recommended)
  6. Start the WiFi Config program on the Pi's desktop, click on the Scan button, and double-click the required network from the listed networks which have been found.
  7. Choose TKIP encryption, enter the WiFi Wireless Key code into PSK, then Add - that's it!
  8. Move both the Edimax dongle and the SD card to the PiBot's Pi (mine has a Model A Pi).
  9. Download PUTTY (I used this before in an earlier post) to the PC
  10. Download TightVNCViewer to the PC (again I used this before in earlier posts)
  11. On the Pi, do a sudo apt-get update, and sudo install tightvncserver
  12. Run tightvncserver by typing vncserver:1 (I used the same command as before - vncserver:1 -geometry 800x600 -depth24 - the extra attributes -geometry 800x600 specifies the size of desktop to be created - the default is 1024 x 768
    -depth24 : 24 specifies the pixel depth in bits of the desktop to be created - the default is 16 and other possible values are 8, 15 and 24 - don't use anything else.
  13. Run  PUTTY and TightVNCViewer on the PC (you can save the VNC session for later convenience) and hey presto - we have the PiBot's Pi desktop running on the PC, through WiFi!!
  14. Download the driver for the USB to CP2102 TTL UART Converter from http://www.geeetech.com/Documents/CP2102%20USB%20Driver.rar, unzip, and run the installer file.
  15. Download the Arduino sketch for the Bot Board's Arduino from https://github.com/pi-oneers/bot-firmware and upload to the Bot Board via the UART Converter.
     
    Okay -  this brings us to the point where we can communicate with the PiBot from the PC over the WiFi network.  But we still need to fill the PiBot's brain with thoughts and intentions which make it do some proper robot stuff!!
      Even before we do this, there's some further preparation to be done - the system needs to be configured:
       
  16. Enable the SPI interface on the Raspberry Pi:
    a.  first do a sudo raspi-config, choose Advanced Options and then choose Enable SPI interface by default
    b.  do a  sudo nano /etc/modprobe.d/raspi-blacklist.conf to open that file for editing, and make sure that the SPI line is NOT blacklisted, by making sure the line is commented out with a '#' at the start
    c.  restart with a sudo reboot
    d.  check that the module has been loaded with a lsmod | grep spi_bcm2708
  17. Install the SPIdev python module:
    sudo apt-get install python-dev
    mkdir python-spi
    cd python-spi
    wget https://raw.github.com/doceme/py-spidev/master/setup.py
    wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c
    cd bot-command && sudo python setup.py install
  18. Download the PiBot code repository from Github
    The following command can be used to do a software update:
    git clone https://github.com/pi-oneers/bot-command
  19. Test the hardware:
    There are some test programs in the /python-spi/bot-command/examples folder:
    test_pibot_hardware.py,
    drive_in_square.py,
    monitor_ultrasonic.py and
    robot_teleop.py
    which should put the hardware through its paces.  These can be executed by using, for example:
    cd python-spi/bot-command/examples
    sudo python test_pibot_hardware.py

    or sudo python drive_in_square.py
    or sudo python monitor_ultrasonic.py
    or sudo python robot_teleop.py
Here's a shot of the PiBot on its pedestal, connected to the laptop, after uploading the Arduino code:

And here's the screen shot to show the headless Pi's desktop and the Arduino IDE with the Bot Board's sketch after loading.

And I got it to move!  Have a look at the following video - it's battery-powered, WiFi-enabled, Arduino-firmwared and controlled by python on the Raspberry Pi!


This python script - drive_in_square.py - simply sends the Bot around the sides of a square (for ever - or until the batteries run out, or, in this case, it collides with something solid!)  I also got the script monitor_ultrasonic.py to run successfully - it gives a continuous reading of distance from the ultrasound transducer to an obstacle, in centimeters.  There are a couple of other test scripts which I have to get going yet - but it's so exciting that the Bot has come to life!!

Next time I will try to explain the software and develop some more python programs.






No comments:

Post a Comment