#!/usr/bin/python #import os, socket, time, simplejson, urllib #from threading import Thread #from Queue import Queue import socket, urllib from BeautifulSoup import BeautifulStoneSoup as soup class airsoft_turret: def __init__(self): self.o_min = 800.0 self.o_max = 2075.0 self.o_center = 1438.0 # self.o_center = int ( (self.o_max - self.o_min ) / 2 ) self.i_min = -90.0 self.i_max = 66.0 #Math also done in __calc_limits for dynamic recomputation self.shift = abs(self.i_min) + 1 self.si_min = self.i_min + self.shift self.si_max = self.i_max + self.shift self.scaler = (self.o_max - self.o_min) / (self.si_max - self.si_min) self.left_align = int(self.o_min - (self.si_min * self.scaler)) # end calc_limits dup self.pos_url = "https://www.iobridge.com/interface/?actionID=RM_jBUd1SKTIrfx&sessionID=o2EfTtEiiQ3DdOE7lpq7&setValue=" self.firing_pos = "" #setState self.firing_pin = "" def __calc_limits(self): self.shift = abs(self.i_min) + 1 self.si_min = self.i_min + self.shift self.si_max = self.i_max + self.shift self.scaler = (self.o_max - self.o_min) / (self.si_max - self.si_min) self.left_align = int(self.o_min - (self.si_min * self.scaler)) def scale_input(self, input = None): if not input: input = self.i_min d = input + self.shift d *= self.scaler d += self.left_align # Just a quick check on the values in case rounding takes it's toll if d < self.o_min: d = self.o_min elif d > self.o_max: d = self.o_max return int(d) # Skipping dynamic scaling for now for speed if input > self.i_max: self.i_max = input # This is only slightly less efficient, guarantees I don't screw up self.__calc_limits() print "Updated input Max to: %s" % ( input ) elif input < self.i_min: self.i_min = input # This is only slightly less efficient, guarantees I don't screw up self.__calc_limits() print "Updated input Min to: %s" % ( input ) d = input + self.shift d *= self.scaler d += self.left_align # Just a quick check on the values in case rounding takes it's toll if d < self.o_min: d = self.o_min elif d > self.o_max: d = self.o_max return int(d) def move_to(self, position = None): if not position: position = self.o_min u2 = "%s%s" % ( self.pos_url, position ) urllib.urlopen(u2) def gen_buffer(): read_size = 640 read_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) read_sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1) try: read_sock.connect(("127.0.0.1", 45054)) except: raise while True: try: buffer = read_sock.recv(read_size, socket.MSG_WAITALL) if not buffer: continue else: yield buffer except: raise StopIteration exit(1) def monitor(turret): last_output = {} gen_soup = (soup(b) for b in gen_buffer()) # gen_v = ( v['value'] for v in gen_soup if v.find("avisvalue", axis="wr") ) # gen_movement = ( m.find("axisvalue", axis="wr")['value'] for m in gen_soup if m ) # v = s.find("device", address='00-1f-c5-21-6f-6d').find("axisvalue", axis="wr")['value'] for s in gen_soup: v = s.findAll("axisvalue", axis="wr")[-1] if not v: continue curr = int(float(v['value'])) last = last_output.get('wr', 0) if abs(curr-last) > 15: last_output['wr'] = curr pos = turret.scale_input(curr) turret.move_to(pos) print "Moved: %s => %s" % ( curr, pos ) def main(): gun = airsoft_turret() monitor(gun) if __name__ == '__main__': main()