def set_pw(self, time, midpoint): # in us
		self.neutral=midpoint
		if time > self.max:
			time = self.max
		elif time < self.min:
			time = self.min

		num_pulses = int(time/self.pulse_length)
		pwt.setPWM(self.channel, 0, num_pulses)
	def __init__(self, channel, freq):
		self.channel = channel
		self.freq = freq
		self.max = 2000
		self.min = 1000
		self.neutral=1500
		
		pwt.setPWMFreq(freq)
		self.pulse_length = 1000000/(freq*4096)
	def calibrate_ROM(self):
		threshold = 50
		x_ = self.neutral

		#hold min
		m = []
		for ii in range (10):
			x = pwt.measure_pw_us(channel)
			# will usually throw away the first point
			if np.absolute(x - x_) > threshold:
				ii -= 1
			else:
				m.append(pwt.measure_pw_us(channel))
			x_ = x
		self.min = sum(m)/len(m)

		#hold max
		M = []
		for ii in range (10):
			x = pwt.measure_pw_us(channel)
			# will usually throw away the first point
			if np.absolute(x - x_) > threshold:
				ii -= 1
			else:
				M.append(pwt.measure_pw_us(channel))
			x_ = x
		self.max = sum(M)/len(M)

		c = []
		for ii in range (10):
			x = pwt.measure_pw_us(channel)
			# will usually throw away the first point
			if np.absolute(x - x_) > threshold:
				ii -= 1
			else:
				c.append(pwt.measure_pw_us(channel))
			x_ = x
		self.neutral = sum(c)/len(c)
motor_in = 38
rudder_in = 37


gpio.setmode(gpio.BOARD)
gpio.setup(aileron_in, gpio.IN)
gpio.setup(elevator_in, gpio.IN)
gpio.setup(motor_in, gpio.IN)
gpio.setup(rudder_in, gpio.IN)

print('Inputs initialized')

#=========================
# DEFINE TIMING VARIABLES
#=========================
freq = int(pwt.measure_freq(aileron_in))
stdout.write("\rFrequency (Hz): %d\n" %freq)
pulse_length = 1000000 / (freq * 4096)

print('Timing variables initialized')

#=========================
# INITIALIZE OUTPUTS	
#=========================
aileron_out = 0 # Servo driver board on frequency calibrated earlier
elevator_out = 1
motor_out = 2
rudder_out = 3

aileron = ct.Servo(aileron_out, freq)
elevator = ct.Servo(elevator_out, freq)