servoMax = 480 servo_units_per_degree = (servoMax - servoMin) / 360 * 2 serv_center = ((servoMax - servoMin) / 2) + servoMin def setServoPulse(channel, pulse): pulseLength = 1000000 # 1,000,000 us per second pulseLength /= 60 # 60 Hz print "%d us per period" % pulseLength pulseLength /= 4096 # 12 bits of resolution print "%d us per bit" % pulseLength pulse *= 1000 pulse /= pulseLength pwm.setPWM(channel, 0, pulse) pwm.setPWMFreq(60) # Set frequency to 60 Hz #boat_handler = boat_handling() while (True): # Change speed of continuous servo on channel O wind_angle = read_wind() sail_angle = boat_handler.desired_sail_angle(wind_angle) print "wind_angle %.2f sail_angle %.2f servo_out %s" % (wind_angle,sail_angle,int(serv_center + sail_angle * servo_units_per_degree)) pwm.setPWM(0, 0, int(serv_center + sail_angle * servo_units_per_degree)) time.sleep(.1) # pwm.setPWM(0, 0, servoMax) # time.sleep(1)
pulseLength /= 60 # 60 Hz print "%d us per period" % pulseLength pulseLength /= 4096 # 12 bits of resolution print "%d us per bit" % pulseLength pulse *= 1000 pulse /= pulseLength pwm.setPWM(channel, 0, pulse) pwm.setPWMFreq(60) # Set frequency to 60 Hz servo_history = 0 backlash_servo_out = 0 servo_hysteresis = 0 while (True): # Change speed of continuous servo on channel O wind_angle = (read_wind() - 4) % 360 #calibration if (wind_angle < -180): wind = wind_angle + 360 if (wind_angle > 180): wind = wind_angle - 360 else: wind=wind_angle sail_angle = wind servo_out = int(serv_center + sail_angle * servo_units_per_degree) # if (servo_out < servo_history): # servo_hysteresis = 2 # print 'bl'