import smbus as smbus import time class Juice(object): def __init__(self): self.bus = smbus.SMBus(1) self.last_x_pos = 1500 self.last_y_pos = 1500 print("juice ready") def __del__(self): class_name = self.__class__.__name__ print (class_name, "finished") def up(self): if(self.last_y_pos > 1000): y = self.last_y_pos while(y>1000): self.bus.write_word_data(0x48, 4, y) y-=50 time.sleep(0.2) self.last_y_pos = y def down(self): if(self.last_y_pos < 2000): y = self.last_y_pos while(y<2000): self.bus.write_word_data(0x48, 4, y) y+=50 time.sleep(0.2) self.last_y_pos = y def left(self): if(self.last_x_pos < 2500): x = self.last_x_pos while(x<2500): self.bus.write_word_data(0x48, 1, x) x+=50 time.sleep(0.2) self.last_x_pos = x def right(self): if(self.last_x_pos > 600): x = self.last_x_pos while(x>600): self.bus.write_word_data(0x48, 1, x) x-=50 time.sleep(0.2) self.last_x_pos = x def center(self): self.bus.write_word_data(0x48, 1, 1500) self.last_x_pos = 1550 self.bus.write_word_data(0x48, 4, 1500) self.last_y_pos = 1500 def main(self): self.bus.write_word_data(0x48, 4, 1600) time.sleep(0.5) self.bus.write_word_data(0x48, 1, 1500) time.sleep(0.5) self.last_x_pos = 1500 self.last_y_pos = 1500 def auto(self): self.bus.write_word_data(0x48, 4, 1800) time.sleep(0.5) self.bus.write_word_data(0x48, 1, 1500) time.sleep(0.5) x = 1800 pos = 1800 for iteration in range (1, 4): for i in range(1000, 2000, 10): self.bus.write_word_data(0x48, 1, i) time.sleep(0.1) pos = x while pos > x-(iteration * 60): self.bus.write_word_data(0x48, 4, pos) time.sleep(0.1) pos-=5 x = pos for i in range(2000, 1000, -10): self.bus.write_word_data(0x48, 1, i) time.sleep(0.1) pos = x while pos > x-(iteration* 70): self.bus.write_word_data(0x48, 4, pos) time.sleep(0.1) pos-=5 x = pos print("iteration", iteration) self.bus.write_word_data(0x48, 4, 1500) print("done") if __name__ == "__main__": juice = Juice() juice.auto()