Sleepy Pi - Ã land Sailing Robots
Transcription
Sleepy Pi - Ã
land Sailing Robots
Sleepy Pi Introduction The Sleepy Pi is essentially a Smart Power Management board for the Raspberry Pi that includes a low power Arduino that stays powered when the Raspberry Pi is off. In this way it can reduce the overall power used by the RPi, particularly when powered from batteries in applications where the RPi doesn’t need to be on all the time. You can think of it as adding a “Sleep” mode to the Raspberry Pi, because the Arduino can periodically wake the RPi up to perform a task and then put it back to sleep. As a side benefit you also get some Arduino I/O to play with, like Analogue Inputs and PWM’s. Key benefits include: • • • • • • • Reduce the effective Power consumption of the Raspberry Pi Power directly from batteries up to 17V Adds Arduino I/O to the Raspberry Pi such as Analogue In or PWM Break out the Raspberry Pi GPIO & Arduino I/O to screw terminals Prototyping area and expansion headers for daughter boards Power button for manual switch on/off “Wake” or “Sleep” in response to events or triggers such as: o Analogue value crosses a threshold o Digital input changes o Specific time to “wake” via Real-time Clock Sleepy Pi Information & code examples All Sleepy Pi Info! • • • • • • • • Sleepy Pi FAQ Installation and Powering Setting up the Arduino IDE on Raspbian Programming from the Arduino IDE Writing Arduino Code on the Sleepy Pi Getting the Sleepy Pi to Shutdown the Raspberry Pi Programming the Sleepy Pi as a Standalone Board Accessing the Real-Time Clock from the Raspberry Pi Here: http://spellfoundry.com/sleepy-pi/ Sleepy Pi examples and libraries here: https://github.com/SpellFoundry/SleepyPi Setting up the Arduino IDE on Raspbian (It should be possible to apply these same steps for ARCH Linux) The Arduino processor on the Sleepy Pi can be programmed directly from the Arduino IDE running on the Raspberry Pi. Step 1: Arduino IDE Installation The first step is to load the Arduino environment onto the Raspberry Pi. It is a good idea to ensure that your OS is up to date by executing the following lines (we are going to need internet access to our RPi): sudo apt-get update sudo apt-get update –fix-missing sudo apt-get dist-upgrade This last command could need several minutes. It will ensure that we have the latest versions of RPi.GPIO which will be required later. Now install the Arduino IDE: sudo apt-get install arduino Sleepy Pi Step 2: Disable Serial login The Arduino processor on the SPi can be programmed directly from the RPi using the serial GPIO lines on the RPi and another GPIO line to reset the Arduino to allow automatic code upload. These pins are: • • • GPIO 14: TXD GPIO 15: RXD GPIO 22: Reset (see next section) By default RPi has exclusive access to the serial pins to output status, debug data and logging in. Raspbian allows us to login using the serial port. To use the Sleepy Pi we need to disable this, so we need to edit the following document: sudo leafpad /etc/inittab We need to comment the last line out with “#” and save it. #Spawn a getty on Raspberry Pi Serial line #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 Step 3: Disable Boot info When Raspbian boots up it outputs boot information to the serial port and hence streams in to the SPi (which is not particularly intersested in it). To disable this we need to edit this document: sudo leafpad /boot/cmdline.txt We need to delete the following part in bold style: dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p6 rootfstype=ext4 elevator=deadline rootwait Step 4: Link the Serial port to the Arduino IDE The Arduino EDE wants to use the /dev/ttyS0 serial port, but we need to use the /dev/ttyAMA0 which is linked to the GPIO. In order to do this, we need to create a permanent link that maps AMA0 to S0, so we need to create a small file: sudo leafpad We type the following lines in the new file and save it called “80-sleepypi.rules” to /etc/udev/rules.d/ : KERNEL=="ttyAMA0", SYMLINK+="ttyS0",GROUP="dialout",MODE:=0666 KERNEL=="ttyACM0", SYMLINK+="ttyS1",GROUP="dialout",MODE:=0666 Sleepy Pi Step 5: Setting up the Reset (DTR) pin The Sleepy Pi Arduino processor reset line in connected to GPIO 22. To automatically upload code from the Arduino IDE we need to pulse this line low to rest the Arduino and enter bootload mode. On a normal Arduino system connected to a computer via a USB / serial cable the reset line is connected to the DTR line. To replicate this behavior on the Raspberry Pi we need to hack the AVRDude programming software. Dean Mao has detailed a great hack for this. He’s produced a modified version of Avrdude (avrdude-autoreset) and written a piece of python code (autoreset) that runs in the background and pulses the GPIO line when required. Download code as a ZIP file https://github.com/SpellFoundry/avrdude-rpi After unzip the file, we use the following commands: cd ./avrdude-rpi-master/ sudo cp autoreset /usr/bin sudo cp avrdude-autoreset /usr/bin sudo mv /usr/bin/avrdude /usr/bin/avrdude-original This renames our original avrdude, so we have a backup and replace it with the new one. sudo ln –s /usr/bin/avrdude-autoreset /usr/bin/avrdude Link the new avrdude-autoreset to avrdude so that when something calls for it, the new version runs instead. Step 6: Adding the SPi to the Arduino environment To enable the Sleepy Pi to be selected from the IDE you need to add a folder and file to your sketchbook. If it is a fresh install and you haven’t yet run the Arduino environment you’ll need to create a sketchbook folder (skip this step if it already exists). mkdir /home/pi/sketchbook mkdir /home/pi/sketchbook/hardware mkdir /home/pi/sketchbook/hardware/Sleepy_pi Sleepy Pi Now we have to create a new file boards.txt with the configuration RPi will need: sleepypi.name=Sleepy Pi sleepypi.upload.protocol=arduino sleepypi.upload.maximum_size=30720 sleepypi.upload.speed=57600 sleepypi.bootloader.low_fuses=0xFF sleepypi.bootloader.high_fuses=0xDA sleepypi.bootloader.extended_fuses=0x05 sleepypi.bootloader.path=arduino:atmega sleepypi.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex sleepypi.bootloader.unlock_bits=0x3F sleepypi.bootloader.lock_bits=0x0F sleepypi.build.mcu=atmega328p sleepypi.build.f_cpu=8000000L sleepypi.build.core=arduino:arduino sleepypi.build.variant=arduino:standard Step 7: Reboot the RPi sudo reboot Programming from the Arduino IDE The Arduino IDE can be found on the Main GUI menu in the Electronics section. Sleepy Pi Once loaded the Sleepy Pi should be selected as a target from the Tools menu. Press the “Upload” button and the sketch will be compiled and uploaded.