hardware hacking con arduino y rad

53
Hardware Hacking en Ruby: Arduino y RAD Esti Álvarez y Svet Ivantchev http://www.efaber.net Conferencia Rails 2008

Upload: estialvarez

Post on 14-May-2015

3.187 views

Category:

Technology


5 download

DESCRIPTION

Presentación sobre Arduino y RAD para la Conferencia Rails 2008

TRANSCRIPT

Page 1: Hardware Hacking con Arduino y RAD

Hardware Hacking en Ruby: Arduino y RAD

Esti Álvarez y Svet Ivantchevhttp://www.efaber.net

Conferencia Rails 2008

Page 2: Hardware Hacking con Arduino y RAD

Sistemas integrados

Page 3: Hardware Hacking con Arduino y RAD

Physical Computing

Page 4: Hardware Hacking con Arduino y RAD

¿Qué es Arduino?

http://www.arduino.cc

Page 5: Hardware Hacking con Arduino y RAD

¿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€)

Page 6: Hardware Hacking con Arduino y RAD

La familia Arduino

Page 7: Hardware Hacking con Arduino y RAD

El Hardware

Page 8: Hardware Hacking con Arduino y RAD

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

Page 9: Hardware Hacking con Arduino y RAD

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

Page 10: Hardware Hacking con Arduino y RAD
Page 11: Hardware Hacking con Arduino y RAD
Page 12: Hardware Hacking con Arduino y RAD

El Software

http://arduino.cc/en/Main/Software

Page 13: Hardware Hacking con Arduino y RAD
Page 14: Hardware Hacking con Arduino y RAD

Arduino

Pin 13

Hello World: Hardware

Page 15: Hardware Hacking con Arduino y RAD

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}

Page 16: Hardware Hacking con Arduino y RAD

Demo

Page 17: Hardware Hacking con Arduino y RAD
Page 18: Hardware Hacking con Arduino y RAD

Demo

Page 19: Hardware Hacking con Arduino y RAD
Page 20: Hardware Hacking con Arduino y RAD

Arduino

pin 9

pin 10

pin 11

PWM

Difuminado RGB: Hardware

Page 21: Hardware Hacking con Arduino y RAD

PWM

Page 22: Hardware Hacking con Arduino y RAD

/* 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}

Page 23: Hardware Hacking con Arduino y RAD

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”

Page 24: Hardware Hacking con Arduino y RAD

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

Page 25: Hardware Hacking con Arduino y RAD

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

Page 26: Hardware Hacking con Arduino y RAD

Hello Worldclass HelloWorld < ArduinoSketch

output_pin 7, :as => :led

def loop led.blink 500 end

end

Page 27: Hardware Hacking con Arduino y RAD

Compilar y transferir

$ rake make:upload

Page 28: Hardware Hacking con Arduino y RAD

Demo

Page 29: Hardware Hacking con Arduino y RAD
Page 30: Hardware Hacking con Arduino y RAD

Twitter Lámpara

Page 31: Hardware Hacking con Arduino y RAD

Twitter Lámpara

Page 32: Hardware Hacking con Arduino y RAD

Twitter Lámpara

Page 33: Hardware Hacking con Arduino y RAD

Twitter Lámpara

Page 34: Hardware Hacking con Arduino y RAD

Twitter Lámpara

Page 35: Hardware Hacking con Arduino y RAD

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.

Page 36: Hardware Hacking con Arduino y RAD

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

Page 37: Hardware Hacking con Arduino y RAD

TwitAmbiLight: Hardware

Page 38: Hardware Hacking con Arduino y RAD

Arduino

pin 9

pin 10

pin 11

+5 V +5 V +5 V

USB

Internet

PWM

TwitAmbiLight: Hardware

Page 39: Hardware Hacking con Arduino y RAD

# 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

Page 40: Hardware Hacking con Arduino y RAD

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

Page 41: Hardware Hacking con Arduino y RAD

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

Page 42: Hardware Hacking con Arduino y RAD

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

Page 43: Hardware Hacking con Arduino y RAD

...

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

Page 44: Hardware Hacking con Arduino y RAD

Demo

Page 45: Hardware Hacking con Arduino y RAD

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

Page 46: Hardware Hacking con Arduino y RAD

XBee XBee Arduino Shield

Page 47: Hardware Hacking con Arduino y RAD

pin 9

pin 10

pin 11

+5 V +5 V +5 V

Internet

ArduinoXbee

Xbee

802.15.4

serie

serie

WiTwitAmbiLight: Hardware

Page 48: Hardware Hacking con Arduino y RAD

Demo

Page 49: Hardware Hacking con Arduino y RAD

Nunchuck como sensor

• Interfaz I2C standard

• Accelerómetro de 3 ejes

• Joystick analógico de 2 ejes

• 2 botones

Page 50: Hardware Hacking con Arduino y RAD

pin 9

pin 10

pin 11

+5 V

ArduinoXbee

Xbee

serie

serie

PWM

i2c

802.15.4

Arduino

Ejemplo Nunchuck

Page 51: Hardware Hacking con Arduino y RAD

Demo

Page 52: Hardware Hacking con Arduino y RAD

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/

Page 53: Hardware Hacking con Arduino y RAD

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/