prototipare col raspberry pi

28
Prototipare col Raspberry Pi Gabriele Guizzardi [email protected]

Upload: gabriele-guizzardi

Post on 29-Jan-2018

311 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Prototipare col raspberry pi

Prototipare col Raspberry Pi

Gabriele [email protected]

Page 2: Prototipare col raspberry pi

Perché?

Il costo?

La potenza?

I linguaggi o i vari sistemi operativi?

LA COMUNITA’ ONLINE!!!

Page 3: Prototipare col raspberry pi

Sensori

Page 4: Prototipare col raspberry pi

Motori

Page 5: Prototipare col raspberry pi

Principali Sistemi Operativi

Raspbian (da Debian)

Ubuntu Mate

Snappy Ubuntu Core

Arch Linux Arc

Gentoo

Debian ARM

Windows 10 IoT (Pi 2)

Page 6: Prototipare col raspberry pi

Principali linguaggi con libreria per la GPIO

Python (RPi.GPIO)

C (wiringPi)

Processing (processing.io.*)

Java (Pi4J)

da riga di comando o Bash usando i relativi files (/sys/class/gpio/….)

Page 7: Prototipare col raspberry pi

La GPIO

Page 8: Prototipare col raspberry pi

Nomenclatura dei pin

Page 9: Prototipare col raspberry pi

Accendereun LED

Page 10: Prototipare col raspberry pi

Accendere un LEDimport RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.OUT)

# ciclo 10 volte, on/off di 1 sec.

for i in range(10):

GPIO.output(7,True)

time.sleep(1)

GPIO.output(7,False)

time.sleep(1)

GPIO.cleanup()

Page 11: Prototipare col raspberry pi

Accendere un LED

Page 12: Prototipare col raspberry pi

Pulsanti

Page 13: Prototipare col raspberry pi

PulsantiPull Down

Pull Up

Page 14: Prototipare col raspberry pi

Pulsantiimport RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM) # Broadcom SOC channel

GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:

input_state = GPIO.input(4)

if input_state == False:

print('Pulsante premuto')

time.sleep(0.5)

Page 15: Prototipare col raspberry pi

I2C (inter integrated circuit)

MAX112

Page 16: Prototipare col raspberry pi

I2C

Page 17: Prototipare col raspberry pi

I2C

Page 18: Prototipare col raspberry pi

I2C

Page 19: Prototipare col raspberry pi

IC2 #!/usr/bin/python

import smbus

bus = smbus.SMBus(1) # 0 oppure 1 se RPi è 256 o 512 Mb di RAM

DEVICE_ADDRESS = 0x70 # indirizzo 7 bit

DEVICE_REG_MODE1 = 0x00

DEVICE_REG_LEDOUT0 = 0x1d

#Scrive un singolo char [long write_byte_data(int addr,char cmd,char val)]

bus.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE1, 0x80)

#Lettura

(msb, lsb, xsb) = bus.read_i2c_block_data(addr, REG_MSB, 3)

Page 20: Prototipare col raspberry pi

PWM (pulse-width modulation)

Page 21: Prototipare col raspberry pi

PWM (duty-cycle)

Page 22: Prototipare col raspberry pi

PWM

def color(R, G, B): # range colore da 0-100% RED.ChangeDutyCycle(R) GREEN.ChangeDutyCycle(G) BLUE.ChangeDutyCycle(B)

# spegne tutti i LED time.sleep(.02) RED.ChangeDutyCycle(0) GREEN.ChangeDutyCycle(0) BLUE.ChangeDutyCycle(0)

# Settaggio dei pin come outputGPIO.setup(17, GPIO.OUT)GPIO.setup(18, GPIO.OUT)GPIO.setup(27, GPIO.OUT) RED = GPIO.PWM(17, 100) # 100HzRED.start(0) # 0 = spentoGREEN = GPIO.PWM(18, 100)GREEN.start(0)BLUE = GPIO.PWM(27, 100)BLUE.start(0)

while 1: for r in range(0,2): for g in range(0,2): for b in range(0,2): # loop per il 100% di ogni colore for i in range(0,101): color((x*i),(y*i),(z*i))

Page 23: Prototipare col raspberry pi

SPI (serial peripheral interface)

Page 24: Prototipare col raspberry pi

SPI

Page 25: Prototipare col raspberry pi

SPI#!/usr/bin/python

import spidev

import time

spi = spidev.SpiDev()

spi.open(0, 0)

spi.max_speed_hz = 7629

# Divide un intero in un array di 2 byte per spedirlo via SPI

def write_pot(input):

msb = input >> 8

lsb = input & 0xFF

spi.xfer([msb, lsb])

# Loop infinito di apertura/chiusura della porta del MCP4151

while True:

write_pot(0x1FF)

time.sleep(0.5)

write_put(0x00)

time.sleep(0.5)

Page 26: Prototipare col raspberry pi

UART Universal Async. Receiver-Transmitter

Page 27: Prototipare col raspberry pi

UART

Page 28: Prototipare col raspberry pi

UARTimport serial

ser = serial.Serial("/dev/ttyAMA0")

ser.write("messaggio da spedire")

read = ser.read()

print read

ser.close()