Skip to content

Instantly share code, notes, and snippets.

@aitormendez
Last active November 8, 2020 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aitormendez/7797a6bc52e6f20c27abcad241225c9a to your computer and use it in GitHub Desktop.
Save aitormendez/7797a6bc52e6f20c27abcad241225c9a to your computer and use it in GitHub Desktop.
Elevator stepper motor python
import time
import smbus
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
DEVICE = 0x20 # Device address (A0-A2)
IODIRA = 0x00 # Pin direction register
IODIRB = 0x01 # Pin direction register
OLATA = 0x14 # Register for outputs
OLATB = 0x15 # Register for outputs
GPIOA = 0x12 # Register for inputs
GPIOB = 0x13 # Register for inputs
# Set all GPA pins as outputs by setting
# all bits of IODIRA register to 0
bus.write_byte_data(DEVICE,IODIRA,0x00)
bus.write_byte_data(DEVICE,IODIRB,0xFF)
lectura = bus.read_byte_data(DEVICE,IODIRB)
print(lectura)
class Ascensor:
def __init__(self, piso_actual = 0, piso_destino = 1, altura_piso = 400, direccion = "sube"):
self.piso_actual = piso_actual
self.piso_destino = piso_destino
self.altura_piso = altura_piso
self.direccion = direccion
self.pasos = (self.piso_destino - self.piso_actual) * self.altura_piso
self.halfstep_seq_cw = [
0b00001000, # 1,0,0,0
0b00011000, # 1,1,0,0
0b00010000, # 0,1,0,0
0b00110000, # 0,1,1,0
0b00100000, # 0,0,1,0
0b01100000, # 0,0,1,1
0b01000000, # 0,0,0,1
0b01001000 # 1,0,0,1
]
self.halfstep_seq_ccw = [
0b01001000, # 1,0,0,1
0b01000000, # 0,0,0,1
0b01100000, # 0,0,1,1
0b00100000, # 0,0,1,0
0b00110000, # 0,1,1,0
0b00010000, # 0,1,0,0
0b00011000, # 1,1,0,0
0b00001000 # 1,0,0,0
]
def vamos(self, piso_destino):
self.piso_destino = piso_destino
self.pasos = (self.piso_destino - self.piso_actual) * self.altura_piso
print(self.pasos)
if self.pasos < 0:
self.direccion = "baja"
self.pasos = self.pasos * -1
elif self.pasos > 0:
self.direccion = "sube"
else:
self.direccion = "para"
for i in range(self.pasos):
for halfstep in range(8):
if self.direccion == "sube":
bus.write_byte_data(DEVICE,OLATA,self.halfstep_seq_cw[halfstep])
elif self.direccion == "baja":
bus.write_byte_data(DEVICE,OLATA,self.halfstep_seq_ccw[halfstep])
time.sleep(0.001)
self.piso_actual = self.piso_destino
print(f"Estamos en el piso {self.piso_actual}")
bus.write_byte_data(DEVICE,OLATA,0b00000000)
asc = Ascensor()
while True:
lectura = bus.read_byte_data(DEVICE,GPIOB)
if lectura == 0b00000001:
print(f"bot bajo! dec: {lectura} bin: {bin(lectura)}" )
asc.vamos(0)
time.sleep(0.5)
elif lectura == 0b00000010:
print(f"bot primero! dec: {lectura} {bin(lectura)}")
asc.vamos(1)
time.sleep(0.5)
elif lectura == 0b00000100:
print(f"bot segundo! dec: {lectura} {bin(lectura)}")
asc.vamos(2)
time.sleep(0.5)
elif lectura == 0b00001000:
print(f"bot tercero! dec: {lectura} {bin(lectura)}")
asc.vamos(3)
time.sleep(0.5)
elif lectura == 0b00010000:
print(f"bot cuarto! dec: {lectura} {bin(lectura)}")
asc.vamos(4)
time.sleep(0.5)
elif lectura == 0b00100000:
print(f"bot quinto! dec: {lectura} {bin(lectura)}")
asc.vamos(5)
time.sleep(0.5)
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment