Upload
estialvarez
View
3.205
Download
5
Embed Size (px)
DESCRIPTION
Presentación sobre Arduino y RAD para la Conferencia Rails 2008
Citation preview
Hardware Hacking en Ruby: Arduino y RAD
Esti Álvarez y Svet Ivantchevhttp://www.efaber.net
Conferencia Rails 2008
Sistemas integrados
Physical Computing
¿Por qué Arduino?
• Es Open Source
• Es multiplataforma (Linux, Mac, Windows)
• Se puede programar con cable USB
• Pensado para hacer prototipos
• El hardware es barato (30€)
La familia Arduino
El Hardware
El Hardware• 14 pins IO digitales
• 6 pins Input analógicos
• 6 pins Output analógicos (PWM)
• 16Kb memoria Flash (Apollo 11: 74 Kb)
• 1Kbyte RAM (Apollo 11: 4Kb)
• Microcontrolador ATMega168 a 16MHz
• Alimentación por USB o pila de 9V
Otras piezas
• Sensores “binarios”: interruptores, termostatos, switches magnéticos, de posición, de luz, etc
• Sensores “analógicos”: termistores, accelerómetros.
• “Actuadores”: leds, motores, servos, LCDs
El Software
http://arduino.cc/en/Main/Software
Arduino
Pin 13
Hello World: Hardware
Hello World: Software/* Blink * The basic Arduino example.Turns on an LED on for one second, * http://www.arduino.cc/en/Tutorial/Blink */
int ledPin = 13; // LED connected to digital pin 13
void setup() { // run once, when sketch starts pinMode(ledPin, OUTPUT); // sets the digital pin as output}
void loop() { // run over and over again digitalWrite(ledPin, HIGH);// sets the LED on delay(1000); // waits for a second digitalWrite(ledPin, LOW); // sets the LED off delay(1000); // waits for a second}
Demo
Demo
Arduino
pin 9
pin 10
pin 11
PWM
Difuminado RGB: Hardware
PWM
/* Code for cross-fading 3 LEDs, RGB, or one tri-color LED */int redPin = 9; int greenPin = 10; int bluePin = 11;int redVal = 255; int greenVal = 1; int blueVal = 1;int i = 0; // Loop counter
void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); }
void loop() { i += 1; // Increment counter if (i < 255) { redVal -= 1; greenVal += 1; blueVal = 1; } else if (i < 509) { redVal = 1; greenVal -= 1; blueVal += 1; } else if (i < 763) { redVal += 1; greenVal = 1; blueVal -= 1; } else { i = 1; } analogWrite(redPin, redVal); analogWrite(greenPin, greenVal); analogWrite(bluePin, blueVal); delay(10); // Pause for 10 milliseconds before resuming the loop}
RAD: Ruby Arduino Development
• Añadir a Arduino las ventajas de programar en Ruby
• Convenciones y helpers inspirados en Rails
• Testing Framework aprovechando el dinamismo de Ruby
• Entorno de simulación gráfica usando “Shoes GUI”
RAD: Ruby Arduino Development
$ sudo gem install rad$ rad hello_worldSuccessfully created your sketch directory.
...Added hello_world/hello_world2.rbAdded hello_world/RakefileAdded hello_world/configAdded hello_world/config/hardware.ymlAdded hello_world/config/software.yml
RAD: Ruby Arduino Development
$ ls -l hello_world
-rw-r--r-- 1 esti esti 31 Rakefile
drwxr-xr-x 4 esti esti 136 config
drwxr-xr-x 42 esti esti 1428 examples
drwxr-xr-x 7 esti esti 238 hello_world
-rw-r--r--@ 1 esti esti 106 hello_world.rb
drwxr-xr-x 5 esti esti 170 vendor
Hello Worldclass HelloWorld < ArduinoSketch
output_pin 7, :as => :led
def loop led.blink 500 end
end
Compilar y transferir
$ rake make:upload
Demo
Twitter Lámpara
Twitter Lámpara
Twitter Lámpara
Twitter Lámpara
Twitter Lámpara
TwitAmbiLight
• Comunicación por puerto serie
• Datos del REST API de Twitter
• Los colores de nuestra lámpara dependerán del humor de nuestros followers en Twitter.
TwitAmbiLight: Ingredientes
• 1 Arduino y 1 breadboard
• 1 LED RGB y 3 resistencias de 220 ohms
• 1 Cable USB y unos cuantos de conexión
$ gem install toholio-serialport
$ gem install twitter
$ gem install mbbx6spp-twitter4r
TwitAmbiLight: Hardware
Arduino
pin 9
pin 10
pin 11
+5 V +5 V +5 V
USB
Internet
PWM
TwitAmbiLight: Hardware
# Se conecta al API de Twitter y busca colores en formato RGB # Hexadecimal entre los replies a @raduino# Ejemplo: @raduino #FF0000
%w(rubygems twitter serialport).each {|g| require g}gem 'mbbx6spp-twitter4r'
# params para puerto serieport_str = "/dev/tty.usbserial-A4001lmt" # puede cambiar para tibaud_rate = 9600data_bits = 8stop_bits = 1parity = SerialPort::NONEsp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
...
TwitterAmbiLight: Software Ordenador
def get_color_from_twitter client = Twitter::Client.new(:login => 'raduino', :password => 'mypass') last_status = client.status(:replies).first color = last_status.text.gsub('@raduino', '').strip || ''end
5.times do puts "Cojo el color" color = get_color_from_twitter puts "Devuelve #{color}" if color.match(/#[A-F0-9]{6}$/i) puts "Envio en color al puerto serie" sp.write color end puts "-------------" sleep(10)endsp.close
class TwitAmbiLight < ArduinoSketch output_pin 9, :as => :red_led output_pin 10, :as => :green_led output_pin 11, :as => :blue_led r = 255; g = 255; b = 255 @first_byte = byte; @byte1 = byte; @byte2 = byte; @byte3 = byte @byte4 = byte; @byte5 = byte; @byte6 = byte
serial_begin def setup red_led.digitalWrite HIGH green_led.digitalWrite HIGH blue_led.digitalWrite HIGH end ...
TwitterAmbiLight: Software Arduino
...def loop if serial_available # Si el primer byte es #, leer los 6 siguientes @first_byte = serial_read if @first_byte == '#' @byte1=serial_read; serial_print "Primer caracter:";serial_println @byte1 @byte2 = serial_read; serial_print "2 caracter:"; serial_println @byte2 @byte3 = serial_read; serial_print "3 caracter:"; serial_println @byte3 @byte4 = serial_read; serial_print "4 caracter:"; serial_println @byte4 @byte5 = serial_read; serial_print "5 caracter:"; serial_println @byte5 @byte6 = serial_read; serial_print "6 caracter:"; serial_println @byte6
r = 255 - h2d(@byte1) * 16 + h2d(@byte2) g = 255 - h2d(@byte3) * 16 + h2d(@byte4) b = 255 - h2d(@byte5) * 16 + h2d(@byte6) end serial_print "Rojo: "; serial_println r serial_print "Verde: "; serial_println g serial_print "Azul:"; serial_println b red_led.analogWrite(r); green_led.analogWrite(g); blue_led.analogWrite(b) delay(100) endend...
...
def h2d(c) if c >= 48 && c <= 57 # c esta entre 0 y 9 return c - '0' elsif c >= 'A' && c <= 'F' # c esta entre A y F return c - 65 + 10 end end end
Demo
Opciones WirelessZigbee
802.15.4GSM/GPRS 802. 11 Bluetooth
Aplicación
Duración batería
Ancho de banda
Alcance típico
Ventajas
Moritorizado y control
Voz y datos Internet rápido Conectividad entre dispositivos
~ semanas ~ días ~ horas ~ horas
250 kbps Hasta 2 Mbps Hasta 54 Mbps 720 kbps
100-1000 metros Kilómetros 150-300 metros 10-100 metros
Bajo consumoInfraestructura
existente Velocidad Comodidad
XBee XBee Arduino Shield
pin 9
pin 10
pin 11
+5 V +5 V +5 V
Internet
ArduinoXbee
Xbee
802.15.4
serie
serie
WiTwitAmbiLight: Hardware
Demo
Nunchuck como sensor
• Interfaz I2C standard
• Accelerómetro de 3 ejes
• Joystick analógico de 2 ejes
• 2 botones
pin 9
pin 10
pin 11
+5 V
ArduinoXbee
Xbee
serie
serie
PWM
i2c
802.15.4
Arduino
Ejemplo Nunchuck
Demo
Referencias
• Getting Started with Arduino. Make Magazine
• http://www.arduino.cc/
• http://rad.rubyforge.org/
• http://github.com/atduskgreg/rad/tree/master
• http://todbot.com/blog/bionicarduino/
Algunos ejemplos
• http://www.cs.colorado.edu/~buechley/LilyPad/build/turn_signal_jacket.html
• http://vimeo.com/1261369
• http://vimeo.com/1650051
• http://www.botanicalls.com/kits/